Assigning randomly selected options to embedded data | XM Community
Solved

Assigning randomly selected options to embedded data

  • 21 March 2024
  • 7 replies
  • 57 views

Badge +3

Hello,

I have a situation where respondents can select multiple responses to Q1 (a list that could have up to 100 possible options). I then want to narrow the selected list down to 5 random options and assign these 5 responses to some embedded variables, for later use. 

So that I can make sure these random 5 options are evenly distributed amongst the total sample, I’m utalising the 'Advanced Ramodization' feature to select my 5, but I’m not sure how to then extract the 5 selected options into the relevant embedded variables.

My current thinking was to use the following script, but it isn’t working…

Qualtrics.SurveyEngine.addOnload(function() {
// Retrieve selected options from the advanced randomization
var selectedOptions = this.getSelectedChoices();

// Store the selected options in embedded data elements
for (var i = 0; i < selectedOptions.length; i++) {
var embeddedDataName = 'SelectedOption_' + (i + 1); // Name for embedded data element (e.g., SelectedOption_1, SelectedOption_2, etc.)
Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataName, selectedOptions[i]);
}
});

Can anyone advise where I might be going wrong and how I could get this to work?

 

Thanks.

 

icon

Best answer by cgillon 25 March 2024, 07:06

View original

7 replies

Userlevel 7
Badge +21

It should work if placed in addOnPageSubmit. 

Badge +3

Thank you for that tip - that indeed got the script working.

 

Now that it’s working, however, I see that my script returns the value label of each of the options, whereas I ‘m wanting it to return the option name (e.g. if option ‘1’ =”Brand1”, I want to embed ‘Brand1’).

Do you know how to amend the script to give this alternate output?

Badge +3

Further to my initial query, I have made the following changes to my script (where QID3 is the question that holds the list of options that I want to assign to embedded data)...

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
var selectedOptions = "${q://QID3/ChoiceGroup/SelectedChoices}";

// Split the selectedOptions string into an array of individual choices
selectedOptions = selectedOptions.split(",");

// Loop through the selected options and set embedded data variables
for (var i = 0; i < selectedOptions.length; i++) {
var embeddedDataName = 'SelectedOption_' + (i + 1);
Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataName, selectedOptions[i]);
}
});

This, however, as created a strange situation.

When I pipe in text from say ‘${e://Field/SelectedOption_1}’ it correctly outputs the right response option label from QID3 (‘Brand1’ for instance), but if I try to reference ‘${e://Field/SelectedOption_1}’ in any javascript, it says that the data is empty.

I don’t understand how the label is there for piping but not be there for js use.

Does anyone know what might be happening here, and how I can fix it so that I can reference the value of ‘Brand1’ in js?

 

P.S. I have created SelectedOption_1 to SelectedOption_5 in the survey flow, set them to ‘text’ and set the default value to “xx” (for no particular reason).

Userlevel 4
Badge +12

Hi @cgillon you can try doing this without the JS as well with branch logic and embedded data but it can prove to be lengthy as you said it can have as many as 100 options.

After setting up your multi select question along with the randomization, go to the survey flow and follow the below setup.

Value will be assigned to the embedded data if that particular choice is being displayed. You can use the value as per your wish, here I have used the term ‘displayed’, you can use your exact option name.
I have only done for 5 choices, you can do for as many as you like.

 

Badge +3

Hi @cgillon you can try doing this without the JS as well with branch logic and embedded data but it can prove to be lengthy as you said it can have as many as 100 options.

After setting up your multi select question along with the randomization, go to the survey flow and follow the below setup.

Value will be assigned to the embedded data if that particular choice is being displayed. You can use the value as per your wish, here I have used the term ‘displayed’, you can use your exact option name.
I have only done for 5 choices, you can do for as many as you like.

 

Thank you for taking the time to draft up this alternative approach. Just to confirm, in the case that there were 100 combinations, would this require creating a separate branch for each of those 100?

 

If that is the case, it wouldn’t be feasible. I am trying to work out an approach that I can systematically apply with minimal manual work, as it will be used over a number of future projects with varying degrees of complexity. If I can build the functionality through JS, then I can automate the creation of the required code.

Badge +3

Back to my previous query, I am wondering if, since my new code is pulling the response options label, is that why it isn’t being encoded as a value in the embedded variable?

Do I need to convert that label into a text value that the embedded variable will then recognise as a ‘value’? If so, does anyone know how to modify my script to achieve that?

Badge +3

FYI: I found my previous script was adding a space to the start of the the selected choices it was extracting from the earlier question, so adding a ‘trim’ function to the working help resolve some of my troubles.

Qualtrics.SurveyEngine.addOnload(function() {
var selectedOptions = "${q://QID3/ChoiceGroup/SelectedChoices}".split(",");

for (var i = 0; i < selectedOptions.length; i++) {
var embeddedDataName = 'SelectedOption_' + (i + 1);
var trimmedOption = selectedOptions[i].trim();
Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataName, trimmedOption);
}
});

Thanks.

Leave a Reply