Skip to content

Instantly share code, notes, and snippets.

@mirshko
Last active July 5, 2017 19:51
Show Gist options
  • Save mirshko/ce6548a60a827a1504bffa98e39dfe61 to your computer and use it in GitHub Desktop.
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
<a href="/page" cta-label="Event Name In Google Analytics">Button</a>
$(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