I am a beginner in React Native. I'm basically trying to perform the integration between my application in React Native and MailChimp, what I want to do is: From the moment the user provides us with their email, and send the form, then an email is triggered for it through MailChimp, I use Firebase to create my own email base, but I would like to automate the sending task through mailchimp, the method I use to save emails in firebase is as follows:
saveEmail() {
var id = firebase.database().ref().child('xxx').push().key;
const valueEmail = this.state.input;
const valueName = this.state.name;
firebase.database().ref('/xxx/' + id).set({
email: valueEmail,
name: valueName
});
//here where the post for the mailchimp API will be carried out
this.setState(
{
isModalVisible: !this.state.isModalVisible,
valid: false,
name: '',
input: ''
}
);
}
Thanks everyone!
everything quiet around here? In the middle of some research and thoughts, a possible task for a problem solution, below to perform an integration between MailChimp and Firebase for a React Native Application.
The first of all, done from some research, is seen as a server-side database, since I was already using the Firebase Realtime Database and then I decided to use it as Cloud Functions to solve this problem by doing a a Cloud Function, which is checking the state of the database, and when it is given, a POST to a MailChimp API is triggered, here is a function created for this:
const functions = require('firebase-functions');
\\Code suppressed here
exports.onDataAdded = functions.database.ref('/xxx/{id}').onCreate((snap, context) => {
const data = snap.val();
var fetch = require('isomorphic-fetch');
var btoa = require('btoa');
console.log(data.email);
// POST /lists/{list_id}/members
// Add a new list member
var MAILCHIMP_API_KEY = 'Key Here';
var listId = 'Id Here';
// NOTE: mailchimp's API uri differs depending on your location. us6 is the east coast.
var url = 'https://<dc>.api.mailchimp.com/3.0/lists/' + listId + '/members';
var method = 'POST';
var headers = {
'authorization': "Basic " + btoa('randomstring:' + MAILCHIMP_API_KEY),
'Accept': 'application/json',
'Content-Type': 'application/json'
};
var body = JSON.stringify(
{
email_address: data.email,
status: 'subscribed',
'language': data.language,
merge_fields: { FNAME: data.name }
}
);
return fetch(url, {
method,
headers,
body
}).then(resp => resp.json())
.then(resp => {
return console.log(resp)
})
.catch(error => {
throw new Error(error.statusText)
});
});
With this function created in the Cloud Functions of Firebase, from the moment that some insertion of Realtime Database is made, that trigger is triggered that triggers this function.
Thanks for the quick reply!!