Created
June 13, 2013 09:44
-
-
Save acarrillo/5772508 to your computer and use it in GitHub Desktop.
On Google Spreadsheet form submission, push new entry to a MailChimp subscription list with an `onformsubmit` trigger. Uses the MailChimp API and Google Apps Script.
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 API_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; | |
var mc_base_url = 'http://us7.api.mailchimp.com/1.3/?method=listSubscribe'; | |
var mc_list_id = 'xxxxxxxxxx'; | |
var mc_double_optin = false; | |
/** | |
* Uses the MailChimp API to add a subscriber to a list. | |
*/ | |
function sendToMailChimp_(fn, ln, em, yr){ | |
var payload = { | |
"method": "listSubscribe", | |
"apikey": API_KEY, | |
"id": mc_list_id, | |
"merge_vars[FNAME]":fn, | |
"merge_vars[LNAME]":ln, | |
"merge_vars[YEAR]":yr, | |
"email_address": em, | |
"double_optin": mc_double_optin, | |
"update_existing": false | |
}; | |
var options = { | |
"method": "post", | |
"payload": payload | |
}; | |
var response = UrlFetchApp.fetch(mc_base_url,options); | |
Logger.log(response) | |
} | |
/** | |
* 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) { | |
var fname = e.namedValues['Name'][0].split(" ")[0]; | |
var lname = e.namedValues['Name'][0].split(" ")[1]; | |
var email = e.namedValues['Email'][0]; | |
var year = e.namedValues['Stuyvesant Graduation Year'][0]; | |
sendToMailChimp_(fname,lname,email,year); | |
} | |
/** | |
* 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