Skip to content

Instantly share code, notes, and snippets.

@cdmo
Created June 28, 2018 02:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdmo/e8283ce56e2ebfe252902fc709de54c1 to your computer and use it in GitHub Desktop.
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
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