Skip to content

Instantly share code, notes, and snippets.

@aronbudinszky
Last active December 13, 2016 13:59
Using data attributes to load Google Analytics.
<!-- This is the BAD and ugly way! -->
<a href="{{baseurl}}something/" onclick="ofw.track('Something', 'click', 'Bad way')">Something</a>
<!-- This is the GOOD way! -->
<a href="{{baseurl}}something/" data-track-category="Something" data-track-label="Good way" class="goodway">Something</a>
Optionally you can add data-track-action (defaults to 'click') and data-track-value (defaults to empty).
// Let's subscribe to a specific element
$('.goodway').on('ofw:track:trigger', function(event, category, action, label, value){
// Do something here!
});
// Let's subscribe to all read actions
$('[data-track-action="read"]').on('ofw:track:trigger', function(event, category, action, label, value){
// Do something here!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment