← Other topics

Coding Psych Experiments
jsPsych Save Results

Timecode of this content: 5:35:42

Video Notes

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:

  1. In our “development environment” results will be saved as .csv text files to our computer via our local web server.
  2. In our “production environment” where we’ll share our experiment with real-world participants, the .csv text files will be saved to OSF.io (Open Science Framework), an open-source platform designed to facilitate open and reproducible research.

Setup

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:

Test local saving

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).

Unlock all the notes for $4

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!

Payment Info

/
$4 6 months
$25 forever
Please check the form for errors
Questions? help@codewithsusan.com
← Other topics