Skip to content

Instantly share code, notes, and snippets.

@65
Last active January 15, 2022 17:15
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 65/5316324a1692adafc61f1785cec45a70 to your computer and use it in GitHub Desktop.
Save 65/5316324a1692adafc61f1785cec45a70 to your computer and use it in GitHub Desktop.
On Google Spreadsheet / Google Form submission, push new entry to a MailChimp subscription list with an `onformsubmit` trigger. Uses the MailChimp 3.0 API and Google Apps Script.
/**
* Mailchimp API v3
* Add a subscriber to a list with interests on Google Form submit
*
* Updated to 3.0 from https://gist.github.com/acarrillo/5772508
*
* Edit the Google Sheet connected to your form
* Menu > Tools > Script Editor
* Copy and paste this into the file, remove anything there already
* Update the variables in the top section
* If you want to use interests insert / edit that part too
* In the script editor hit run > myFunction to create the trigger
*
*
*/
// Your API Key from https://us3.admin.mailchimp.com/account/api/
var API_KEY = 'xxxxxxxxxxxxxxxxxxx-yyy';
// get the right server by taking yyy from the API key
// replace bbbbb with the list id you wish to work on
var mc_base_url = 'https://yyy.api.mailchimp.com/3.0/lists/bbbbbbbbb/members';
// subscribed (no opt in confirmation)
// unsubscribed
// cleaned
// pending (triggers double optin)
// transactional
var status = 'pending';
// What is the question name of the email address in the form?
var fieldEmail = 'Your Email address';
/**
* Uses the MailChimp 3.0 API to add a subscriber to a list.
*/
function sendToMailChimp(em){
Logger.log('sendToMailChimp');
var interests = {
"cccccccccc":true // add in the ID of the Interest (under the List > Interest Categories)
};
var payload = {
"email_address": em,
"status": status,
"interests" : interests
};
var headers = {
"Authorization": 'Basic ' + Utilities.base64Encode('anystring:' + API_KEY, Utilities.Charset.UTF_8)
};
var options = {
"method": "post",
"payload": JSON.stringify(payload),
"headers": headers,
"muteHttpExceptions" : true
};
Logger.log(options);
try {
var response = UrlFetchApp.fetch(mc_base_url,options);
if(response.getResponseCode() === 200) {
// all good!
Logger.log('Success');
Logger.log(response);
} else{
Logger.log('Issues');
Logger.log(response);
}
} catch (err) {
// handle the error here
Logger.log('Error');
Logger.log(err);
}
}
/**
* Trigger function. Based on Google Script tutorial.
* @param {Object} e The event parameter for form submission to a spreadsheet;
* see https://developers.google.com/apps-script/understanding_events
*/
function onFormSubmit(e) {
Logger.log('on Form Submit');
Logger.log(e);
Logger.log(e.namedValues[fieldEmail]);
var em = e.namedValues[fieldEmail][0];
Logger.log(em);
if (em.length){
sendToMailChimp(em);
}else{
Logger.log('Error: couldnt find an email address in submission');
}
}
/**
* Main function. Creates onFormSubmit trigger.
*/
function myFunction(){
// Was separated line by line for debugging purposes.
var sheet = SpreadsheetApp.getActive();
var a = ScriptApp.newTrigger("onFormSubmit");
var b = a.forSpreadsheet(sheet);
var c = b.onFormSubmit();
var d = c.create();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment