# PsychoPy Survey Component demo (embedding surveys in experiments)

**URL:** <https://discourse.psychopy.org/t/psychopy-survey-component-demo-embedding-surveys-in-experiments/33974>\
**Category:** Pavlovia Surveys\
**Tags:** pavlovia\
**Created:** [March 22, 2023, 3:12pm UTC](https://discourse.psychopy.org/t/psychopy-survey-component-demo-embedding-surveys-in-experiments/33974 "2023-03-22T15:12:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [March 22, 2023, 3:12pm UTC](https://discourse.psychopy.org/t/psychopy-survey-component-demo-embedding-surveys-in-experiments/33974/1 "2023-03-22T15:12:36Z")

</div>

As of PsychoPy 2023.1.1, surveys can be embedded in online PsychoPy experiments, which gives a step change in the level of functionality available.

I’ve just made my survey test public so you can try it and fork it.

# Survey Test [code](https://gitlab.pavlovia.org/vespr/survey-test) | [try it](https://run.pavlovia.org/vespr/survey-test)

Surveys can be selected using the Survey ID (so they always run the latest version) of Survey Model File (so they protected from changes to the online version). The results of each survey are saved to the PsychoPy data file. However, if you want to access the data during your experiment you should use code of the form `surveyResponse = surveyComponentName.getResponse()` which copies the data into a dictionary.

In my demo I check whether four consent items have been ticked using `if len(surveyResponse['consent']) == 4:` and then present the mean happiness score on screen using `surveyResponse['OHQmean']`

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [August 25, 2023, 5:43pm UTC](https://discourse.psychopy.org/t/psychopy-survey-component-demo-embedding-surveys-in-experiments/33974/2 "2023-08-25T17:43:02Z")

</div>

An update on this.

Currently, if you want to embed surveys in your experiments, please install 2023.1.3. There are a couple of bugs in the survey component in 2023.2, the fixes for which missed the 2023.2.1 release.

I’ve just reinstalled the older version so I can use it for next week’s [topic workshop](https://workshops.psychopy.org/topics/making-surveys.html#topicssurveys).

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [September 5, 2023, 9:55am UTC](https://discourse.psychopy.org/t/psychopy-survey-component-demo-embedding-surveys-in-experiments/33974/3 "2023-09-05T09:55:46Z")

</div>

I’ve just checked my test suite and this is solved in 2023.2.2 now available from [Releases · psychopy/psychopy · GitHub](https://github.com/psychopy/psychopy/releases)

[https://run.pavlovia.org/test-suite/survey-test/](https://run.pavlovia.org/test-suite/survey-test/)

---

<div class="post-metadata">

**Author:** ![abraver](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/abraver/32/37935_2.png) [@abraver](https://discourse.psychopy.org/u/abraver)\
**Post date:** [September 5, 2023, 5:29pm UTC](https://discourse.psychopy.org/t/psychopy-survey-component-demo-embedding-surveys-in-experiments/33974/4 "2023-09-05T17:29:52Z")

</div>

FYI @wakecarter: on mobile (iOS, Safari), I get an error after the participant info window is dismissed:

 ![IMG_65F092CC2BB5-1](https://us1.discourse-cdn.com/flex002/uploads/psychopy/original/3X/c/d/cd825813c337016ac753a0f84726ee89e38745c7.jpeg)

But after dismissing the error, the experiment seems to run fine.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [September 6, 2023, 7:15pm UTC](https://discourse.psychopy.org/t/psychopy-survey-component-demo-embedding-surveys-in-experiments/33974/5 "2023-09-06T19:15:44Z")

</div>

Thank you. What version of iOS do you have? Are you able to try Chrome on the same device?

---

<div class="post-metadata">

**Author:** ![abraver](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/abraver/32/37935_2.png) [@abraver](https://discourse.psychopy.org/u/abraver)\
**Post date:** [September 8, 2023, 12:47am UTC](https://discourse.psychopy.org/t/psychopy-survey-component-demo-embedding-surveys-in-experiments/33974/6 "2023-09-08T00:47:52Z")

</div>

I’m on iOS 16.6.1 (the latest). The error also occurs with Chrome and Firefox.

---

<div class="post-metadata">

**Author:** ![wakecarter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/wakecarter/32/5753_2.png) [@wakecarter](https://discourse.psychopy.org/u/wakecarter)\
**Post date:** [December 10, 2024, 10:50pm UTC](https://discourse.psychopy.org/t/psychopy-survey-component-demo-embedding-surveys-in-experiments/33974/7 "2024-12-10T22:50:10Z")

</div>

I’ve just noticed that my survey test was failing in 2024.2.4 for two reasons.

1. The old counterbalance code has been replaced with a counterbalance routine.
2. Survey response now include their block name, so my dissent code is now

```auto
surveyResponse = consent.getResponse()
if surveyResponse['block_1/consent']:
    if len(surveyResponse['block_1/consent']) == 4:
        continueRoutine = False

```

and the code to score the GAD is:

```auto
surveyResponse = instrument.getResponse()
print(surveyResponse)

nAnswers = 0
gad = 0
if surveyResponse['block_1/question1']['Row 1']:
    nAnswers += 1
    gad += int(surveyResponse['block_1/question1']['Row 1'])
if surveyResponse['block_1/question1']['Row 2']:
    nAnswers += 1
    gad += int(surveyResponse['block_1/question1']['Row 2'])
if surveyResponse['block_1/question1']['Row 3']:
    nAnswers += 1
    gad += int(surveyResponse['block_1/question1']['Row 3'])
if surveyResponse['block_1/question1']['Row 4']:
    nAnswers += 1
    gad += int(surveyResponse['block_1/question1']['Row 4'])
if surveyResponse['block_1/question1']['Row 5']:
    nAnswers += 1
    gad += int(surveyResponse['block_1/question1']['Row 5'])
if surveyResponse['block_1/question1']['Row 6']:
    nAnswers += 1
    gad += int(surveyResponse['block_1/question1']['Row 6'])
if surveyResponse['block_1/question1']['Row 7']:
    nAnswers += 1
    gad += int(surveyResponse['block_1/question1']['Row 7'])

thisExp.addData('GAD-7',gad)
thisExp.addData('nAnswers',nAnswers)

gad = str(gad) + ' out of ' + str(nAnswers * 3)
if nAnswers < 7:
    gad += ' You only answered ' + str(nAnswers) + ' items out of 7.'

```
