Skip to content

Instantly share code, notes, and snippets.

@jskopek
Created December 24, 2016 00:18
Show Gist options
  • Save jskopek/a29c3737a1b79e5a0c6f6016265885ef to your computer and use it in GitHub Desktop.
Save jskopek/a29c3737a1b79e5a0c6f6016265885ef to your computer and use it in GitHub Desktop.
// Patch the a `track` method on the mixpanel library
// This method is identical to `mixpanel.track`, but it executes the callback if mixpanel has not returned within 350ms
// Addresses an issue in the mixpanel library where the library has not been loaded (becuase of a timeout or blocked by ad blocker),
// and the initialized version is used instead. Due to a bug in the mixpanel loader, callbacks are never triggered when this occurs
var unpatchedMixpanelTrack = mixpanel.track;
mixpanel.track = function(event_name, properties, callback) {
if(!callback) {
return unpatchedMixpanelTrack(event_name, properties);
} else {
// make it 50ms over mixpanel's default 300ms callback timeout
var callbackSafetyTimeout = setTimeout(function() { callback(); }, 350);
unpatchedMixpanelTrack(event_name, properties, function() {
clearTimeout(callbackSafetyTimeout);
callback();
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment