Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google analytics event tracker
// Google analytics implementation
window.Analytics = (function(ga) {
function Analytics() { }
Analytics.prototype.trackEvent = function(category, action, label, value) {
ga.apply(this, ['send', 'event'].concat(Array.prototype.slice.call(arguments)));
};
Analytics.prototype.trackPage = function(options) {
ga.apply(this, ['send', 'pageview'].concat(Array.prototype.slice.call(arguments)));
};
return Analytics;
}(ga));
// Initialize analytics
window.analytics = {
google: new window.Analytics(),
//clickView: new ClickViewAnalytics()
};
$('*[data-analytics-category]').on('click', function() {
var $this = $(this);
// Get event data
var category = $this.data('analytics-category');
var action = $this.data('analytics-action
');
var label = $this.data('analytics-label');
var value = $this.data('analytics-value');
// Track event
window.analytics.google.trackEvent(category, action, label, value);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.