Rule for creating new User in Mailchimp when User signs up with Auth0

Hi everyone, new to here to Auth0. Looking to create a rule for a User that signs up on Auth0 to then add the user to Mailchimp.

Having trouble adding these fields below

  • MC_TOKEN: Mailchimp API Token. E.g.: ef235a44355dda3e61ea074ae0d439a2-us20
  • MC_LIST_ID: ID of the Mailchimp list you would like to populate. E.g.: ca9eb2555e
  • MC_API_URL: Mailchimp API URL. E.g.: https://us20.api.mailchimp.com/3.0

Can someone help show where they go? Thanks! :pray:

Found this code:

function (user, context, callback) {

    // Short-circuit if the user signed up already
    if (context.stats.loginsCount > 1 || context.protocol === 'oauth2-refresh-token') {
        return callback(null, user, context);
      }
  
    const request = require('request');
  
    const ctx = {
        email_address: user.email,
        merge_fields : {"FNAME": user.given_name || "",
                        "LNAME": user.family_name || ""},
        status: "subscribed"
    };
 
    var auth = "Basic " + new Buffer("token:" + configuration.MC_TOKEN).toString("base64");

    request.post({
      url: configuration.MC_API_URL + "/lists/" + configuration.MC_LIST_ID + "/members",
      headers : {"Authorization" : auth},
      json: ctx
    });
  
    // Don’t wait for the Mailchimp call to finish, return right away (the request will continue on the sandbox)`
    callback(null, user, context);
}
1 Like

Hi @christopherkuchta,

Welcome to the Auth0 Community!

There are a couple of things I would like to address.

First, Rules are executed Post-Login. In general, whenever a user signs up for your app, they are immediately logged in afterward. In the Rule snippet you shared, it hacks around this by checking if the loginCount is less than 1 and executing this Rule only once. It works, however, Rules will be deprecated sometime this year in 2022 and we encourage you to migrate to Actions.

In this case, I recommend using an Auth0 Post-User Registration Action to accomplish this. The Mailchimp documentation will also be useful for looking at their API reference to make these requests.

See below of the Action snippet:

exports.onExecutePostUserRegistration = async (event) => {
  const client = require('@mailchimp/mailchimp_marketing');

  client.setConfig({
    apiKey: event.secrets.MC_API_KEY,
    server: event.secrets.MC_SERVER_PREFIX
  });

  const run = async () => {
    const response = await client.lists.addListMember(event.secrets.MC_LIST_ID, {
      email_address: "example_user@gmail.com",
      status: "pending",
    });
    console.log(response);
  };

  run();
};

Once you have included this Action, please click on the Secrets button to include the MC_TOKEN, MC_LIST_ID, AND MC_API_URL values. These values can be called by using the event.secrets prefix, for example event.secrets.MC_TOKEN.

And include the appropriate dependency:

After this is complete, you should be able to create a new user in Mailchimp whenever a new user signs up onto your Auth0 application.

Please let me know how this goes for you.

Thank you.

Hi @rueben.tiow Awesome. Thank you so much for your detailed response :raised_hands: I am going to work on this later this evening and will let you know if I have any problems.

Hi @rueben.tiow I just implemented everything on Auth0 and not having luck getting the Post registration user into the Mailchimp list. Any idea if I am missing something?



Hi @christopherkuchta,

Thank you for your response.

I have tested this thoroughly and did not find your same observations.

Instead, after I added the member to the list, I called the Mailchimp Marketing API List members info endpoint and verified that the users are added to the specific list.

I also encountered the “user already exists” error when adding the user to the list again.

In this case, could you please verify if your new user (example_user@gmail.com) is added to the member’s list?

Looking forward to your response.

Thanks.

Hi @rueben.tiow thanks for your continued help. Sorry for my lack of experience with back end dev, I think it may not be working for me because I do not have this exact code added

const client = require("mailchimp-marketing");

client.setConfig({
  apiKey: "YOUR_API_KEY",
  server: "YOUR_SERVER_PREFIX",
});

const run = async () => {
  const response = await client.lists.setListMember(
    "list_id",
    "subscriber_hash",
    { email_address: "Marcel81@gmail.com", status_if_new: "pending" }
  );
  console.log(response);
};

run();

Right now the code currently looks like this for the action:

exports.onExecutePostUserRegistration = async (event) => {
  const client = require('@mailchimp/mailchimp_marketing');

  client.setConfig({
    apiKey: event.secrets.MC_API_KEY,
    server: event.secrets.MC_SERVER_PREFIX
  });

  const run = async () => {
    const response = await client.lists.addListMember(event.secrets.MC_LIST_ID, {
      email_address: "example_user@gmail.com",
      status: "pending",
    });
    console.log(response);
  };

  run();
};


Also did not see example_user@gmail.com added to the mailchimp list

Hi @christopherkuchta,

Thank you for your response.

Could you please clarify if you used the Mailchimp Marketing API List members info endpoint to check if the users are added to the specific list?

When using this endpoint, I successfully saw the member’s list populated.

Looking forward to your reply.

Thanks.

Hi @rueben.tiow sorry I am only a front-end developer trying to help out a client set this up that I normally am only front-end building in Webflow.

I think I have the initial setup all correct but I am not sure where to put this Mailchimp API? I tried adding it as a new action but did not work.

Are you able to help out the client directly or is there another contractor from Auth0 that could set this connection up with Mailchimp for them?

Thanks so much :pray:t2:

Hi @christopherkuchta,

Thank you for your response.

Could you please elaborate further what you mean by the Action did not work? Did you get any errors?

Sure! I can help the customer if they can reach out to me through email or DM on the Community Forum.

Please let me know what they decide.

Thank you.

I’m not seeing the new contact in Mailchimp when I have the action implemented but I may totally have something setup wrong. I am going to have the customer reach out to you directly and we can figure this out! Thanks again :pray:t2:

1 Like

Hi @christopherkuchta,

Thank you for your response.

After digging into this, I was able to find the same observations as you when looking at my Mailchimp Dashboard. However, when calling the Mailchimp API’s List members info endpoint, I managed to see the included user.

I will look into this further to see what is happening. Once I have more information.

Thanks!

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.