A simple node serverless function that uses the Mailchimp API to figure out how many free campaigns you have left to send (e.g, Sends / Contacts), then sends you an email with that result after the campaign is sent!
If you're using the free tier of MailChimp, you're currently limited to 12000 email sent per month. How many campaings this will allow you to send, is therefore dependent on the size of your subscriber list.
On the MailChimp account page, it shows you how many sends you have left, and how many contacts you currently have, but it doesn't show you how many campaigns you have left?
Sure, we could do some simple math, but why not automate this!
- After a MailChimp campaigns sends, it hits a webhook URL
- That URL is the the serverless function (in my case, a Google Cloud function)
- That function then hits the MailChimp API and determines how many campaigns are left
- Once that's complete, it uses Gmail to send an email back to the person who sent the campaign, with a message similar to this:
Hi! You can send 11 more MailChimp Campaigns before the 9th day of this month!
Statistics
983 emails sent of your 12000 email limit
982 subscribers
11017 more emails you can send
11.218940936863543 campaigns left.
- Head over to the Google Cloud Function and sign up.
- Create a new project, then create a new function.
- Name: I'm using a long string of random characters as the name (As I'm keeping it simple, there is no security so if someone knows the URL of this function, they'll be able to execute it. Therefore keeping the functin name long and random makes that less likely.)
- Memory: I left it at 256MB (which is overkill)
- Trigger: HTTP
- Authentication: Yes, allow unuthenticated invocations
- Source Code: Copy and paste the contents of
index.js
andpackage.json
into the editor (or upload a zip if you prefer) - Function to execute:
mailchimpCampaignsLeft
- Click on "Environment variables, networking, timeouts and more"
- Scroll down to the
Name/Key
value area and add the following values: API_KEY
- This is your MailChimp API keyRESET_DAY
- This is the day of the month that your mailchimp account resets (you can find this on the MailChimp Account page)EMAIL_LIMIT
- This is your MailChimp email limit -- probably12000
if you're on the free tier.GOOGLE_CLIENT_ID
,GOOGLE_CLIENT_SECRET
,GOOGLE_REFRESH_TOKEN
-- Follow this guide for how to generate these credentials for your app. (Steps 2 - 4).FROM_EMAIL
- The address you want the email to come from (ideally same as the authenticated gmail account)TO_EMAIL
- One or more comma seperated emails to send the report to- Finally, hit "Create" to deploy your function.
- Once that's complete, head to the trigger tab of your function and copy the URL
- Head to your MailChimp acount, click on Audience
- On the right side, click "Manage Audience" and head to "Settings"
- Scoll down and click on "Webhooks"
- Click "Create new webook"
- Enter the URL you copied from the above 10 step into the
Callback URL
field - Unselect all the boxes except for
Campaing sending
- Click
Save
That's it! The next time you send a MailChimp campaign, shortly thereafter you should receive an email of stats letting you know how many more campaigns you can send!