I'm running a study online and would like to know when participants in the study switch tabs (e.g., as a rough, indirect proxy of attention). I've done this before on Qualtrics and was trying to adapt the logic, but I haven't succeeded thus far. I have:
Disabled the XSS filter.
Added the following code to custom.js (thinking this will be ran in every page so it will create a string with the question IDs where the tabswitching took place):
Code:
$(document).on('ready pjax:scriptcomplete',function(){// When the window is blurred (i.e., loses focus)
$(window).blur(function(){if(tabSwitches !== undefined){// We check if we have defined TB beforevar tabSwitches ="";// If not, we define it}
tabSwitches +=("{SGQ}");// Then we add the question ID to the string
tabSwitches +="&";// And add & as a separator});});
Created a short free text question after all the questions in which I want tab switching to be registered. It then has the following code within the question field:
Code:
<script type="text/javascript" charset="utf-8">
$(document).on('ready pjax:complete',function(){// Hide this answer field
document.getElementById("answer{SGQ}").style.display='none';// Save tabSwitches
$('input#answer{SGQ}').text(tabSwitches);// Troubleshoot
alert("SGQ is: {SGQ} and tabSwitches is: "+ tabSwitches);});</script>
As is, the alert isn't running at all (haven't checked the database, but I bet it's empty as well). I think it's because tabSwitches is undefined and spoils the rest (if I remove the tabSwitches variable, the alert runs correctly and replaces {SGQ} with the correct number), so I would love if anyone could help me out, particularly in understanding if it is possible to get custom.js to run in every page or whether I have to turn what I have there into a function that I call in every single question. Thanks!