Created
January 26, 2018 14:16
-
-
Save ao5357/edff8a880310fde6ba4fce53c7bc5791 to your computer and use it in GitHub Desktop.
GA Event for anchors in jQuery 1.7+
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
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