I have a Loop & Merge that loops over 300 audio files. For each audio file, I want to store the number of times the audio replays.
Qualtrics.SurveyEngine.addOnReady(function(){
var plays = 0;
function audioEnded(){
if (plays == 0) Qualtrics.SurveyEngine.addEmbeddedData( "replays${lm://CurrentLoopNumber}", plays);
plays++;
Qualtrics.SurveyEngine.setEmbeddedData("replays${lm://CurrentLoopNumber}", plays);
}
jQuery('audio').on('ended', audioEnded);
});
This is my solution, but it doesn't seem to work because my embedded data isn't actually being stored in the survey (as in, they're not included in the survey flow). Creating 300 embedded data variables by hand doesn't seem feasible. Also, my Loop & Merge has random order and I don't think the embedded data will capture this random order.
A better solution I can think of is to append the number of replays to the user's answer itself (text answer) with some sort of delimeter. I'm not really sure how this is possible either however.
Any tips?
Yes, you can save them in a single delimited list. Add an audioId as a Loop & Merge field if you don't already have one (it could just be numbers 1 through 300). Also, you only need to update the embedded data variable once at the end. The following will save audioId:replays for each audio in their randomized order separated by commas. Initialize replaysList as empty in the survey flow before the Loop & Merge block.
Qualtrics.SurveyEngine.addOnload(function(){
var replaysArr = "${e://Field/replaysList}".split(",").filter(Boolean);
var plays = 0;
jQuery('audio').on('ended', function() { plays++; });
Qualtrics.SurveyEngine.addOnPageSubmit(function() {
replaysArr.push("${lm://Field/1}:"+(plays-1));
Qualtrics.SurveyEngine.setEmbeddedData("replaysList",replaysArr.join(","));
});
});