Last active
August 29, 2015 14:19
-
-
Save bmcorser/ae17235c7da176427aa7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import collections | |
import itertools | |
import hashlib | |
import json | |
import requests | |
import mixpanel # https://pypi.python.org/pypi/mixpanel-py/4.0.2 | |
def get_mixpanel_datapoints(): | |
url = 'https://data.mixpanel.com/api/2.0/export/' | |
params = { | |
'from_date': '2015-02-20', | |
'to_date': '2015-04-19', | |
# ... | |
} | |
params_http = itertools.starmap("{0}={1}".format, params.items()) | |
params_str = ''.join(sorted(fmt_params)) + '<secret>' | |
params['sig'] = hashlib.md5(sig).hexdigest() | |
return map(json.loads, requests.get(URL, params=PARAMS).content.split('\n')) | |
def main(): | |
mp = mixpanel.Mixpanel('<token>') | |
users = get_users_from_database() | |
datapoints = get_mixpanel_datapoints() | |
existing_ids = collections.defaultdict(set) | |
for datapoint in datapoints: | |
existing_ids[datapoint['user']].add(datapoint['distinct_id']) | |
for user in users: | |
# link profile to previous distinct ids | |
# https://mixpanel.com/help/reference/python#alias | |
for original_id in existing_ids[user['email']]: | |
mp.alias(user['email'], original_id) | |
for user in users: | |
# link profile to user email | |
mp.people_set(user['email'], {'$email': user['email']}) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment