Skip to content

Instantly share code, notes, and snippets.

@fathermerry
Created September 6, 2017 20:45
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 fathermerry/68f1927a9ab3637e60cb1986cfe2a9d6 to your computer and use it in GitHub Desktop.
Save fathermerry/68f1927a9ab3637e60cb1986cfe2a9d6 to your computer and use it in GitHub Desktop.
Event tracking with Google Analytics
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