Updating Mailchimp Interest Groups with Code and Webhooks by Zapier

  • 1 December 2021
  • 1 reply
  • 855 views
Updating Mailchimp Interest Groups with Code and Webhooks by Zapier
Userlevel 6
Badge +1

Zapier’s Add/Update Subscriber Action for Mailchimp already provides the ability to Update Interest Groups for a Subscriber.

AduzsLxCXY9YX72Fi0ul3Dil_kFs0y7kpFK_B8DC26qfeLO5dv4RVT6Xsijoi7wN_WBgobO8B2oYmqCAtUsKZIZqqo05FS0zqcQxtRNHfClO4gbtoqYC1QKxpnf7AfZbpZ47DE86

 

For most users - this is the preferred and easiest method for updating interest groups in Mailchimp.

However - some users have a large number of Interest Categories in Mailchimp.

When there are too many Interest Categories and Groups - the Zap might timeout when attempting to lookup the Interest Group by Category.

The error we might see when this happens in a live Zap Run is:

The app did not respond in-time. It may or may not have completed successfully.

 

MMEez_sctgzlSVN8wgGPqmxZ205ZUQnyS1mM22hmj25EVHwHyoAplxOTtDQjbh3Voa1QJ84ML66N1TGKDStHjlBlW2xVyZysyw6KcbJYspnDLaEkEg-FfmqtbIoFLWRfHt6NJdPH

 

In that case - we can still follow the steps below and use a Code Step and Webhooks by Zapier to update the Interest Groups on a Subscriber in Mailchimp.

What We Need from Mailchimp

There are 2 things we need from Mailchimp directly to make this work.

First - we want to follow Mailchimp’s Instructions for generating an API Key.

You can find those here.

And next - we want to take note of the URL we see when we are logged into Mailchimp.

d828AZBz7LCd6aZAgB_6PzLkrcHEnQcZ9_0_2itLvvKk6PFVOD4NBDf-xLAClR4mgghJ4Vu0OUiykDG1HeOAFBnnhsTB_J5Ny7lgZCZcKKWzprlm3RcelELY95Dc3YZBlZTr4lPt

 

The first part of this URL will be different for different users - and we’ll need that piece in our Webhooks step below.

Once we have those 2 pieces of data from Mailchimp - we’re ready to move on to our Zap.

Building Our Zap

I’m assuming that you already have a trigger that is providing the email address for an existing Subscriber we are wanting to update Interest Groups for.

And at this point we are just focused on how to make the Webhook request to Mailchimp to update the Groups.

Step 1 - Finding Our Mailchimp IDs

The first thing we need to do is add a test contact to Mailchimp with the Group ID’s we are wanting to update using the Add/Update Subscriber action in Zapier directly.

In the screenshot below - I’ve done the following:

  • Added a test email address that does not exist already in the Audience I want to update the Subscriber in

  • Chosen Add Only for the option on Replace Groups

  • Selected the 3 Groups I want this Zap to Add to the Subscriber when it runs live.

Up-jcMiwcl6lYUJSJslOnd-9l0XXYt2oEfT_JJ3zEIff6XSxIPRdbqOWDcotmtmNElG-CrwsDd5Rga-b6HbaujmZQpF7o_qAXEfVp383DWFqR1hd4qLON0Fdz7lmbL4lkDkgw5Qz

 

After we test the step above - we’ll see something like this:

Z2ZXhN0pNq36qSuMsfKLFVBOMAeLCBrZLGS-4FP3ev3Mv4AK0-2SUQ8FDKZ6V-XkXgSAkgAST_6G6ivLXPxDBIRk-AMH3QAYKAhWrWCkQkBhR4RCLUdcqJCLD64g7Hfdqy-qW6bZ

 

The ID Codes with “true” next to them are the 3 Group IDs I selected in the Zap Editor.

Copy the IDs for the “true” values - as we will need those in our Webhooks Step below.

In my case the ID’s are:

9840e79490
7395033c08
9cfe128775

We also want to scroll down and get the List ID as well.

r9uX-JFcleMxsA6lC1ba0Y7cLIBosdFwB8IYxSXh1PsshhCqeS_E-5AffEYD8Led_5BEgZ998hg91ZL4jV4NLS0_1kjoabw3S7zDy8rmFha3wRoJnZh44e5RszYIzLfki2N_WVVC

In my case this is bb8167e2be

Step 2 - Hash Our Email Address

When we make our Webhook request to Mailchimp - we have to translate our Email Address into its MD5 Hash equivalent - as this is what Mailchimp requires in the Webhook request.

To do that - we’re going to use a Code by Zapier (Javascript) action.

These Code steps are an advanced feature in Zapier - and beyond what our Support Team can help users with.

And I freely admit to borrowing that code below one of Zapier’s founders here. :) 

xfjWLLEH9z1IuEJ0xUGgoTQKBeHeFLFwfQPqlVWYn_guMYsMsHWHICgyztnoUxTpDUXdrrm3sK6usj9m-b_eg6kAS6Xm66k1E-dM_ctMb4IZ16Z5Tewcs-jXGA5vtAlkcE2XoLbY

 

To make that step above - you would use the same name for the input data section (email) - and then map in the email address from your previous step in the Zap.

Then just copy and paste the code below.

var crypto = require('crypto');

var emailStr = inputData.email;

var hashStr = crypto.createHash('md5').update(emailStr).digest('hex');

return {emailHash: hashStr}

When we run the test - we’ll see something like this.

v-M7hglUL_37-pWbaRNQA3PcxLaY_xNW59OGU4-PI26Zi0PT7oMy5xd00VXT9Mw89uRrZfzSWlYx72swgLd7xO-ImzC2EiO9m6SUjEexaF9f9giWlgeqDRqad0wH7XZr1coozNbz

 

The “emailHash” value is the MD5 Hash of my test email.

Step 3 - Configure our Webhook

Now we’re ready to put it all together and configure our Webhook to update the Interest Groups.

First - we want to choose Webhooks by Zapier as the action App - and then choose Custom Request as the Action Event.

X03rSzAXfQhGl-k2NKrBB4b5deV38xbP2fKwRtwJl78pdb-6S6ZloCGlxNzX4akmYhdIhbTDBAnbFke_t-0K-jrF2UxlvhWeC8OehyzP9NFCZbIXn49X4k-MuOIWX0qf-sP90G8o

 

Then we’d configure our Webhook like this.

uqUjvmGxbhmN5LvFzJWJ2qGjjRyjQ6GesydzNSw-7UBCOIy6tHYgf0cPobJGW3lqnHQPV7_jmtB1fu5zLWVVHMQEkpeDPHJJNvBH3ewRHBMlxWXZ-65HMFUMaCCsDx_Kqo83UEaK

 

Method: PUT

URL: 

For the URL we are going to start with this template.

https://YOURURL.api.mailchimp.com/3.0/lists/YOURLIST/members/

You would replace “YOURURL” with the value you see from the URL when you login to Mailchimp. In my case this is “us5”.

And then you would also replace “YOURLIST” with the List ID we identified above. In my case this is “bb8167e2be”

So my final URL is:

https://us5.api.mailchimp.com/3.0/lists/bb8167e2be/members/

And I would then map in the MD5 Hash Value for the email address at the end of that.

AOoBVl4IQ1Keoio_loGT_yA_2HqsqyErilKeC7jT5FwJRN7qNKdV4bep4ADKsBbQzKXMbVyYyxCBpZSsDRCP2VKDrH4XNwwryenMctnL7vFkcgfZgf5IqKmWVSHI0meBkLR_a844

Data:

You can copy and paste this as a template.

{
"email_address": "MAP_EMAIL_HERE",
"interests": {
"9840e79490": true,
"7395033c08": true,
"9cfe128775": true
}
}

You would replace my Group ID’s with the Group ID’s you identified in Step 1 above.

And also replace MAP_EMAIL_HERE with the email address mapped from your Zap.

If you need to add more or remove some - that is fine as well. Just make sure the last row does not have a comma at the end.

Headers:

We want to add 2 Headers.

“Content-type” with a value of “application/json”

AND

“Authorization” with a value of “Bearer YOURAPIKEY”

Where you would replace “YOURAPIKEY” with the API Key you generated in Mailchimp.

Testing the Webhook

To test this I’m going to make sure my test email doesn’t have any Groups in Mailchimp.

eds8vEwaX-JpG2bmE-LgwOUZxS-gQwMbmUOOJSrITLfLZd71K3SiFXd_r-xNzvL5UaKXUQ1765SUmRt3WkALZSNnwuaK7GRxwazmJT2a5zybPgwTkVO5p8i6FKTI4NBIRVS282_g

 

Then I’m going to run the test in Zapier.

fY6X3msG5rnvg41ygkfqyihS4l2O-SixfRWQvfkAFPRdzgbeHn-ARPrf2CRJgSaxFkXiIdNdgIY16zQe5f75pwXiQjJ10Xsp9zmzqLXDztz7pIvPgbjFGAXqvUnCDhfugbWlMHk6

Finally go back to check Mailchimp once the test completes.

And...Success!

The Interest Groups are now on the Subscriber in Mailchimp.

WQyrgYvIB3d0XgHYqfConwe6rSTWS1jYzu_jGPWoPfg5kyNwVwr1uJ4ayPUHzOS0V4w3uNs-2jZ3rqhMaDJUKHzD-1redN100ggLmmPeu4RtnIlFT-g-fJqdRQTz1ARjfKkjFA34

Questions

Will this work for New Subscribers that are being added to the list?

It will - but we’d want to add some additional fields to the request for it to work to add a New Subscriber.

I’ve hardcoded the values for email address and FNAME and LNAME below - but you would want to dynamically map that data from the previous steps of your Zap.

_Nav8cYhbRY2bCM5e_lLlEneBv2QfEMPsbG7ZPFReVwVEtyWZd2j5q0-UI7lLk4NULhHC6dEEVqfu5NOy-tj8wz6i2oWfc6fWCQvzI6pkesctYfSHZDOsc7OHHRccIuc2EquqnVn

Can I use this to update fields other than Interest Groups?

Yes - similar to the example above - we can update additional fields this way as well.

However this could get complex quickly.

You can find Mailchimp’s documentation for this Endpoint here.

And testing from a 3rd party tool like Postman is always advisable to make sure your requests are working outside of Zapier first.

Summary

When we need to update Interest Groups on a Subscriber in Mailchimp - we’d normally do this using the Add/Update Subscriber action.

For some users with a large number of Interest Categories and Groups - this action might timeout - causing an error in the Zap Runs.

We can still update the groups using a Code Step to Hash the email address and a Webhooks by Zapier action to send the request to Mailchimp.

Both Code and Webhooks are advanced features in Zapier - and our Support Team isn’t usually able to help with the exact format of the Code or Webhooks Request.

If you do give this a try - I’d love to hear your experience in the comments below!


1 reply

Very helpful! Any clue on how to do this with a dynamic list of groups (ie,each Zap run contacts a contact for whom we want to add a unique set of groups.)  The webhook data would have to be generated dynamically for each contact.

Reply