Last active
March 16, 2017 17:02
-
-
Save meehow/2655bc5997cc4b14c130a236254b01d5 to your computer and use it in GitHub Desktop.
track outbound links in Google Analytics
This file contains 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
var trackOutboundLink = function(event) { | |
var url = this.href; | |
ga('send', 'event', 'outbound', 'click', url, { | |
'transport': 'beacon', | |
'hitCallback': function(){document.location = url;} | |
}); | |
event.preventDefault(); | |
} | |
Array.prototype.slice.call(document.getElementsByTagName('a')).map(function(el) { | |
if (el.href[0] != '/') el.addEventListener('click', trackOutboundLink); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For wider Browser support, you may not want to rely on
Array.from
that is only available in newer browsers ;-) (hi IE11 and Android browser!)does the same thing, in all it's ugliness ;)