Ok, here it is, the definitive force watch youtube in Qualtrics (Tip of the hat to Google’s Gemini)
This will NOT stop if user opens a different tab, but at least it prevents them from skipping the video, and it will autoadvance once they have finished playing it.
You want to insert your video as an iframe; i.e. copy and paste the embed code provided by youtube on the video to be shown.
Autoplay can be blocled in some browsers, so while I have it activated here, I also enable limited control of the iframe--to do this at the end of the src url in your iframe, add the following text:
?autoplay=1&controls=0showinfo=0&loop=0&enablejsapi=1
and if your src already has a “?” in it, simply replace the “?” in my line above with “&”, like I did below:
<iframe src="https://www.youtube.com/embed/wXZNuwY_5-U?si=7mvu8E1QcDDkpoii&autoplay=1&controls=0showinfo=0&loop=0&enablejsapi=1"></iframe>
The above line is the ENTIRE survey question
autoplay=1 makes it autoplay
controls=0 and showinfo=0 prevent excessive options popping up in the iframe (users can pause and resume, but not fast forward or change resolution)
loop=0 prevents looping which would in-turn could be problematic, because we want the iframe to report the video having ended after it has been watched in its entirety.
enablejsapi=1 is MANDATORY as this allows the youtube player to pass along a “video ended” message to Qualtrics JavaScript
Finally, you only need to alter the onload JS to read as follows:
Qualtrics.SurveyEngine.addOnload(function()
{
/*Hide the next button*/
this.hideNextButton();
/*Get the video element*/
var video = document.querySelector('iframe');
/*Function to check for video end*/
var videoEndListener = function(event) {
if (event.data === YT.PlayerState.ENDED) {
this.clickNextButton();
}
}.bind(this);
/*Load the YouTube IFrame Player API*/
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
/*When the API is ready, create the player and add the event listener*/
window.onYouTubeIframeAPIReady = function() {
var player = new YT.Player(video, {
events: {
'onStateChange': videoEndListener
}
});
};
});
Note that above, I used “.clickNextButton” to autoadvance, however you could alternatively use showNextButton if you want the user to advance to the next question as opposed to doing so automagically.