Skip to content

Instantly share code, notes, and snippets.

@swmcc
Created June 11, 2014 10:26
Show Gist options
  • Save swmcc/eb21878d013fe5f632e9 to your computer and use it in GitHub Desktop.
Save swmcc/eb21878d013fe5f632e9 to your computer and use it in GitHub Desktop.
GA Click Handler
// attach GA click handler
// note: the GA code is stored in a 'data-ga' element
// instead of inserting onclicks directly into the markup.
// <a data-ga="Event, Action, Label"
// id="mode-switch-devices"
// class="ajax"
// title=""
// href=""></a>
$('a').click(function(){
if (typeof $(this).data('ga') === "undefined") {
if (logging == true) {
// Take attributes of the element and fire through instead
// of logging
console.warn('no data-ga for clicked element: ', $(this));
}
} else {
var gaArray = $(this).data('ga').replace(/\s/g,'').split(',');
//console.log(gaArray);
_gaq.push(['_trackEvent', gaArray[0], gaArray[1], gaArray[2]]);
if (logging == true) {
console.log('fired GA: ', ['_trackEvent', gaArray[0], gaArray[1], gaArray[2]]);
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment