Take your Qualtrics surveys to the next level and gather accurate and unbiased data. I'll explore a simple JavaScript hack that will allow you to randomize lists and supercharge your survey methodology.
Let's dive into the magic of list randomization!
This could be an effective way to randomize information that is being presented to participants in the text. (Qualtrics has a feature to randomize the options/answer choices for questions.)
Step 1: Meet the "Durstenfeld Shuffle" algorithm!
First things first, let's introduce you to the secret sauce: the "Durstenfeld Shuffle" algorithm. It's a clever method that is built into Javascript (no extra libraries needed!) that randomizes the elements in an array, making it perfect for list randomization in surveys.
Qualtrics.SurveyEngine.addOnload(function() {
// Randomize array element order using Durstenfeld shuffle algorithm
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
Step 2: Create your variables
Now, let's see how list randomization can enhance your survey methodology. Imagine you have a set of questions related to different items or options. By randomizing the order in which these items appear, you can minimize order bias and ensure more accurate responses. We will also create variables as placeholders for the random lists that will be created.
// List of 7 items
var list1 = ["Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7"];
// List of 15 items
var list2 = [
"Item1", "Item2", "Item3", "Item4", "Item5", "Item6", "Item7", "Item8",
"Item9", "Item10", "Item11", "Item12", "Item13", "Item14", "Item15"
];
// Create empty lists for randomization
var randomizedList1 = [];
var randomizedList2 = [];
Step 3: Plan of action
It's time for the functions, randomizeAndStoreList1 and randomizeAndStoreList2. These functions will:
Randomize the items in list1 and list2 using the "Durstenfeld Shuffle" algorithm.
Store each randomized item as embedded data. This way, you can access and analyze the data in a more sophisticated manner, strengthening your survey methodology.
// Function to randomize list1 and store each item as a separate embedded data field
function randomizeAndStoreList1() {
var randomizedList1 = shuffleArray(list1);
for (let i = 0; i < randomizedList1.length; i++) {
Qualtrics.SurveyEngine.setEmbeddedData('randomizedList1_' + i, randomizedList1[i]);
}
}
// Function to randomize list2 and store each item as a separate embedded data field
function randomizeAndStoreList2() {
var randomizedList2 = shuffleArray(list2);
for (let i = 0; i < randomizedList2.length; i++) {
Qualtrics.SurveyEngine.setEmbeddedData('randomizedList2_' + i, randomizedList2[i]);
}
}
Step 4: Run the functions
Witness the magic! By calling these functions, you'll randomize and save your new lists.
// Call the functions to randomize and store the lists
randomizeAndStoreList1();
randomizeAndStoreList2();
});
Step 6: Use piped text to call on the items of your list
To call upon the information in our newly formed and randomized lists, we will use the newly assigned variable names. Each item in the list is assigned a variable name. This feature preserved the random ordering of our list. You can refer to each item using the naming scheme below. Remember, indexing begins at zero, not at one.
Javascript | Piped Text |
'randomizedList1_' + i | ${e://Field/randomizedList1_0} |
Tips
To add the Javascript to your survey, create a question (or use an existing question). The question must be on a page prior to when you will be using the piped text. Navigate to the options for the question. In the section "Question Behavior," select "Javascript." You will then be presented with a pop-up where you can paste the code. Delete the content that is in the pop-up by default.

The Javascript code must be on one of the pages prior to when you use the piped text. So, if you are wanting to use the piped text in the second block of your survey, you must place the code somewhere in the prior block. You can also use page breaks strategically to set up the code.
Questions that have Javascript can be found by looking for the code icon, "</>", on the top left of the question.

To save variables in the survey so that they are available in the export of your data, your variables should be set up as embedded data in your survey flow. Each variable name must be proceeded by "__js_". You'll have to do this for each variable that you want available in the export.

There you have it, folks! With list randomization, you can improve your survey methodology. Say goodbye to order bias and hello to more precise and accurate data!
Incorporate this easy JavaScript trick into your Qualtrics surveys. Happy surveying!
Comments