Question

How do I create a Zapier workflow that updates my Mailchimp list, assigning a unique word to a Merge field


I've been trying to sort this out for two days now...

I have a random password and QR Code I need to assign to my subscribers Merge Fields. I've asked Chat GPT and this is how far I can get.

I'm hoping for some help. Here is a breakdown of my workflow. I’m not committed to it and will change it if necessary.

Can someone please help me out?

 

Trigger: Scheduled Campaign in Mailchimp

(WORKING) Action 1: Code Step - Generate Random Word

  • Add a "Code" step in Zapier after the trigger.

  • Use the following code to generate a random word: CODE

(WORKING) Action 2: Code Step - Generate QR Code

  • Add another "Code" step in Zapier after the "Generate Random Word" step.

  • Use the following code to generate a QR code based on the random word: CODE

(NOT WORKING) Action 3: Retrieve Subscribers

  • Add a "Code" step to retrieve the list of subscribers from Mailchimp.

  • Use the following code to make an API call and retrieve the list of subscribers: CODE

(NOT WORKING) Loop: Iterate through Subscribers

  • Add a "Loop" step in Zapier after retrieving the subscribers.

  • Set the input data for the loop to be the subscriberslist.

(WILL UPDATE SINGLE SUBSCRIBER NOT BULK ) Action 4: Update Subscriber

  • Add a "Code" step inside the loop to update each subscriber's record in Mailchimp.

  • Use the following code to update the subscriber's record with the generated random word and QR code: CODE

Loop Completion

  • Once all subscribers have been processed, the loop will complete.

Completion Step

  • You can add any additional steps or actions you need after the loop completion to complete your workflow.


This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

12 replies

Userlevel 7
Badge +14

Hi @CCDNB 

Good question.

Are you trying to have a new word and QR code generated for each Mailchimp Subscriber whenever a Campaign is scheduled in Mailchimp?

Hi @Troy Tessalone Yes, that’s exactly what I’m trying to do.

 

I now have this figured out until Action 4:

Action 4: Update Subscriber

  • Add a "Code" step to update each subscriber's record in Mailchimp.

This final step has been very hard for me to implement.

Userlevel 7
Badge +14

@CCDNB

That’s going to be very “costly” to do as the # of subscribers grows.

 

Retrieving subscribers from the Mailchimp API involves pagination that need to be looped thru. (e.g. max 1,000 records per page.)

 

Mailchimp API supports BATCH operations. (read more)

 

FYI: The Looping app only supports a max of 500 records.

 

Zap action steps also have timeouts you need to work around. (e.g. max 30 seconds)

 

If you need help, consider hiring a Certified Zapier Expert: https://zapier.com/experts/automation-ace

Userlevel 7
Badge +14

@CCDNB 

A more efficient approach may be to use Mailchimp exports and imports to prep and update the Subscriber data.

https://mailchimp.com/help/view-export-contacts/

https://mailchimp.com/help/import-contacts-mailchimp/

@Troy Tessalone I’ve actually sorted it all out with Code, removing the loop function all together.

Action 3, retrieving the whole list of subscribers,

import requests

def retrieve_subscribers():
    api_key = '***'
    list_id = '***'

    url = f'https://*.api.mailchimp.com/3.0/lists/{list_id}/members'

    headers = {
        'Authorization': f'Bearer {api_key}',
        'Content-Type': 'application/json'
    }

    response = requests.get(url, headers=headers)
    response_data = response.json()

    return response_data['members']

subscribers = retrieve_subscribers()
return subscribers


And my Action 4 currently looks like:

const previousStep = {
  password: 'generated_password_here',
  qrCodeUrl: 'generated_qr_code_url_here'
};

const apiKey = '***';
const listId = '***';

const updateSubscriber = async (subscriber, password, qrCodeUrl) => {
  const url = `https://*.api.mailchimp.com/3.0/lists/${listId}/members/${subscriber.subscriberId}`;

  const mergeFields = {
    WORD: password,
    QR_CODE: qrCodeUrl
  };

  const response = await fetch(url, {
    method: 'PATCH',
    headers: {
      'Authorization': `Bearer ${apiKey}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      merge_fields: mergeFields
    })
  });

  const responseData = await response.json();

  return responseData;
};

const subscribers = Array.isArray(inputData) ? inputData : [inputData]; // Ensure inputData is an array

const updatedSubscribers = [];

for (const subscriber of subscribers) {
  const password = previousStep.password; // Access the generated password from the previous step
  const qrCodeUrl = previousStep.qrCodeUrl; // Access the generated QR code URL from the previous step

  const updatedSubscriber = await updateSubscriber(subscriber, password, qrCodeUrl);
  updatedSubscribers.push(updatedSubscriber);
}

return updatedSubscribers;


HOWEVER While I have this code ^ I am unsure what to do with it…

The data being collected is the Word, QR Code, and List of Subscribers. I am unsure where to inout them properly into this code to make it work properly.

Userlevel 7
Badge +14

@CCDNB 

Make sure to review the available help articles for using the Code app in Zaps: https://zapier.com/apps/code/help

 

Requiring or Using External Libraries

Unfortunately you cannot require external libraries or install or import libraries commonly referred to as "npm modules".
Only the standard node.js library and the fetch package are available in the Code app.
fetch is already included in the namespace.

@Troy Tessalone 

I’ve been trying to batch mailchimp stuff you linked to. Do you know why it’s timing me out after 10.1 seconds? Mailchimp says it should be 120.

Userlevel 7
Badge +14

@CCDNB 

Help article:

 

I just wanted to say that I completed this solo talking it over with ChatGPT. ChatGPT helped me way more than a real person did.

Userlevel 7
Badge +11

Thanks for following up here to let us know how you solved it @CCDNB. Presumably ChatGPT was able to rewrite the code you were using to fix things here?

At any rate, I’m glad you were able to find a solution here using ChatGPT. Gosh, is there anything it can’t do?! 

Sounds like you’re all set for now but please do reach out in the Community if you ever need help. We’d love to hear from you! 🙂

@CCDNB

A more efficient approach may be to use Mailchimp exports and imports to prep and update the Subscriber data.

https://mailchimp.com/help/view-export-contacts/

https://mailchimp.com/help/import-contacts-mailchimp/

It seems that exporting from mail service defeats the purpose of Zapier. That is what I’m trying to save time on.

@CCDNB

A more efficient approach may be to use Mailchimp exports and imports to prep and update the Subscriber data.

https://mailchimp.com/help/view-export-contacts/

https://mailchimp.com/help/import-contacts-mailchimp/

It seems that exporting from mail service defeats the purpose of Zapier. That is what I’m trying to save time on.



I just retrieved the emails in batches though Python Code. It organizes them into a long row of data for the output, which I used as Input Data and split in a later step.

Here’s the code: https://chat.openai.com/share/1814131f-ce1c-4f65-a613-f1f781f3c56d