Skip to content

Instantly share code, notes, and snippets.

@cwsaylor
Forked from eibrahim/mailchimp-firebase.js
Created June 23, 2017 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cwsaylor/746fa77943d353d055b076cda0394695 to your computer and use it in GitHub Desktop.
Save cwsaylor/746fa77943d353d055b076cda0394695 to your computer and use it in GitHub Desktop.
A node worker for firebase to add a user to mailchimp
var mcapi = require('./node_modules/mailchimp-api/mailchimp');
var usersRef = db.ref('users');
var mc = new mcapi.Mailchimp('xxxxxxxxxx-api-key-us4');
usersRef.orderByChild('added_to_mailchimp').equalTo(null).on('child_added',function(snapshot){
var user = snapshot.val();
var key = snapshot.key;
if(user && user.email){
var listId = 'xxxx-list-id-xxxx';
var name = user.displayName || '';
var fname = name.split(' ')[0];
var lname = name.split(' ').slice(1).join(' ');
mc.lists.subscribe({id: listId,
email:{email:user.email},
merge_vars: {
name: name,
LNAME: lname,
FNAME: fname
},
double_optin: false,
update_existing: true,
send_welcome: false
}, function(data) {
db.ref('users/'+ key +'/added_to_mailchimp').set(true);
},
function(error) {
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment