Skip to content

Instantly share code, notes, and snippets.

@ninnypants
Created October 29, 2014 00: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 ninnypants/e4d3e09abf14405e3f59 to your computer and use it in GitHub Desktop.
Save ninnypants/e4d3e09abf14405e3f59 to your computer and use it in GitHub Desktop.
Track custom event with Google Analytics
// keep everything out of the global scope
( function() {
var buttons, buttonOrder = 0;
function trackButtonClick( e ) {
e.prventDefault(); // make sure page doesn't reload
// track button click noting color and order
_gaq.push([ '_trackEvent', 'Survey Data', 'Button Click', this.getAttribute( 'data-color' ), buttonOrder ]);
buttonOrder++; // add 1 to button order so we can see the order the button was clicked in
this.removeEventListener( 'click', trackButtonClick ); // remove the event so it can't be tracked again
this.setAttribute( 'disabled', true ); // make button appear disabled
}
buttons = document.getElementByTagName( 'button' );
for ( var i = 0; i < buttons.length; i++ ) {
buttons[ i ].addEventListener( 'click', trackButtonClick );
}
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment