Skip to content

Instantly share code, notes, and snippets.

@eibrahim
Created July 22, 2016 13:09
Show Gist options
  • Save eibrahim/23e0526aac232dd6f41016c62be86c41 to your computer and use it in GitHub Desktop.
Save eibrahim/23e0526aac232dd6f41016c62be86c41 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