如何记录从JavaScript到Qualtrics的页面散焦事件数,而不在页面上显示计数

如何记录从JavaScript到Qualtrics的页面散焦事件数,而不在页面上显示计数,javascript,qualtrics,Javascript,Qualtrics,我在这方面是新手(StackOverflow和非统计编码),所以如果我把这个问题搞砸了,我深表歉意 问题/目标: 我让参与者参加一个在线实验(使用Qualtrics,一个调查平台),让他们观看视频。我需要他们看视频,而不仅仅是听,因为我在操纵视频中人的非语言行为 作为一项质量检查,我想计算他们是否从包含视频的浏览器页面散焦(即切换到另一个窗口或选项卡)。我找到了。但是,代码似乎不是为Qualtrics集成而设计的(记录数据),它在屏幕上显示页面焦点数据 我能够修改代码,使其记录到Qualtric

我在这方面是新手(StackOverflow和非统计编码),所以如果我把这个问题搞砸了,我深表歉意

问题/目标: 我让参与者参加一个在线实验(使用Qualtrics,一个调查平台),让他们观看视频。我需要他们看视频,而不仅仅是听,因为我在操纵视频中人的非语言行为

作为一项质量检查,我想计算他们是否从包含视频的浏览器页面散焦(即切换到另一个窗口或选项卡)。我找到了。但是,代码似乎不是为Qualtrics集成而设计的(记录数据),它在屏幕上显示页面焦点数据

我能够修改代码,使其记录到Qualtrics中的嵌入式数据,但它只在显示PageFocus计数器时起作用。我希望能就如何在记录数据的同时使计数器不可见提出任何建议

tl;dr——我想计算参与者在被要求观看Qualtrics视频时切换到另一个选项卡/窗口的频率。我找到了计算页面散焦事件的代码,但数据仅在计数器在屏幕上可见时记录。我可以使计数器不可见,但仍然记录数据吗

(修改后的)页面焦点代码: 上面的代码成功地将PageFocus代码中的对象/变量记录到Qualtrics嵌入数据中,但只有在我没有删除下面PageFocus代码的这一部分时(这对我来说似乎很奇怪,因为我认为这一部分只是在页面上显示内容,但我可能错了):

页面离焦(D)和再聚焦事件(R):

页面散焦事件数:

页面重新聚焦事件数:

最后一页离焦和最后一次重新聚焦事件之间的持续时间(最后一次缺席的持续时间): 秒

页面离焦和重新聚焦事件之间的所有持续时间之和(所有缺勤持续时间之和): 秒


如果有任何建议,我将不胜感激。谢谢大家!

您只需更改
record\u timestamp
函数即可执行Qualtrics调用,并删除所有UI内容(它会生成一个弹出窗口-我不知道是否需要,但我假设不需要)

下面的
record\u timestamp
函数的最后一行将抛出错误。您需要找到用于将所有嵌入数据发送到qualtrics的最佳触发器方法,因为qualtrics.SurveyEngine.navClick可能不是正确的方法

//将所有数据变量放入一个单独的对象中
var fd={};
函数reset(){//reset捕获的数据
fd.focus_data='';
fd.离焦计数=0;
fd.重新聚焦计数=0;
fd.last_duration=0;
fd.duration_sum=0;
fd.pagefocus=true;
}
重置();
记录\时间戳=函数(类型,时间戳){
fd.focus_data+=类型+时间戳+“;”;//向数据记录添加新的页面聚焦事件
控制台日志(fd);
//发送收集的数据
/*这将在如此注释的代码段上中断*/
/*
Qualtrics.SurveyEngine.setEmbeddedData(“持续时间总和”,fd.持续时间总和);
Qualtrics.SurveyEngine.setEmbeddedData(“焦点数据”,fd.focus\u数据);
Qualtrics.SurveyEngine.setEmbeddedData(“离焦计数”,fd.离焦计数);
Qualtrics.SurveyEngine.setEmbeddedData(“重新聚焦计数”,fd.重新聚焦计数);
Qualtrics.SurveyEngine.setEmbeddedData(“last_duration”,fd.last_duration);
如果(类型=“D”)
Qualtrics.SurveyEngine.setEmbeddedData(“离焦时间戳”,时间戳);
else if(类型==“R”)
Qualtrics.SurveyEngine.setEmbeddedData(“重新聚焦时间戳”,时间戳);
Qualtrics.SurveyEngine.navClick(事件“下一个按钮”)
*/
}
函数失去焦点(){//检测到页散焦事件
fd.pagefocus=false;
fd.disfocusing_count++;//计算离焦事件的数量
fd.defocus_timestamp=新日期().getTime();
记录时间戳(“D”,fd.散焦时间戳);
}
函数重新获得了_focus(){//检测到页面重新聚焦事件
如果(!fd.pagefocus){
fd.pagefocus=true;
fd.refocus_timestamp=new Date().getTime();
fd.refocusing_count++;//计算重新聚焦事件的数量
fd.last_duration=fd.refocus_timestamp-fd.defocus_timestamp;//计算最后一页离焦和重新聚焦事件之间的持续时间
fd.duration_sum+=fd.last_duration;//页面离焦和重新聚焦事件之间的总持续时间
记录时间戳(“R”,fd.重新聚焦时间戳);
}
}
函数onfocusout(){//Internet Explorer的变通方法<版本11
清除超时(计时器);
定时器=设置超时(失去焦点,100);
}
函数onfocusin(){//Internet Explorer的变通方法<版本11
清除超时(计时器);
重新获得焦点();
}
如果(文档中的“onfocusin”){//检查Internet Explorer版本是否<11
无功定时器;
document.onfocusin=onfocusin;
document.onfocusout=onfocusout;
}else if(窗口中的“onpageshow”){
//对移动Safari使用onpageshow和onpagehide
window.onfocus=window.onpageshow=重新获得焦点;
window.onblur=window.onpagehide=焦点丢失;
}
    <!DOCTYPE html>
<html>

<head>
<title>PageFocus Demo</title>


<style type="text/css">
  body {
    font-family: sans-serif;
  }
  .numeric_input {
    text-align: right;
  }
  .footnote {
    font-size: 0.8em;
  }
  #popup_warning {
    display: none;
    width: 500px;
    height: 180px;
    position: absolute;
    background-color: #fff;
    z-index: 255;
    text-align: center;
    padding: 0 20px 20px;
    border: 5px solid red;
    top: 50%;
    left: 50%;
    margin-left: -250px;
    margin-top: -90px;
  }
  #popup_warning .ok_button {
    margin-top: 20px;
  }
  #popup_warning .ok_button button {
    width: 100px;
    height: 30px;
  }
  #disabled {
    display: none;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background: #707070;
    filter:alpha(opacity=75);
    opacity: 0.75;
    z-index: 253;
  }
</style>

</head>

<body>

<p><label>Page defocusing (D) and refocusing events (R):
<input type="text" id="events_input" name="events_input" size="80" readonly></label></p>

<p><label>Number of page defocusing events: <input class="numeric_input" type="text" id="defocus_count_input" name="defocus_count_input" size="4" value="0" readonly></label></p>

<p><label>Number of page refocusing events:
<input class="numeric_input" type="text" id="refocus_count_input" name="refocus_count_input" size="4" value="0" readonly></label></p>

<p><label>Duration between last page defocusing and last refocusing event (Duration of last absence):
<input class="numeric_input" type="text" id="last_duration_input" name="last_duration_input" size="10" value="0" readonly> seconds</label></p>

<p><label>Sum of all durations between page defocusing and refocusing events (Sum of all absence durations):

<input class="numeric_input" type="text" id="duration_sum_input" name="duration_sum_input" size="10" value="0" readonly> seconds</label></p>


<p></p>


<div id="popup_warning">
  <h3>PageFocus warning</h3>
  <p>You have just left this window which is not allowed while participating on the test. Please return to the test by confirming this message, and do not leave the test page again.</p>
  <div class="ok_button"><button onclick='toggle_popup_warning("none"); regained_focus(); return false;'>OK</button></div>
</div>
<div id="disabled"></div>

<script type="text/javascript">

// PageFocus version 1.3-1 demo
var focus_data;  // all page defocusing and refocusing events as one string
var defocusing_count;  // number of page defocusing events
var refocusing_count;  // number of page refocusing events
var last_duration;  // duration between the last page defocusing and refocusing events
var duration_sum;  // sum of all durations between page defocusing and refocusing events
var defocus_timestamp;  // timestamp of last page defocusing event
var refocus_timestamp;  // timestamp of last page refocusing event
var pagefocus = true;  // does the current page have the focus?
var popup_visible = false;  // is the popup currently being shown?
// input elements
var popup_checkbox = document.getElementById("popup_warning_checkbox");
var events_input = document.getElementById("events_input");
var defocus_count_input = document.getElementById("defocus_count_input");
var refocus_count_input = document.getElementById("refocus_count_input");
var last_duration_input = document.getElementById("last_duration_input");
var duration_sum_input = document.getElementById("duration_sum_input");
reset()
record_timestamp = function(type, timestamp) {
  focus_data = focus_data + type + timestamp + ";";  // add new page focusing event to the data record
  events_input.value = focus_data;
  events_input.scrollLeft = events_input.scrollWidth;  // scroll input field to the right
}
function lost_focus() {  // page defocusing event detected
  if(!popup_visible) {
    pagefocus = false;
    defocus_timestamp = new Date().getTime();
    record_timestamp("D", defocus_timestamp);
    defocusing_count++;  // count the number of defocusing events
    defocus_count_input.value = defocusing_count;
    if(popup_checkbox.checked) toggle_popup_warning("block");
  }
}
function regained_focus() {  // page refocusing event detected
  if(!pagefocus && !popup_visible) {
    pagefocus = true;
    refocus_timestamp = new Date().getTime();
    record_timestamp("R", refocus_timestamp);
    refocusing_count++;  // count the number of refocusing events
    refocus_count_input.value = refocusing_count;
    last_duration = refocus_timestamp - defocus_timestamp; // calculate the duration between the last page defocusing and refocusing events
    duration_sum += last_duration; // sum durations between page defocusing and refocusing events
    last_duration_input.value = last_duration/1000;
    duration_sum_input.value = duration_sum/1000;
  }
}
function onfocusout() {  // workaround for Internet Explorer < version 11
  clearTimeout(timer);
  timer = setTimeout(lost_focus,100);
}
function onfocusin() {  // workaround for Internet Explorer < version 11
  clearTimeout(timer);
  regained_focus();
}
function reset() {  // reset captured data
  events_input.value = focus_data = '';
  defocus_count_input.value = defocusing_count = 0;
  refocus_count_input.value = refocusing_count = 0;
  last_duration_input.value = last_duration = 0;
  duration_sum_input.value = duration_sum = 0;
}
function toggle_popup_warning(state) {  // show/hide popup warning
  document.getElementById("popup_warning").style.display = state;
  document.getElementById("disabled").style.display = state;
  popup_visible = state == "block";
}
if("onfocusin" in document) {  // check for Internet Explorer version < 11
  var timer;
  document.onfocusin = onfocusin;
  document.onfocusout = onfocusout;
} else if("onpageshow" in window) {
  // use onpageshow and onpagehide for mobile Safari
  window.onfocus = window.onpageshow = regained_focus;
  window.onblur = window.onpagehide = lost_focus;
}
</script>

</body>
</html>
Qualtrics.SurveyEngine.addOnload(function()
{
    /*Place your JavaScript here to run when the page loads*/

$('NextButton').onclick = function (event) {
Qualtrics.SurveyEngine.setEmbeddedData("duration_sum", duration_sum);
Qualtrics.SurveyEngine.setEmbeddedData("focus_data", focus_data);
Qualtrics.SurveyEngine.setEmbeddedData("defocusing_count", defocusing_count);
Qualtrics.SurveyEngine.setEmbeddedData("refocusing_count", refocusing_count);
Qualtrics.SurveyEngine.setEmbeddedData("last_duration", last_duration);
Qualtrics.SurveyEngine.setEmbeddedData("defocus_timestamp", defocus_timestamp);
Qualtrics.SurveyEngine.setEmbeddedData("refocus_timestamp", refocus_timestamp);

// and now run the event that the normal next button is supposed to do
        Qualtrics.SurveyEngine.navClick(event, 'NextButton')
}
});
    <p><label>Page defocusing (D) and refocusing events (R):
<input type="text" id="events_input" name="events_input" size="80" readonly></label></p>

<p><label>Number of page defocusing events: <input class="numeric_input" type="text" id="defocus_count_input" name="defocus_count_input" size="4" value="0" readonly></label></p>

<p><label>Number of page refocusing events:
<input class="numeric_input" type="text" id="refocus_count_input" name="refocus_count_input" size="4" value="0" readonly></label></p>

<p><label>Duration between last page defocusing and last refocusing event (Duration of last absence):
<input class="numeric_input" type="text" id="last_duration_input" name="last_duration_input" size="10" value="0" readonly> seconds</label></p>

<p><label>Sum of all durations between page defocusing and refocusing events (Sum of all absence durations):

<input class="numeric_input" type="text" id="duration_sum_input" name="duration_sum_input" size="10" value="0" readonly> seconds</label></p>


<p></p>