Javascript in Qualtrics: failing to save JS-generated embedded fields?

259 views Asked by At

I have implemented an experiment in Qualtrics where I use Javascript to draw random values for several "attributes" from an array and randomize the order of the attributes. In short, I take the randomly drawn values, place those values into their own array, then shuffle the values in that new array and save them as a string in an embedded data field. The one complexity is that the randomly drawn values of two of the attributes (race and gender) are not presented to the participant but rather are used to create a set of possible values for an attribute (name) who values ARE presented to the participant.

My code has worked as expected for most survey-takers, but occasionally my exported data shows someone who has reached the page where the Javascript runs yet the embedded data fields (e.g. "attr_viewed_names_string" and "attr_all_id_string" in my example) are empty. I have completed my survey many times from both mobile and desktop but have not been able to replicate the process causing values for these embedded fields to fail to appear in my data. The biggest issue is that I do not know if (a) the code is not actually generating the string or (b) the string is not saving to the embedded field.

Below is a simplified version of what I am implementing on the very first page of the survey to create the strings and save them. Am I missing some scenario where the code would fail to save a string of attribute names (e.g. "name|religion|race|gender")

Qualtrics.SurveyEngine.addOnload(function()
{

/////////////////////////////////////////////////////
// DEFINE HELPERS
/////////////////////////////////////////////////////

// Function to randomize the ordering of the elements in arrays
   function shuffle(array){
        var counter = array.length,
            temp, index;
        while (counter > 0){
            index = Math.floor(Math.random() * counter);
            counter = counter-1;
            temp = array[counter];
            array[counter] = array[index];
            array[index] = temp;
        }
        return array;
    };
    
    
// Function to sample K elements from an array without replacement
// This is used for grabbing values for our attributes.
 function getRandom(arr, n) {
    var result = new Array(n),
        len = arr.length,
        taken = new Array(len);
    if (n > len)
        throw new RangeError("getRandom: more elements taken than available");
    while (n--) {
        var x = Math.floor(Math.random() * len);
        result[n] = arr[x in taken ? taken[x] : x];
        taken[x] = --len in taken ? taken[len] : len;
    }
    return result;
};
    
// Function that, given an array X containing arrays 1,2,3...k,
// randomly select one element from each 1, 2, 3, ...k
// and returns an array containing the values. Checks that
// it is an array and returns the original object if the element is
// not an array.
    function get1RandomForEach(array){
        return array.map(attr => Array.isArray(attr) ? getRandom(attr, 1) : attr);
    }
 
/////////////////////////////////////////////////////
// SPECIFY STUDY PARAMETERS
/////////////////////////////////////////////////////
   
// Number of choice tasks each participant completes
var choice_task_n = 1

// Number of profiles in total each participant will observe
var profiles_total = choice_task_n*2

/////////////////////////////////////////////////////
// CREATE ATTRIBUTES
/////////////////////////////////////////////////////
   
// Create the attributes and set of possible values for each
// attribute.
var religion = ["val1", "val2"];
var race = ["B", "W"];
var gender = ["F", "M"];    

// Create array of objects where each object contains key-value pairs
// for a name, race, and gender
var name_table = [{full_name: "name1", race: "W", gender: "F"}, 
{full_name: "name2", race: "B", gender: "F"}, 
{full_name: "name3", race: "B", gender: "M"}, 
{full_name: "nam4", race: "W", gender: "M"}];
    
// Create an array of objects containing all attributes participants will
// observe, an attribute identifier, and the possible values the variables
// can assume. Note that we assign the value "placeholder" to Name since
// the values depend on the gender and race.

var attr_table = [
    {attrib: "Religious Identification", category: "relig", vals: religion},
    {attrib: "Name", category: "name", vals: "placeholder"}
    ];
    
// Shuffle the order of the attributes
var attr_table_shuffled = shuffle(attr_table)

// Create arrays with the attribute names, attribute text, and 
// attribute values. This will be for the variables participants
// observe (in contrast with unobserved set that includes gender
// and race but excludes name)
var attr_viewed_vals = attr_table_shuffled.map(function(e){return e.vals;});
var attr_viewed_names = attr_table_shuffled.map(function(e){return e.attrib;});
var attr_viewed_id = attr_table_shuffled.map(function(e){return e.category;});

// Convert to delimited string for saving
var attr_viewed_names_string  = attr_viewed_names.join("|");
var attr_viewed_id_string  = attr_viewed_id.join("|");

// Add unobserved attributes' IDs to the id string to
// get all attributes' ids
var attr_all_id_string = attr_viewed_id_string + "|race|gender";

// Store the string in embedded data
Qualtrics.SurveyEngine.setEmbeddedData("attr_viewed_names_string",  attr_viewed_names_string);
Qualtrics.SurveyEngine.setEmbeddedData("attr_viewed_id_string",  attr_viewed_id_string);
Qualtrics.SurveyEngine.setEmbeddedData("attr_all_id_string",  attr_all_id_string);
    
});
0

There are 0 answers