Skip to content

Instantly share code, notes, and snippets.

@jgornick
Created October 1, 2010 17:01
Show Gist options
  • Save jgornick/606489 to your computer and use it in GitHub Desktop.
Save jgornick/606489 to your computer and use it in GitHub Desktop.
JS: Google Analytics Helper
/**
* Analytics Helper
*
* Include this script at the bottom of the body when you want to enable tracking outbound links.
*
* To enable outbound tracking, simply add rel="external" to your anchor.
*
* To enable event tracking, add rel="trackevent" to your anchor, then use the fragment
* identifier in the href to set your event params. An example href would look like:
* href="#te_category=Downloads&te_action=PDF&te_label=Document&te_value=Value"
*
* This script will only work with the asynchronous analytics setup
* (http://code.google.com/apis/analytics/docs/tracking/asyncTracking.html).
*
* You will also need to make sure the analytics _gaq is already setup with account information
* and other required options.
*/
(function(global) {
var OUTBOUND_EVENT_CATEGORY = 'Outbound Links',
TRACK_EVENT_DEFAULT_CATEGORY = 'Default Category',
TRACK_EVENT_DEFAULT_ACTION = 'Default Action';
var _gaq = global._gaq || (global._gaq = [])
reExternal = /\s?external\s?/i,
reTrackEvent = /\s?trackevent\s?/i;
function handleClick(e) {
// fix our event when needed
e || (e = global.event);
var target = e.target || e.srcElement, action, i = -1, param, params = {};
// if there isn't a target for some reason, return
if (!target) {
return true;
}
// handle outbound links via rel="external"
if (reExternal.test(target.rel)) {
action = target.href || '';
action = action.replace(/^(https?:\/\/)?(www\.)?/i, '');
_gaq.push(['_trackEvent', OUTBOUND_EVENT_CATEGORY, action]);
}
// handle event tracking via rel="trackevent"
if (reTrackEvent.test(target.rel)) {
action = String(target.href || '');
// pull out our hash from the href
action = action.match(/#(.*?)(?=\?|$)/);
// if nothing matched or it didn't match more than 1, then return;
if (!action || !action.length > 1 ) { return true; }
// split the hash on &'s and loop through to build params object
action = action[1].split('&');
while (param = action[++i]) {
param = param.split('=');
params[param[0].replace('te_', '')] = decodeURIComponent(param[1]);
}
// build our command and push it to the gaq
_gaq.push([
'_trackEvent',
params.category || TRACK_EVENT_DEFAULT_CATEGORY,
params.action || TRACK_EVENT_DEFAULT_ACTION,
params.label,
params.value
]);
// DISCLAIMER: Because we don't want to show the tracking hash in the URL, we are removing
// our tracking parameters from the hash and then setting the window.location to that value.
// We are only preventing default here, which means bubbling and other handler propagation
// should continue.
window.location = target.href.replace(/\&?te_(.*?)\&?(?=\?|$)/, '');
e.preventDefault && e.preventDefault();
e.returnValue && (e.returnValue = false);
return false;
}
}
if (document.addEventListener) {
document.body.addEventListener('click', handleClick, false);
} else if (document.attachEvent) {
document.body.attachEvent('onclick', handleClick);
} else {
document.body.onclick = handleClick;
}
})(this);
<a href="http://twitter.com/github" rel="external" title="Twitter: @github">Twitter: @github</a>
<a href="http://www.facebook.com/MLBFans" rel="external" title="Facebook: MLBFans">Facebook: MLBFans</a>
<a href="#player&te_category=Videos&te_action=Play&te_label=Zoolander" rel="trackevent nofollow">Play Zoolander</a>
<a href="#player&te_category=Videos&te_action=Stop&te_label=Zoolander" rel="trackevent nofollow">Stop Zoolander</a>
<a href="document.pdf#te_category=Downloads&te_action=PDF&te_label=Document" rel="trackevent">Download PDF</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment