Search code examples
ruby-on-railsrubymixpanel

Mixpanel Integration


I got this from mixpanel:

Identify your users To keep track of your users, you need to give them an identity. You can use anything that is unique to the user, like a user id from your database or an email address. mixpanel.identify("12148"); We recommend using the same unique id you use to track actions.

I don't get what I should put in "12148". If I want to track a user using userID, what do I put in there? Also, the thing about storing data that I am not sure about.

mixpanel.people.set({

    "$email": "[email protected]",    // only special properties need the $

    "$created": "2011-03-16 16:53:54",
    "$last_login": new Date(),         // properties can be dates...

    "credits": 150,                    // ...or numbers

    "gender": "Male"                    // feel free to define your own properties
});

Solution

  • As the description mentions it:

    you can use anything that is unique to the user, like a user id from your database or an email address

    I would recommend you to use the user id for this purpose.

    When you create a mixpanel profile for your users, you need to identify them uniquely. Since you already have a User model where you identify the users using their id in rails, its a good practice to use the same user.id to identify users on mixpanel.

    Now what I think your confusion is, where did 12148 come from what is it basically?

    Again as mentioned above its a unique identifier for your user mixpanel profile i.e. something or somevalue using which you can fetch mixpanel information about the user.

    Lets go through the example. Suppose you got some user "amksg" in your model.

    How will you retrieve it?

    Most preferably using its id.

    user = User.find 1 #say 1 is the id for amksg
    

    or

    user = User.find_by_email("[email protected]") # using the unique email id
    

    Similarly now setting mixpanel profile for the user:

    mixpanel = Mixpanel::Tracker.new(PROJECT_TOKEN)
    mixpanel.people.set(user.id, {    # we already have user object, setting its ID using the object
        '$first_name'       => 'John',
        '$last_name'        => 'Doe',
        '$email'            => '[email protected]',
        '$phone'            => '5555555555',
        'Favorite Color'    => 'red'
    });
    

    This will create the profile.

    or to use the email as identifier:

    mixpanel.people.set(user.email, {
            '$first_name'       => 'John',
            '$last_name'        => 'Doe',
            '$email'            => '[email protected]',
            '$phone'            => '5555555555',
            'Favorite Color'    => 'red'
        });