Setting embedded data from a participant's choices on Qualtrics using JS (for dummies)

Sometimes when I design a study I find myself needing to set an embedded data from a participant's response--so for example, you may want to keep track of a participant's score on a quiz, or you may want to display some other part of the survey depending on what the person answered earlier in the survey but it's more complicated than the typical display logic. One of my examples of this was a study I ran where the participant earned money over time and I wanted to display their accumulated money across time to participants.
So in the task, people could either choose to buy a product from store A or B, and depending on the store, the product would be a certain price (deducted from $10). This went on for 10 "weeks".

Anyway, you notice Javascript is enabled in these two items. The first one it's enabled but it's actually blank. I probably thought I could put the code in that item and it'd be fine, but I found it easier to have a "calculating" item that would grab people's choices and set embedded data that way (the second item).

So let's assume what I wanted to do was just set an embedded data for the participant's choice of Store A or Store B.

The code looks like:
var storechoice = "${q://QID3/SelectedChoicesRecode}";
Qualtrics.SurveyEngine.setEmbeddedData("storechoice", (storechoice));
What you need is the index for the choices (e.g., the participant's selected choice)--which you can get from piping the question results into a new question.

So what I did here was just say, save the participant's choice for the Q into a variable storechoice, then second line saves the participant's choice into an embedded data called "store".

And you can do all sorts of stuff. So in the original study, the price of the product @ each store varied by week which is something I set in loop and merge (where each week, the price for Store 1 was taken from Field 1, and price for Store 2 was taken from Field 2)


I wanted to save the price of the product into an embedded called spent, so here's the additional JS:

    if (storechoice == 1) {
        var spent = "${lm://Field/1}";
} else if (storechoice == 2) {
var spent = "${lm://Field/2}";
};

Qualtrics.SurveyEngine.setEmbeddedData("spent", (spent));

The if says, if the participant's choice was 1 (aka Store A), then the variable spent is set to what was in Field 1 for the loop and merge of that "week", but if the choice was 2 (aka Store B), then the variable spent is set to Field 2. Finally, we set the embedded data for spent to the variable spent. With if, I feel like you can do a lot. With JS, boolean is a little different, you have || and && for OR and AND respectively.

Anyway, if you want to do some sort of math with your variable, you should use the function Number() to make the variable act like an integer. So if I wanted to somehow multiple the spent variable by 10, I could do Number(spent)*10.

Important: If you want your embedded data to show up in your data, you still have to set an embedded data called "spent" or "storechoice" in your survey flow. Otherwise, I think the variable just hangs out in the survey for that session, but isn't saved to your data.

The video below just shows us how useful the JS can be. I could not have actually at all been able to display a running total of money people have saved over time.

(shout out to Chris Hydock for helping me code this the first time)




Comments

Popular Posts