Connect your Meteor App with MailChimp
By Vince
Does your application have users? I hope so! Keep them around and active.
The best way to get users engaged, and keep them coming back is to use email marketing. You can create a sign-up workflow, move users to different workflows based on what they are doing in your app, and re-engage users that haven’t logged in for X months/weeks. Its very easy, so here is some code that connects to the MailChimp API and lets you add users to Lists. Lists are what trigger the emails for each email address.
First step is to create a free MailChimp account.
Once you have that out of the way, you need an API key:
- Click your name in the top/right
- Click on Profile
- Under the Extras menu, click API keys
- Click the Create a Key button and a key will be generated
The key will be a string of random characters like c30f72sdfsdfsdfsdfsdfsdffsdfsdf-us1
The other piece of information we need is a List ID. If you don’t have an existing email list setup, then create a new list (this is under the Lists tab, then click “Create New List”).
Once you have identified the list you want to add/remove people from, click the Down Arrow icon on the far right, and goto “Settings”.
At the bottom of settings you will see: “Unique id for list xxxxxx”. Copy the value listed there, it will be something like 5c6e0000a11.
That’s all we need from MailChimp!
Feel free to browse the other API options they offer here:
http://developer.mailchimp.com/documentation/mailchimp/reference/overview/
Here is the code that will connect to their API and add a user:
//new user list - pull this from the meteor settings file,
//or you can set it in the code
const listAid = '5c6e0000a11';
const listAurl = 'https://us1.api.mailchimp.com/3.0/lists/' + listAid + '/members';
//this is your API key from earlier
const apikey = 'c30f72sdfsdfsdfsdfsdfsdffsdfsdf-us1';
//this will add the user to listA which I use
//as a new user email automation/DRIP campaign
//MailChimp only requires an email address,
//so the firstname is optional but useful if you collect it
const MCNewUser = function MCNewUser(email, firstname = '') {
"use strict";
//this comes from the MailChimp API specifications,
//we are changing their status to subscribed
let data = {"email_address": email,
"status": "subscribed",
"merge_fields": {"FNAME": firstname}};
//this is our API function call,
//which we can re-use for other mailchimp requests down the road
//it is a POST request since we are adding data/creating a user
MCApi('POST', listAurl, data);
};
//this is our API function call
const MCApi = function (method, apiUrl, data) {
//console.log('running API now', method, apiUrl, data);
let options = {'auth': apikey};
options['data'] = data || '';
// try…catch allows you to handle errors properly
try {
var response = HTTP.call(method, apiUrl, options).data;
// A successful API call returns no error
// but the contents from the JSON response
//console.log('response', response);
} catch (error) {
//there are many ways to handle an error message,
//you can put it in a Meteor.error message or just move along
console.log(error);
//also send an email if there is an error so we can figure out what happened
//You can also add logic in to re-try it at a later time but their API is very reliable
//use defer so it can happen outside of this function call since its not important
Meteor.defer(function () {
//this will run async and not block your call. Good for methods
var text = JSON.stringify(error);
Email.send({
from: '[email protected]',
to: '[email protected]',
subject: 'API Error encountered - Mailchimp',
text: text
});
});
return error;
}
};
export { MCnewuser }
That’s it! Not too bad once you get the hang of it. This pattern can be repeated for almost any API call that doesn’t require an OAuth workflow. That’s a different story!