Skip to content

Instantly share code, notes, and snippets.

@keithmgould
Created November 13, 2011 20:38
Show Gist options
  • Save keithmgould/1362648 to your computer and use it in GitHub Desktop.
Save keithmgould/1362648 to your computer and use it in GitHub Desktop.
<script type="text/javascript">
var mpq = [];
mpq.push(["init", "1c8c35d989982401990706fce6659790"]);
(function(){var b,a,e,d,c;b=document.createElement("script");b.type="text/javascript";b.async=true;b.src=(document.location.protocol==="https:"?"https:":"http:")+"//api.mixpanel.com/site_media/js/api/mixpanel.js";a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(b,a);e=function(f){return function(){mpq.push([f].concat(Array.prototype.slice.call(arguments,0)))}};d=["init","track","track_links","track_forms","register","register_once","identify","name_tag","set_config"];for(c=0;c<d.length;c++){mpq[d[c]]=e(d[c])}})();
</script>
<script type="text/javascript">
var MixPanelHelp = function() {
return {
// augments an event with basic info such as current user
// then fires the event off
track : function(eventName, moreProperties, callback) {
var properties = new Array();
if (typeof optionalArg != "undefined"){
properties = $.extend(moreProperties);
}
properties["url"] = document.URL;
// add in the current user if there is one
if(current_user){
properties["username"] = current_user.hash.username;
properties["gender"] = current_user.hash.gender;
properties["zipcode"] = current_user.hash.zipcode;
}
// make the callback
if (typeof callback == "undefined"){
mpq.track(eventName,properties);
}else{
mpq.track(eventName,properties, callback);
}
},
// fires our track wrapper, protected by a delay
delayed_track : function(eventName, moreProperties, callback){
var t = window.setTimeout(callback,300);
MixPanelHelp.track(eventName,moreProperties);
// make the callback
if (typeof callback == "undefined"){
mpq.track(eventName,properties);
}else{
mpq.track(eventName,properties, callback);
}
}
};
}();
$(function() {
// MixPanelHelp ephemeral link tracker
// Used on links that would otherwise
// not give the event enough time to fire.
// NOTE: Requires data-trackname to be set
// ex: <a href="google.com" class="track_link", data-trackname="to Google">go to google</a>
$('.track_link').click(function(e) {
e.preventDefault();
var href = $(this).attr('href');
var t = window.setTimeout("window.location = href",300);
MixPanelHelp.track($(this).data("trackname"),"",function(){
window.clearTimeout(t);
window.location = href;
});
});
// MixPanelHelp ephemeral button tracker
// Used on buttons that would otherwise
// not give the event enough time to fire
// (Such as form submission buttons)
// NOTE: Requires data-trackname to be set
$('.track_button').click(function(e) {
e.preventDefault();
var form = $(this).parents("form")[0];
var t = window.setTimeout("form.submit()",300);
MixPanelHelp.track($(this).data("trackname"),"",function(){
window.clearTimeout(t);
form.submit();
});
});
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment