Randomise the embedded data to be shown in piped text across different questions | XM Community
Skip to main content

Hi, I'm fairly new to Qualtrics (and have zero (0) knowledge about JavaScript) so here I am because I couldn't find an answer :,)
Context:
For my survey, respondents have to read 30+ vignettes. After each vignette, respondents have to answer 3 questions (which are the same for all vignettes). So each vignette + 3 standard qns have to be in a block.
The presentation of the vignette-blocks will be randomised, but respondents must read & respond to all of them.
Problem:
Every vignette contains a fictional person's name. I have a list of fictional names to be presented to respondents, and would like the name displayed in each vignette to be randomised. (e.g., Respondent 1 would see 'Tom' for Vig.1 & 'Harry' for Vig.2; respondent 2 would see 'Harry' for Vig.1 & 'Tom' for Vig.2) Each name can only appear once per respondent.
From my understanding, use of embedded data and piped text are the most efficient ways to achieve this. However, while I have tried many different methods, so far I cannot figure out how to:
(a) display a different name for each vignette -- I tried inserting a Randomiser element, but all the vignettes ended up displaying the one randomly selected name; and
(b) ensure that each fictional name appears only once across all the vignettes.
(c) I was also wondering if it was possible to insert the same name twice in the vignette, something like:
, a student [vignette's heading]
is having trouble with coding... [vignette's body]
^Hence, the same name would have to be displayed within each vignette.

My priorities are to achieve (a) and (b); (c) is ideal but optional!
Thank you so much in advance :") If the solution requires JS I will learn it.......

Yes we can create three embedded data and set those ED with three different names randomly. Now in the block/ vignette's body add the name using JS.


https://community.qualtrics.com/XMcommunity/discussion/comment/42746#Comment_42746rondev Thank you so much for responding!!
I'm a bit confused as to why 3 embedded data would be required? The names wouldn't be appearing in the 3 standard questions!
Would it be possible for you to show me an example of how the embedded data would have to be nested in the survey flow? I think I could try to search for a JS code myself :)


You need to randomly assign 30 unique names to unique embedded fields and pipe them into your vignettes (i.e., field name1 piped into vignette 1, name2 piped into vignette 2, etc.). One way to do this:

  1. Sometime before the first vignette, create a MC question with the 30 names as choices

  2. Randomize the choices

  3. Add JS to the question to loop through the choices and save the choice labels (i.e., the names) into embedded data fields (name1, name2, etc.)

  4. Use JS to hide the question

BTW, you could use a loop and merge block to do all the vignettes in one block.


https://community.qualtrics.com/XMcommunity/discussion/comment/42784#Comment_42784TomG Thank you so much for your response!!! I'm really grateful for your help!
To clarify:
Step 2: Do you mean that the order of the choices appearing should be randomised? So that when I embed the choice fields as data, the choice fields (ie selected names) would be different each time the survey is taken?
Step 3: Which question(s) should I add the JS to -- the MCQ question containing the list of names? I'm also not too sure what you mean by 'loop through the choices', and also not entirely sure how to do that! :-(


Step 2: Do you mean that the order of the choices appearing should be randomised? So that when I embed the choice fields as data, the choice fields (ie selected names) would be different each time the survey is taken?

Yes and yes, the names would be in a different order for each respondent.
Step 3: Which question(s) should I add the JS to -- the MCQ question containing the list of names? I'm also not too sure what you mean by 'loop through the choices', and also not entirely sure how to do that! :-(

Yes, add the JS to the MC question with names. This JS does all of it:
Qualtrics.SurveyEngine.addOnload(function() {
var q = jQuery("#"+this.questionId);
q.hide();
q.find("label.SingleAnswer>span").each(function(i) {
Qualtrics.SurveyEngine.setEmbeddedData("name"+(i+1),jQuery(this).text());
});
})


Hello Tom,
I have a similar problem where I have several different names I want to include. I believe your code already leads me in a very correct direction! Thank you for that! Now, it is not fully working yet and I was wondering whether you could help me with that:
I have one block with 4 names that have to be randomized, as well as 3 other corresponding questions where the same name should appear. On top of that, I have videos that should be linked to a certain name. My block kind of looks like this now:

  1. MC question hidden

  2. vignette name 1

  3. question name 1

  4. video name 1

  5. question name 1

  6. vignette name 2

  7. question name 2

  8. video name 2

  9. question name 2

  10. vignette name 3

  11. question name 3

  12. video name 3

  13. question name 3

  14. vignette name 4

  15. question name 4

  16. video name 4

  17. question name 4


  1. What piped text from the MC question do I have to insert? I tried it with ${q://1_QID1009/ChoiceDescription/1} but then it is tied to what I typed in there and the randomization does not work.

  2. Every name belongs to a certain set face/video. Is there a way to connect name 1 with video 1 and so on?

  3. The MC question is hidden now... however, one needs to click the 'next' button on an empty page. Is it somehow possible to avoid that?


Thank you so much for your insights! :)
https://community.qualtrics.com/XMcommunity/discussion/comment/42784#Comment_42784


Marla ,
If I understand correctly, I don't think you need the JS. Since the names are matched to the videos, all you need is a loop & merge block where the loop & merge fields are name (Field 1) and video url (Field 2). The loop & merge setup would have 4 rows.
Then you would have one loop & merge block something like this:

  1. Vignette with pipe ${lm://Field/1}

  2. Question with pipe ${lm://Field/1}

  3. Video ${lm://Field/2}

  4. Question with pipe ${lm://Field/1}



https://community.qualtrics.com/XMcommunity/discussion/comment/47089#Comment_47089Hi Tom!
Thanks for your comment! It worked! You saved my life! I somehow thought that this would mean not all names will be shown but that is not the case. Thank you!!
I have two more questions then.

  1. Let's say I have another factor that I want to randomize, which is the vignettes - these are not fixed to the names and videos though. In addition, I have a second block of 4 names (the first block is male, the second is female). Now, I want to randomize the vignettes across all 8 people in total. This is especially complicated because the vignettes contain the piped text so I wouldn't know how to 'pipe' it without interfering with the already piped text within a vignette. I hope what I write makes sense. Do you know of any way to make this possible?

  2. Before the task, there should also be some instructions and an example case (basically name 5). However, this example should preferably also be a randomized one from the list of names. Is it somehow possible to create an if-condition so that whatever name is taken for the example will not be taken into the loop of the actual task?


Let's say I have another factor that I want to randomize, which is the vignettes - these are not fixed to the names and videos though. In addition, I have a second block of 4 names (the first block is male, the second is female). Now, I want to randomize the vignettes across all 8 people in total. This is especially complicated because the vignettes contain the piped text so I wouldn't know how to 'pipe' it without interfering with the already piped text within a vignette. I hope what I write makes sense. Do you know of any way to make this possible?

You would randomize the vignettes with a hidden question and JS like I provided above. Instead of the choices being names, they would be the vignette text you want to randomly pipe. Then pipe the embedded fields (e.g., vignette1, vignette2, etc.) into a loop & merge field.
Before the task, there should also be some instructions and an example case (basically name 5). However, this example should preferably also be a randomized one from the list of names. Is it somehow possible to create an if-condition so that whatever name is taken for the example will not be taken into the loop of the actual task?

You can add a question hidden with JS with the 5 names and randomly display 1 name. Then you can pipe the displayed choice into the instruction example. Then based your loop and merge off the undisplayed choices the hidden question.


https://community.qualtrics.com/XMcommunity/discussion/comment/47119#Comment_47119Dear Tom,
Thanks for your help! I think I am almost there. What I still am not fully sure about is how I should pipe the embedded fields into the L&M field because the L&M field already contains the name/video loop. I tried to pipe in the embedded data field in an empty text field when it is supposed to appear but then it always just chooses the first choice of the randomized question. It doesn't really randomize the vignettes.
I have the suspicion that I should maybe pipe the given embedded data field in the text box where I want it but it doesn't give me this option.
This is my loop (names, videos, male name tags for videos):
image.pngThis is the MC question with the vignettes:
image.pngThis is the JS code you provided:
image.png


https://community.qualtrics.com/XMcommunity/discussion/comment/47145#Comment_47145Row 1, Field 6 should be ${e://Field/vignette1}, etc.


https://community.qualtrics.com/XMcommunity/discussion/comment/47154#Comment_47154TomG you shall receive my eternal gratefulness. It works, thank you!!


https://community.qualtrics.com/XMcommunity/discussion/comment/47154#Comment_47154Hello Tom!
It's me again! I just discovered one more error in the study regarding this topic and I have not the slightest clue why it happens.

I have two blocks. In block 1, I applied what we had talked about, it looks kind of like this:

  1. Vignette (piped in from Field 6 in the L&M section and based on the code of the randomized question with 8 choices that are saved as embedded data).

  2. Question

  3. Video

  4. Question

image.pngNow, this works perfectly. However, I have 8 vignettes in total and I am using 4 of them in this aforementioned block and the next 4 I use in the 2nd block. The second block looks the same but Field 6 is different:
image.pngUnfortunately, this does not work at all in the second block. It seems like whenever I pipe in Field 6, one vignette repeats itself constantly even though it has already been displayed in the previous block. Do you have any idea why this could happen?



https://community.qualtrics.com/XMcommunity/discussion/comment/47308#Comment_47308I don't know. I would add a block before your second loop & merge and pipe vignette5 through vignette8 to check the values.


https://community.qualtrics.com/XMcommunity/discussion/comment/47309#Comment_47309So, I did that and it seems like the names (field 1) are now tied to the vignettes (field 6) because of the randomized question I used in block 1, in which I just looped over one gender (1 gender = 1 block). So that means if I have 8 vignettes and 4 names in the block 1 loop, I have two names per vignette and they don't change for the second block. And in the second block, I have different names to loop over (different gender) but if I pipe just the vignette it will be according to the previous loop if that makes sense.
So, what I tried to do now was:

  1. isolate the randomized question and do no loop there at all - does not work because I have piped text within the vignettes (names, videos, etc) which is then not going to be tied to the vignettes at all.

  2. isolate randomized question and do a loop - does not work because the same thing happened as in the beginning - names and vignettes were tied and didn't 'refresh' based on the loop in the second block.

  3. put the randomized question in both blocks - does not work because it uses all the names again, including the ones from the gender who's turn it already was.

The only way I see is to randomize the vignettes somehow beforehand and then do a randomized question in both blocks but only with 4 of the randomized vignettes depending on which vignettes were there before. However, I haven't figured out yet how to do that.
This is how my randomized question looks like:

image.pngThe last thing that came to mind is just to put everything in one block. But then, I couldn't do the randomization within gender and whether male or female comes first (except maybe in case there is a way of an if condition within loops through javascript?)...


https://community.qualtrics.com/XMcommunity/discussion/comment/42827#Comment_42827TomG thank you very much for providing this. I followed your steps, but when I try to pipe in the choice options from the hidden MC into another question, I don't see the option under embedded data. How do I now pipe one random option from the list I created in the MC? TIA!


https://community.qualtrics.com/XMcommunity/discussion/comment/47327#Comment_47327Since the choices are not embedded data fields, you would pipe a choice description from a question.
In the answer above, the hidden question is meant to populate a Loop & Merge block. So you would pipe Loop & Merge field 1 inside the loop.


@TomG Hi Tom, thanks so much for all your guidance. I’ve had a very similar issue as the original post, and resolved by using a MC question to randomize my question labels, using JS to assign those to embedded data, and piping in the embedded data in the loop and merge fields. My issue is that the text that I’m trying to pipe in, i.e. the question labels, need to be formatted (e.g., bolded). I’m using html tags (e.g. <strong>) to add formatting to the text, and it seems to work fine the in the survey builder, but in the survey itself the formatting doesn’t seem to carry over within the loop and merge. I think that the JS code may be interfering with the formatting somehow. Do you know if there’s a way to preserve the formatting?


Hi @TomG, I’ve also been finding your advice here really useful for my own problem.

I have several faces, names and personality traits that I want to present to participants in a completely random fashion. So I’ve created two hidden multiple choice questions, one with names, and one with traits, using your provided custom JavaScript code to shuffle the choices and save each in an embedded data field. 

I then use a randomised loop & merge question to randomly combine the face pictures with the names and traits, which I pipe in using the embedded data fields. That this works perfectly suggests to me that the names and traits are stored in the embedded data fields as intended.

My issue is now with accessing the information about which order of names & traits was presented to each participant to be stored in the data export. As I understand it, I would need to define this as embedded data in the survey flow, using the embedded data fields I previously defined with the custom code. However when I try to set values using ‘Insert Piped Text’ → ‘Embedded Data Field’, there is nothing to choose from. 

 

It would be fantastic if you (or someone else) could point me to where I’m going wrong here.

Thanks!


@BiancaS,

Embedded data fields only defined in JS don’t show up when you use ‘Insert Piped Text’.  You’ll need to type them in the survey flow. Make sure you define the embedded data fields in the survey flow prior to the block where their values are set by JS.


dear @TomG, thanks so much for your super rapid and helpful response. I hadn’t realised I had to define the embedded variable in the flow before they were set up in the custom JS code - this totally did the trick.


Leave a Reply