Adding reaction time and number of audio playback times to each multiple choice question | XM Community
Skip to main content

Hi! I’ve been struggling with setting embedded data fields in the survey flow, so that they show up in the report. I have a number of multiple choice questions within one block (“Main Survey”), and I want to see both the reaction time (it is called cumulative reaction time) and the number of times an audio has been played for each of those questions. Here is my JS code (I’ve set a number of alerts to check if the code works, and it does give me correct values):

 

Qualtrics.SurveyEngine.addOnload(function() {
    // Get the audio element on the page
    var QuestionID = this.questionId;
    var audioElement = document.querySelector('audio');

    // Check if the audio element is found
    if (audioElement) {
        // Set up variables to store cumulative reaction time and playback count
        var cumulativeReactionTime = 0;
        var played = false; 
        var playbackCount = 0;
        var firstPlayEnded = 0;

        // Event listener to capture the end of audio playback
        audioElement.addEventListener('ended', function() {
            if (!played) {
                firstPlayEnded = Date.now();
            }
            played = true;
            // Increment the playback count
            playbackCount++;
        });
        
        // Play the audio automatically
        audioElement.play();
    }

    // Event listener to capture the timestamp when an answer is selected
    Qualtrics.SurveyEngine.addOnPageSubmit(function() {
        alert('Hello1');
        alert(playbackCount > 0);
        // Check if the audio has completed at least one full playback
        alert(QuestionID);
        if (playbackCount > 0) {
            alert('Hello2');

            // Calculate the total reaction time across all playbacks
            cumulativeReactionTime = (Date.now() - firstPlayEnded) / 1000;

            // Get the current question ID
            alert(QuestionID);

            // Check the name of the embedded data field variable for CumulativeReactionTime
            var cumulativeReactionTimeFieldName = 'CumulativeReactionTime_' + QuestionID;
            alert('Embedded Data Field: ' + cumulativeReactionTimeFieldName);

            // Check the corresponding value for CumulativeReactionTime
            var cumulativeReactionTimeValue = cumulativeReactionTime;
            alert('Embedded Data Value: ' + cumulativeReactionTimeValue);

            // Store the cumulative reaction time as embedded data for CumulativeReactionTime
            Qualtrics.SurveyEngine.setEmbeddedData(cumulativeReactionTimeFieldName, cumulativeReactionTime);
            alert('CumulativeReactionTime_' + QuestionID);
            alert(cumulativeReactionTime);

            // Repeat the process for PlaybackCount
            var playbackCountFieldName = 'PlaybackCount_' + QuestionID;
            alert('Embedded Data Field: ' + playbackCountFieldName);

            // Check the corresponding value for PlaybackCount
            var playbackCountValue = playbackCount;
            alert('Embedded Data Value: ' + playbackCountValue);

            // Store the playback count as embedded data for PlaybackCount
            Qualtrics.SurveyEngine.setEmbeddedData(playbackCountFieldName, playbackCount);
            alert('PlaybackCount_' + QuestionID);
            alert(playbackCount);

            // Reset variables for the next question
            cumulativeReactionTime = 0;
            playbackCount = 0;
        }
    });
});

Qualtrics.SurveyEngine.addOnReady(function() {
    /* Place your JavaScript here to run when the page is fully displayed */
});

Qualtrics.SurveyEngine.addOnUnload(function() {
    /* Place your JavaScript here to run when the page is unloaded */
});

 

I have also added the embedded fields to survey flow (right after the “Main Survey” block) as it’s shown on the attached image. However, those fields are still empty in the survey report after I test it. I’d really appreciate any help!

 

Be the first to reply!

Leave a Reply