Search code examples
javascriptqualtrics

How can I record the equivalent of Qualtrics "page submit" time in seconds using Javascript?


I am trying to write javascript code that calculates the "page submit" time (time spent from start of view to next button click) in Qualtrics. I can't use the regular timed Qualtrics Question because I have other code that works with the next button, which does not work when I add a Qualtrics timing question to the page. (I've omitted that code from below just to make things simpler.)

I believe the problem is that I'm having trouble passing the start time from the Qualtrics.SurveyEngine.addOnload section to the Qualtrics.SurveyEngine.addOnPageSubmit section.

I have two embedded data fields: StartHolder and TimeDiff, both set to 0. I'm using the following code to try and calculate the time elapsed:

Qualtrics.SurveyEngine.addOnload(function()
{
    Qualtrics.SurveyEngine.setEmbeddedData('StartHolder',new Date);
});

Qualtrics.SurveyEngine.addOnPageSubmit(function() {
    var TimeDiff = new date() - Date.parse("${e://Field/StartHolder}");
    Qualtrics.SurveyEngine.setEmbeddedData('TimeDiff',TimeDiff);
});

I think the problem is I'm not pulling the StartHolder variable back into my code properly, but I'm not sure how to fix it. Any advice would be appreciated.


Solution

  • You can't set and pipe and an embedded variable on the same page. You should set a JS variable as the start time then put your addOnPageSubmit function inside your addOnLoad function.

    Qualtrics.SurveyEngine.addOnload(function() {
        var startHolder = new Date();
        Qualtrics.SurveyEngine.addOnPageSubmit(function() {
           var TimeDiff = new Date() - startHolder;
           Qualtrics.SurveyEngine.setEmbeddedData('TimeDiff',TimeDiff);
        });
    });