Created
July 22, 2016 13:09
-
-
Save eibrahim/23e0526aac232dd6f41016c62be86c41 to your computer and use it in GitHub Desktop.
A node worker for firebase to add a user to mailchimp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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