# How to conditionally end an online experiment in PsychoPy with Pavlovia surveys

**URL:** <https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073>\
**Category:** Online experiments\
**Created:** [November 26, 2024, 3:57pm UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073 "2024-11-26T15:57:54Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Angelika](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/angelika/32/43445_2.png) [@Angelika](https://discourse.psychopy.org/u/Angelika)\
**Post date:** [November 26, 2024, 3:57pm UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/1 "2024-11-26T15:57:54Z")

</div>

Hello everyone,

I’m working on an online PsychoPy experiment including Pavlovia surveys. I’ve included one survey for participant screening. My goal is to ensure that participants who do not meet the inclusion criteria (e.g., age \<18 years) are shown a message like “Unfortunately, you do not meet the inclusion criteria. You can now close the browser window,” and are prevented from continuing to the main experiment.

Here’s the setup:  
In the screening questionnaire, there is one critical question with two response options:

- “\<18 years”
- “\>18 years”  
If participants select “\<18 years”, I want them to be stopped entirely and not proceed to the actual experiment. If they select the other option, they will proceed to the actual experiment.

I’ve tried implementing this following the approach from Wakefield’s video ([https://www.youtube.com/watch?v=rPXuHs5ykS4](https://www.youtube.com/watch?v=rPXuHs5ykS4)), but I’ve encountered a problem:  
While I can direct participants to the correct survey page that says they are ineligible, the navigation buttons cannot be disabled for just that page. This means participants can still click “Proceed” and move on to the main PsychoPy experiment, even though they’ve been told they’re not eligible. The logic function works quite well, but how can I stop the experiment from continuing after the Pavlovia survey?

Can you tell me, how to implement a solution where ineligible participants are completely stopped from proceeding after a Pavolovia survey? Ideally, I’d like to disable or remove the “Proceed” button on the ineligibility page or find another way to terminate the experiment at this stage.

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [November 26, 2024, 4:08pm UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/2 "2024-11-26T16:08:11Z")

</div>

Hello

I had a similar problem. The participants have to agree to the GDPR, otherwise they cannot participate. My survey is included in a PsychoPy experiment, so I get the relevant information from the survey with a code component. The survey component is called demo.

Begin Experiment

```auto
VPn = " ";
VPCode = " ";

zuStimmung = " ";

```

Begin routine

```auto
VPn = expInfo["participant"];
surveyResp = demo.getResponse();
VPCode = surveyResp["VP-Code"];

zuStimmung = surveyResp["ZustimmungDatenschutz"];

if ((zuStimmung === "FALSE")) {
    quitPsychoJS('Vielen Dank für Ihr Interesse', false);
    psychoJS.setRedirectUrls("https://www.uni-muenster.de/Psychologie/", "https://www.uni-muenster.de/Psychologie/");
}

```

If you are only using a survey, create a logical expression on the Survey Logic tab and close the survey.

![grafik](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/d/3/d36142281417f16187264c5a0162f9d2957e9a42.png) is the survey “equivalent”.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![Angelika](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/angelika/32/43445_2.png) [@Angelika](https://discourse.psychopy.org/u/Angelika)\
**Post date:** [November 27, 2024, 1:08pm UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/3 "2024-11-27T13:08:39Z")

</div>

Thanks, Jens! Your second suggestion is, what I tried before, but it doesn’t work in my case. The logic itself works fine — when participants select a certain condition (e.g., they do not meet the inclusion criteria), the survey correctly terminates as expected. However, the subsequent routine in PsychoPy still starts, which I want to avoid. In other words, while the survey closes based on the logic, the experiment continues running afterward. My goal is to completely stop the experiment at this point if participants do not meet the criteria.

So, I think your second suggestion should be the right approach. I tried to adapt your code to my experiment, but it didn’t work. Could you please clarify where exactly to include this code component (in the subsequent routine in PsychoPy?) and how to correctly access the corresponding response from the survey results file?

Thank you for your help!

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [November 28, 2024, 8:01am UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/4 "2024-11-28T08:01:16Z")

</div>

Hello @Angelika

Here is my solution to this problem. Note that I don’t check the age of a participant, but whether they agree to the GDPR statement.

Here are the two routines

 ![{7F3651BD-8635-4F59-A5DB-43830D12B093}](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/6/2/621bbdf24567ca8be677b7baa9d7534ae9fd9980.png)

**demo** is in PsychoPy the embedded survey, **getDemo** is the routine to get the values from the **demo** survey. It contains a JavaScript only code component.

In **Begin Experiment** I initialize a couple of variables.

```auto
VPn = " ";
VPCode = " ";
zuStimmung = " ";

```

In **Begin Routine** I query the values from the embedded survey

```auto
VPn = expInfo["participant"];
surveyResp = demo.getResponse();
VPCode = surveyResp["VP-Code"];

zuStimmung = surveyResp["ZustimmungDatenschutz"];

if ((zuStimmung === "FALSE")) {
    quitPsychoJS('Vielen Dank für Ihr Interesse', false);
    psychoJS.setRedirectUrls("https://www.uni-muenster.de/Psychologie/", "https://www.uni-muenster.de/Psychologie/");
}

```

I check the value of **zuStimmung** and abort the experiment if its value is **FALSE**. Upon exiting, I redirected the participant to the institute’s website regardless of whether the experiment was completed or not completed.

I hope that helps.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![Angelika](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/angelika/32/43445_2.png) [@Angelika](https://discourse.psychopy.org/u/Angelika)\
**Post date:** [November 29, 2024, 2:46pm UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/5 "2024-11-29T14:46:19Z")

</div>

> [@JensBoelte](#):
>
> `quitPsychoJS`

Thanks again!  
It finally works! 🙂 But only as long as you redirect to another website. If you don’t, the entire experiment restarts after being aborted. I can’t understand why, because normally using `quitPsychoJS` should end the experiment.

Anyway, since I’m redirecting to another website, your solution works perfectly for me. Awesome!

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [November 29, 2024, 6:29pm UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/6 "2024-11-29T18:29:58Z")

</div>

Hello Angelika

I never tried this approach without a redirect to a website.

I glad that it worked.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![JuleSchilling](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/juleschilling/32/44499_2.png) [@JuleSchilling](https://discourse.psychopy.org/u/JuleSchilling)\
**Post date:** [March 12, 2025, 9:14am UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/7 "2025-03-12T09:14:55Z")

</div>

Dear all,

I have exactly the same issue, but the code does not work for me. I do not receive an error message or anything but my experiment does not quit.

I create a variable in javascript in the begin experiment tab of the routine with the custom code (right after the survey):  
`no_consent = " ";`

My version of your code in the Begin Routine tab is

```auto
surveyResp = info_survey.getResponse();
no_consent = surveyResp["consent_dsgvo"];

if ((no_consent === "Item 2")) {
    quitPsychoJS('Vielen Dank für Ihr Interesse', false);
    psychoJS.setRedirectUrls("https://www.uni-muenster.de/Psychologie/", "https://www.uni-muenster.de/Psychologie/");
} 

```

I have tried multiple variants by now and I am kind of stuck.

Thanks a lot in advance!

P.S. Of course, I’ll chose another website once the code works

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 12, 2025, 9:51am UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/8 "2025-03-12T09:51:08Z")

</div>

Hello @JuleSchilling

Add a `print(no\_consent)’ after retrieving the value from your survey. Your if-statement will probably not be evaluated as true.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![JuleSchilling](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/juleschilling/32/44499_2.png) [@JuleSchilling](https://discourse.psychopy.org/u/JuleSchilling)\
**Post date:** [March 12, 2025, 10:19am UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/9 "2025-03-12T10:19:38Z")

</div>

Thanks a lot for the fast reply, Jens! I now inserted

```auto
console.log("no_consent: " + no_consent);

```

right after

```auto
surveyResp = info_survey.getResponse();

```

As I have read that there might be problems with values which include a space, I now changed the value to KeineZustimmung. When I check my console, it displays:  
no\_consent: KeineZustimmung

Thus, the value is stored but my experiment still does not quit.

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 12, 2025, 11:23am UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/10 "2025-03-12T11:23:05Z")

</div>

Hello @JuleSchilling

here is little toy-experiment with a toy-survey.

[GDPR.psyexp](https://discourse.psychopy.org/uploads/short-url/xM3yiaKVRlXTzykLETp2EsTaOCW.psyexp) (10.0 KB)

[ConsentGDPR.json](https://discourse.psychopy.org/uploads/short-url/es1RXzPsdV6CFGCgogJco9DfLH2.json) (755 Bytes)

The experiment ends at [google.de](http://google.de) when you do not give consent or at the English version of our department website when you give consent. In addition, the message at the end of the experiment is different.

Best wishes Jens

---

<div class="post-metadata">

**Author:** ![JuleSchilling](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/juleschilling/32/44499_2.png) [@JuleSchilling](https://discourse.psychopy.org/u/JuleSchilling)\
**Post date:** [March 12, 2025, 2:29pm UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/11 "2025-03-12T14:29:33Z")

</div>

Thank you so much for taking your time, @Jens!

I couldn’t run your experiment as the survey is private, however, I had a look at the builder and I’ve found a way to quit my experiment. Thus, for transparency I wanted to share my solution as well:

I had issues with identifying the values of my GDPR variable. “no\_consent” displayed “Item 2” or whatever value the _I disagree with terms and conditions checkbox_ had, correctly, However, it would not have been identified in the if-loop.

My final code is:

```auto
surveyResp = info_survey.getResponse();
no_consent = surveyResp["block_1/consent_dsgvo"];
console.log("no_consent: " + no_consent);

if (String(no_consent).trim() === "no_consent_dsgvo") {
    quitPsychoJS('Vielen Dank für Ihr Interesse', false);
    psychoJS.setRedirectUrls("https://www.google.de/", "https://www.google.de/");
}

else {
    quitPsychoJS('Thank you for your interest in the experiment.', false);
    psychoJS.setRedirectUrls("https://www.uni-muenster.de/Psychologie/en", "https://www.uni-muenster.de/Psychologie/en");
    }

```

Thus, I added that the value of “no\_consent” must be a string; also, I needed to add “block\_1/” to correctly identify my GDPR variable.

Oh, and for me, it is also not necessary to redirect participants for the experiment to end.

Thanks a lot again for your help!  
Best,

Jule

---

<div class="post-metadata">

**Author:** ![JensBoelte](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/jensboelte/32/9060_2.png) [@JensBoelte](https://discourse.psychopy.org/u/JensBoelte)\
**Post date:** [March 12, 2025, 3:05pm UTC](https://discourse.psychopy.org/t/how-to-conditionally-end-an-online-experiment-in-psychopy-with-pavlovia-surveys/43073/12 "2025-03-12T15:05:59Z")

</div>

Hello @JuleSchilling

Ok, extra spaces in the string. The toy experiment was not intended to run but to give you a framework.

Good that it works now.

Best wishes Jens
