Hi, I am trying to record the time a participant watches an embedded video by tracking the time lapsed on page when a participant presses play and when they press pause. I currently have put the below JavaScript (provided by AI support -- I am not very familiar with JS) in the individual question (each on their own page with nothing but the qualtrics hidden timing question to know when they click to the next page), created embedded fields with the two variable names (image attached for verification, this is at the very start of my survey), and added <script src="https://www.youtube.com/iframe_api"></script> to my header (image also attached) However, there are no metrics showing in my data, just two empty column called FirstPlayTime and FirstPauseTime. I tried with the simple layout and flat layout, tested in different browsers on both published and preview versions. Any guidance would be much appreciated, thank you!
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
});
Qualtrics.SurveyEngine.addOnReady(function() {
console.log("Survey Engine Ready");
// Load the YouTube IFrame API
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')n0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// Function to create the YouTube player
window.onYouTubeIframeAPIReady = function() {
console.log("YouTube IFrame API Ready");
var iframe = document.querySelector('iframe');
if (iframe) {
console.log("Iframe found");
var player = new YT.Player(iframe, {
events: {
'onStateChange': function(event) {
if (event.data == YT.PlayerState.PLAYING) {
console.log("Video playing");
if (!Qualtrics.SurveyEngine.getEmbeddedData('FirstPlayTime')) {
var firstPlayTime = new Date().getTime();
Qualtrics.SurveyEngine.setEmbeddedData('FirstPlayTime', firstPlayTime);
console.log("First Play Time: " + firstPlayTime);
}
} else if (event.data == YT.PlayerState.PAUSED) {
console.log("Video paused");
if (!Qualtrics.SurveyEngine.getEmbeddedData('FirstPauseTime')) {
var firstPauseTime = new Date().getTime();
Qualtrics.SurveyEngine.setEmbeddedData('FirstPauseTime', firstPauseTime);
console.log("First Pause Time: " + firstPauseTime);
}
}
}
}
});
} else {
console.log("Iframe not found");
}
};
});
Qualtrics.SurveyEngine.addOnUnload(function()
{
/*Place your JavaScript here to run when the page is unloaded*/
});
IF NEEDED, this is the code for the youtube video:
<iframe title="YouTube video player" style="width:100%" src="https://www.youtube.com/embed/oB2AYcZfc9Q?si=9_EkHg4q71ehTSnq?enablejsapi=1" height="400"></iframe>