Skip to content

Instantly share code, notes, and snippets.

@jamesmrobinson
Last active December 14, 2015 20:18
Show Gist options
  • Save jamesmrobinson/5142524 to your computer and use it in GitHub Desktop.
Save jamesmrobinson/5142524 to your computer and use it in GitHub Desktop.
Google Analytics event tracking function. Uses HTML5 data-attributes. Any anchor with the data-event="ga" will fire a Google Analytics event. The event details should also be specified as data-attributes: data-category, data-action, data-label. Requires jQuery > 1.4
eventTracking = function () {
$t = $('[data-event="ga"]');
$t.click(function () {
var gaCat = $(this).data('category') ? $(this).data('category') : '',
gaAct = $(this).data('action') ? $(this).data('action') : '',
gaLab = $(this).data('label') ? $(this).data('label') : '';
try {
_gaq.push(['_trackEvent', gaCat, gaAct, gaLab]);
} catch (e) {
console.log(e);
}
});
}
@doro
Copy link

doro commented May 20, 2013

I think there is a small mistake, inside the click handler you should not use $t but $(this).

@jamesmrobinson
Copy link
Author

Thanks doro, that's updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment