addEmbeddedData not working | XM Community
Solved

addEmbeddedData not working

  • 5 August 2020
  • 2 replies
  • 4 views

Hi all,
My survey uses a different person's name for each question, and I want them randomized from a list
For example, for Q1 I would pipe in "name1", for Q2 I would pipe in "name2"... so I want to create variables name1 to name30.
I put the following code in the js of an introductory page of my survey that doesn't use a name. I create an array, shuffle the order, then assign values to the new field names.
But even after this page, the names don't show up. Can someone tell me if/where I've done something wrong?
Qualtrics.SurveyEngine.addOnload(function()
{
let names = ["Jennifer", "Jessica", "Amanda", "Ashley", "Sarah", "Stephanie", "Melissa", "Nicole", "Elizabeth", 
"Heather", "Tiffany", "Michelle", "Amber", "Megan", "Amy", "Rachel", "Kimberly", "Christina", 
"Lauren", "Crystal", "Michael", "Chris", "Matt", "Josh", "David", "James", "Daniel",
"Robert", "John", "Joe", "Jason", "Justin", "Andrew", "Ryan", "William", "Brian", "Brandon", "Jon",
"Nick", "Tony", "Eric", "Adam", "Kevin", "Tom", "Steve", "Tim", "Richard", "Jeremy", "Jeff", "Kyle"];
shuffle(names);
for (let i = 0; i < names.length; i++) {
Qualtrics.SurveyEngine.addEmbeddedData("name"+i, names[i]);
}
});

icon

Best answer by SurajK 5 August 2020, 19:04

View original

2 replies

Userlevel 5
Badge +4

Hi,
Did you created shuffle function to randomize the array, the above code will give you an error that shuffle is not defined.
Add the below code in JS, it should work fine.
function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;


  // While there remain elements to shuffle...
  while (0 !== currentIndex) {


    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;


    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }


  return array;
}


Qualtrics.SurveyEngine.addOnload(function()
{
let names = ["Jennifer", "Jessica", "Amanda", "Ashley", "Sarah", "Stephanie", "Melissa", "Nicole", "Elizabeth", 
"Heather", "Tiffany", "Michelle", "Amber", "Megan", "Amy", "Rachel", "Kimberly", "Christina", 
"Lauren", "Crystal", "Michael", "Chris", "Matt", "Josh", "David", "James", "Daniel",
"Robert", "John", "Joe", "Jason", "Justin", "Andrew", "Ryan", "William", "Brian", "Brandon", "Jon",
"Nick", "Tony", "Eric", "Adam", "Kevin", "Tom", "Steve", "Tim", "Richard", "Jeremy", "Jeff", "Kyle"];
shuffle(names);
for (let i = 0; i < names.length; i++) {
Qualtrics.SurveyEngine.addEmbeddedData("name"+i, names[i]);
}
});


You are wonderful! Thank you so much, it worked perfectly.

Leave a Reply