Skip to content

Instantly share code, notes, and snippets.

@baado
Created April 30, 2012 07:04
Show Gist options
  • Save baado/2556135 to your computer and use it in GitHub Desktop.
Save baado/2556135 to your computer and use it in GitHub Desktop.
Tracking Non-HTML Object with Google Analytics
var _gaq = _gaq || [];
// please replace 'UA-XXXXXX-X' to your analytics code.
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);
function recordObjectLink(link) {
try {
var linkedURL = link.href.replace('http://' + document.domain, '');
linkedURL = linkedURL.replace('https://' + document.domain, '');
_gaq.push(['_trackPageview', linkedURL]);
var cl = '';
if (link.target && link.target !== '_self') {
switch (link.target) {
case '_parent':
cl = 'parent.document.location = "' + linkedURL + '"';
break;
case '_top':
cl = 'top.document.location = "' + linkedURL + '"';
break;
default:
cl = 'var w = window.open("' + linkedURL + '", "' + link.target + '");if(!w){document.location = "' + linkedURL + '"}';
break;
}
} else {
cl = 'document.location = "' + linkedURL + '"';
}
setTimeout(cl, 100);
} catch (err) {}
return false;
}
(function () {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment