Created
June 23, 2017 10:10
-
-
Save khajvahmac/ff3fbc92b32e15cf80f6df4525aadd72 to your computer and use it in GitHub Desktop.
This file contains 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 json | |
import collections | |
import csv | |
from urllib import request | |
def get_campaigns(app_group_id): | |
campaign_list_url = "https://api.appboy.com/campaigns/list?app_group_id=" + app_group_id + "&page=" | |
page = 0 | |
campaigns = json.loads(request.urlopen(campaign_list_url + str(page)).read().decode()).get('campaigns') | |
while len(campaigns) > 0: | |
yield campaigns | |
page += 1 | |
campaigns = json.loads(request.urlopen(campaign_list_url + str(page)).read().decode()).get('campaigns') | |
def get_campaign_stats(app_group_id, campaign_id, length, needed_stats=('direct_opens', 'total_opens', 'sent')): | |
campaign_stats_url = "https://api.appboy.com/campaigns/data_series?app_group_id=" + app_group_id + \ | |
"&campaign_id=" + campaign_id + "&length=" + str(length) | |
all_data = json.loads(request.urlopen(campaign_stats_url).read().decode()) | |
message_types = ["ios_push", "android_push", "email"] | |
totals = collections.defaultdict(dict) | |
totals['unique_recipients'] = 0 | |
for data in all_data['data']: | |
totals['unique_recipients'] += data['unique_recipients'] | |
for message_type in message_types: | |
typed_data = data['messages'].get(message_type) | |
if typed_data is None: | |
continue | |
for i in typed_data: | |
variation_name = i.get('variation_name') | |
if variation_name is None: | |
variation_name = "variant 1" | |
if message_type not in totals or variation_name not in totals[message_type]: | |
totals[message_type][variation_name] = {j: 0 for j in needed_stats} | |
for elem in needed_stats: | |
try: | |
totals[message_type][variation_name][elem] += i[elem] | |
except KeyError: | |
continue | |
return totals | |
def write_to_csv(file, data, meta): | |
print("Writing to csv") | |
writer = csv.DictWriter(file, fieldnames=['name', 'sent', 'total_opens', 'direct_opens', 'unique recipients']) | |
unique_rec = data.pop('unique_recipients') | |
for message_type in data: | |
for variant in data[message_type]: | |
row = { | |
'name': meta['name'] + " - " + message_type + " - " + variant, | |
'unique recipients': unique_rec | |
} | |
row.update(data[message_type][variant]) | |
writer.writerow(row) | |
if __name__ == "__main__": | |
with open('appboy.csv', 'w') as f: | |
writer = csv.writer(f).writerow(['name', 'sent', 'total_opens', 'direct_opens', 'unique recipients']) | |
for campaigns in get_campaigns('fc096d94-08f7-4768-b603-b07d28c54e8f'): | |
print("processing batch") | |
for campaign in campaigns: | |
data = get_campaign_stats('fc096d94-08f7-4768-b603-b07d28c54e8f', campaign['id'], 100) | |
write_to_csv(f, data, campaign) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment