Created
June 9, 2015 19:14
-
-
Save DenisLeblanc/39181bab55e689521517 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
// 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