Integration guides

Logging data with Javascript

If you want to track things like button clicks, it's easiest to use Ajax. We have a javascript file that you can load located at http://api.mixpanel.com/site_media/js/api/mixpanel.js This is really simple to use and it has no external dependencies.


Setup

To start tracking with Javascript, paste the following code into the page you want to track. Make sure to change 'YOUR_TOKEN' to the project token from your Mixpanel dashboard.

If you want, you can also log from other javascript functions. If you are doing a bunch of javascript, you may want to track each time a user interacts with something:

This will send off an Ajax request each time the function is called, which comes in really handy for javascript-heavy sites.

Tracking things on page load

If you want to track events that occur when the user arrives at the page, it's important to wait until the document is fully loaded. You can avoid errors in Internet Explorer by calling:

It's important to wait because mpmetrics.track() appends an element to the body of the page, which you don't want to do while the page is still loading.

Tracking navigation

If you try tracking a link with an onclick() event, there won't be enough time for the data to be sent before the page changes. What you can do instead is track the event and then move the page with a callback.

Advanced notes

If you decide to use a variable that is not mpmetrics when you instantiate MixpanelLib you will also need to pass the object name as well so that our callbacks fire off correctly and leave no errors on your page. This is done very simply:

var newObj = new MixpanelLib("YOUR TOKEN", "newObj");

Javascript Library API

MixpanelLib(token:string)
    Initialize a Mixpanel object.
    
    Required:   
        @token: Your unique project token
        
    Usage:
        var mpmetrics = new MixpanelLib('ds098dssll1122k2jd');

mpmetrics.track(event:string, properties:object, callback:function)
    Track an event.  This is an object method, so you must initialize a 
    MixpanelLib object before use.
    
    Required:
        @event: Name of event
    Optional:
        @properties: Properties for this event.
        @callback: Anonymous function to call after tracking the event.
        
    Example: mpmetrics.track("Open slideshow", {'show': 'Xmas'}, function(){ myfunction() });
        
mpmetrics.track_funnel(funnel:string, step:int, goal:string, properties:object, callback:function)
    Track a funnel step. This is an object method, so you must initialize a 
    MixpanelLib object before use.
    
    Required:
        @funnel: Funnel name, same for all steps of a single funnel
        @step: Step number, starting with 1
        @goal: Human readable name for this step
    Optional:
        @properties: Properties for this funnel step.
        @callback: Anonymous function to call after tracking the funnel step.
        
    Example: mpmetrics.track_funnel("Signup", 1, "Landing page", {'from': 'Google'});
        
mpmetrics.register(properties:object, type:string, days:int)
    Register a set of super properties, which are included with all events/funnels.
    This will overwrite previous super property values. This is an object method, so you must initialize a 
    MixpanelLib object before use.
    
    Required: 
        @properties: associative array of properties to store about the user
    Optional:
        @type: "all", "events", or "funnels".  Determines the types of events to send this data with.
                Default "all".
        @days: Age of cookie in days. The cookie is set frequently so this has little effect.
                Default 7.
        
    Example: mpmetrics.register({'user type': 'free trial', 'status': 4}, "all");
        
mpmetrics.register_once(properties:object, type:string, default_value, days:int)
    Register a set of super properties only once.  This will not overwrite previous super property 
    values, unlike register(). However, if default_value is specified, current super properties 
    with that value will be overwritten.
    
    Required: 
        @properties: associative array of properties to store about the user
    Optional:
        @type: "all", "events", or "funnels".  Determines the types of events to send this data with.
                Default "all".
        @default_value: Value to override if already set in super properties (ex "False")
                Default "None".
        @days: Age of cookie in days. The cookie is set frequently so this has little effect.
                Default 7.
    Example: mpmetrics.register_once({'user type': 'free trial'}, "all", "False", 31);

Logging data with Javascript for Facebook apps

If you are writing iframe apps, you can go ahead and use our normal Javascript library, located at http://api.mixpanel.com/site_media/js/api/mixpanel.js. The integration guide for standard Javascript is located here.

However, if you are using FBML, that library won't work - it doesn't play nice with the Facebook sandbox. We have a separate library, http://api.mixpanel.com/site_media/js/api/mixpanel_fb.js, that works perfectly with Facebook. Usage is exactly the same, and we get all of the data (including IP address) that we do with the normal version.


Setup for FBML apps

To use this javascript library, add a link to it from the page you want to track. Make sure to change 'YOUR_TOKEN' to your project token from the Mixpanel dashboard.

Then, if you want to track an HTML element you can use mpmetrics.track() as the onclick function:

If you want, you can also log from other javascript functions. If you are doing a bunch of javascript, you may want to track each time a user interacts with something:

Each time the mpmetrics.track() event is fired, it will add a transparent pixel to your page. These are set so that there can be a maximum of 100 images loaded at once - once a user has interacted more than 100 times, we remove a pixel before adding a new one. This will ensure that Mixpanel will not cause performance issues on your end, but gives us enough time to log all of your data before the pixel is removed.

Logging data with Python

So, you want to log data to Mixpanel from your backend, and your application is written in Python.

You have two options:

1. If you don't have a huge amount of traffic, you can go with the simple python example code located at the code examples page. This is simpler to install and use.

2. If you are worried about performance, we suggest using RabbitMQ, which allows you to efficiently send logs to a workqueue, where they are processed by another program. To learn more about RabbitMQ, please visit Rabbits and warrens.


If you are using the cURL logger from the example code page, first save the code to a file called logger.py. Then, to log data, import the file and call the log function:

If you are using Django, an easy way to grab the user's IP address is through request.META.get('REMOTE_ADDR').

Logging data with PHP

So, you want to log data to Mixpanel from your backend, and your application is written in PHP.

You have two options:

1. If you don't have a huge amount of traffic, you can go with the simple PHP example code located at the code examples page. This is simpler to install and use.

2. If you are worried about performance, we suggest using RabbitMQ, which allows you to efficiently send logs to a workqueue, where they are processed by another program. To learn more about RabbitMQ, please visit Rabbits and warrens.


If you are using the cURL logger from the example code page, first save the code to a file called logger.php. Then, to log data, import the file, create a new MetricsTracker object and call the track function. You'll notice that it's possible to include the visitor's IP address with the request, which allows us to calculate unique visitors and geolocation data.