Question

The mixpanel api uses success callbacks to trigger code after a tracking event is completed, so that you can be sure that the event is logged before running your next function, like:

leavePage = function(){ window.location = 'http://google.com'; }
mixpanel.track('event', null, leavePage)

I would like to do the same thing with an alias call, like:

mixpanel.alias('am-i-done-yet@example.com', leavePage)

The mixpanel docs don't seem to mention any more than one argument to alias, I guessed at the above undocumented API without success. Anyone know of workarounds to trigger a success event when aliasing is complete?

Was it helpful?

Solution

The mixpanel javascript library does not support a callback to the alias call. However, you can achieve the effect by sending a request to the REST api and using any framework for registering the callback.

https://mixpanel.com/help/reference/http#distinct-id-alias

In fact, since the alias call is implemented in terms of the track call, you should be able to the following code to avoid manually providing the distinct id and token to the alias call.

var leavePage = function(){ window.location = 'http://google.com'; }
var registerEvent = function(){ mixpanel.track('event', null, leavePage); }
mixpanel.track('$create_alias', {'alias': 'am-i-done-yet@example.com'}, registerEvent);

This will make two calls to mixpanel (one to register the alias and one to register the event) and then will cause the page to redirect.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top