Last active
January 19, 2022 15:38
-
-
Save kjulin/9ada78a631ed6d65afe6dacb6a1af1af to your computer and use it in GitHub Desktop.
This code can be copied to Google App Script to send data from Google From responses to Introist. Read detailed instructions at https://resources.introist.com/updating-data-from-google-form-responses
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
// TODO: Fill in your identifier mapping and update mapping | |
// Example: Introist Employee Attribute with variable name emailwork would be the value of response to question with ID 185293331 | |
// To find question IDs, read the detailed instructions https://resources.introist.com/updating-data-from-google-form-responses | |
var identifierMapping = { | |
emailwork: 185293331 | |
}; | |
var updateMapping = { | |
supervisorname: 1487711633 | |
}; | |
// If you want to only update Journey for specific workflow, replace undefined with workflow ID from Introist, e.g. "fa95396b-c29b-47f9-b825-35333e05c85a" | |
var workflowId = undefined; | |
// If you want to name this update source, replace undefined with source name, e.g. "My test form" | |
var source = undefined; | |
//Replace api key below with your Introist api key | |
var apiKey = 'YOUR-INTROIST-API-KEY-HERE'; | |
// Or you can add the API key to App Script project properties and uncommend the following line to get it from there | |
//var apiKey = PropertiesService.getScriptProperties().getProperty('apikey'); | |
// YOU DON'T NEED TO MAKE ANY CHANGES BELOW THIS LINE | |
function onSubmit(e) { | |
var url = 'https://api.introist.com/v1/ext/journeys/data'; | |
var items = e.response.getItemResponses(); | |
var identifiers = collectDataByMapping(identifierMapping, items); | |
var updates = collectDataByMapping(updateMapping, items); | |
var payload = { | |
identifiers: identifiers, | |
updates: updates, | |
workflowId: workflowId, | |
source: source | |
}; | |
var options = { | |
"method" : "post", | |
"payload" : JSON.stringify(payload), | |
"contentType": "application/json", | |
"headers": { | |
"authorization": apiKey | |
}, | |
"muteHttpExceptions": true | |
}; | |
var response = UrlFetchApp.fetch(url,options); | |
var responseStatus = response.getResponseCode(); | |
if (responseStatus !== 200) { | |
throw new Error(`Introist request failed with status ${responseStatus}: ${response.getContentText()}`); | |
} | |
} | |
function collectDataByMapping(mapping, items) { | |
var data = {}; | |
Object.keys(mapping).forEach((attributeVariable) => { | |
const itemID = mapping[attributeVariable]; | |
const response = findResponseByItemID(items, itemID); | |
data[attributeVariable] = response; | |
}); | |
return data; | |
} | |
function findResponseByItemID(itemResponses, itemID){ | |
var item = itemResponses.find(i => i.getItem().getId() === itemID); | |
if (!item) { | |
throw new Error(`Item with ID ${itemID} not found from response`); | |
} | |
return item.getResponse(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment