VingtCinq / python-mailchimp

Python client for v3 of MailChimp API
MIT License
463 stars 131 forks source link

Need some assistance on client.reports.email_activity.all #239

Closed ujvalgandhi1 closed 3 years ago

ujvalgandhi1 commented 3 years ago

I am new to Python so this might be a stupid question

I am using the client.reports.email_activity.all to pull data on all the emails for a specific set of campaigns Below is the code I am using but I am unable to get the timestamp OR the IP from the activity portion.

Can someone please assist?

for idx in campaign_ids: email_activity = client.reports.email_activity.all(campaign_id=idx,get_all=True)

df = pd.DataFrame(columns=['campaign_id', 'list_id', 'email_address', 
                        'opentimestamp', 'openip', 
                          ])

response = email_activity
print(response)
for email_address in email_activity['emails']:

    #print(email_address['email_address'])
    #print(email_address['activity'])    

    row = {
            'campaign_id':email_address['campaign_id'],
            'list_id': email_address['list_id'], 
            'email_address':email_address['email_address']
           # 'opentimestamp' : email_address['activity'].str['timestamp'],
            #'openip': email_address['activity'].get('ip'), 

        }

    df = df.append(row, ignore_index='True')

df = df.fillna(0)

#print(df)
stephenross commented 3 years ago

As you might have seen in https://mailchimp.com/developer/marketing/api/email-activity-reports/list-email-activity/, the timestamp and ip are provided in activity, however they are provided individually in the activity. The activity portion of the return is a list, and so there is no timestamp or ip key to examine, which is likely giving you your errors. If you loop through the activity objects, you can find the timestamp and ip by checking to make sure the action is "open".