Using survey responses to conditionally end main experiment

Hi Everyone,

I use a survey (embedded in a survey routine in PsychoPy) to present participant information and to obtain consent.

In the main experiment, I would now like to make continuation of the rest of the study conditional upon the participant having given consent, but for whatever reason simply can’t get this to work.

This is what things look like in Builder:

The “survey_PIPConsentDemo” routine is the embedded survey. The CheckConsent routine consists of the following code:

surveyResponse = survey_PIPConsentDemo.getResponse();
if (surveyResponse["block1/consent_questions"]) {
    if ((! surveyResponse["block1/consent_questions"].length === 5)) {
        psychoJS.quit({message:'You indicated in the consent form that you are not eligible and/or willing to participate in this study. The study was thus aborted'});
    }    
}

I have 5 consent questions, hence the looking for length === 5, but regardless of whether I fill out all 5 consent questions or not, the study continues on to the main experiment (i.e. the Welcome routine).

I have also tried:

But these options give either TypeError messages indicating undefined variables, error messages associated with the len/.string operation, or messages indicating the surveyResponse variable has no content (which I suppose is also the reason the experiment continues regardless of whether all 5 consent questions ticked or not in my code above).

At this point, I don’t know what else to try. For whatever reason, the main experiment seems to not be able to access the survey responses.

I am using the latest stable release of PsychoPy: v2025.1.1 on a Windows computer.

Any help would be greatly appreciated.

Best wishes,

Bianca

Hello @bianhaan

You could use the console.log() command to print the value of surveyResponse to the console. I find it strange that you check the length of consent_questions rather than its value.

Best wishes Jens

Hi Jens,

Thank you for your response.

I have 5 consent questions that all need to be confirmed for the participant to be able to continue with the study (e.g. that they meet eligibility requirements, that they consent to data being used, consent to participate, etc.), so following the example from wakecarter, just making sure they tick all 5 seemed more efficient than individually querying each of the subquestions):

When I use console.log, I get no output to my console. In previous iterations I inserted a text object with text set as the value of surveyResponse, which then generated the text “hello world”, suggesting an empty variable.

Are you checking print(surveyResponse) ?

You should get something if:
a) you are ticking at least one box

and

b) Your line surveyResponse = survey_PIPConsentDemo.getResponse(); is referring to the correct survey routine.

Hello @bianhaan

I made a little toy-survey myself. I noticed the following differerences when using the defaults of the survey. My first block was named block_1 not block1. The consent checkboxes were part of the only question in that block and therefore the question was called by default question1. So perhaps, there is just a speeling problem.

I also changed the abort code to

if ((surveyResponse["block_1/question1"].length !== 5)) {
        psychoJS.quit({message:'You indicated in the consent form that you are not eligible and/or willing to participate in this study. The study was thus aborted'});
}  

With this I get an abort message at the end of the experiment.

Best wishes Jens

Hi wakecarter,

With an embedded survey, the whole experiment only runs on Pavlovia, so have been using console.log(surveyResponse). I also tried using the surveyResponse variable in a text object, where it then shows as empty (printing only Hello World)

The survey routine is definitely called survey_PIPConsentDemo.

Hi Jens,

Nice catch - the block1 was indeed a typo and that may have been the key factor underlying my (many many many) failed attempts to get this code to work.

I also changed the code to

surveyResponse = survey_PIPConsentDemo.getResponse();
if (surveyResponse["block_1/consent_questions"]) {
    if ((surveyResponse["block_1/consent_questions"].length !== 5)) {
        psychoJS.quit({message:'You indicated in the consent form that you are not eligible and/or willing to participate in this study. The study was thus aborted'});
    }    
}

… and that seems to work

Thank you so much :smiley: (slaps forehead repeatedly)

1 Like