Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
This is a simple class to track custom events on Google Analytics.
/* gaSimpleEventTrack.js */
var tracking = function (settings) {
this.settings = settings;
this.init = function () {
this.setup();
this.bind();
};
this.setup = function () {
this.settings = $.extend({
target: '.target',
category: 'n/a',
action: 'Click',
label: 'n/a'
}, this.settings);
this.target = $(this.settings.target);
this.category = this.settings.category;
this.action = this.settings.action;
this.label = this.settings.label;
};
this.bind = function () {
var _this = this;
this.target.on('click', function () {
_this.trackEvent();
});
};
this.trackEvent = function () {
ga('send', 'event', this.category, this.action, this.label);
};
this.init();
}
$(document).ready(function () {
var toTrack = [
{
selector: '.slider a',
category: 'Slideshow'
},{
selector: '.banner a',
category: 'Banners'
},{
selector: '.nav a',
category: 'Navigation'
}
];
for (var i = 0, len = toTrack.length; i < len; i++) {
var target = $(toTrack[i].selector),
category = toTrack[i].category;
target.each(function () {
new tracking({
target: this,
category: category,
label: $(this).attr('title')
});
});
}
});
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.