Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/bin/bash
MC_API_KEY="apikey:YOUR_KEY"
MC_LIST_ID="YOUR LIST ID"
# don't forget to update usN to your own MailChimp data center ID
TOTAL_ITEMS=`curl --silent --request GET --url "https://usN.api.mailchimp.com/3.0/lists/$MC_LIST_ID/members?fields=total_items" --user $MC_API_KEY | jq -r ".total_items"`
CHUNK_SIZE=1000
echo "id,email_address,status,avg_open_rate,avg_click_rate,timestamp_signup,last_changed,member_rating" > mailchimp_list.csv
echo "action,timestamp,url,type,campaign_id,title,parent_campaign,subscriber_id" > mailchimp_list_activity.csv
for (( OFFSET=0; OFFSET<=$TOTAL_ITEMS; OFFSET=OFFSET+$CHUNK_SIZE ))
do
RESPONSE=`curl --silent --request GET --url "https://us10.api.mailchimp.com/3.0/lists/c7a47b24bb/members?fields=members.id,members.email_address,members.status,members.stats.avg_open_rate,members.stats.avg_click_rate,members.timestamp_signup,members.last_changed,members.member_rating&count=$CHUNK_SIZE&offset=$OFFSET" --user $MC_API_KEY`
# write subscribers to mailchimp_list.csv, one subscriber to a line
echo $RESPONSE | jq -r ".members[] | [.id,.email_address,.status,.stats.avg_open_rate,.stats.avg_click_rate,.timestamp_signup,.last_changed,.member_rating] | @csv" >> mailchimp_list.csv
for SUB_ID in `echo $RESPONSE | jq -r ".members[] | [.id] | @tsv"`
do
# write activity for each subscriber to mailchimp_list_activity.csv, append SUB_ID to the end of each line with sed because MailChimp's API doesn't bother to return the subscriber ID in the response data for /members/activity/
curl --silent --request GET --url "https://us10.api.mailchimp.com/3.0/lists/$MC_LIST_ID/members/$SUB_ID/activity" --user $MC_API_KEY | jq -r ".activity[] | [.action,.timestamp,.url,.type,.campaign_id,.title,.parent_campaign] | @csv" | sed "s/$/\"$SUB_ID\"/" >> mailchimp_list_activity.csv
done
echo "Fetched data for $OFFSET subscribers"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.