Capture embeded click counts of an audio file within a randomised loop | XM Community
Skip to main content

Hi,
My test needs to count how many times a participant listens to an audio file. I used timing in qualtrics, but timing makes mistakes in recording the results. For example, it also counts clicks when people click the blank page.
So I would like to use javascript to solve this problem.
How do I instantiate embedded data inside Javascript? My current survey randomly chooses a series of audio files and questions in loop& merges (which has three fields; i.e., field 1 is audio files, field 2 is a yes/no question, and field 3 is a picture), and I would like click counts to be counted and named in accordance with the specific question dynamically, is this possible?

My current code in HTML is below

Press the button to play the audio file. Once the audio file has finished playing, you will be able to play it again.






${lm://CurrentLoopNumber} of ${lm://TotalLoops}


Javescript is below
Qualtrics.SurveyEngine.addOnReady(function() //OnLoadComplete
{
//Create variables
var seconds = 0;     //Create emtpy seconds int
var playCount = 0;    //Create iterator to count times played
var sound = document.getElementById("sound1"); //Get this audio file based on ID defined in HTML
sound.volume = 1;

//Count upwards in seconds
const interval = setInterval(function()
{
   seconds++;
    Qualtrics.SurveyEngine.setEmbeddedData("_secondsCount", seconds); 
   }, 1000);

//Function created to play sound and record input
function playSound() 
{
playCount++;                                  //Iterate counter
console.log("Sound playing");   //inform console
  console.log("Times sound played:" + playCount); 
  var button = document.getElementById("audiobutton"); //button from HTML reference
var question_id = this.questionId;

var embeddedDataFieldname = 'playCount' + question_id ;


  button.disabled = true;                          //Disable while audio is playing
button.style.background='#110000';
  sound.play();                                  //play the sound
  
  //update _playCount embedded data to record this result
  Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataFieldname,this.question); 

};

//This is the reference into the HTML button which runs the function
jQuery("#audiobutton").on("click", playSound);

//When the sound has finished playing, add one to the counter
  sound.onended = function() 
  {
     console.log("Sound finished");

  
  var button = document.getElementById("audiobutton"); //button from HTML reference
  button.disabled = false;                          //reactivate button 
    button.style.background='#4CAF50';
}
});
//**********************************************************************************************************************


Qualtrics.SurveyEngine.addOnUnload(function() //OnUnloaded
{
clearInterval(interval); //release function when page is killed
});
Screen Shot 2022-04-09 at 1.47.25 AM.png//*****************************************************************************************************************

Cheers,

Yue

Hi Yue,
try the below script to count the clicks on the start button
Pleas mind:

  1. you need to define the embedded data in the survey flow before the loop

  2. to get the loopId (not the count of loops) you need to refer to the question Id and split it

  3. you can initiate the functions initiated by clicking on the play button with this.questionclick

  4. I have only dealt here with the click count and writing the results back into the embedded data fields.


Qualtrics.SurveyEngine.addOnReady(function()
{

// this script only collects the counts of the clicks on the play button
let loopId = this.questionId.split('_')[0] // gets the actual loop number in which the randomly selected file is shown
let embeddedDataFieldName = "clickCount_"+loopId //initiate the embedded data field. these needs to be defined in the survey flow before the loop
console.log(embeddedDataFieldName)
let clickCount = 0; // initial value of the click count of the play button

this.questionclick = function(event,element)
  {
    
    console.log(event, element);
    if (element.id == 'startAudio') // insert the id of your play button
    {
      clickCount = clickCount+1
      alert('You clicked the start button' + clickCount + ' times');
Qualtrics.SurveyEngine.setEmbeddedData(embeddedDataFieldName,clickCount); //write the current count into the embededded data field
       
    }
  }
});
Hope this helps.
best regards

Rudi


Leave a Reply