'Export data from mixpanel to Pandas
I am trying multiple ways to export data from mix panel to pandas. But I am unable to export in Step1 with API response.
import base64
import urllib.request
import ssl
try:
import json
except ImportError:
import simplejson as json
class Mixpanel(object):
ENDPOINT = 'https://mixpanel.com/api'
DATA_ENDPOINT = 'http://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
"""
params['format'] = format
# print(base64.b64encode(self.api_secret).decode("ascii"))
request_url = '/'.join([self.ENDPOINT, str(self.VERSION)] + methods)
if http_method == 'GET':
data = None
request_url = request_url + '/?' + self.unicode_urlencode(params)
else:
data = self.unicode_urlencode(params)
auth = base64.b64encode(self.api_secret).decode("ascii")
headers = {'Authorization': 'Basic {encoded_secret}'.format(encoded_secret=auth)}
request = urllib.request.Request(request_url, data, headers)
# print(request)
context = ssl._create_unverified_context()
response = urllib.request.urlopen(request, context=context, timeout=120)
str_response = response.read().decode('utf8')
lines = str_response.splitlines(True)
records = []
for line in lines:
obj = json.loads(line)
records.append(obj)
return records
def unicode_urlencode(self, params):
"""
Convert lists to JSON encoded strings, and correctly handle any
unicode URL parameters.
"""
if isinstance(params, dict):
params = list(params.items())
for i,param in enumerate(params):
if isinstance(param[1], list):
params.remove(param)
params.append ((param[0], json.dumps(param[1]),))
return urllib.parse.urlencode(
[(k, v) for k, v in params]
)
if __name__ == '__main__':
encoded_secret = b'my_secret'
# byteAPISecret = bytes(encoded_secret + ':', "utf-8")
api = Mixpanel(api_secret=encoded_secret)
data = api.request(['events'], {
'event': ['sample event'],
'unit': 'hour',
'interval': 24,
'type': 'general'
})
print(data)
data_iter = api.request(['export'], {
'event': ['sample event'],
'to_date': "2022-02-16",
'from_date': "2022-02-16"
})
print(data_iter)
it is printing for data like
[{'data': {'series': ['2022-02-20 19:00:00', '2022-02-20 20:00:00', '2022-02-20 21:00:00', '2022-02-20 22:00:00', '2022-02-20 23:00:00', '2022-02-21 00:00:00', '2022-02-21 01:00:00', '2022-02-21 02:00:00', '2022-02-21 03:00:00', '2022-02-21 04:00:00', '2022-02-21 05:00:00', '2022-02-21 06:00:00', '2022-02-21 07:00:00', '2022-02-21 08:00:00', '2022-02-21 09:00:00', '2022-02-21 10:00:00', '2022-02-21 11:00:00', '2022-02-21 12:00:00', '2022-02-21 13:00:00', '2022-02-21 14:00:00', '2022-02-21 15:00:00', '2022-02-21 16:00:00', '2022-02-21 17:00:00', '2022-02-21 18:00:00', '2022-02-21 19:00:00', '2022-02-21 20:00:00'], 'values': {'Course Content Start': {'2022-02-20 00:00:00': 79, '2022-02-20 01:00:00': 52, '2022-02-20 02:00:00': 69, '2022-02-20 03:00:00': 101, '2022-02-20 04:00:00': 92, '2022-02-20 05:00:00': 77, '2022-02-20 06:00:00': 79, '2022-02-20 07:00:00': 77, '2022-02-20 08:00:00': 112, '2022-02-20 09:00:00': 134, '2022-02-20 10:00:00': 164, '2022-02-20 11:00:00': 173, '2022-02-20 12:00:00': 124, '2022-02-20 13:00:00': 144, '2022-02-20 14:00:00': 154, '2022-02-20 15:00:00': 95, '2022-02-20 16:00:00': 88, '2022-02-20 17:00:00': 71, '2022-02-20 18:00:00': 89, '2022-02-20 19:00:00': 45, '2022-02-20 20:00:00': 39, '2022-02-20 21:00:00': 32, '2022-02-20 22:00:00': 46, '2022-02-20 23:00:00': 73, '2022-02-21 00:00:00': 65, '2022-02-21 01:00:00': 72, '2022-02-21 02:00:00': 65, '2022-02-21 03:00:00': 92, '2022-02-21 04:00:00': 124, '2022-02-21 05:00:00': 147, '2022-02-21 06:00:00': 159, '2022-02-21 07:00:00': 136, '2022-02-21 08:00:00': 161, '2022-02-21 09:00:00': 159, '2022-02-21 10:00:00': 108, '2022-02-21 11:00:00': 141, '2022-02-21 12:00:00': 143, '2022-02-21 13:00:00': 126, '2022-02-21 14:00:00': 96, '2022-02-21 15:00:00': 109, '2022-02-21 16:00:00': 139, '2022-02-21 17:00:00': 150, '2022-02-21 18:00:00': 81, '2022-02-21 19:00:00': 21, '2022-02-21 20:00:00': 0, '2022-02-21 21:00:00': 0, '2022-02-21 22:00:00': 0, '2022-02-21 23:00:00': 0}}}, 'legend_size': 1, 'computed_at': '2022-02-22T03:29:41.928367+00:00'}]
but it is not working for data_iter(even changed to data point).getting errors like
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
I have to fetch the data and store that into pandas.Any way to export the data from Mixpanel and store that to pandas or iterate in python?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
