Record Time Spent On Page or Survey In Milliseconds

Important Update to Custom Scripting

SurveyGizmo's CustomScript Action now supports the LUA programming language. Visit our NEW Lua Scripting Resources!

Legacy Custom Scripting Language Deprecation Plans 

  1. New accounts (created after October 29, 2018) will only have the option to use Lua in scripts.
  2. As of October 29, 2018 Custom Scripting Actions will default to Lua as the scripting type in the Custom Scripting Action for accounts created before this date. You will be able to switch to the Legacy Custom Scripting; though we highly encourage using Lua.
  3. In the long term, Legacy Custom Scripting Actions will be switched to read-only. The exact date on this is to be determined; we will send notifications well ahead of time.

We have a way to record time spent on page in seconds within a Hidden Value. If you wish to be more precise by recording the time spent in milliseconds you can do so with a little bit of JavaScript!

Add a survey with these scripts to your account!

Record Time (Milliseconds) Spent On Page

On the page you'd like to record, add a Hidden Value action and a JavaScript action. Leave the Hidden Value blank (no populated value). Copy and paste the below JavaScript in the JavaScript Action:


$(document).ready(function(){
  var start = new Date();
  $("#sg_FormFor2181434").submit(function(){  //change the survey ID in this line
    var elapsed = new Date() - start;
    $("#sgE-2181434-1-8-element").val(elapsed); //ID of your hidden value goes here
  });
});

Customizations

  • Change the survey ID in this line: the "sg_FormFor2181434" should read "sg_FormForYOURSIDHERE"
  • Change the ID of the hidden value to the one for your page. Learn how to find this ID or replace the numbers in the ID like this: "#sgE-SIDhere-PageIDhere-QIDhere-element"

You can use this on as many pages as you like; just have a separate JavaScript Action and Hidden Value on each page, and make sure to change the hidden value ID in each JavaScript Action. 

Record Time (Milliseconds) Spent On Survey

Once you have a time in milliseconds recorded in a Hidden Value on each page, you can use a Custom Script Action to add them up and store a time spent on survey in another Hidden Value. You could even do some math to get a time spent in seconds with a few decimal places (SurveyGizmo rounds to the nearest second when using the built-in time spent on survey). Here's how!

For time spent on survey in milliseconds, we'll just need to add up all of the Hidden Values where we've stored time spent on page, and then store that sum in another Hidden Value. We suggest putting the Custom Script Action on the Thank You page, and the Hidden Value wherever you'd like it on the survey (after the last question is fine).

Just copy and paste this script in the Custom Script action making sure to change each of the IDs to correspond with the IDs in your survey.


%%page1 = sgapiGetValue(8); //hidden value ID, time spent on page 1
%%page2 = sgapiGetValue(10); //hidden value ID, time spent on page 2

%%surveyTime = (12); // ID of hidden value to store total survey time

%%sum = %%page1 + %%page2; //add the numbers together

sgapiSetValue(%%surveyTime,%%sum); //set hidden value with sum

If you'd like to record the time spent on survey in seconds with a decimal instead of raw milliseconds, copy and paste the below script into your Custom Script Action making sure to change each of the IDs to correspond with the IDs in your survey.


%%page1 = sgapiGetValue(8); //hidden value ID, time spent on page 1
%%page2 = sgapiGetValue(10); //hidden value ID, time spent on page 2

%%surveyTime = (12); // ID of hidden value to store total survey time

%%sum = %%page1 + %%page2; //add the numbers together
%%seconds = %%sum / 1000; //divide milliseconds by 1,000 to get seconds

sgapiSetValue(%%surveyTime,%%seconds); //set hidden value with seconds

Scripting and Other Out-of-the-Box Customizations

We’re always happy to help you debug any documented script. That said, we do not have the resources to write scripts on demand.

If you have customization ideas that you haven't figured out how to tackle, we're happy to be a sounding board for SurveyGizmo features and functionality ideas that might meet your customization. Beyond this, you might want to consult with someone on our Programming Services Team; these folks might have the scripting chops to help you to achieve what you are looking for!

@plans @pro @ent