Created
January 15, 2018 13:54
-
-
Save coliff/4314060186a135a920fc510e40d2f9c3 to your computer and use it in GitHub Desktop.
Track outbound links with Google's gtag.js
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
<script> | |
var trackOutboundLink = function(url) { | |
gtag('event', 'click', { | |
'event_category': 'outbound', | |
'event_label': url, | |
'transport_type': 'beacon' | |
}); | |
} | |
function onclickCallback(e) { | |
var e = window.e || e; | |
if ( (e.target.tagName !== 'A') || (e.target.host == window.location.host) ) return; | |
trackOutboundLink(e.target); // GA event tracker | |
} | |
if (document.addEventListener) | |
document.addEventListener('click', onclickCallback, false); | |
else | |
document.attachEvent('onclick', onclickCallback); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment