Created
October 19, 2017 08:40
-
-
Save woutrbe/e9df9d5eff0222a67765aa8d49dad62f 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
(function() { | |
var links = document.getElementsByTagName('a'); | |
for(var i = 0; i < links.length; i++) { | |
if(links[i].href.indexOf(location.host) === -1) { | |
links[i].onclick = function() { | |
gtag('event', 'click', { | |
'event_category': 'outbound', | |
'event_label': this.href | |
}); | |
} | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment