Lua Scripting Resources

Important Update to Custom Scripting

SurveyGizmo's CustomScript Action now supports the LUA programming language.


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. Read-only scripts will continue to function; you will just be prevented from editing. The exact date on this is to be determined; we will send notifications well ahead of time.


 Go to our Legacy Scripting Documentation.

While SurveyGizmo is one of the most flexible survey tools around, we get requests for customizations that are not available out of the box. This is where JavaScript and Custom Scripting can save the day. If you have scripting chops you can use the JavaScript action or the Custom Scripting action to achieve the survey of your dreams.


[article("bodfy")]

We have a built-in feature for recording time spent on a survey 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 and Custom Scripting.

Add a survey with this script and setup to your account.

Step 1: Record Time (Milliseconds) Spent On Page

On the page you'd like to record time, 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
  });
});

Required Customizations

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. 

Step 2: Record Time (Milliseconds) Spent On a 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 the survey in another Hidden Value. You could even do some math to get a time spent in seconds with a few decimal places (Alchemer rounds to the nearest second when using the built-in time spent on the survey). Here's how!

For time spent on the 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 = getvalue(8) --hidden value ID, time spent on page 1
page2 = getvalue(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

setvalue(surveyTime,sum) --set hidden value with sum

If you would like to record the time spent on the 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 = getvalue(8) --hidden value ID, time spent on page 1
page2 = getvalue(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

setvalue(surveyTime,seconds) --set hidden value with seconds