If you ever need help, please email us at support@mixpanel.com
Back to topics

A tutorial on exporting data

Updated Sept. 11, 2012
This article is about exporting data programmatically via our API. If you would like to download CSV versions of our reports, please read about the export functionality of the individual reports

The Scenario

Mixpanel provides a robust API for exporting data. For purposes of this tutorial, we will assume you are measuring a "Player Created" event in a game. Below is the graph of the event in the Segmentation report:

This is the data we'll pull programatically.

The goal of this tutorial is to pull down the data points programmatically. We'll use PHP, but any language will do.

Get your project's key, and secret

To grab the data for the segmentation report, the first thing you'll need is your project's key and secret. These are available in the settings page of your account

Download a library for the language of your choice

Though you could craft an HTTP request manually using the details here, for this tutorial we'll use the PHP library. Data export libraries are available for Python, PHP, Ruby and Javascript.

Create a new instance of the Mixpanel class

Include the Mixpanel class either above the following code or by including it. Then create a new instance of it with your key and secret as constructor arguments. Using the key and secret from before as an example:

    $api_key = 'a18776c73645814738da01a5130caf67';
    $api_secret = 'c07c26e129c2ebb30e9d5439a24659d0';
    $mp = new Mixpanel($api_key, $api_secret);

Choose your API endpoint

In this case, we just want the raw totals for a certain event, broken out by day. The best API endpoint to use for this is the Segmentation endpoint. The documentation tells us that the URI for this is "http://mixpanel.com/api/2.0/segmentation/." Any part of the URI that comes after "http://mixpanel.com/api/2.0/" (in this case, 'segmentation') is the identifier the library uses to identify the endpoint. It will be the first argument of the request() method. Note, it should be submitted as the only element of an array:

    $endpoint = array('segmentation');
    $data = $mp->request($endpoint,... //to be continued

Pick your parameters

The segmentation endpoint requires that you set the following parameters:


Required Name Type Description
required event string

The event that you wish to segment on.

required from_date string

The date in yyyy-mm-dd format from which to begin querying for the event from. This date is inclusive.

required to_date string

The date in yyyy-mm-dd format from which to stop querying for the event from. This date is inclusive. The date range may not be more than 30 days.

In this case, the event is "Player Create", the from_date is "2011-11-06" and the to_date is "2011-11-08." These should be submitted as the second argument of request(), as an associative array:

    $parameters = array( 
    'event' => 'Player Create', 
    'from_date' => '2011-11-06', 
    'to_date' => '2011-11-08', 
    $data = $mp->request($endpoint,$parameters);

Putting it all together

Here is the completed program

     * PHP library for Mixpanel data API -- http://www.mixpanel.com/
     * Requires PHP 5.2 with JSON
    class Mixpanel
    	private $api_url = 'http://mixpanel.com/api';
    	private $version = '2.0';
    	private $api_key;
    	private $api_secret;
    	public function __construct($api_key, $api_secret) {
    		$this->api_key = $api_key;
    		$this->api_secret = $api_secret;
    	public function request($methods, $params, $format='json') {
    		// $end_point is an API end point such as events, properties, funnels, etc.
    		// $method is an API method such as general, unique, average, etc.
    		// $params is an associative array of parameters.
    		// See https://mixpanel.com/docs/api-documentation/data-export-api
    		if (count($params) < 1)
    			return false;
    		if (!isset($params['api_key']))
    			$params['api_key'] = $this->api_key;
    		$params['format'] = $format;
    		if (!isset($params['expire'])) {
    			$current_utc_time = time() - date('Z');
    			$params['expire'] = $current_utc_time + 100000; // Default 10 minutes
    		$param_query = '';
    		foreach ($params as $param => &$value) {
    			if (is_array($value))
    				$value = json_encode($value);
    			$param_query .= '&' . urlencode($param) . '=' . urlencode($value);
    		$sig = $this->signature($params);
    		$uri = '/' . $this->version . '/' . join('/', $methods) . '/';
    		$request_url = $uri . '?sig=' . $sig . $param_query;
    		curl_setopt($curl_handle,CURLOPT_URL,$this->api_url . $request_url);
    		$data = curl_exec($curl_handle);
    		return json_decode($data);
    	private function signature($params) {
            $param_string ='';
    		foreach ($params as $param => $value) {
    			$param_string .= $param . '=' . $value;
    		return md5($param_string . $this->api_secret);
    //Insert Key and Secret
    $api_key = 'a18776c73645814738da01a5130caf67';
    $api_secret = 'c07c26e129c2ebb30e9d5439a24659d0';
    //Create Mixpanel Object
    $mp = new Mixpanel($api_key, $api_secret);
    //Create single-entry array with API endpoint
    $endpoint = array('segmentation');
    //Create array of properties to send
    $parameters = array( 
    'event' => 'Player Create', 
    'from_date' => '2011-11-06', 
    'to_date' => '2011-11-08'
    //Make the request
    $data = $mp->request($endpoint,$parameters);
    //print the result

Understanding the returned data

Most libraries return the results as an object. Here is the printed display of the object returned for our query:

    stdClass Object
        [legend_size] => 1
        [data] => stdClass Object
                [series] => Array
                        [0] => 2011-11-06
                        [1] => 2011-11-07
                        [2] => 2011-11-08
                [values] => stdClass Object
                        [Player Create] => stdClass Object
                                [2011-11-06] => 3592
                                [2011-11-07] => 4052
                                [2011-11-08] => 7137

The API returned the event totals for the three days that we queried it for.

Next Steps

Please review the full data API to learn how to access all the Mixpanel reports.