Last active
January 4, 2024 21:14
-
-
Save scottopolis/acd9db17189ed2627312bdae23a25faf to your computer and use it in GitHub Desktop.
Google Analytics track all outbound links with no markup change
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
// listen for all clicks on anything. You could change document to a selector like #content to make it more efficient. | |
$(document).on('click', function( e ) { | |
// if this is a link, and the href does not contain our domain, it's an external link. Track it. | |
if( e.target.href && e.target.href.indexOf( window.location.hostname ) < 0 ) { | |
ga('send', 'event', { | |
eventCategory: 'Outbound Link', | |
eventAction: 'click', | |
eventLabel: e.target.href, // the outbound link url will appear as the event label in GA. Find under Behavior -> Events -> Event Label | |
transport: 'beacon' // not supported in some versions of IE | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
does it still work with the new analytics 4 version? Seems not to work for me. i'm from germany we have special law for google analytics coul this be the problem? thank you