Skip to content

Instantly share code, notes, and snippets.

@johnyzed
Created August 7, 2014 11:08
Show Gist options
  • Save johnyzed/8b378f1a607624adb2f5 to your computer and use it in GitHub Desktop.
Save johnyzed/8b378f1a607624adb2f5 to your computer and use it in GitHub Desktop.
Python script that collect data from Mixpanel API and return funnels data in JSON
import calendar, datetime,time
import hashlib
import pycurl
import cStringIO
import json
def get_sig(api_key,api_secret,expire_time,funnel_id,unit):
api_str='api_key=%s' % api_key
expire_str='expire=%s' % expire_time
funnel_str='funnel_id=%s' % funnel_id
unit_str='unit=%s' % unit
if not funnel_id:
args=api_str+expire_str
else:
args=api_str+expire_str+funnel_str+unit_str
sig=hashlib.md5(args+api_secret).hexdigest()
return sig
def get_curl(url,api_secret,api_key,time_to_expiration,funnel,unit):
buf = cStringIO.StringIO()
expire_time = calendar.timegm(datetime.datetime.utcnow().utctimetuple())+int(time_to_expiration)
curl = pycurl.Curl()
curl.setopt(pycurl.URL,"%s" % url)
if not funnel:
sig=get_sig(api_key, api_secret, expire_time,'','')
curl.setopt(pycurl.POSTFIELDS, "api_key=%s&sig=%s&expire=%s" % (api_key,sig,expire_time) )
else:
sig=get_sig(api_key, api_secret, expire_time,funnel,unit)
buff="funnel_id=%s&expire=%s&sig=%s&api_key=%s&unit=%s" % (funnel,expire_time,sig,api_key,unit)
curl.setopt(pycurl.POSTFIELDS,str(buff))
curl.setopt(pycurl.WRITEFUNCTION, buf.write)
curl.perform()
json_result = buf.getvalue()
return json.loads(buf.getvalue())
def main():
expire_time = calendar.timegm(datetime.datetime.utcnow().utctimetuple())+300
api_key='KEY'
api_secret='SECRET'
url="http://mixpanel.com/api/2.0/"
api=url+"funnels/list"
all_funnels=get_curl(api,api_secret, api_key,300, '','')
funnel_dict={}
for funnel in range(0,len(all_funnels)):
buff_dic=all_funnels[funnel]
funnel_dict[str(buff_dic['funnel_id'])]=buff_dic['name']
api=url+"funnels"
for key in funnel_dict:
funnel=key
res=get_curl(api, api_secret, api_key, 300, funnel,'day')
#print json.dumps(res).replace('\'', '\"')
date=res['meta']['dates'][1]
hour = datetime.datetime.fromtimestamp(time.time()).strftime('%H:%M:%S')
date_time=str(date)+" "+str(hour)
json_string="{'_time':'%s','funnel_id':'%s','funnel_name':'%s','data': [%s]}" % (date_time,funnel,funnel_dict[funnel],json.dumps(res['data'][date]))
print json_string.replace('\'', '\"')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment