We'd like to keep our company db in sync with Mailchimp, specifically
ensuring that unsubscribes get reflected in our customer records. Our
situation is perhaps unusual in that we create a new Mailchimp list
every time we do a mass mail, so we need to ensure we process
unsubscribes before the next list gets created.
Does this approach look sound? It seems to work from my quick tests,
but I'm a bit hazy on Mailchimp terminology (lists vs campaigns) so
would appreciate comments.
My planned approach:
Every hour (or other configured period), sync the latest unsubscribes
down from Mailchimp to keep our company db up to date.
1. Get lists:
-------------
First need to get all lists defined on our account
Request:
http://api.mailchimp.com/1.2/?method=lists&apikey=xxxxxxx&output=xml
(Need to use correct apikey for production account)
Output:
<MCAPI type="array">
<struct key="0" type="array">
<id type="string">12345678</id>
<web_id type="integer">106793</web_id>
<name type="string">Chris test list</name>
<date_created type="string">2009-06-02 14:03:58</date_created>
<member_count type="double">3</member_count>
<unsubscribe_count type="double">1</unsubscribe_count>
<cleaned_count type="double">0</cleaned_count>
</struct>
</MCAPI>
2. Get unsubscribes for each list:
----------------------------------
Loop through all lists returned, getting the unsubscribes for each one
since the last run. Mark each user as 'opted out' in our company db.
Request (for each list):
http://api.mailchimp.com/1.2/?method=listMembers&apikey=xxxxxxx&id=12345678&since=2009-06-01+09%3A00%3A00&status=unsubscribed&limit=15000&output=xml
(Need to use the correct apikey; also populate 'since' with the last
run time; and populate 'id' with each list's id as we loop through
lists from the previous request)
Outputs:
<MCAPI type="array">
<struct key="0" type="array">
<email type="string">
chr...@example.com</email>
<timestamp type="string">2009-06-02 14:16:51</timestamp>
</struct>
</MCAPI>
For each such user, mark them as unsubscribed in our company db
Et voila!
TIA,
Chris