Conditional Scripts
Conditional Scripts allow you to write a script that returns true or false. In Voiceform logic, there is a UI solution to combine conditions related to question answers, but sometimes more complex logic is needed. That's when it's a good idea to use Conditional Scripts. For example, imagine we want to ask for an email only if the email doesn’t exist in the metadata, or if we want to disqualify a respondent who filled out the survey suspiciously quickly. This is where conditional scripting becomes useful.
How to Use Conditional Scripts
- Go to the logic action.
- Add a new action.
- Find the script switch button on the condition. This switch will toggle from a UI condition to a script condition.
- Click on script editing to write the script. You may use all the capabilities of the script editor. The only requirement is that the script must return a boolean value.
Note
This document doesn’t cover Script Editor capabilities. Please refer to the Script Editor document for more details.
Examples
Here are a couple of examples to give you an idea of how to apply conditional scripts in specific use cases:
Hiding Questions Example
Let's assume we want to show an email question within our survey only if it is not provided in our metadata.
-
Create an Email question and place it at the beginning of the survey.
-
Go to the "Logic Actions" section of the question properties and click to add an action.
-
We want to conditionally skip the question if the
email
metadata property exists. So, we need to select a skip action. -
In the first condition, switch from UI to conditional scripting mode by clicking the code tag button.
-
Edit the script. We want to create a condition that checks if the email exists in the metadata and, if yes, return
true
(skip this question). Here's a code example:if (dynamicVariables.metadata.email) { return true; } return false;
-
Save the action and publish changes.
-
Let's test it! If we go to our survey and pass the query parameter
[email protected]
in the survey link, the email question should be skipped. If not, the question should be present.
Disqualify Respondents Example
Sometimes we want to disqualify respondents, for example, if they filled out the survey suspiciously quickly. Let's implement this behavior.
Assume we have a survey with 3 open-ended questions and 3 quantitative questions. Based on our experience, a high-quality response should take longer than 1.5 minutes, so we want to disqualify respondents who complete the survey in less than 1.5 minutes. When we disqualify a respondent, we want to redirect them to the page https://mywebsite.com/disqualified?customId={{metadata.customId}}
, where customId
is a custom identifier passed via metadata.
- Go to the last question and click to add a new action.
- Select the "Redirect to a webpage" action. Unselect "Submit answers before redirect." Enter the page link
https://mywebsite.com/disqualified?customId={{metadata.customId}}
. We will passcustomId
via dynamic variables into the redirect link{{metadata.customId}}
. - Implement a conditional script to check the time spent on the survey. Switch to script condition mode and add the following script:
const MINIMUM_TIME = 1.5 * 60 * 1000; // Redirect if time spent on screen is less than 1.5 minutes if (dynamicVariables.utility.totalOnScreenTime && dynamicVariables.utility.totalOnScreenTime < MINIMUM_TIME) { return true; } return false;
- Save the action and publish the changes.
- Let's test it! If we go to our survey and pass the query parameter
?customId=1234abc
, then fill out the survey quickly, we should be redirected to our disqualify page with the customId passed.
Updated 5 months ago