Last active
September 24, 2020 22:23
-
-
Save mkiser/f7d0614e11db27a9e3fe9321c3fdac37 to your computer and use it in GitHub Desktop.
A Python script for pulling MailChimp unsubscribe messages and saving as a CSV.
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 mailchimp_marketing as MailchimpMarketing | |
from mailchimp_marketing.api_client import ApiClientError | |
import csv | |
try: | |
client = MailchimpMarketing.Client() | |
client.set_config({ | |
"api_key": "XX", | |
"server": "XX" | |
}) | |
lst=[] | |
# Get X-number of campaign reports | |
campaign_id = client.reports.get_all_campaign_reports(count=1000) | |
# Iterate over campaigns, grab IDs, request unsubscribe reports | |
for id in campaign_id["reports"]: | |
unsubs = client.reports.get_unsubscribed_list_for_campaign(id["id"], count=1000) | |
reasons = unsubs["unsubscribes"] | |
# Extract reasons for unsubscribe. | |
for unsub in reasons: | |
if unsub["reason"].startswith(("None given","No longer interested","Spammy content","Did not signup for list","Inappropriate content")): | |
pass | |
else: | |
lst.append(unsub["reason"].encode("utf-8")) | |
# print(unsub["reason"]) | |
print(lst) | |
with open("/XX/XX/XX/XX/file.csv", "wt") as csvfile: | |
wr = csv.writer(csvfile, dialect='excel') | |
wr.writerow(lst) | |
except ApiClientError as error: | |
print("Error: {}".format(error.text)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment