In the Results note set we learned how to access the results from our experiment and output them to the web inspector console.
This was useful for us to examine/make sure we were collecting the correct data, but of course we will need a way to save these results from each participant so that the data can then be studied and analyzed.
To accomplish this, we will have two modes for saving data:
Saving our results to text files will require more code than just outputting the results to the console, so let’s re-organize our experiment so the “save results” code is in its own trial rather than embedded in the debrief trial. This re-organization will also allow us to have a “Please wait while we save your results...” message appear while the save process executes.
Before the debriefTrial, add a new resultsTrial with the following code:
let resultsTrial = {
type: jsPsychHtmlKeyboardResponse,
choices: ['NO KEYS'],
async: false,
stimulus: `
<h1>Please wait...</h1>
<p>We are saving the results of your inputs.</p>
`,
on_start: function () {
// ⭐ Update the following three values as appropriate ⭐
let prefix = 'lexical-decision';
let dataPipeExperimentId = 'your-experiment-id-here';
let forceOSFSave = false;
// Filter and retrieve results as CSV data
let results = jsPsych.data
.get()
.filter({ collect: true })
.ignore(['stimulus', 'trial_type', 'plugin_version', 'collect'])
.csv();
// Generate a participant ID based on the current timestamp
let participantId = new Date().toISOString().replace(/T/, '-').replace(/\..+/, '').replace(/:/g, '-');
// Dynamically determine if the experiment is currently running locally or on production
let isLocalHost = window.location.href.includes('localhost');
let destination = '/save';
if (!isLocalHost || forceOSFSave) {
destination = 'https://pipe.jspsych.org/api/data/';
}
// Send the results to our saving end point
fetch(destination, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Accept: '*/*',
},
body: JSON.stringify({
experimentID: dataPipeExperimentId,
filename: prefix + '-' + participantId + '.csv',
data: results,
}),
}).then(data => {
console.log(data);
jsPsych.finishTrial();
})
}
}
timeline.push(resultsTrial);
There’s a lot of new code in this trial which will be explained in more detail in the video that accompanies this note set, but the following is a summary of key points:
on_start method, the saving happens.async is set to false. This means the code that communicates with the server (either our local server or OSF) must complete before the trial will complete. This ensures our data is saved before the experiment continues.prefix with the name of your experiment (in this example, it’s set to ldt (short for lexical decision)). This prefix will be used as part of your results filenames./save route on our web server, which is pre-programmed to save the results to a file within our project’s /data directory.With the above code in place, run through your experiment and then confirm results are being written to psy1903/data.
When developing/testing experiments, this is the method you should use for collecting results as it’s quicker and uses less resources then sending the results to OSF.io (to be covered in Week 8).
No subscriptions, no auto-renewals.
Just a simple one-time payment that helps support my free, to-the-point videos without sponsered ads.
Unlocking gets you access to the notes for this video plus all 200+ guides on this site.
Your support is appreciated. Thank you!