我正在尝试计算我嵌入调查平台 Qualtrics(即使用 iframe)的视频游戏参与者的鼠标点击次数。我改编了 (https://math.bu.edu/people/jackwalt/qualtrics-mousetracking/) 中的代码,每 10 毫秒吐出 x/y 鼠标坐标。但是,我无法获取代码来计算 iframe 窗口内发生的鼠标单击次数。不过,让它计算 iframe 之外的点击次数非常容易。当我运行此代码时,它会在数据输出(即 csv 文件中的一列)中给出“0”计数,但现在它也不会像最初那样给出 x/y 坐标。计数应该通过 addclick 函数进行。我假设我把东西放在错误的顺序/位置。
Qualtrics.SurveyEngine.addOnload(function()
{
/*Place your JavaScript here to run when the page loads*/
Qualtrics.SurveyEngine.addOnPageSubmit(function(type)
{
if(type == "next")
{
Qualtrics.SurveyEngine.setEmbeddedData("xPos", delayedXPos.join());
Qualtrics.SurveyEngine.setEmbeddedData("yPos", delayedYPos.join());
Qualtrics.SurveyEngine.setEmbeddedData("time", delayedTime.join());
Qualtrics.SurveyEngine.setEmbeddedData("click", numberOfClicks); // Store the number of clicks;
}
});
});
document.onmousemove = getMousePosition;
//set document to record mouse position
//initialize arrays
var delayedXPos = new Array();
var delayedYPos = new Array();
var delayedTime = new Array();
var iframe = document.getElementById('myFrame');
var xPos = new Array();
var yPos = new Array();
//initialize time variables
var initTime = new Date().getTime();
var timer_is_on=0;
var t;
//time interval for data collection in ms
var dt=10;
let numberOfClicks = 0;
//flag signaling whether getMousePosition has been called
mp_called = 0;
//function that determines action when mouse moves
function getMousePosition(mp)
{
var divPos = getPosition(document.getElementById("Questions"));
xPos.push(mp.pageX - divPos[0]);
yPos.push(mp.pageY - divPos[1]);
mp_called = 1;
return true;
}
function addclick()
{
iframe.addEventListener('load', function() {
// Access the content of the iframe
var iframeContent = iframe.contentDocument || iframe.contentWindow.document;
numberOfClicks++;
document.getElementById('clicks').innerHTML = numberOfClicks;
});
}
iframeContent.addEventListener("click", addclick);
function timedCount()
{
if(mp_called){
delayedXPos.push(xPos[xPos.length-1]);
delayedYPos.push(yPos[yPos.length-1]);
var timeInSec = (new Date().getTime()-initTime) / 1000.;
delayedTime.push(timeInSec);
}
t=setTimeout("timedCount()",dt);
}
function doTimer()
{
if (!timer_is_on)
{
initTime = new Date().getTime();
timer_is_on=1;
timedCount();
}
}
function getPosition(obj){
var topValue= 0,leftValue= 0;
while(obj)
{
leftValue+= obj.offsetLeft;
topValue+= obj.offsetTop;
obj= obj.offsetParent;
}
return [leftValue, topValue];
}
//start collecting data after page loads
document.onload = doTimer();
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*/
});
这里还有嵌入式游戏的 HTML 代码:
<iframe id="myFrame" src="https://minmaxia.com/c2/" width="1024" height="800" style="border:1px solid black;" title="clickpocalypse 2"></iframe>
我不确定我哪里出了问题,任何帮助将不胜感激!!
如果您希望它在 iframe 中跟踪鼠标活动,则需要将此代码放入 iframe 中