Node + Mailchimp NPM: how to add a subscriber to the list and include his first and last name?

Mailchimp is an almost perfect company, with the exception of their documentation on the Node API, it does not exist. How to add a subscriber to my new list and indicate their first and last name? The code below successfully adds the subscriber, but first and last names are not added.

var MCapi = require('mailchimp-api');
MC        = new MCapi.Mailchimp('***********************-us3');

addUserToMailchimp = function(user, callback) {
  var merge_vars = [
      { EMAIL: user.email }, 
      { LNAME: user.name.substring(user.name.lastIndexOf(" ")+1) },
      { FNAME: user.name.split(' ')[0] }
  ];

  MC.lists.subscribe({id: '1af87a08af', email:{email: user.email}, merge_vars: merge_vars, double_optin: false }, function(data) {
      console.log(data);
  }, function(error) {
      console.log(error);
  });
}; // addUserToMailchimp
+4
source share
1 answer

The merge variables presented should be passed as a single object, not an array of objects. See my example below:

var mcReq = {
    id: 'mail-chimp-list-id',
    email: { email: 'subscriber-email-address' },
    merge_vars: {
        EMAIL: 'subscriber-email-address',
        FNAME: 'subscriber-first-name',
        LNAME: 'subscriber-last-name'
    }
};

// submit subscription request to mail chimp
mc.lists.subscribe(mcReq, function(data) {
    console.log(data);
}, function(error) {
    console.log(error);
});

It looks like you provided your actual API key for the mail chimpanzee in your question. If so, you should remove it immediately.

+19
source

Source: https://habr.com/ru/post/1540653/


All Articles