Skip to content

Instantly share code, notes, and snippets.

@ekho
Created June 6, 2012 04:06
Show Gist options
  • Save ekho/2879859 to your computer and use it in GitHub Desktop.
Save ekho/2879859 to your computer and use it in GitHub Desktop.
Track outgoing links with google analytics
(function(){
function addEventListener(element, event, listener) {
if (typeof listener != 'function')
return;
if (event == 'ready' && element == document){
if (/(loaded|complete)/.test(document.readyState)) {
listener();
return;
}
if (element.addEventListener) {
window.addEventListener(event, listener, false);
document.addEventListener('DOMContentLoaded', listener, false);
}
if (element.attachEvent) {
document.attachEvent("onreadystatechange", listener);
window.attachEvent( "onload", listener );
}
} else {
if (element.addEventListener)
element.addEventListener(event, listener, false);
if (element.attachEvent)
element.attachEvent('on'+event, listener);
}
}
// track external links
(function(){
if (typeof _gaq != 'object') return;
var rules = [];
var dwnFileExts = [];
var useTrackPageview = false;
var useTrackEvent = true;
for (var i=0; i<_gaq.length; i++)
{
switch (_gaq[i][0]) {
case '_setExtLinksRules':
rules = _gaq[i].slice(1);
delete _gaq[i];
break;
case '_setExtLinksDownloadableFileExtensions':
dwnFileExts = _gaq[i].slice(1);
delete _gaq[i];
break;
case '_setExtLinksUseTrackPageview':
useTrackPageview = _gaq[i].slice(1);
if (typeof useTrackPageview == 'object' && useTrackPageview.length>0)
useTrackPageview = useTrackPageview[0];
delete _gaq[i];
break;
case '_setExtLinksUseTrackEvent':
useTrackEvent = _gaq[i].slice(1);
if (typeof useTrackEvent == 'object' && useTrackEvent.length>0)
useTrackEvent = useTrackEvent[0];
delete _gaq[i];
break;
}
}
var aTags = document.getElementsByTagName('A');
if (!aTags.length) return;
var a, ahost, chost = typeof _gbh != 'string' ? ((document.location.hostname || document.location.host).replace(/:[0-9]+$/,'')+'.').split('.').slice(-3,-1).join('.') : _gbh;
chost = new RegExp('(^|\\.)'+chost+'$', 'i');
for (var i=0; i<aTags.length; i++) {
a = aTags[i];
ahost = (a.hostname || a.host).replace(/:[0-9]+$/,'');
if (a.href=="" || !/^https?:$/.test(a.protocol) || a.elEventExists || chost.test(ahost)) continue;
a.gaLink = String(ahost + '/' + a.pathname.replace(/^\//,'') + a.search + a.hash).replace(/^www\./,'').replace(/\/+$/,'');
addEventListener(a, 'click', function(event) {
var a = (event.currentTarget || event.target || event.srcElement);
if (a.tagName.toLowerCase() != 'a' || !a.gaLink) return true;
var evtCat, evtAct, evtVal;
for (var i=0; i<rules.length; i++)
if ( RegExp(rules[i][0], 'i').test(a.gaLink) ) {
evtCat = rules[i][1];
evtAct = rules[i][2];
evtVal = a.gaLink;
window._gatPushFunc = function(useTrackPageview, useTrackEvent, evtCat, evtAct, evtVal) {
if (useTrackPageview)
_gaq.push(["_trackPageview", "/" + evtCat + "/" + evtVal]);
if (useTrackEvent)
_gaq.push(["_trackEvent", evtCat, evtAct, evtVal]);
if ($.browser == 'msie')
window['_gatPushFunc'] = null;
else
delete window['_gatPushFunc'];
}
// hack for download links
if (RegExp('\\.('+dwnFileExts.join('|')+')$', 'i').test(a.gaLink))
window.setTimeout('window._gatPushFunc(' + useTrackPageview.toString() + ', ' + useTrackEvent.toString() + ', "' + evtCat + '", "' + evtAct + '", "' + evtVal + '");', 10);
else
window._gatPushFunc(useTrackPageview, useTrackEvent, evtCat, evtAct, evtVal);
}
return true;
});
a.elEventExists = true;
}
})();
// track ajax-requests by GA
addEventListener(document, 'ready', function() {
if (typeof jQuery != 'undefined')
jQuery(document).ajaxSend(function(e, x, s) {
if (typeof _gaq == 'object')
_gaq.push(['_trackPageview', s.url]);
});
});
})();
(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);
})();
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(["_setAccount","UA-0000000-00"]);
_gaq.push(["_setExtLinksRules",[".*","external-link","click"]]);
_gaq.push(["_setExtLinksDownloadableFileExtensions","rar","zip","exe","torrent"]);
_gaq.push(["_setExtLinksUseTrackPageview",true]);
_gaq.push(["_trackPageview"]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'path/to/gat.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment