Mixpanel – event tracking

Mixpanel tracks events and actions to see what features users are using the most and how they are trending. You could use it for real-time analysis of visitor retention or funnels.

Installation

To start using the Mixpanel integration, you must have installed the django-analytical package and have added the analytical application to INSTALLED_APPS in your project settings.py file. See Installation and configuration for details.

Next you need to add the Mixpanel template tag to your templates. This step is only needed if you are not using the generic analytical.* tags. If you are, skip to Configuration.

The Mixpanel Javascript code is inserted into templates using a template tag. Load the mixpanel template tag library and insert the mixpanel tag. Because every page that you want to track must have the tag, it is useful to add it to your base template. Insert the tag at the bottom of the HTML head:

{% load mixpanel %}
...
{% mixpanel %}
</head>
<body>
...

Configuration

Before you can use the Mixpanel integration, you must first set your token.

Setting the token

Every website you track events for with Mixpanel gets its own token, and the mixpanel tag will include it in the rendered Javascript code. You can find the project token on the Mixpanel projects page. Set MIXPANEL_API_TOKEN in the project settings.py file:

MIXPANEL_API_TOKEN = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

If you do not set a token, the tracking code will not be rendered.

Internal IP addresses

Usually you do not want to track clicks from your development or internal IP addresses. By default, if the tags detect that the client comes from any address in the MIXPANEL_INTERNAL_IPS setting, the tracking code is commented out. It takes the value of ANALYTICAL_INTERNAL_IPS by default (which in turn is INTERNAL_IPS by default). See Identifying authenticated users for important information about detecting the visitor IP address.

Identifying users

If your websites identifies visitors, you can pass this information on to Mixpanel so that you can tie events to users. By default, the username of an authenticated user is passed to Mixpanel automatically. See Identifying authenticated users.

You can also send the visitor identity yourself by adding either the mixpanel_identity or the analytical_identity variable to the template context. If both variables are set, the former takes precedence. For example:

context = RequestContext({'mixpanel_identity': identity})
return some_template.render(context)

If you can derive the identity from the HTTP request, you can also use a context processor that you add to the TEMPLATE_CONTEXT_PROCESSORS list in settings.py:

def identify(request):
    try:
        return {'mixpanel_identity': request.user.email}
    except AttributeError:
        return {}

Just remember that if you set the same context variable in the RequestContext constructor and in a context processor, the latter clobbers the former.

Mixpanel can also receive properties for your identified user, using mixpanel.people.set. If want to send extra properties, just set a dictionary instead of a string in the mixpanel_identity context variable. The key id or username will be used as the user unique id, and any other key-value pair will be passed as people properties. For example:

def identify(request):
    try:
        return {
            'mixpanel_identity': {
                'id': request.user.id,
                'last_login': str(request.user.last_login),
                'date_joined': str(request.user.date_joined),
            }
        }
    except AttributeError:
        return {}

Tracking events

The django-analytical app integrates the Mixpanel Javascript API in templates. To tracking events in views or other parts of Django, you can use Wes Winham’s mixpanel-celery package.

If you want to track an event in Javascript, use the asynchronous notation, as described in the section titled “Asynchronous Tracking with Javascript” in the Mixpanel documentation. For example:

mixpanel.track("play-game", {"level": "12", "weapon": "sword", "character": "knight"});