Coding: Implementing Alias for Mixpanel in WordPress

I had a bit of trouble implementing Mixpanel’s user tracking in WordPress – some of the functionality needed to be client-side, some server-side. Finally, figured out how to do it. Perhaps this will save you some headache 🙂

First, catch user registrations:

function on_wp_register_user( $user_id )
{

$mp = Mixpanel::getInstance( MIXPANEL_KEY );
$d_id = $_COOKIE[‘mixpanel_distinct_id’];
$mp->createAlias( $d_id, $user_id );
$mp->track( “Created an account” );

}
add_action( ‘user_register’, ‘on_wp_register_user’, 10, 1 );

Then, make sure to pass in mixpanel’s distinct ID (callback for mixpanel.init ):

document.cookie = "mixpanel_distinct_id=" + mixpanel.get_distinct_id();

And that should work. Only took me a whole bunch of hours… FML.

Leave a comment

Filed under Development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s