Setting Embedded data in custom javascript | XM Community
Skip to main content
Hi,



I'm attempting to program a conjoint experiment using code from here, but the embedded data fields are not updating. Here is the javascript code:



Qualtrics.SurveyEngine.addOnload(function()

{



// seed random number generator from embedded data fields

// conjoint profile 1

Math.seedrandom('${e://Field/seed1}');



// Create Variables for Traits associated with each dimension.

var vparty = ["Democratic"];

var vplaceholder = [""];



var n = Math.random();

if (n<.16666) {

var vname = ["Jack McCormick","Luke Rich"];

} else if (n <.33333) {

var vname = ["Jack McCormick","Connor Beck"];

} else if (n<.5) {

var vname = ["Luke Rich","Jack McCormick"];

} else if (n < .66666) {

var vname = ["Luke Rich","Connor Beck"];

} else if (n < .8333333) {

var vname = ["Connor Beck","Jack McCormick"];

} else {

var vname = ["Connor Beck","Luke Rich"];

}



var n = Math.random();

if (n<.166666) {

var vexperience = ["Former City Councilor","Former City Comptroller"];

} else if (n <.333333) {

var vexperience = ["Former City Councilor","Former City Planner"];

} else if (n<.5) {

var vexperience = ["Former City Comptroller","Former City Councilor"];

} else if (n < .666666) {

var vexperience = ["Former City Comptroller","Former City Planner"];

} else if (n < .8333333) {

var vexperience = ["Former City Planner","Former City Councilor"];

} else {

var vexperience = ["Former City Planner","Former City Comptroller"];

}



var n = Math.random();

if (n<.166666) {

var vaccomplishment = ["Helped reduce traffic in many parts of the city","Helped fund improvements to local fire departments"];

} else if (n <.333333) {

var vaccomplishment = ["Helped reduce traffic in many parts of the city","Helped establish a popular job training program"];

} else if (n<.5) {

var vaccomplishment = ["Helped fund improvements to local fire departments","Helped reduce traffic in many parts of the city"];

} else if (n < .666666) {

var vaccomplishment = ["Helped fund improvements to local fire departments","Helped establish a popular job training program"];

} else if (n < .833333) {

var vaccomplishment = ["Helped establish a popular job training program","Helped reduce traffic in many parts of the city"];

} else {

var vaccomplishment = ["Helped establish a popular job training program","Helped fund improvements to local fire departments"];

}



var n = Math.random();

if (n<.166666) {

var vweakness = ["Some have criticized this candidate for a newly discovered recording where the candidate said he would likely vote against his party on several key issues","Detractors have criticized this candidate for saying #MeToo should pay more attention to due process."];

} else if (n <.333333) {

var vweakness = ["Some have criticized this candidate for a newly discovered recording where the candidate said he would likely vote against his party on several key issues.","Critics have argued that this candidate's qualifications will make him less competitive in the general election than other candidates."];

} else if (n<.5) {

var vweakness = ["Detractors have criticized this candidate for saying #MeToo should pay more attention to due process.","Some have criticized this candidate for a newly discovered recording where the candidate said he would likely vote against his party on several key issues."];

} else if (n < .666666) {

var vweakness = ["Detractors have criticized this candidate for saying #MeToo should pay more attention to due process.","Critics have argued that this candidate's qualifications will make him less competitive in the general election than other candidates."];

} else if (n < .833333) {

var vweakness = ["Critics have argued that this candidate's qualifications will make him less competitive in the general election than other candidates.","Some have criticized this candidate for a newly discovered recording where the candidate said he would likely vote against his party on several key issues."];

} else {

var vweakness = ["Critics have argued that this candidate's qualifications will make him less competitive in the general election than other candidates.","Detractors have criticized this candidate for saying #MeToo should pay more attention to due process."];

}



if (Math.random() >= 0.5) {

var voccupation = ["Attorney", "Small Business Owner"];

} else {

var voccupation = ["Small Business Owner", "Attorney"];

}



// Set traits for each dimension for candidate A

traits_a = [

vname[0],

vparty[0],

vexperience[0],

(Math.floor(Math.random() * (75 - 35 + 1)) + 35).toString(),

voccupation[0],

vaccomplishment[0],

vplaceholder[0],

vweakness[0]];





// Set traits for each dimension for candidate B

traits_b =[

vname[1],

vparty[0],

vexperience[1],

(Math.floor(Math.random() * (75 - 35 + 1)) + 35).toString(),

voccupation[1],

vaccomplishment[1],

vplaceholder[0],

vweakness[1]];



// Create list of variables to use when setting attributes

a_list = ["a1","a2","a3","a4","a5","a6","a7","a8"];

b_list = ["b1","b2","b3","b4","b5","b6","b7","b8"];



// set html values in conjoint table

for(i=0;i<13;i++){

document.getElementById(a_list[i]).innerHTML = traits_a[i];

document.getElementById(b_list[i]).innerHTML = traits_b[i];

}



// store values as embedded data fields

Qualtrics.SurveyEngine.setEmbeddedData('traits1a', traits_a.join("|"));

Qualtrics.SurveyEngine.setEmbeddedData('traits1b', traits_b.join("|"));





});





I have tried quite a few things including using addOnReady rather than addOnload, changing the "type" of the embedded data in the survey flow, etc. but haven't been able to crack this. I have attached a .qsf file here to facilitate troubleshooting.



Thanks very much for any responses.
Be the first to reply!

Leave a Reply