Last active
April 22, 2019 10:35
-
-
Save gneutzling/7845452 to your computer and use it in GitHub Desktop.
This is a simple class to track custom events on Google Analytics.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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