Track participant time off page in Qualtrics

The below describes one method for tracking the total number of times a participant leaves an experiment page, either by visiting another browser tab or window, or clicking away from the browser (to their desktop or another program, for example).  It also displays a popup the first time a participant leaves an experiment page, and tracks the total number of milliseconds spent away from your experiment.  Note that this code acts on a single experimental block at a time, so it must be added to at least one question within each block in order to track departures and time away throughout your experiment.  If you have multiple blocks, you’ll need to add additional embedded data elements to your survey flow, and add this code to one question within each block.  In your analysis, you’ll need to add the values across all embedded data elements tracking number of leaves to find the total number of leaves, and add the values across all embedded data elements tracking total time away to find the total amount of milliseconds spent away from the experiment.

Step one: set embedded data
*  Navigate to the “Survey” tab and select “Survey Flow”
*  Select “Add New Element Here” > “Embedded Data”
*  Create two new embedded data elements called “numLeaves” and “totalTimeOff”
*  Drag the green “Set Embedded Data” box to the top of the survey flow

Step two: add custom JavaScript 
*  Click on the settings button next to the first question in the survey block
*  Select “Add JavaScript”

*  A popup will appear with the preloaded code below

*  In the section that begins “Qualtrics.SurveyEngine.addOnReady(function()” add
the below code

Qualtrics.SurveyEngine.addOnReady(function()
{
    /*popupCounter is a variable that keeps track of the number of times*/ 
    /*the subject leaves the page*/
    var popupCounter = 0;
    /*totalTimeOffPage is a variable that keeps track of the total number of */ 
    /*milliseconds the subject spends away from the page*/ 
    var totalTimeOffPage = 0;

    /*window.onblur is triggered whenever the subject leaves the page, but */
    /*IS ONLY triggered TRIGGERED AFTER THE SUBJECT INTERACTS WITH THE PAGE */
    /*AT LEAST ONCE*/ 
    window.onblur = function() {
        /*this start variable captures the current system time on the*/ 
        /*subject's machine*/ 
        var start = new Date(); 
        /*if the subject has not yet received a popup, then a popup is */
        /*triggered warning the subject not to leave the page*/ 
        if (popupCounter < 1) {     
        /*You can set whatever alert message you want inside the parentheses*/
        /*below*/ 
            alert("Please don't leave this HIT until it is complete!"); 
        }
        /*the popupCounter is incremented each time the participant leaves*/
        /*the page*/ 
        popupCounter ++;  
        /*each time the popupCounter is incremented, the Embedded Data value*/ 
        /*"numLeaves" is updated with the new total number of leaves*/ 
        Qualtrics.SurveyEngine.setEmbeddedData('numLeaves', popupCounter);

        /*when the subject returns to the page, the window.onfocus function is*/
        /*triggered*/ 
        window.onfocus = function() {
            /*the end variable captures the current system time on the */
            /*subject's machine*/ 
            var end = new Date(); 
            /*blipTime calculates the total number of MILLISECONDS that*/
            /*passed between the most recent onblur event, and subsequent*/
            /*onfocus event*/ 
            blipTime = end - start;
            /*blipTime is added to the running total time off page in*/
            /*the variable totalTimeOffPage*/
            totalTimeOffPage = totalTimeOffPage + blipTime; 
            /*the Embedded Data value totalTimeOff is updated to reflect*/
            /*the current running total of time off the page*/ 
            Qualtrics.SurveyEngine.setEmbeddedData('totalTimeOff', totalTimeOffPage);
        }
    }
});

*  Click “√ Save”

Step 3: test your code!
It is very important that you test the functionality of this code and verify that it is tracking the metrics you intend to track, in the manner you intend to track them, before using it in a live experiment.  Note that the code does not capture any activity until *after* the participant interacts with the page in some way (for example, clicking on a radial button).  Also, note that the code tracks time away in milliseconds *not* seconds.  I highly recommend using a stopwatch with millisecond timing to verify the code is capturing the full amount of time off the page.  I also highly recommend testing the code across multiple system configurations (Browser + Operating System), as JavaScript may function differently across configurations.  Finally, note that Qualtrics often updates their platform, and in particular the way their platform accommodates custom JavaScript, without explicitly notifying researchers.  Just because this code works the first time you test it, does not mean it will work days, weeks, or months later.