Last active
July 5, 2017 19:51
-
-
Save mirshko/ce6548a60a827a1504bffa98e39dfe61 to your computer and use it in GitHub Desktop.
Post Google Analytics Events easily with data from data attributes on html elements
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
<a href="/page" cta-label="Event Name In Google Analytics">Button</a> |
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
$(document).ready(function () { | |
$('body').on('click', '[cta-label]', function () { | |
var label = this.getAttribute('cta-label') || this.getAttribute('href') || 'button' | |
ga('send', { | |
hitType: 'event', | |
eventCategory: 'Call-to-Action', | |
eventAction: 'Click', | |
eventLabel: label | |
}); | |
}) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment