Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Example Mixpanel raw event export script
#! /usr/bin/env python
# Mixpanel, Inc. --
# Python API client library to consume analytics data.
import hashlib
import urllib
import time
import json
except ImportError:
import simplejson as json
class Mixpanel(object):
VERSION = '2.0'
def __init__(self, api_key, api_secret):
self.api_key = api_key
self.api_secret = api_secret
def request(self, methods, params):
methods - List of methods to be joined, e.g. ['events', 'properties', 'values']
will give us
params - Extra parameters associated with method
params['api_key'] = self.api_key
params['expire'] = int(time.time()) + 600 # Grant this request 10 minutes.
if 'sig' in params: del params['sig']
params['sig'] = self.hash_args(params)
request_url = '/'.join([self.ENDPOINT, str(self.VERSION)] + methods) + '/?' + self.unicode_urlencode(params)
request = urllib.urlretrieve(request_url, output_file)
def unicode_urlencode(self, params):
Convert lists to JSON encoded strings, and correctly handle any
unicode URL parameters.
if isinstance(params, dict):
params = params.items()
for i, param in enumerate(params):
if isinstance(param[1], list):
params[i] = (param[0], json.dumps(param[1]),)
return urllib.urlencode(
[(k, isinstance(v, unicode) and v.encode('utf-8') or v) for k, v in params]
def hash_args(self, args, secret=None):
Hashes arguments by joining key=value pairs, appending a secret, and
then taking the MD5 hex digest.
for a in args:
if isinstance(args[a], list): args[a] = json.dumps(args[a])
args_joined = ''
for a in sorted(args.keys()):
if isinstance(a, unicode):
args_joined += a.encode('utf-8')
args_joined += str(a)
args_joined += '='
if isinstance(args[a], unicode):
args_joined += args[a].encode('utf-8')
args_joined += str(args[a])
hash = hashlib.md5(args_joined)
if secret:
elif self.api_secret:
return hash.hexdigest()
api_key = raw_input("API Key: ")
api_secret = raw_input("API Secret: ")
from_date = raw_input("From Date: ")
to_date = raw_input("To Date: ")
output_file = raw_input("Output Filename: ")
api = Mixpanel(
api_key = api_key,
api_secret = api_secret
data = api.request(['export'], {
#'event': [''],
'from_date': from_date,
'to_date': to_date,
#'where': ''

This comment has been minimized.

Copy link

@tdevitt tdevitt commented Aug 6, 2015

For output_file, what kind of file do you save this as? I read that the endpoint returns jsonlines so do you set the file string as "somefilename.jsonl" ? How do you open said file?

Also, do you have an example that uses a 'where' parameter? I'm trying to filter on a specific distinct_id and not getting back any data, so I assume I am either treating the file incorrectly or using an incorrect 'where' parameter.


This comment has been minimized.

Copy link

@Chitrank-Dixit Chitrank-Dixit commented Jul 19, 2017

I am getting the data using the mixpanel script but I am not able to get the time in proper format

{"event":"Detail","properties":{"time":1498780809,...... ....... "utm_source":"android"}}

now here I am getting the time as the integer , but not able to get the time as {"time": "02 Jul 2017 17:22"}


This comment has been minimized.

Copy link

@maduxi maduxi commented Nov 16, 2017

@Chitrank-Dixit refer to Mixpanel documentation for that, specifically the last paragraph here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.