Skip to content

Instantly share code, notes, and snippets.

@yahelc
Created February 2, 2012 23:54
Show Gist options
  • Save yahelc/1726582 to your computer and use it in GitHub Desktop.
Save yahelc/1726582 to your computer and use it in GitHub Desktop.
$.fn.analytics plugin
$("#foo").analytics(function(){});
//that's just a simplified and less-error prone way of doing
$("#foo").one("mousedown keydown", function(){});
//(with special fixes for <form> submits and non-Enter mousedowns)
//If you specify a callback function, `this` is bound to
//the DOM element, like in any jQuery function
$("#foo").analytics(function(){ console.log( this.id, $(this).attr("href") ); });
// =============
// Special Syntactic Sugar for Analytics Functions
// These only trigger when you pass a non-function argument
//IMPORTANT: NONE OF THE BELOW HAVE ACCESS TO `this`.
$("#foo").analytics(["category", "action", "label", 1]);
//GA syntactic sugar, same as:
$("#foo").analytics(function(){
_gaq.push(["category", "action", "label", 1]);
});
//Optimizely
//pass a string, it'll trigger optimizely.push(["trackEvent","foo"]);
$("#foo").analytics("foo");
Same as:
$("#foo").analytics(function(){ optimizely.push(["trackEvent","foo"]); });
//Optimizely with Revenue Tracking:
//pass a two element array (where the 2nd element is a number),
$("#foo").analytics(["foo", 100]);
//it'll trigger optimizely.push(["trackEvent","foo", 100]); Same as:
$("#foo").analytics(function(){ optimizely.push(["trackEvent","foo", 100]); });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment