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.