Skip to content

Instantly share code, notes, and snippets.

@DenisLeblanc
Created June 9, 2015 19:14
Show Gist options
  • Save DenisLeblanc/39181bab55e689521517 to your computer and use it in GitHub Desktop.
Save DenisLeblanc/39181bab55e689521517 to your computer and use it in GitHub Desktop.
Track Outbound links in Google Analytics
// track outbound links
// usage <a class="button button_large" href="http://amzn.com/1628651628" rel="external" target="_blank">
function trackOutboundClick(e) {
var $link = $(e.currentTarget);
var url = $link.attr('href');
var target = $link.attr('target');
ga('send', 'event', 'outbound', 'click', url, {
'hitCallback': function() {
if (target !== '_blank') {
document.location = url;
}
}
});
}
$('[rel="external"]').on('click', trackOutboundClick);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment