Last active
October 18, 2018 13:38
-
-
Save alexmill/f4d668cb398ae83d78db to your computer and use it in GitHub Desktop.
Track clicks on outbound links from your website with Google Analytics event tracking
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 */ | |
/* Requires jQuery */ | |
/* Paste this immediately below your Google Analytics snippet */ | |
var outbounds = $("a").filter(function(index){ | |
try{ | |
var h = $(this).attr("href"); | |
return h.indexOf("http") > -1 && h.indexOf(window.location.host) == -1; | |
} catch(err){ | |
return false; | |
} | |
}); | |
$(outbounds).click(function(){ | |
ga('send', 'event', 'Outbound Link', $(this).attr("href"), 'From page: '.concat(document.URL) ); | |
console.log($(this).attr("href")) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment