Created
June 28, 2018 02:53
-
-
Save cdmo/e8283ce56e2ebfe252902fc709de54c1 to your computer and use it in GitHub Desktop.
Event tracking for google analytics on the penn state bento search results page
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
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