Randomzing names, using embedded data and javascript, and view text/graphic in dataset | XM Community
Skip to main content

Hello, I am trying to create a survey for teachers (our respondents).

They are going to evaluate a test completed by 25 students. I want to show the teachers a random name from a list of names (female, male, foreign, native) similar to this CV experiment here:

How to randomly assign a word into the text of a question? | XM Community (qualtrics.com)

Except we’re not doing CVs but student tests.

So for example a teacher will first see a test answered by a student with a native female name, then a test answered by a student with a male foreign name, etc. in a random order without replacement.

And then they are asked to grade these students.

I want the randomized name to appear in a text/graphic question type in qualtrics before the student’s answer, and then the same name at the end in a dropdown multiple choice question for the respondent to give an appropriate grade. 

 

 

I have created a javascript that works fine individually on one question:

Qualtrics.SurveyEngine.addOnload(function()
{
var bucket = u
'Bob',
'Tom',
'Anne',
'Theodor',
];

function getRandomFromBucket() {
var randomIndex = Math.floor(Math.random() * bucket.length);
return bucket.splice(randomIndex, 1)n0];
}


qc = this.getQuestionContainer().querySelector(".QuestionText");
console.log(qc);
qc.innerText = "Name: " + getRandomFromBucket();

});

The trouble is that I have to specify the bucket for each question, so now the teacher that is evaulating the tests from students can see two Bobs in a row (I want randomization without replacement, and since I am still figuring this out I have not created all the different names). 

 

Is there any way to create the bucket variable in the embedded data in the survey flow, and then reference that embedded data in my javascript for each question?

 

The way my javascript works now, I can’t see what name the respondent saw when I download the data.

 

Any explanation on how embedded data can work with javascript randomization functions is much appreciated. Hope anyone can help.

@T2deB Hi, It’s me again
If you want to save the name that is showed you have to set up a embedded data first in the survey flow then use this line of code for saving info to that Embdedded Data: 

Qualtrics.SurveyEngine.setEmbeddedData('FieldName', Value);

And yes you can dynamically save the bucket in form of Embedded Data. 


Hello again, thanks for the input!

I’m not sure I understand the setEmbeddedData line you provided.I created embedded data in the surveyflow and named it “Navn” (name in Norwegian), and my javascript code for randomizing names without replacement looks like this:
 

Qualtrics.SurveyEngine.addOnload(function()
{

Qualtrics.SurveyEngine.setEmbeddedData( 'Navn', value );
var bucket = "${e://Field/Navn}";


function getRandomFromBucket() {
var randomIndex = Math.floor(Math.random() * bucket.length);
return bucket.splice(randomIndex, 1)d0];
}


qc = this.getQuestionContainer().querySelector(".QuestionText");
console.log(qc);
qc.innerText = "Navn: " + getRandomFromBucket();



});

I imagine I have to put something other than “value” in setEmbeddedData, should I just put all the names I’ve listed in my embedded data in the surveyflow?

Also I’m not sure if my bucket variable works now. How do I save it dynamically in form of Embedded Data?

My embedded data now looks like this:
 

 

And when I preview survey nothing shows in the question text.

 

Sorry for so many questions… but I really appreciate the help @Nam Nguyen !


@T2deB Your set-up should be this with a field that hold your names string and 4 field to save what respondent saw in the 1st-4th question.

Then this is the code that have a line to update your Navn string and also record what’;s being shown (You have to change the field name according to the question)

Qualtrics.SurveyEngine.addOnload(function() {
var navnString = "${e://Field/Navn}";
var navnArray = navnString.split(',');
function getRandomFromBucket() {
var randomIndex = Math.floor(Math.random() * navnArray.length);
var randomNavn = navnArray.splice(randomIndex, 1) 0];
Qualtrics.SurveyEngine.setEmbeddedData('Navn', navnArray.join(','));
Qualtrics.SurveyEngine.setEmbeddedData('N1', randomNavn); //Change the field according to question
return randomNavn;
}

qc = this.getQuestionContainer().querySelector(".QuestionText");
console.log(qc);
qc.innerText = "Navn: " + getRandomFromBucket();
});

 


Thanks a lot, this works!

 

Now what do I do if I want to refer to the first random name in another question text field? I tried this javascript:

 

Qualtrics.SurveyEngine.addOnload(function() {

var randomName = "${e://Field/N1}";


qc = this.getQuestionContainer().querySelector(".QuestionText");
console.log(qc);
qc.innerText = "Navn: " + randomName();



});

I also just tried the piped text for Field/N1 but both show up blank…

 

Thanks again for the help so far @Nam Nguyen !


Thanks a lot, this works!

Now what do I do if I want to refer to the first random name in another question text field? I tried this javascript:

Qualtrics.SurveyEngine.addOnload(function() {

var randomName = "${e://Field/N1}";


qc = this.getQuestionContainer().querySelector(".QuestionText");
console.log(qc);
qc.innerText = "Navn: " + randomName();



});

I also just tried the piped text for Field/N1 but both show up blank…

Thanks again for the help so far @Nam Nguyen !

@T2deB Just pip-text embedded field N1. Make sure you have a page break. Look at your result, if the field N1 got saved, it can be pip-text but after the 1st question and you have to have page break to save the embedded field first.


Thanks for the great explanation, everything works now!

I might have a few more questions but I need to set up everything first.

Thanks again for all the help.

 


Any simple ideas on how to randomize 1/5 of my respondents to see no names at all @Nam Nguyen ?

It seems the randomization in the survey flow only has evenly present elements as an option? Unless I want to manually edit count while the survey is available for the respondents, but that seems very impractical…

 

So 1/5 of my respondents can see “student 1” in place of a name on N1 for instance “student 2” for N2 and so on. 

 

Any help is much appreciated!


@Nam Nguyen If you want a rate like that the viable option is to randomize 5 element in survey flow with 1 of your desire outcome and 4 others. So 4 block of normal Navn and 1 block of Navn=, , , ,


Yeah, I feared this was the solution. It’s just that I have a lot of blocks so it will be a little bit of manual work. But thanks for all the help @Nam Nguyen !


Leave a Reply