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:
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.]