Last active
October 18, 2018 12:01
-
-
Save chrisshennan/398646c25abaa6b8405f to your computer and use it in GitHub Desktop.
How to track outbound links
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
/* | |
* Track outbound links | |
*/ | |
$(function() { | |
// Track outbound links | |
if(typeof(ga) !== 'undefined') { | |
// Track outbound links | |
$('a[href*=http]').on('click', function(e) { | |
var el = $(this); | |
var href = el.attr('href'); | |
var http = 'http://' + window.location.host; | |
var https = 'https://' + window.location.host; | |
// If the link does not belong to the site then record it | |
if(href.substr(0, http.length) != http && href.substr(0, https.length) != https) { | |
var parts = href.split('/'); | |
parts.splice(3); | |
var domain = parts.join('/'); | |
ga('send', 'event', 'Outbound Link', domain, href); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment