How to load data from MailChimp to Google BigQuery

Extract your data from MailChimp

First of all, Mailchimp updated its API to v3 recently, so ensure that anything you do will be with this version as the previous are all deprecated although still supported, for more information visit MailChimp API v3.0 documentation. MailChimp was always a promoter of APIs and encouraged integration with other systems. It has a rich API that exposes a large number of endpoints for interacting with the resources of the applications; more specifically, there are endpoints for the following resources:

  • Automations – offers functionality related to automated tasks that we define on MailChimp
  • Batch operations – for managing batch processes on our MailChimp account
  • Campaign folders – helps you organize your campaigns into groups
  • Campaigns – for managing your campaigns
  • Conversations – helps you track threads of emails with specific users
  • File manager files – for managing your static assets like images
  • File manager folders – for creating folders to organize your assets
  • Lists – CRUD operations on lists of users
  • Reports – for accessing reports with statistics on your campaigns
  • Template folders – operations on creating folders for organizing your templates for your emails
  • Templates – operations on templates for your emails

Something interesting to note about the MailChimp API is that we don’t see a root-level resource for users anywhere. This makes sense, if we consider that MailChimp is all about mail campaigns so users or to put in the correct context, subscribers are not a stand-alone resource but instead exist only inside the lists we manage. It is important to understand that every service perceives the world from a different perspective. Of course, it’s relevant to the value it offers, so even if what we care about is information about our users, it makes complete sense for MailChimp to organize everything around lists and campaigns. Suppose we check the model of another service, like Intercom, for example. In that case, we’ll notice that the user is at the top resources, which again makes sense because Intercom is all about one-to-one communication with users.

MailChimp and any other service that you might be using have figured out (hopefully) the optimal model for its operations. Still, when we fetch data from them, we usually want to answer questions or do things that are not part of the context that these services operate, making these models sub-optimal for your analytics needs. For this reason, we should always keep in mind that when we work with data coming from external services, we need to remodel it and bring it to the right form for our needs.

Interacting with the MailChimp REST API can be done using tools like CURL or Postman or by using HTTP clients for your favorite language or framework. A few suggestions:

The MailChimp REST API supports OAuth 2.0 authentication. More information can be found in the Authorised apps section of the API documentation. After you successfully authenticate with the REST API, you have to start interacting with its resources and fetching data to load them on your data warehouse.

Extract your user data from the MailChimp API

Let’s assume that we want to get all the information we got on MailChimp for our users to enrich our user records inside our data warehouse. To do that, we need to do the following.

First, we need to fetch all the lists that we have created on MailChimp. We can do this by performing a GET request to the appropriate endpoint, as follows:

  • curl --request GET --url 'https://usX.api.mailchimp.com/3.0/lists' --user 'anystring:apikey' —include

We should get back a response like the following:

JAVASCRIPT
{ "lists": [ { "id": "57afe96172", "name": "Freddie's Jokes", "contact": { "company": "MailChimp", "address1": "675 Ponce De Leon Ave NE", "address2": "Suite 5000", "city": "Atlanta", "state": "GA", "zip": "30308", "country": "US", "phone": "" },

Using the ID we get from the response for each list, we iterate through all the lists and make requests to the appropriate end-points to get the members for a list

JAVASCRIPT
curl --request GET --url 'https://usX.api.mailchimp.com/3.0/lists/57afe96172/members' --user 'anystring:apikey' —include

and we should get a response back like the following:

JAVASCRIPT
{ "members": [ { "id": "f777bbffab8d1ceca8b757df63c47cb8", "email_address": "urist.mcvankab+1@freddiesjokes.co", "unique_email_id": "882e9bec19", "email_type": "html", "status": "subscribed", "status_if_new": "", "merge_fields": { "FNAME": "", "LNAME": "" }, "interests": { "9143cf3bd1": true, "3a2a927344": false, "f9c8f5f0ff": false, "f231b09abc": true, "bd6e66465f": false },

Keep in mind that a user might appear in more than one list, which means that you also need to reduplicate your results based on this fact. After we get the results we need and collect all the user-related information from various other resources, we need to map it to our Data Warehouse repository model before we do the actual loading.

Prepare your Mailchimp Data for Google BigQuery

Before you load your data into BigQuery, you should make sure that it is presented in a format supported by it. For example, if the API you pull data from returns XML, you must first transform it into a serialization that BigQuery understands. Currently, two data formats are supported:

You also need to make sure that the data types you are using are the ones supported by BigQuery, which are:

  • STRING
  • INTEGER
  • FLOAT
  • BOOLEAN
  • RECORD
  • TIMESTAMP

For more information, please check the Preparing Data for BigQuery page on the documentation.

Load Data from Mailchimp to Google BigQuery

If you want to load MailChimp data to Google BigQuery, you have to use one of the following supported data sources.

  1. Google Cloud Storage
  2. Sent data directly to BigQuery with a POST request
  3. Google Cloud Datastore Backup
  4. Streaming insert
  5. App Engine log files
  6. Cloud Storage logs

From the above list of sources, 5 and 6 are not applicable in our case.

For Google Cloud Storage, you first have to load your data into it, and there are a few options for doing this. For example, you can use the console directly as described here, and do not forget to follow the best practices. Another option is to post your data through the JSON API. APIs play an important role in both the extraction and the loading of data into our data warehouse. It’s just a matter of one HTTP POST request using a tool like CURL or Postman in its simplest case. It should look like the following example.

JAVASCRIPT
POST /upload/storage/v1/b/myBucket/o?uploadType=media&name=myObject HTTP/1.1 Host: www.googleapis.com Content-Type: application/text Content-Length: number_of_bytes_in_file Authorization: Bearer your_auth_token your Mailchimp data

and if everything went ok, you should get something like the following as a response from the server:

HTML
HTTP/1.1 200 Content-Type: application/json { "name": "myObject" }

Working with Curl or Postman is good only for testing. If you would like to automate loading your data into Google Bigquery, you should write some code to send your data to Google Cloud Storage. In case you are developing on the Google App Engine, you can use the library that is available for the languages it supports:

Working with Curl or Postman is good only for testing. If you would like to automate loading your data into Google Bigquery, you should write some code to send your data to Google Cloud Storage. In case you are developing on the Google App Engine, you can use the library that is available for the languages it supports:

  1. Python
  2. Java
  3. PHP
  4. Go

If you are using one of the above languages and are not coding for the Google App Engine, you can access the Cloud Storage from your environment. Interacting such a feature-rich product like Google Cloud Storage can become quite complicated depending on your use case. For more details on the different options that exist, you can check Google Cloud Storage documentation. If you are looking for a less engaged and more neutral way of using Cloud Storage, you can consider RudderStack.

After you have loaded your data into Google Cloud Storage, you have to create a Load Job for BigQuery to load the data into it. This Job should point to the source data in Cloud Storage that have to be imported. This happens by providing source URIs that point to the appropriate objects.

The previous method described a POST request to the Google Cloud Storage API for storing the data there and then loading it into BigQuery. Another way to go is to do a direct HTTP POST request to BigQuery with the data you would like to query. This approach is similar to how we loaded the data to Google Cloud Storage through the JSON API, but it uses the appropriate end-points of BigQuery to load the data there directly. The way to interact with it is quite similar, for more information can be found on the Google BigQuery API Reference and on the page that describes how to load data into BigQuery using POST. You can interact with it using the HTTP client library of the language or framework of your choice, a few options are:

The best way to load data from Mailchimp to Google BigQuery and possible alternatives

So far we just scraped the surface of what can be done with Google BigQuery and how to load data into it. The way to proceed relies heavily on the data you want to load, from which service they are coming from, and your use case’s requirements. Things can get even more complicated if you want to integrate data coming from different sources. Instead of writing, hosting, and maintaining a flexible data infrastructure, a possible alternative is to use RudderStack that can automatically handle this kind of problem for you.

RudderStack integrates with multiple sources or services like databases, CRM, email campaigns, analytics, and more. Quickly and safely move all your data from MailChimp into your data warehouse and start generating insights from your data.

Sign Up For Free And Start Sending Data
Test out our event stream, ELT, and reverse-ETL pipelines. Use our HTTP source to send data in less than 5 minutes, or install one of our 12 SDKs in your website or app.
Don't want to go through the pain of direct integration? RudderStack's Mailchimp integration makes it easy to send data from Mailchimp to Google BigQuery.