Skip to content

Instantly share code, notes, and snippets.

@gregorynicholas
Created June 30, 2011 17:58
Show Gist options
  • Save gregorynicholas/1056788 to your computer and use it in GitHub Desktop.
Save gregorynicholas/1056788 to your computer and use it in GitHub Desktop.
jQuery Google Analytics Javascript Outbound Click Traffic Event Logging
jQuery(document).ready(function () {
jQuery('a').each(function () {
var a = jQuery(this);
var href = a.attr('href');
if (href == undefined) return;
var url = href.replace('http://', '').replace('https://', '');
var hrefArray = href.split('.').reverse();
var extension = hrefArray[0].toLowerCase();
var hrefArray = href.split('/').reverse();
var domain = hrefArray[2];
var downloadTracked = false;
if (jQuery.inArray(extension, analyticsFileTypes) != -1) {
downloadTracked = true;
a.click(function () {
if (analyticsEventTracking == 'enabled') {
_gaq.push(['_trackEvent', 'Downloads', extension.toUpperCase(), href]);
} else _gaq.push(['_trackPageview', analyticsDownloadsPrefix + url]);
});
}
if ((href.match(/^http/)) && (!href.match(document.domain)) && (downloadTracked == false)) {
a.click(function () {
if (analyticsEventTracking == 'enabled') {
_gaq.push(['_trackEvent', 'Outbound Traffic', href.match(/:\/\/(.[^/]+)/)[1], href]);
} else _gaq.push(['_trackPageview', analyticsOutboundPrefix + url]);
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment