我在 Qualtrics 中实现了一个实验,我使用 Javascript 从数组中为多个“属性”绘制随机值并随机化属性的顺序。简而言之,我采用随机绘制的值,将这些值放入它们自己的数组中,然后将新数组中的值打乱,并将它们作为字符串保存在嵌入的数据字段中。一个复杂性是随机抽取的两个属性(种族和性别)的值不会呈现给参与者,而是用于为属性(名称)创建一组可能的值,这些属性(名称)的值将呈现给参与者。
对于大多数调查对象,我的代码按预期工作,但有时我导出的数据显示有人已经到达运行 Javascript 的页面,但嵌入的数据字段(例如,我的示例中的“attr_viewed_names_string”和“attr_all_id_string”)为空。我已经通过移动设备和桌面设备多次完成了我的调查,但无法复制导致这些嵌入字段的值无法出现在我的数据中的过程。最大的问题是我不知道(a)代码是否实际上没有生成字符串,或者(b)字符串没有保存到嵌入字段。
下面是我在调查的第一页上实施的简化版本,用于创建字符串并保存它们。我是否错过了一些代码无法保存属性名称字符串的场景(例如“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);
});