Mailchimp to Redshift

This page provides you with instructions on how to extract data from MailChimp’s backend and load it into Amazon Redshift. (If this manual process is a bit more involved than you’d prefer, check out Stitch, which can do all the heavy lifting for you in just a few clicks.)

Pulling Data Out of MailChimp

In order to get your MailChimp data into AWS Redshift, you have to start by extracting it from MailChimp’s servers. You can do this using the MailChimp API, which is available to all MailChimp customers. Full API documentation can be accessed at this link

The tricky thing about Mailchimp is that it generates a LOT of data. Anytime you send an email or someone opens and reads one, you are generating data and that can add up very quickly. Depending on your needs, you may want to use Webhooks technology in order to continuously receive streaming updates of these events as they happen. Unfortunately, that means you’ll also need to build code on your end to receive this streaming data.

Mailchimp also offers a more durable API for syncing campaign information and stats that may be more what you’re looking for. Check out the docs through the lens of your use case to make the right call for you.

Sample MailChimp Data

The Mailchimp API returns JSON-formatted data. Below is an example of the kind of response you might see when querying the accounts endpoint.

HTTP/1.1 200 OK
{
 "account_id": "8d3a3db4d97663a9074efcc16",
 "account_name": "Freddie's Jokes",
 "contact": {
   "company": "Freddie's Jokes",
   "addr1": "675 Ponce De Leon Ave NE",
   "addr2": "Suite 5000",
   "city": "Atlanta",
   "state": "GA",
   "zip": "30308",
   "country": "US"
 },
 "last_login": "2015-09-15 14:25:37",
 "total_subscribers": 413,
 "_links": [
   {
     "rel": "self",
     "href": "https://usX.api.mailchimp.com/3.0/",
     "method": "GET",
     "targetSchema": "https://api.mailchimp.com/schema/3.0/Root.json"
   },
   {
     "rel": "lists",
     "href": "https://usX.api.mailchimp.com/3.0/lists",
     "method": "GET",
     "targetSchema": "https://api.mailchimp.com/schema/3.0/Lists/Collection.json",
     "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Lists.json"
   },
   {
     "rel": "reports",
     "href": "https://usX.api.mailchimp.com/3.0/reports",
     "method": "GET",
     "targetSchema": "https://api.mailchimp.com/schema/3.0/Reports/Collection.json",
     "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Reports.json"
   },
   {
     "rel": "conversations",
     "href": "https://usX.api.mailchimp.com/3.0/conversations",
     "method": "GET",
     "targetSchema": "https://api.mailchimp.com/schema/3.0/Conversations/Collection.json",
     "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Conversations.json"
   },
   {
     "rel": "campaigns",
     "href": "https://usX.api.mailchimp.com/3.0/campaigns",
     "method": "GET",
     "targetSchema": "https://api.mailchimp.com/schema/3.0/Campaigns/Collection.json",
     "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Campaigns.json"
   },
   {
     "rel": "automations",
     "href": "https://usX.api.mailchimp.com/3.0/automations",
     "method": "GET",
     "targetSchema": "https://api.mailchimp.com/schema/3.0/Automations/Collection.json",
     "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Automations.json"
   },
   {
     "rel": "templates",
     "href": "https://usX.api.mailchimp.com/3.0/templates",
     "method": "GET",
     "targetSchema": "https://api.mailchimp.com/schema/3.0/Templates/Collection.json",
     "schema": "https://api.mailchimp.com/schema/3.0/CollectionLinks/Templates.json"
   },
   {
     "rel": "file-manager",
     "href": "https://usX.api.mailchimp.com/3.0/file-manager",
     "method": "GET",
     "targetSchema": "https://api.mailchimp.com/schema/3.0/FileManager/Namespace.json"
   },
   {
     "rel": "authorized-apps",
     "href": "https://usX.api.mailchimp.com/3.0/authorized-apps",
     "method": "GET",
     "targetSchema": "https://api.mailchimp.com/schema/3.0/AuthorizedApps/Collection.json"
   }
 ]
}

Preparing MailChimp Data for Redshift

Now you need to map the data that comes out of each MailChimp API endpoint into a schema that can be inserted into a Redshift database. This means that, for each value in the response, you need to identify a predefined datatype (i.e. INTEGER, DATETIME, etc.) and build a table that can receive them. The MailChimp API documentation can give you a good sense of what fields will be provided by each endpoint, along with their corresponding datatypes.

Inserting MailChimp Data into Redshift

Once you have identified all of the columns you will want to insert, you can use the CREATE TABLE statement in Redshift to create a table that can receive all of this data.

With a table built, it may seem like the easiest way to add your data (especially if there isn’t much of it), is to build INSERT statements to add data to your Redshift table row-by-row. If you have any experience with SQL, this will be your gut reaction. But beware! Redshift isn’t optimized for inserting data one row at a time, and if you have any kind of high-volume data being inserted, you would be much better off loading the data into Amazon S3 and then using the COPY command to load it into Redshift.

Keeping Data Up-To-Date

So, now what? You’ve built a script that pulls data from MailChimp and loads it into Redshift, but what happens tomorrow when you have ten new transactions?

The key is to build your script in such a way that it can also identify incremental updates to your data. Thankfully, MailChimp’s API results include fields like created_at that allow you to quickly identify records that are new since your last update (or since the newest record you’ve copied into Redshift). You can set your script up as a cron job or continuous loop to keep pulling down new data as it appears.

Other Data Warehouse Options

Redshift is totally awesome, but sometimes you need to start smaller or optimize for different things. In this case, many people choose to get started with Postgres, which is an open source RDBMS that uses nearly identical SQL syntax to Redshift. If you’re interested in seeing the relevant steps for loading this data into Postgres, check out MailChimp to Postgres

Easier and Faster Alternatives

If all this sounds a bit overwhelming, don’t be alarmed. If you have all the skills necessary to go through this process, chances are building and maintaining a script like this isn’t a very high-leverage use of your time.

Thankfully, products like Stitch were built to solve this problem automatically. With just a few clicks, Stitch starts extracting your MailChimp data via the API, structuring it in a way that is optimized for analysis, and inserting that data into your Amazon Redshift data warehouse.