Created
August 11, 2015 23:23
-
-
Save fform/0b07b4aff75da3c1cd54 to your computer and use it in GitHub Desktop.
Example Parse.User afterSave, export name to mail chimp
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 mailchimpData = { | |
apikey: 'your-api-key', | |
id: 'your-list-id', | |
email: { | |
email: '[email protected]' | |
}, | |
merge_vars: { | |
'FNAME': 'First name', | |
'LNAME': 'Last Name' | |
}, | |
double_optin: false, | |
send_welcome: false | |
}; | |
var dataCenter = "us11"; | |
var url = "https://"+dataCenter+".api.mailchimp.com/2.0/lists/subscribe.json"; | |
Parse.Cloud.httpRequest({ | |
method: 'POST', | |
url: url, | |
body: JSON.stringify(mailchimpData), | |
success: function (httpResponse) { | |
console.log(httpResponse.text); | |
response.success("Successfully subscribed"); | |
}, | |
error: function (httpResponse) { | |
console.error('Request failed with response code ' + httpResponse.status); | |
console.error(httpResponse.text); | |
response.error('Mailchimp subscribe failed with response code ' + httpResponse.status); | |
} | |
}); | |
// | |
// Example of how you could put this in Parse Cloud code and auto-add on a new user | |
// | |
Parse.Cloud.afterSave(Parse.User, function(request) { | |
var user = request.object; | |
var mailchimpData = { | |
apikey: 'your-api-key', | |
id: 'your-list-id', | |
email: { | |
email: user.get('email') | |
}, | |
// Presuming your registration asks for and assigns the name somewhere | |
merge_vars: { | |
'FNAME': user.get('firstName'), | |
'LNAME': user.get('lastName') | |
}, | |
double_optin: false, | |
send_welcome: false | |
}; | |
var dataCenter = "us11"; | |
var url = "https://"+dataCenter+".api.mailchimp.com/2.0/lists/subscribe.json"; | |
Parse.Cloud.httpRequest({ | |
method: 'POST', | |
url: url, | |
body: JSON.stringify(mailchimpData), | |
success: function (httpResponse) { | |
console.log(httpResponse.text); | |
console.log('Success'); | |
}, | |
error: function (httpResponse) { | |
console.error('Request failed with response code ' + httpResponse.status); | |
console.error(httpResponse.text); | |
response.error('Mailchimp subscribe failed with response code ' + httpResponse.status); | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment