Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Richtermeister/91dd190f6b37190ab1e3fd876297ca55 to your computer and use it in GitHub Desktop.
Save Richtermeister/91dd190f6b37190ab1e3fd876297ca55 to your computer and use it in GitHub Desktop.
mixpanel helper
$(function() {
if (window.mixpanel == undefined) {
return;
}
var mixpanelRegex = /\((.*)\)/;
function handleMixpanelTag(tag, $el) {
var bits = tag.split(':');
var parametersJson = mixpanelRegex.exec(tag);
var parameters = [];
if (parametersJson) {
parameters = JSON.parse(parametersJson[1]);
tag = tag.substr(0, parametersJson.index);
}
if (bits[0] == 'view') {
mixpanel.track(tag, parameters);
} else if (bits[0] == 'click') {
$el.click(function(){
mixpanel.track(tag, parameters);
})
}
}
$('[data-mixpanel]').each(function(){
handleMixpanelTag($(this).data('mixpanel'), $(this));
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment