How to load data from MailChimp to Google BigQuery

Blendo Team

The purpose of this guide is to help you define a pipeline, to load data from MailChimp to BigQuery for further analysis. Alternatively, you can use an ETL as a service product like Blendo that can handle this kind of problems automatically for you.

Information will be given on how to access and extract your data from MailChimp through its API and how to load it into BigQuery, this process requires from you to write the code to get the data and make sure that this process will run every time new data are generated.

What we will see:

  • Extract your data from MailChimp and MailChimp API (the hard way)
  • Prepare your MailChimp Data for Google BigQuery
  • Load Data from MailChimp to Google BigQuery
  • The best way to load data from MailChimp to Google BigQuery (the easy way)

Extract your data from MailChimp

First of all, Mailchimp updated its API to v3 recently, so make sure 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
  • ListsCRUD 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. Which makes sense of course, if we take into consideration 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 they exist only inside the lists we manage. It is important to understand that every service perceives the world from a different perspective which 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 someone like MailChimp to organize everything around lists and campaigns. If we check the model of another service, like Intercom, for example, 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, has figured out (hopefully) the optimal model for its operations, but 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, something that makes 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 by 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 start fetching data from it in order to load them on your data warehouse.

You will need more time to read this post than integrating Mailchimp to BigQuery.

Effortlessly Sync All Your Mailchimp Data to BigQuery

Extract your user data from the MailChimp API

So let’s assume that we want to get all the information we got on MailChimp for our users in order 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.

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:

{ "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

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:

{ "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 lists, 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 the model of our Data Warehouse repository 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, so for example, if the API you pull data from returns XML you have to first transform it into a serialisation 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 the following:

  • 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, there are a few options on how to do this, for example you can use the console directly as it is described here and do not forget to follow the best practices. Another option is to post your data through the JSON API, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse.. In it’s simplest case it’s just a matter of one HTTP POST request using a tool like CURL or Postman. It should look like the following example.

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:

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 the process of 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 that are supported by it:

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

If you are using one of the above languages and you are not coding for the Google App Engine, you can use it to 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 a solution like Blendo.

After you have loaded your data into Google Cloud Storage, you have to create a Load Job for BigQuery to actually 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, used a POST request to the Google Cloud Storage API for storing the data there and then load 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 the requirements of your use case. Things can get even more complicated if you want to integrate data coming from different sources. A possible alternative, instead of writing, hosting and maintaining a flexible data infrastructure, is to use an ETL as a service product like Blendo that can handle this kind of problems automatically for you.

Blendo 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.

Help your marketing and executive team take ownership of the insights that live in your MailChimp email marketing platform to transform the performance and ROI of your campaigns.

Blendo is the easiest way to automate powerful data integrations.

Try Blendo free for 14 days. No credit card required.