We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36623
    • 2 Posts
    Good Morning MODx Community,

    I've created the following site using MODx Revolution:

    http://www.jesusreport.com

    The client has asked for the "sharethis" links to be put in, and here lies my dilemma. The concept for the page was a "drudge report" feel, where the page contains no actual content, but instead links to content all over the web based on what the client chooses. To achieve this, each news item is a Weblink and the image is a template variable.

    When I add sharethis, it seems I have two options, it either shares the Article itself, so the end results bypasses my page entirely (hence, the client loses the tracking on the hit), or it can only share the home page. I am looking for a solution where it shares the article, with the headline and the image, but links through the site so the hit can be tracked.

    I am very very new with MODx and JQuery, so feel free to treat me like a total newbie, as that is basically where I sit.

    Thanks in advance!
    DD
      • 33968
      • 863 Posts
      I'm a bit confused - are you trying to track when someone 'shares' a link? Or track when other people are clicking on 'shared' links, perhaps from their facebook or twitter?

      To do the latter, you would need to ensure the shared link is something like:

      http://www.yoursite.com/redirect?url=http%3A%2F%2Fwww.external-link.com


      And then create a 'redirect' page on your site with a snippet to accept the link, grab the '?url=' parameter, register a hit (see Event Tracking) and then forward the visitor on to the external link.

      It would mean people would click a link expecting to go visit a website, but suddenly find themselves on your site before being redirected... That's messy and not nice from a usability viewpoint, but it's the only way that comes to mind to accomplish this. [ed. note: okyanet last edited this post 15 years, 1 month ago.]
        • 36623
        • 2 Posts
        Thanks for the quick reply!

        The client really wants to accomplish 2 things, track how many hits total we are getting from all the shares, and see which articles get the most number of hits, so the redirect is probably the best way to accomplish this. I have told him there would be a flash of the redirect, and that's OK as long as he gets the tracking info.

        The only issue I see with the method you recommend, is that there would only be 1 set of tracking data for all clicks from shares (this is how i understand it from my newbie standpoint, feel free to correct me if I'm wrong)

        I would really like to track that the article about, for example, "Casey Anthony", brought in 1000 clicks from shares, while the article about the upcoming "Courageous" movie only drew 200 clicks. That's the important information that allows him to find out what his reader is reading and sharing, and refine the articles.

        My idea for thinking was changing the weblinks to docs, with a redirect in the header, based on a template variable URL that would be in the page, though I'm actually not sure how I'd accomplish this, as my knowledge of scripting is basically nonexistent (I'm learning as I go) Any help would be greatly appreciated!

        -DD

          • 33968
          • 863 Posts

          The only issue I see with the method you recommend, is that there would only be 1 set of tracking data for all clicks from shares (this is how i understand it from my newbie standpoint, feel free to correct me if I'm wrong)
          With event tracking you can set a MODx placeholder in your GA code and it will log a different event for each url that comes in.

          I think you can achieve this as I suggested and without having to convert all weblinks to documents... I'm happy to suggest some code for you but it will have to wait until tomorrow smiley
            • 33968
            • 863 Posts
            Ok, what you need to do is ensure your shared links are of the format:

            http://www.yoursite.com/share?link=33

            Where '33' is the weblink id being shared (see how to customise sharethis here).

            Then people who click the link will end up on a 'share' page on your site, where the following snippet will grab the weblink id from the url, display a 'redirecting...' message, and redirect via meta refresh after a couple of seconds. You can use a virtual pageview to register the 'hit' on Google Analytics.



            Snippet code refRedirect:
            <?php
            $output = '';
            
            // grab time delay from snippet call, default to 2s
            $delay  = $modx->getOption('delay',$scriptProperties,2);
            $link   = $_GET['link'];
            
            /* check link id is set and an integer value */
            if (!empty($link) && intval($link)) {
            
            	$link = $modx->getObject('modResource',$link);
            	
            	/* if valid link present, redirect after $delay seconds */
            	if ($link) {
            		$url  = $link->get('content');
            		$placeholders = array(
            			'ref.id'    => $link->get('id'),
            			'ref.title' => $link->get('pagetitle'),
            			'ref.url'   => $link->get('content'),
            			'ref.meta'  => '<meta http-equiv="Refresh" content="'.$delay.';URL='.$url.'">',
            			'ref.msg'   => 'Redirecting to <a href="'.$url.'">'.$url.'</a>…',
            		);
            		$modx->toPlaceholders($placeholders);
            		return $output;
            	}
            	
            } else {
            	
            	/* if not, redirect to home page */
            	$url = $modx->makeUrl($modx->getOption('site_start'));
            
            	$placeholders = array(
            		'ref.id'    => 'n/a',
            		'ref.title' => 'bad link',
            		'ref.url'   => 'n/a',
            		'ref.meta'  => '<meta http-equiv="Refresh" content="'.$delay.';URL='.$url.'">',
            		'ref.msg'   => 'Sorry, that item does not exist. Redirecting to home page...',
            	);
            	$modx->toPlaceholders($placeholders);
            }
            
            return $output;
            

            Create a new page with alias 'share', containing the following in the content area:
            [[!refRedirect]]
            [[+ref.msg]]
            

            Add this placeholder between your <head> tags:
            [[+ref.meta]]
            

            And add/replace this code where your GA script is:
            <script type="text/javascript"> 
               var _gaq = _gaq || [];
              _gaq.push(['_setAccount', 'UA-XXXXXXXX-X']);
              _gaq.push(['_trackPageview', '[[+ref.url]]']);
            </script> 
            

            Use +ref.title or +ref.id if you would like the link title or id tracked instead of the url.

            Note the placeholders you have available: +ref.id, +ref.url, +ref.title, +ref.meta, and +ref.msg.

            Also, the GA code above might not be ideal as it will register shared links as actual pageviews on your site. It might be better to set them up as events instead:
            // _gaq.push(['_trackEvent', 'category', 'action', 'opt_label', opt_value]);
             _gaq.push(['_trackEvent', 'Shared Links', 'Redirect', [[+ref.url]], [[+ref.id]]);
            

            The snippet will redirect to your site's home page if the link does not exist. [ed. note: okyanet last edited this post 15 years, 1 month ago.]