Created
September 6, 2017 20:45
-
-
Save fathermerry/68f1927a9ab3637e60cb1986cfe2a9d6 to your computer and use it in GitHub Desktop.
Event tracking with Google Analytics
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
var signupLinks = document.querySelectorAll('[data-signup]'); | |
for (i = 0; i < signupLinks.length; i++) { | |
signupLinks[i].addEventListener("click", function(event){ | |
event.preventDefault(); | |
var eventLabel = event.target.getAttribute('data-signup'); | |
var gaIsLoaded = window[window['GoogleAnalyticsObject'] || 'ga']; | |
if (gaIsLoaded) { | |
ga('send', 'event', { | |
eventCategory: 'Signup', | |
eventAction: 'click', | |
eventLabel: eventLabel | |
}); | |
} | |
window.location = event.target.getAttribute('href'); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment