Created
March 17, 2014 13:46
-
-
Save simondahla/9599432 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Track outbounds | |
(function trackOutbounds() { | |
var hitCallbackHandler = function(url,win) { | |
if (win.length > 0) { | |
window.open(url, win); | |
} else { | |
window.location.href = url; | |
} | |
}; | |
if (document.getElementsByTagName) { | |
var el = document.getElementsByTagName('a'); | |
var getDomain = document.domain.split('.').reverse()[1] + '.' + document.domain.split('.').reverse()[0]; | |
// Look thru each a element | |
for (var i=0; i < el.length;i++) { | |
// Extract it's href attribute | |
var href = (typeof(el[i].getAttribute('href')) == 'string' ) ? el[i].getAttribute('href') : ''; | |
// Query the href for the top level domain (xxxxx.com) | |
var myDomain = href.match(getDomain); | |
// If link is outbound and is not to this domain | |
if ((href.match(/^https?\:/i) && !myDomain) || href.match(/^mailto\:/i)) { | |
// Add an event to click | |
el[i].addEventListener('click', function(e) { | |
var url = this.getAttribute('href'), win = (typeof(this.getAttribute('target') == 'string')) ? this.getAttribute('target') : ''; | |
// Log even to Analytics, once done, go to the link | |
ga('send', 'event', 'outbound', 'click', url, | |
{'hitCallback': hitCallbackHandler(url,win)}, | |
{'nonInteraction': 1} | |
); | |
e.preventDefault(); | |
}); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment