Skip to content

Instantly share code, notes, and snippets.

@ao5357
Created January 26, 2018 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ao5357/edff8a880310fde6ba4fce53c7bc5791 to your computer and use it in GitHub Desktop.
Save ao5357/edff8a880310fde6ba4fce53c7bc5791 to your computer and use it in GitHub Desktop.
GA Event for anchors in jQuery 1.7+
var $body = jQuery('body');
$body.on('click', 'a', function() {
var $this = $(this),
thisHref = $this.attr('href');
if (thisHref.length
&& thisHref.substring(0, 1) === '#'
&& typeof ga === 'function'
) {
ga('send', 'event', {
eventCategory: 'Anchor',
eventAction: 'click',
eventLabel: $this.text().trim().substr(0, 140).trim(),
transport: 'beacon'
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment