Skip to content

Instantly share code, notes, and snippets.

@dbrewitz
Last active February 27, 2019 19:39
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 dbrewitz/1a862ceb7383b0c6115792dcd06e5fa3 to your computer and use it in GitHub Desktop.
Save dbrewitz/1a862ceb7383b0c6115792dcd06e5fa3 to your computer and use it in GitHub Desktop.
Google Analytics event tracking
// Universal Analytics events that include common element attributes
var gaPush = {
p: function (element, cat, action, type) {
var el = document.querySelectorAll(element)
for (var i = 0; i < el.length; i++) {
el[i].addEventListener('click', function () {
var attr = {
title: eval('this.title'),
alt: eval('this.alt'),
value: eval('this.value'),
href: eval('this.href'),
text: eval('this.text')
}
var label = attr[type]
ga('send', 'event', cat, action, label);
});
};
}
}
// set your type to what attribute you want sent - title, alt, value, href, or text
// uncomment following line to test on any link click
// gaPush.p('a', 'hyperlink', 'Click link', 'text')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment