Python API Integration

The Mixpanel Python library is designed to be used for scripting, or in circumstances when a user isn't directly interacting with your application on the web or a mobile device.

Mixpanel also provides a powerful and easy to use client-side JavaScript library for web applications. This library offers platform-specific features and conveniences that can make Mixpanel implementations much simpler, and can scale naturally to an unlimited number of clients. The client-side JavaScript library is the preferred way to add Mixpanel to user-facing web applications.

Installing the library

You can get the library using pip.

pip install mixpanel

Once the library is installed, use the Mixpanel library in your applications with:

import mixpanel

Sending events

Mixpanel events are sent using an instance of the Mixpanel class.

You can instantiate an instance of Mixpanel with a String containing your Mixpanel project token. You can find your project token in the settings dialog of the Mixpanel web application.

Once you have an instance of the tracker, you can track events by providing the event name and properties to Mixpanel.track().

from mixpanel import Mixpanel

mp = Mixpanel(PROJECT_TOKEN)

# Tracks an event, 'Sent Message',
# with distinct_id user_id
mp.track(user_id, 'Sent Message')

# You can also include properties to describe
# the circumstances of the event
mp.track(user_id, 'Plan Upgraded', {
    'Old Plan': 'Business',
    'New Plan': 'Premium'
})

Mixpanel determines default geolocation data ($city, $region, mp_country_code) using the IP address on the incoming request. As all server-side calls will likely originate from the same IP (that is, the IP of your server), this can have the unintended effect of setting the location of all of your users to the location of your datacenter. Read about best practices for geolocation with server-side implementations.

Linking two user IDs

In situations where you want to link the two IDs (in practice, this really just means when a previously anonymous user signs up) you can use Mixpanel.alias(). Alias sends an update to our servers linking an existing distinct_id with a new id, so that events and profile updates associated with the new id will be associated with the existing user's profile and behavior.

# This call will send a request to Mixpanel to
# associate all future events with new_internal_id
# with existing events associated with
# original_anonymous_id
mp.alias(new_internal_id, original_anonymous_id)

Typically, you will call Mixpanel.alias() at most once in a user's whole lifetime, when the user first signs up and is assigned an internal id.

Aliases don't take effect until the alias request hits the Mixpanel server. Because of this, you'll need to take special care if you're using Mixpanel.alias() with a custom consumer, so you can be sure that your alias message arrives before any events or updates associated with the new alias.

Storing user profiles

In addition to events, you can send People Analytics profile updates to Mixpanel. Mixpanel can maintain a profile of each of your users, storing information you know about them. An update is a message that changes the properties of a People Analytics profile.

You can use profiles to explore and segment users by who they are, rather than what they did. You can also use profiles to send notifications, such as emails, SMS, or push notifications.

Mixpanel determines default geolocation data ($city, $region, mp_country_code) using the IP address on the incoming request. As all server-side calls will likely originate from the same IP (that is, the IP of your server), this can have the unintended effect of setting the location of all of your users to the location of your datacenter. Read about best practices for geolocation with server-side implementations.

Setting profile properties

Instances of Mixpanel have a method to send profile updates.

# create or update a profile with First Name, Last Name,
# E-Mail Address, Phone Number, and Favorite Color
mp.people_set('12345', {
    '$first_name'    : 'John',
    '$last_name'     : 'Doe',
    '$email'         : 'john.doe@example.com',
    '$phone'         : '5555555555',
    'Favorite Color' : 'red'
})

This call to people_set() will change the value of properties on user 12345's profile. If there isn't a profile with distinct_id 12345 in Mixpanel already, a new profile will be created. If user 12345 already has has any of these properties set on their profile, the old values will be overwritten with the new ones.

Appending to list properties

Use people_append() to add an item to an existing list-valued property. The values you send with the append will be added to the end of the list for each named property. If the property doesn't exist, it will be created with a one element list as its value.

mp.people_append('12345', {
    'Favorite Fruits' : 'Apples'
})

Other types of profile updates

There are a few other types of profile updates. You can get more information about them from the Mixpanel Library API Reference.

Tracking revenue

Mixpanel makes it easy to analyze the revenue you make from individual customers. By associating charges with user profiles, you can compare revenue across different customer segments and calculate things like lifetime value.

You can track a single transaction with Mixpanel.people_track_charge(). Sending a message created with people_track_charge will add transactions to the individual user profile, which will also be reflected in the Mixpanel Revenue report.

# Records a charge of $9.99 from user '12345'
mp.people_track_charge('12345', 9.99)

# records a charge of $30.50 on the 2nd of January
mp.people_track_charge("12345", 30.50, {
    '$time': '2013-01-02T09:32:00'
})

Scaling your server-side tracking

By default, the Mixpanel class sends a request to Mixpanel immediately for every tracking message or profile update. This is convenient for getting started quickly, but almost all server-side use of the Mixpanel library will eventually want to do the IO associated with tracking in a separate thread or process from the events being tracked.

The Mixpanel library provides the Consumer class for finer control of your tracking IO.

In addition to your token, the Mixpanel constructor takes an optional consumer argument. This argument, if provided and not None, should be an object with a method named send(), that takes two arguments:

endpoint
string
Either the string 'events' (for messages intended to go to the Mixpanel /track endpoint), or the string 'people' (for messages intended for the Mixpanel /people endpoint).
json_message
string
A JSON message, encoded in a string, that can be used by the service at the named endpoint. The structure of meaningful JSON messages is described in the Mixpanel HTTP API reference.

You can use the send() method of your Consumer to send your messages to a separate process, add them to a queue, or write them to a log.

class LoggingConsumer(object):
    def __init__(self):
        self.mp_log = open("MIXPANEL_LOG.txt", "w+")

    def send(self, endpoint, json_message):
        self.mp_log.write("{0}::{1}\n".format(endpoint, message))

# Whenever you track with logging_mp, your messages will
# be written to MIXPANEL_LOG.txt rather than being sent
# to the Mixpanel servers
logging_mp = Mixpanel(YOUR_TOKEN, LoggingConsumer())

Using a custom consumer is most powerful when you combine it with the existing consumers in the mixpanel module.

import mixpanel

# The default Mixpanel consumer will take
# endpoints and messages and send them to Mixpanel
consumer = mixpanel.Consumer()
with open("MIXPANEL_LOG.txt", "r+") as read_log:
    for line in read_log:
        (endpoint, message) = line.split('::', 1)
        consumer.send(endpoint, message)

The combination of package-provided consumers and your own custom consumer makes it simple to use the Mixpanel library with a queueing system. For example:

# In your time-sensitive process
class EnqueueingConsumer(object):
    def send(self, endpoint, json_message):
        YOUR_QUEUE.set('mixpanel_queue', JSON.dumps([ endpoint, json_message ]))

mp = mixpanel.Mixpanel(YOUR_TOKEN, EnqueueingConsumer())

# Track just like you would in any other situation
mp.track(user_id, 'Sent Message')
mp.people_increment(user_id, {
    'Messages Sent': 1
})

# In a worker process on another machine
consumer = mixpanel.Consumer()
while True:
    job = YOUR_QUEUE.get('mixpanel_queue')
    consumer.send(*JSON.loads(job))

For applications where overall load is light, but the latency of a particular process is an issue, you can also use mixpanel-python-async, a third party consumer that processes tracking requests efficiently in a separate thread.