Record Loop and Merge Fields

jmhesketh UKCommunity Member Qubie ✭
edited March 23 in Custom Code

I am trying to randomly display and keep track of three videos I show to participants.

The videos are split into three categories, Novice, Intermediate, and Expert. I currently have it so that a Randomizer block randomly chooses one out of three question blocks. The questions blocks are identical except for the data stored in their Loop and Merge. The Novice block contains videos with score below X. The Intermediate block contains videos with score higher than X but lower than Y. The Expert block contains videos with score higher than Y.

Each of these blocks need to show three random videos from the Loop and Merge data. Field 1 of this data stores the score of the video shown, and Field 2 has the embed link that I embed into the code block.

I was going to store the data through javascript with the following:

var score = parseInt("${lm://Field/1}");

Qualtrics.SurveyEngine.setEmbeddedData("condition", "Z");

Qualtrics.SurveyEngine.setEmbeddedData("scoreVideo", score);

where Z is the condition of the block. However, I realise if I loop through 3 times, it will overwrite the embedded data. I guess my question is two fold:

1) Is there a better way of doing this such that I can every video on the same page without needing set up Loop and Merge data that contains a row for every video shown (I'll have around 20-30 videos for each condition, meaning for a combination of any 3 videos without replication would be 4060 without order or repetition)?

2) How can I keep track of what loop I'm on in the Loop and Merge so I can save the score under some embedded data like var scoreString = "scoreVideo" + loopNum?

Thank you very much for reading! I understand it's a little complex.

Best Answer

  • ahmedA
    ahmedA IndiaCommunity Member, XMPN Member Wizard ✭✭✭✭✭
    Accepted Answer

    Loop and merge has an embedded data called something like loop number or loop count (see the exact name in the piped text dropdown).

    Just add that to the names of your embedded variables. Something like:

    let loop_count = "{loop and merge count}";

    Qualtrics.SurveyEngine.setEmbeddedData(loop_count+"condition", "Z");

    Qualtrics.SurveyEngine.setEmbeddedData(loop_count+"scoreVideo", score);

    You'll need to create these 20-30 embedded variables in the survey flow to download them.

    Also, I don't see how you got to 4060?


  • jmhesketh
    jmhesketh UKCommunity Member Qubie ✭

    Thanks @ahmedA for your answer! That helped. The code I ended up with was:

    var loop_count = parseInt("${lm://CurrentLoopNumber}");

    var score = parseInt("${lm://Field/1}");

    Qualtrics.SurveyEngine.setEmbeddedData("condition", "Amateur");

    Qualtrics.SurveyEngine.setEmbeddedData("video" + loop_count, score);

    I embed the variables "condition", "video1", "video2", and "video3" after consent. This works extremely well.

    With regards to 4060, I calculated it doing n choose k which is given as n!/(k!(n-k)!). In this case of having say 30 videos to choose from and 3 videos chosen gives 30!/(3!(30-3)!) = 4060.