Skip to content

Instantly share code, notes, and snippets.

@gneutzling
Last active April 22, 2019 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gneutzling/7845452 to your computer and use it in GitHub Desktop.
Save gneutzling/7845452 to your computer and use it in GitHub Desktop.
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