Last active
February 27, 2019 19:39
-
-
Save dbrewitz/1a862ceb7383b0c6115792dcd06e5fa3 to your computer and use it in GitHub Desktop.
Google Analytics event tracking
This file contains 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
// 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