Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Example Mixpanel raw event export script Raw
#! /usr/bin/env python
#
# Mixpanel, Inc. -- http://mixpanel.com/
#
# Python API client library to consume mixpanel.com analytics data.
#
# Copyright 2010-2013 Mixpanel, Inc
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import base64
import urllib
import urllib2
try:
import json
except ImportError:
import simplejson as json
class Mixpanel(object):
ENDPOINT = 'https://data.mixpanel.com/api'
VERSION = '2.0'
def __init__(self, api_secret):
self.api_secret = api_secret
def request(self, methods, params, http_method='GET', format='json'):
"""
methods - List of methods to be joined, e.g. ['events', 'properties', 'values']
will give us http://mixpanel.com/api/2.0/events/properties/values/
params - Extra parameters associated with method
"""
request_url = '/'.join([self.ENDPOINT, str(self.VERSION)] + methods)
if http_method == 'GET':
data = None
request_url = request_url + '/?' + self.unicode_urlencode(params)
print request_url
else:
data = 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=3600).read()
event_raw = response.split('\n')
event_raw = [json.loads(x) for x in event_raw[:-1]]
return event_raw
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]
)
if __name__ == '__main__':
api_secret = raw_input("API Secret: ")
from_date = raw_input("From Date: ")
to_date = raw_input("To Date: ")
output_file = raw_input("Output Filename (i.e. my_events.txt): ")
api = Mixpanel(api_secret=api_secret)
data = api.request(['export'], {
#'event': [''],
'from_date': from_date,
'to_date': to_date,
#'where': ''
})
with open(output_file, 'w') as outfile:
outfile.write(json.dumps(data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment