Skip to content

Instantly share code, notes, and snippets.

@anthkris
Created September 16, 2018 04:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthkris/aba948535e894e8440a196e97569c5e5 to your computer and use it in GitHub Desktop.
Save anthkris/aba948535e894e8440a196e97569c5e5 to your computer and use it in GitHub Desktop.
Sending xAPI statements from a Typeform to an LRS using Zapier
var output = [];
// Make a POST request to your LRS
const doFetch = (answer) => {
// You may need to add on a '/statements' to the end of your endpoint URL
fetch("YOUR_ENDPOINT", {
method: "POST",
headers: { // ALL of these headers are necessary
"X-Experience-API-Version": "1.0.3",
"Content-Type": "application/json",
"Authorization": "Basic YOUR_BASIC_AUTH"
},
body: JSON.stringify(answer)
}).then(function(response) {
return response.json();
}).then((json) => {
console.log(json);
output = json || [];
callback(null, {output: output});
}).catch(callback);
};
// Function to help structure xAPI statements
const createStatement = (question) => {
var assessment = {
"actor": {
"objectType": "Agent",
"name": question.name,
"mbox": question.email
},
"verb": {
"id": "http://adlnet.gov/expapi/verbs/answered",
"display": {"en-US": "answered"}
},
"object": {
"id": "https://www.knanthony.com/typeformchat/questions/question-" + question.questionNum,
"definition": {
"description": { "en-US": question.question },
"type": "http://activitystrea.ms/schema/1.0/question",
"name": { "en-US": "Question " + question.questionNum }
}
},
"result": {
"completion": true,
"success": true,
"response": question.response
}
};
doFetch(assessment);
}
// This is the way I sent my statements
// It could, perhaps be done better?
var num = 1;
var email = "mailto:" + inputData.emailQuestion;
var name = inputData.nameQuestion;
createStatement({name: name, email: email, question: "What are you trying to get better at?", response: inputData.developQuestion, questionNum: num});
num++;
createStatement({name: name, email: email, question: "What kinds of questions do you have about accessible design?", response: inputData.goDeeperQuestion, questionNum: num});
num++;
createStatement({name: name, email: email, question: "Since you want to get better at accessible design, where do you think you'd like to start?", response: inputData.whereToStartQuestion, questionNum: num});
num++;
createStatement({name: name, email: email, question: "Now that you have a starting point, what's your plan? What will you do this week to make your products more accessible?", response: inputData.actionPlanQuestion, questionNum: num});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment