This script stores the length of time a participant takes to complete a question (in seconds). The time is counted from when the question page loads to when the participant clicks next and moves to the next step in the survey.
Detailed Steps
- In your survey, add a Page element.
- Add the question that you want to time to the Page element.
-
Create another Page element and add the following questions to the new Page element for use as your timer. Ensure the question names and types match what is listed below:
- Page_Start (Short Answer)
- Page_Last (Short Answer)
- Page_End (Short Answer)
- Time_Spent_Page (Number – Text field)
- Total_Time_Since (Short Answer)
By now, your survey structure should look similar to this. The question you want to time is on the first page. All the questions you need to set up the timer are on the second page. - Open the first Page element for editing and click the Scripting button.
- Copy and paste Script 1 into the On Load tab.
- While the first Page element is still open for editing, copy and paste Script 2 into the On Complete tab.
- Click Save.
- Preview and test your survey.
Script 1
/* Script Name: How do I time how long it takes a participant to complete a question? (Modern Surveys) (Script 1) To ensure your script works as expected, always copy the original script from the Script Library found in the Alida Help Center. Failure to use the latest script from the script library may cause unexpected results. */ const pageStart = response.getDataPoint('Page_Start'); const pageLast = response.getDataPoint('Page_Last'); // Get the current dateTime as a UTC string
var start = new Date().toUTCString(); // Check the 'start' hidden question to see if it has a value, populate it if not
const cur = pageStart.get(); if (cur == null){ pageStart.set(start); } // Always populate the 'last' hidden question so its updated in cases such as the back button
pageLast.set(start);
Script 2
/* Script Name: How do I time how long it takes a participant to complete a question? (Modern Surveys) (Script 2) To ensure your script works as expected, always copy the original script from the Script Library found in the Alida Help Center. Failure to use the latest script from the script library may cause unexpected results. */ const pageStart = response.getDataPoint('Page_Start'); const pageEnd = response.getDataPoint('Page_End'); const pageLast = response.getDataPoint('Page_Last'); const totalTime = response.getDataPoint('Total_Time_Since'); const timeOnPage = response.getDataPoint('Time_Spent_Page'); // Freshly set the end date each time var end = new Date(); pageEnd.set(end.toUTCString()); // Calculate total time since page was first visted. Set into an open end if (pageStart.get() != null){ var startDate = new Date(pageStart.get()); var time = (end - startDate) / (1000); if (time != null && !isNaN(time)) { // Convert to a string with desired number of decimal places var setTime = time.toFixed(2); totalTime.set(setTime); } } // Calculate cumulative time spent on the page. Set into a numeric if (pageLast.get() != null){ var lastDate = new Date(pageLast.get()); var pageTime = (end - lastDate) / (1000); if (pageTime != null && !isNaN(pageTime)) { // Convert to desired decimal places and then parse back to a float/decimal value var newTime = parseFloat(pageTime.toFixed(2)); // Get the current amount of time spent on the page and add the newly calculated value var currentTimer = timeOnPage.get(); if (currentTimer != null) newTime += currentTimer; timeOnPage.set(newTime); } }
Example
First page with the timed Single Choice question:
Second page with the timer data populated:
Comments
0 comments
Article is closed for comments.