Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node + Mailchimp NPM: How to add a subscriber to a list and include their first and last name?

Mailchimp is almost a perfect company, except their Node API documentation is non-existent. How can I add a subscriber to my new list and include their first name and last name? The code below successfully adds the subscriber, but first and last names are not being 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
like image 316
ac360 Avatar asked Dec 02 '22 16:12

ac360


1 Answers

The supplied merge variables should be passed as a single object, not an array of objects. Please 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 supplied your actual mail chimp API key in your question. If so, you should remove it immediately.

like image 152
Mark Bonano Avatar answered Dec 23 '22 03:12

Mark Bonano