Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Mixpanel - Exporting people profiles to CSV
''' people export'''
import base64
import csv
import sys
import time
import urllib # for url encoding
import urllib2 # for sending requests
import json
except ImportError:
import simplejson as json
class Mixpanel(object):
def __init__(self, api_secret):
self.api_secret = api_secret
def request(self, params, format='json'):
'''let's craft the http request'''
data = None
request_url = ''
request_url = request_url + self.unicode_urlencode(params)
headers = {'Authorization': 'Basic {encoded_secret}'.format(encoded_secret=base64.b64encode(self.api_secret))}
request = urllib2.Request(request_url, data, headers)
response = urllib2.urlopen(request, timeout=120)
def unicode_urlencode(self, params):
''' Convert stuff to json format and correctly handle 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]),)
result = urllib.urlencode([(k, isinstance(v, unicode) and v.encode('utf-8') or v) for k, v in params])
return result
def get_and_write_results(self, params):
response = api.request(params)
parameters['session_id'] = json.loads(response)['session_id']
parameters['page'] = 0
global_total = json.loads(response)['total']
print "Session id is %s \n" % parameters['session_id']
print "Here are the # of people %d" % global_total
paged = self._page_results(response, parameters, global_total)
self.export_csv("people_export_" + str(int(time.time())) + ".csv", paged)
def _page_results(self, response, parameters, global_total):
fname = "people_export_" + str(int(time.time())) + ".txt"
parameters['page'] = 0
has_results = True
total = 0
while has_results:
responser = json.loads(response)['results']
total += len(responser)
has_results = len(responser) == 1000
self._write_results(responser, fname)
print "%d / %d" % (total, global_total)
parameters['page'] += 1
if has_results:
response = api.request(parameters)
return fname
def _write_results(self, results, fname):
with open(fname, 'a') as f:
for data in results:
f.write(json.dumps(data) + '\n')
def export_csv(self, outfilename, fname):
'''takes a file name of a file of json objects and the desired name of the csv file that will be written'''
subkeys = set()
with open(fname, 'rb') as r:
with open(outfilename, 'wb') as w:
# Get all properties (will use this to create the header)
for line in r:
# Create the header
header = ['$distinct_id']
for key in subkeys:
# Create the writer and write the header
writer = csv.writer(w)
# Return to the top of the file, then write the events out, one per row, 0)
for line in r:
entry = json.loads(line)
row = []
for subkey in subkeys:
except AttributeError:
except KeyError:
if __name__ == '__main__':
if len(sys.argv) > 1:
api_secret = sys.argv[1]
api_secret = raw_input("API Secret: ")
api = Mixpanel(
Here is the place to define your selector to target only the users that you're after
selector = '(datetime(1458587013 - 86400) > properties["Created"] and behaviors["behavior_79"] > 0'
behaviors = '[{"window": "90d", "name": "behavior_79", "event_selectors": [{"event": "Edit Colors"}]}]'
# Leave 'r' before the behaviors and selector strings so that they are interpreted as a string literals to handle escaped quotes
selector = r''
behaviors = r''
Optionally export just specific properties. Exports will always include $distinct_id and $last_seen
output_properties = ['property1', 'property2', 'property3']
output_properties = []
if not behaviors:
parameters = {'selector': selector}
time_offset = int(raw_input("Project time offset from GMT (ex. PST = -8): "))
parameters = {'selector': selector, 'behaviors': behaviors, 'as_of_timestamp': int(time.time()) + (time_offset * 3600)}
if output_properties:
parameters['output_properties'] = output_properties

cggaurav commented Oct 4, 2016

Traceback (most recent call last):
  File "", line 141, in <module>
  File "", line 43, in get_and_write_results
    response = api.request(params)
  File "", line 27, in request
    response = urllib2.urlopen(request, timeout=120)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 127, in urlopen
    return, data, timeout)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 410, in open
    response = meth(req, response)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 448, in error
    return self._call_chain(*args)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 382, in _call_chain
    result = func(*args)
  File "/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request

Looks like you didn't provide API secret. (Instructions on where to find it:

Once you run python in a teminal it prompts: API secret:
Just paste you API secret into terminal and press return key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment