How do I set embedded data with Qualtrics using Javascript? | XM Community

How do I set embedded data with Qualtrics using Javascript?

  • 6 September 2022
  • 7 replies
  • 3279 views

Badge +4

Hello,
I am working on programming a conjoint survey experiment in Qualtrics using the following example: https://github.com/leeper/conjoint-example. However, I am unable to set the choices shown to survey respondents as embedded data using Javascript. I have tried a variety of approaches, and nothing seems to be working.
I have a Javascript array called a_list which contains the list of attributes for option A shown to survey respondents. I am sure that a_list contains the proper information because it is properly displayed with the question. I am trying to save a_list to embedded data as a single string variable, as in the above example, using Qualtrics.SurveyEngine.setEmbeddedData("traits1a", a_list.join("|"));. However, traits1a always shows up empty in my data.
(1) I have defined the embedded data at the beginning of the survey flow (prior to attempting to set it using Javascript).
(2) I have tried putting the code "Qualtrics.SurveyEngine.setEmbeddedData("traits1a", a_list.join("|"));" in an addOnPageSubmit() block, in the addOnload() block, in the addOnUnload() block, and in the addOnReady() block. Regardless of where I place the setEmbeddedData() code, the traits1a embedded data shows up blank in the data.
(3) I have tried using addEmbeddedData() instead of setEmbeddedData().
(4) I have tried defining a variable prior to setting the embedded data:
var traits_a = a_list.join("|");
Qualtrics.SurveyEngine.setEmbeddedData("traits1a", traits_a);
I'm not sure how to get this to work, and any help would be appreciated.


7 replies

Userlevel 7
Badge +27

Post your code (be sure to format it as a code block).

Badge +4

Hi TomG. Thank you for your response! Here's my code:
Qualtrics.SurveyEngine.addOnload(function()
{
// import seeded random number generator code
// https://github.com/davidbau/seedrandom/blob/released/seedrandom.min.js
!function(a,b){function c(c,j,k){var n=[];j=1==j?{entropy:!0}:j||{};var s=g(f(j.entropy?[c,i(a)]:null==c?h():c,3),n),t=new d(n),u=function(){for(var a=t.g(m),b=p,c=0;q>a;)a=(a+c)*l,b*=l,c=t.g(1);for(;a>=r;)a/=2,b/=2,c>>>=1;return(a+c)/b};return u.int32=function(){return 0|t.g(4)},u.quick=function(){return t.g(4)/4294967296},u["double"]=u,g(i(t.S),a),(j.pass||k||function(a,c,d,f){return f&&(f.S&&e(f,t),a.state=function(){return e(t,{})}),d?(b[o]=a,c):a})(u,s,"global"in j?j.global:this==b,j.state)}function d(a){var b,c=a.length,d=this,e=0,f=d.i=d.j=0,g=d.S=[];for(c||(a=[c++]);l>e;)g[e]=e++;for(e=0;l>e;e++)g[e]=g[f=s&f+a[e%c]+(b=g[e])],g[f]=b;(d.g=function(a){for(var b,c=0,e=d.i,f=d.j,g=d.S;a--;)b=g[e=s&e+1],c=c*l+g[s&(g[e]=g[f=s&f+b])+(g[f]=b)];return d.i=e,d.j=f,c})(l)}function e(a,b){return b.i=a.i,b.j=a.j,b.S=a.S.slice(),b}function f(a,b){var c,d=[],e=typeof a;if(b&&"object"==e)for(c in a)try{d.push(f(a[c],b-1))}catch(g){}return d.length?d:"string"==e?a:a+"\\0"}function g(a,b){for(var c,d=a+"",e=0;e
// seed random number generator from embedded data fields
// conjoint profile 1
//randomize trait display order (same order for all questions for a single respondent)
Math.seedrandom('${e://Field/seed1}');
var attRaw = ["Age", "Race/Ethnicity", "Religion", "Education", "Party"];
var att= ["Age", "Race/Ethnicity", "Religion", "Education", "Party"];
var attributes = ["", "", "", "", ""];
for(i=0; i var rand1 = Math.floor(Math.random()*((attRaw.length - i)));
attributes[i] = att[rand1];
att.splice(rand1, 1);
}
var issuesRaw = ["Abortion", "Red-Flag Laws for Firearms",
"Raising Taxes on Individuals Making More Than $250,000",
"Legitimacy of 2020 Election"];
var iss = ["Abortion", "Red-Flag Laws for Firearms",
"Raising Taxes on Individuals Making More Than $250,000",
"Legitimacy of 2020 Election"];
var issues = [ "", "", "", ""];
for(i=0; i var rand1 = Math.floor(Math.random()*((issuesRaw.length - i)));
issues[i] = iss[rand1];
iss.splice(rand1, 1);
}

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

// Create Variables for Traits associated with each dimension.
var vconstituency = ["Urban", "Rural", "Suburban"];
if (Math.random() >= 0.5) {
  var vparty = ["Republican", "Democrat"];
} else {
  var vparty = ["Democrat", "Republican"];
}
var veduc = ["No college", "Community college", "College", "Graduate degree"];
var vcommittee = ["Yes", "No"];
var vabort = ["Completely ban and criminally charge women for seeking abortions",
"Ban at 6 weeks with no exceptions",
"Ban at 15 weeks with exceptions",
"No restrictions",
"Restore Roe v. Wade",
"Restrict late-term abortions with exceptions to protect the life of the mother"];
var vredflag = ["Supports", "Opposes", "Opposes, but supports stronger gun restrictions"];
var vtaxes = ["Supports", "Opposes"];
var vlegit = ["2020 Election was illegitimate and should be decertified",
"2020 Election was illegitimate, but it's time to move on",
"2020 Election was legitimate",
"2020 Election was legitimate, but we should reform electoral security"];


// Functions for setting race and religion approximately proportionately
function getRace(){
  // 60% non-hispanic white; 15% black; 15% hispanic; 10% asian
  var n = Math.floor(Math.random()*100);
  if (n<10) {
  var out = 3;
  } else if (n <25) {
  var out = 2;
  } else if (n<40) {
  var out = 1;
  } else {
  var out = 0;
  }
  var vrace = ["White", "African American", "Hispanic", "Asian American"];
  return vrace[out];
}
function getReligion(){
  // 20% evangelical; 20% mainline; 20% catholic; 10% jewish; 10% muslim; 20% none
  var n = Math.floor(Math.random()*100);
  if (n<20) {
  var out = 5;
  } else if (n<30) {
  var out = 4;
  } else if (n<40) {
  var out = 3;
  } else if (n<60) {
  var out = 2;
  } else if (n<80) {
  var out = 1;
  } else {
  var out = 0;
  }
  var vreligion = ["Evangelical protestant", "Mainline protestant", "Catholic", "Jewish", "Muslim", "None"];
  return vreligion[out];
}

// Use math.random to randomly select traits for each dimension for candidate A
var age_a = (Math.floor(Math.random() * (75 - 21 + 1)) + 21).toString();
var race_a = getRace();
var relig_a = getReligion();
var educ_a = veduc[Math.floor(Math.random()*veduc.length)];
var party_a = vparty[0];
var abort_a = vabort[Math.floor(Math.random()*vabort.length)];
var redflag_a = vredflag[Math.floor(Math.random()*vredflag.length)];
var taxes_a = vtaxes[Math.floor(Math.random()*vtaxes.length)];
var legit_a = vlegit[Math.floor(Math.random()*vlegit.length)];

// Use math.random to randomly select traits for each dimension for candidate B
var age_b = (Math.floor(Math.random() * (75 - 21 + 1)) + 21).toString();
var race_b = getRace();
var relig_b = getReligion();
var educ_b = veduc[Math.floor(Math.random()*veduc.length)];
var party_b = vparty[1];
var abort_b = vabort[Math.floor(Math.random()*vabort.length)];
var redflag_b = vredflag[Math.floor(Math.random()*vredflag.length)];
var taxes_b = vtaxes[Math.floor(Math.random()*vtaxes.length)];
var legit_b = vlegit[Math.floor(Math.random()*vlegit.length)];

// Create list of variables to use when setting attributes
var attributes2 = attributes.concat(issues);

var age_index = attributes2.indexOf("Age");
var race_index = attributes2.indexOf("Race/Ethnicity");
var relig_index = attributes2.indexOf("Religion");
var educ_index = attributes2.indexOf("Education");
var party_index = attributes2.indexOf("Party");
var abort_index = attributes2.indexOf("Abortion");
var redflag_index = attributes2.indexOf("Red-Flag Laws for Firearms");
var taxes_index = attributes2.indexOf("Raising Taxes on Individuals Making More Than $250,000");
var legit_index = attributes2.indexOf("Legitimacy of 2020 Election");

var a_list = new Array(attributes2.length);
var b_list = new Array(attributes2.length);
a_list[age_index] = age_a;
a_list[race_index] = race_a;
a_list[relig_index] = relig_a;
a_list[educ_index] = educ_a;
a_list[party_index] = party_a;
a_list[abort_index] = abort_a;
a_list[redflag_index] = redflag_a;
a_list[taxes_index] = taxes_a;
a_list[legit_index] = legit_a;

b_list[age_index] = age_b;
b_list[race_index] = race_b;
b_list[relig_index] = relig_b;
b_list[educ_index] = educ_b;
b_list[party_index] = party_b;
b_list[abort_index] = abort_b;
b_list[redflag_index] = redflag_b;
b_list[taxes_index] = taxes_b;
b_list[legit_index] = legit_b;

att_list = ["att1", "att2", "att3", "att4", "att5", "att9", "att10", "att11", "att12"];
a_out = ["a1","a2","a3","a4","a5", "a9", "a10", "a11", "a12"];
b_out = ["b1","b2","b3","b4","b5", "b9", "b10", "b11", "b12"];

// set html values in conjoint table--this displays the correct values
for(i=0;i<12;i++){
document.getElementById(att_list[i]).innerHTML = attributes2[i];
  document.getElementById(a_out[i]).innerHTML = a_list[i];
  document.getElementById(b_out[i]).innerHTML = b_list[i];
}

// set embedded data corresponding to display order and specific traits displayed
Qualtrics.SurveyEngine.setEmbeddedData("order1", attributes2.join("|"));
Qualtrics.SurveyEngine.setEmbeddedData("traits1a", a_list.join("|"));
Qualtrics.SurveyEngine.setEmbeddedData("traits1b", b_list.join("|"));

});

Badge +4

Hi TomG. Thanks for your response! I'm having a bit of trouble posting my code to this thread, but I've uploaded it at the following link: https://github.com/SamuelFrederick/conjoint-code/blob/main/conjoint.js.

Userlevel 7
Badge +27

I'm not sure what the issue is, but I would add this line before the setEmbeddedData for traits1a:
console.log("a_list array",a_list,"a_list string",a_list.join("|"));
Then check the console. Hopefully, it will give you a clue if something is wrong with a_list. If nothing is amiss try piping

${e://Field/traits1a}
on the next page.

Badge +4


https://community.qualtrics.com/XMcommunity/discussion/comment/49172#Comment_49172There doesn't seem to be an issue with the array or string in the console, and when I pipe ${e://Field/traits1a} on the next page, it appears as it should. It seems that traits 1a does not save to embedded data unless I pipe it on a separate page. Do you know of any other way to get Qualtrics to write the embedded data without piping directly in a separate question?

Userlevel 7
Badge +27

https://community.qualtrics.com/XMcommunity/discussion/comment/49184#Comment_49184You don't need to pipe it to save it. Two things:

  1. Check to make sure 'traits1a' is spelled exactly the same in the survey flow

  2. Someone recently mentioned that a field set with setEmbeddedData() in the last question of the survey didn't get saved. I didn't quite believe it, but if your question is the last in the survey it seems that it might be a Qualtrics bug. Luckily, it is easy enough to work around. I would first try adding a survey flow branch after the block that checks the value of traits1a. I think that would fix it. If not, you can add another page.

Badge +4

Thank you for all the help, TomG! The embedded data now seems to be saving properly. The fix appeared to be switching the order such that Qualtrics.SurveyEngine.setEmbeddedData() came before the code filling in the HTML. I've updated the code here which should now work.

Leave a Reply