Search code examples
javascriptqualtrics

Record answer randomization in Qualtrics using javascript


I am making a survey in Qualtrics. This survey has a repeating question with six answer choices. The six choices are randomized (in the standard way, no javascript). The question is being repeated using loop&merge, which works great because it's the same question structure over and over (36 times), but I can use the field function to adjust the question and answers for every iteration.

However, one problem I am running into is that Qualtrics does not (as standard) support the recording of the randomization data in the results - i.e. how it has randomized the six answer choices in each iteration. When I use the 'Export Randomized Viewing Order data' function when downloading results, it only shows the answer order of the last time it asked the question. So it seems that this is a value that gets overwritten after each iteration.

So now I'm looking to record the answer order for each iteration through javascript. However, I haven't found a function that gives the order answer (after randomization). I have consulted the Qualtrics javascript API and found some functions that seem promising, such as getChoices (), but when I try this all I get back is the order of answers without randomization (i.e. just 1,2,3,4,5,6).

Does anyone know a way to record the randomized choice order for each iteration, using javascript or otherwise?


Solution

  • I think the thing here is to look at the order of choices in the DOM. Qualtrics provides the getChoiceContainer() method to get the div containing the choices. Here's a snippet I wrote and minimally tested:

    //get the div containing the choices, then get all input child elements of that div
    var choices = this.getChoiceContainer().getElementsByTagName("input");
    //initialize an array for the IDs of the choices
    var choiceIDs = []
    //add the ID of each choice to the array
    for (var i=0; i < choices.length; i++) {
    	choiceIDs.push(choices[i].id);
    		
    }
    //get the current choice order from embedded data and add this loop to it.
    //Add a | to distinguish between loops.
    var choiceOrder = "${e://field/choiceorder}" + choiceIDs.toString() + "|";
    //set the embedded data with the new value
    Qualtrics.SurveyEngine.setEmbeddedData("choiceorder", choiceOrder);

    A couple of notes/caveats: I only tested this on a basic multiple choice question with radio buttons. It may need to be adjusted for different question types.

    I also just got the IDs of the question choices. You could probably modify it pretty easily to get other information, like the label of the choice, or the numeric value it corresponds to.