Skip to content

Instantly share code, notes, and snippets.

@mkiser
Last active September 24, 2020 22:23
Show Gist options
  • Save mkiser/f7d0614e11db27a9e3fe9321c3fdac37 to your computer and use it in GitHub Desktop.
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.
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