Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Last active January 4, 2024 21:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottopolis/acd9db17189ed2627312bdae23a25faf to your computer and use it in GitHub Desktop.
Save scottopolis/acd9db17189ed2627312bdae23a25faf to your computer and use it in GitHub Desktop.
Google Analytics track all outbound links with no markup change
// 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
});
}
});
@Mag91
Copy link

Mag91 commented Jan 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment