Skip to content

Instantly share code, notes, and snippets.

@forest
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forest/9287214 to your computer and use it in GitHub Desktop.
Save forest/9287214 to your computer and use it in GitHub Desktop.
Google Forms Integration with Constant Contact API
/**
* A trigger-driven function that adds the user to Constant Contact lists.
*
* @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('onFormSubmit');
try {
var user = {
first_name: e.namedValues['First Name'][0],
last_name: e.namedValues['Last Name'][0],
email: e.namedValues['Email Address'][0],
teams: e.namedValues['Teams'][0],
street: e.namedValues['Street'][0],
city: e.namedValues['City'][0],
zip: e.namedValues['Zip'][0],
phone: e.namedValues['Phone'][0]
};
Logger.log(user);
createOrUpdateContact(user);
} catch (error) {
MailApp.sendEmail("my@email.com", "Error report", error.message);
}
}
function APIRequestOptions() {
return { "headers" : { "Authorization": "Bearer 12345" } };
}
function APIKey() { return '67891' }
function findContactByEmailURL(email) {
return Utilities.formatString('https://api.constantcontact.com/v2/contacts?api_key=%s&email=%s', APIKey(), email);
}
function createContactUrl() {
return Utilities.formatString('https://api.constantcontact.com/v2/contacts?action_by=ACTION_BY_OWNER&api_key=%s', APIKey());
}
function updateContactUrl(contactId) {
return Utilities.formatString('https://api.constantcontact.com/v2/contacts/%s?action_by=ACTION_BY_OWNER&api_key=%s', contactId, APIKey());
}
// returns contact object or null.
function findContactByEmail(email) {
var contact = null,
url = findContactByEmailURL(email),
response = UrlFetchApp.fetch(url, APIRequestOptions());
if (response.getResponseCode() == "200" || response.getResponseCode() == "201") {
var content = JSON.parse(response.getContentText()),
results = content.results;
if (results.length > 0) {
contact = results[0];
}
}
return contact;
}
function createOrUpdateContact(userDetails) {
var contact = findContactByEmail(userDetails.email);
if (contact == null) {
createContact(userDetails);
} else {
updateContact(contact, userDetails)
}
}
function createContact(userDetails) {
Logger.log("createContact");
var contact = {
"addresses": [{
"city": userDetails.city,
"line1": userDetails.street,
"postal_code": userDetails.zip,
"state_code": "CA"
}],
"lists": getListsFromTeams(userDetails.teams),
"cell_phone": userDetails.phone,
"first_name": userDetails.first_name,
"last_name": userDetails.last_name,
"email_addresses": [{
"email_address": userDetails.email
}]
};
Logger.log(contact);
var url = createContactUrl(),
options = APIRequestOptions();
options.contentType = "application/json";
options.method = "post";
options.payload = JSON.stringify(contact);
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() == "200" || response.getResponseCode() == "201") {
var content = JSON.parse(response.getContentText());
Logger.log(content);
return true;
}
return false;
}
function updateContact(contact, userDetails) {
Logger.log("updateContact");
if (contact.addresses && contact.addresses[0]) {
contact.addresses[0].city = userDetails.city;
contact.addresses[0].line1 = userDetails.street;
contact.addresses[0].postal_code = userDetails.zip;
} else {
contact.addresses = [{
"city": userDetails.city,
"line1": userDetails.street,
"postal_code": userDetails.zip,
"state_code": "CA"
}];
}
if (contact.lists && contact.lists[0]) {
contact.lists = contact.lists.concat(getListsFromTeams(userDetails.teams));
} else {
contact.lists = getListsFromTeams(userDetails.teams);
}
contact.cell_phone = userDetails.phone;
contact.first_name = userDetails.first_name;
contact.last_name = userDetails.last_name;
contact.email_addresses[0].email_address = userDetails.email;
Logger.log(contact);
var contactId = contact.id,
url = updateContactUrl(contactId),
options = APIRequestOptions();
options.contentType = "application/json";
options.method = "put";
options.payload = JSON.stringify(contact);
var response = UrlFetchApp.fetch(url, options);
if (response.getResponseCode() == "200" || response.getResponseCode() == "201") {
var content = JSON.parse(response.getContentText());
Logger.log(content);
return true;
}
return false;
}
function getListsFromTeams(teams) {
var re = /\s*,\s*/,
teamList = teams.split(re),
lists = [];
for (var i=0; i < teamList.length; i++) {
var listId = getListIdFromTeam(teamList[i]);
if (listId) {
lists.push({"id": listId});
}
}
return lists;
}
function getListIdFromTeam(team) {
var listId = null;
switch (team) {
case "Dress Tailors":
listId = "1883400855";
break;
case "Food Servers":
listId = "2065485373";
break;
case "Hair Stylists (professionals only)":
listId = "1238011459";
break;
case "Make-Up Artists":
listId = "1838226340";
break;
case "Parking Attendants":
listId = "1072775953";
break;
case "Cheering at the Red Carpet! (Paparazzi)":
listId = "1151295661";
break;
case "Set Up/Tear Down Crew":
listId = "1090779649";
break;
case "Hosting a Guest (ages 15-25 only)":
listId = "1075833246";
break;
case "Anywhere I’m needed!":
listId = "1680913527";
break;
}
return listId;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment