Overview
You can use Liquid in a Mobile Commons message to calculate a subscriber's total score based on their answers to multiple questions. By adding the values saved in custom fields, you can determine the subscriber's score and send a personalized result based on the total.
This guide explains how to set up the custom fields, calculate the score, and use conditional logic to return the appropriate result.
1. Set up the quiz questions
For each multiple-choice question, assign a numerical value to each answer.
For example, if each question has four possible answers:
| Answer | Point value |
|---|---|
| A | 1 |
| B | 2 |
| C | 3 |
| D | 4 |
Under Message Response, use Save response as to save each answer to its own custom field.
For a quiz with four questions, you could use the following field names:
question1question2question3question4
If you create these custom fields during setup, select the Text data type.
In the answer configuration, set each choice's standard value to its corresponding numerical value. This allows subscribers to reply with a letter while Mobile Commons saves the numerical value to the custom field.
We also recommend enabling Require a valid response so the question is sent again if a subscriber provides an unexpected answer.
2. Calculate the total score using Liquid
Once the answers have been saved, use Liquid in the final message to add the custom field values and determine the subscriber's result.
For example, suppose the quiz uses the following score ranges:
| Total score | Result |
|---|---|
| 4 | Result A |
| 5–8 | Result B |
| 9–12 | Result C |
| 13 or higher | Result D |
Use the following Liquid code in the final message:
{% assign score = question1 | plus: 0 | plus: question2 | plus: question3 | plus: question4 %}{% if score <= 4 %}Your result is A! [Add result message]{% elsif score <= 8 %}Your result is B! [Add result message]{% elsif score <= 12 %}Your result is C! [Add result message]{% else %}Your result is D! [Add result message]{% endif %}
Replace the example result messages with the appropriate text for your quiz.
How the code works
Calculate the total: The
assignstatement adds the values stored in all four custom fields.Convert the first value to a number: The
plus: 0filter ensures the first field is treated as a number for the addition.Determine the result: The
if,elsif, andelsestatements compare the total against the defined score ranges and select the corresponding message.
The example ranges assume that each question has a minimum value of 1 and a maximum value of 4. Adjust the conditions to match your quiz's scoring rules.
3. Test the Liquid code
Before sending the quiz to subscribers, verify that the field names and score ranges work as expected.
For the example above, test the following scores:
| Test score | Expected result |
|---|---|
| 4 | Result A |
| 5 | Result B |
| 8 | Result B |
| 9 | Result C |
| 12 | Result C |
| 13 | Result D |
| 16 | Result D |
You can use the Liquid tester to validate the logic and identify any invalid field names.
4. Test the complete quiz with a real phone
After validating the Liquid code, test the full quiz flow using a small test group that includes your own phone number.
Schedule the test broadcast for a minute or two in the future.
Complete all the questions, trying a mix of letter and numerical replies.
Check the subscriber's profile to confirm that each custom field contains the expected numerical value.
Confirm that the final message displays the correct result based on the total score.
Note: The Send Test feature sends every message in the conversation at once. Scheduling a test broadcast is a better way to verify the complete question-and-answer flow.
Troubleshooting tips
Invalid field error: Confirm that all custom fields exist and that their names match the Liquid code exactly, including capitalization.
Incorrect score: Check that each answer's standard value is configured correctly and that the custom fields contain the expected values.
Unexpected result: Verify that the conditions in the Liquid code match your intended score ranges.
Missing answers: An empty custom field may be treated as zero during addition. Ensure subscribers complete all required questions before the final message is sent so their score reflects all answers.
Summary
To calculate a quiz score in Mobile Commons, save each answer's numerical value to a separate custom field, add the fields using Liquid, and use conditional statements to select the appropriate result message. Testing the score boundaries and the complete quiz flow helps ensure subscribers receive the correct result.