Skip to content

Instantly share code, notes, and snippets.

@MxAshUp
Created October 14, 2014 21:38
Show Gist options
  • Save MxAshUp/0b70e0a970b9fdc4c881 to your computer and use it in GitHub Desktop.
Save MxAshUp/0b70e0a970b9fdc4c881 to your computer and use it in GitHub Desktop.
Track Google Analytics Events in either Classic or Universal Analytics
//This function tracks a google analytics event regaurdless if using UA or classic trackin code
/**
* Tracks a GA event using either Classic or Universal Analytics.
* @param {[String or Object]} categoryorobject [Either Event Category, or an object containing all parameters]
* @param {[String]} action [The Event Action]
* @param {[String]} label [The Event Lable]
* @param {[String]} value [The Event Value]
*/
function trackEvent(categoryorobject, action, label, value) {
if(typeof(ga) == "function") {
//check argument length to determine parameters
switch(arguments.length) {
case 1:
//If there is one parameter, it can only be an object to define parameters
if(typeof categoryorobject == "object" && categoryorobject.eventCategory && categoryorobject.eventAction) {
//Send it off like it is!
categoryorobject.hitType = 'event';
return ga('send', categoryorobject);
break;
}
case 2:
return ga('send', 'event', categoryorobject, action);
break;
case 3:
return ga('send', 'event', categoryorobject, action, label);
break;
case 4:
return ga('send', 'event', categoryorobject, action, label, value);
break;
default:
//Oops!
return false;
}
} else if(typeof(_gaq) == "object") {
switch(arguments.length) {
case 1:
//If there is one parameter, it can only be an object to define parameters
if(typeof categoryorobject == "object") {
//The onyl reason you may need to pass an object, is to define the hitcallback, so let's set it
if(categoryorobject.hitCallback && typeof categoryorobject.hitCallback == "function") {
_gaq.push(['_set', 'hitCallback', function(){
categoryorobject.hitCallback.apply();
}]);
}
//Check remaining object properties for optional parameters
//Will make a recursive call with properties passed as normal parameters
if(categoryorobject.eventValue && categoryorobject.eventLabel && categoryorobject.eventAction && categoryorobject.eventCategory) {
trackEvent(categoryorobject.eventCategory,categoryorobject.eventAction,categoryorobject.eventLabel,categoryorobject.eventValue);
} else if(categoryorobject.eventLabel && categoryorobject.eventAction && categoryorobject.eventCategory) {
trackEvent(categoryorobject.eventCategory,categoryorobject.eventAction,categoryorobject.eventLabel);
} else if(categoryorobject.eventAction && categoryorobject.eventCategory) {
trackEvent(categoryorobject.eventCategory,categoryorobject.eventAction);
}
}
case 2:
return _gaq.push(['_trackEvent', categoryorobject, action]);
break;
case 3:
return _gaq.push(['_trackEvent', categoryorobject, action, label]);
break;
case 4:
return _gaq.push(['_trackEvent', categoryorobject, action, label, value]);
break;
default:
//Oops!
return false;
}
}
}
/**
* Here's an example for tracking an event using hitCallback
*/
trackEvent( {'eventCategory':'quote-form',
'eventAction':'submitted',
'eventLabel':'contact-me',
'eventValue':0,
'hitCallback': function() {
thisForm.submit(); //Called after event is logged
}
});
//Without hitcallback we can use the standard parameters
trackEvent('quote-form','submitted','contact-me',0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment