Skip to content

Instantly share code, notes, and snippets.

@cdmo cdmo/eventracking.js
Created Jun 28, 2018

Embed
What would you like to do?
Event tracking for google analytics on the penn state bento search results page
function EventTrackingBento(element, category) {
var tags = {};
var category = category;
this.boxes = function() {
return document.querySelectorAll(element);
};
this.tagEvents = function() {
if (this.boxes) {
jQuery.each(this.boxes(), function(key, box) {
var boxName = box.querySelector('a').getAttribute('name');
findLinks(box, category, boxName);
});
}
};
var findLinks = function(box, category, boxName) {
var links = box.querySelectorAll('a[href]');
bindLinks(links, category, boxName);
};
var bindLinks = function(links, category, boxName) {
jQuery.each(links, function(key, link) {
link.addEventListener('click', function(event) {
ga('send', 'event', {
eventAction: boxName,
eventCategory: category,
eventLabel: link.dataset.name
});
});
});
}
}
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.