Writing custom code to calculate new values based on existing data
Computed Fields are a core feature of Embeddables, allowing you to calculate new values based on existing data.They take in inputs, from the User Data JSON object, and return an output.This output is then part of the User Data JSON object, and can be used elsewhere in the Embeddable. For example, it can be displayed in a text component, used in a condition, or even used as an input for another Computed Field.Computed Fields have access to the User Data JSON object, a series of helper functions like setUserData and triggerAction, and extra context about how the Computed Field was triggered.
Go to the Logic sidebar and click on the Computed Fields tab.
Click + Add New Computed Field.
Give it a key to be used for the output value in the User Data JSON object, like full_name.
Hit Add.
2
Write your custom code
Computed Fields must start with a function called result().
You can write any custom JS code inside the result() function.
The first argument of result() is the User Data, and the other arguments
are described here.
The code will execute on the frontend, in the main window environment,
so has access to window functions and variables.
Computed Fields are executed client-side, in the main window environment.This means that they have access to the main window’s functions and variables, such as window.location, document, and localStorage.
Here’s an example of a Computed Field called full_name, that takes the user’s first and last name and concatenates them together.
Copy
Ask AI
// All Computed Fields must contain a function called result()function result(userData, helperFunctions, triggerContext) { // Get the user's first and last name from the User Data JSON object, // and join them together with a space in the middle return `${userData.first_name} ${userData.last_name}`;}
Here’s an example of a Computed Field called bmi, that takes the user’s weight and height and calculates their BMI.
Copy
Ask AI
// All Computed Fields must contain a function called result()function result(userData, helperFunctions, triggerContext) { // Get the user's weight and height from the User Data JSON object // In this example, we're assuming the weight is in kilograms and the height is in centimeters const weight = userData.weight; const height = userData.height; const bmi = weight / (height * height); // Return the BMI value return bmi;}
Here’s an example of a Computed Field called is_over_18, that takes the user’s date of birth and returns a boolean value indicating whether they are over 18.
Copy
Ask AI
// All Computed Fields must contain a function called result()function result(userData, helperFunctions, triggerContext) { // Get the user's date of birth from the User Data JSON object // Assumes the date of birth is stored as a 'YYYY-MM-DD' string, e.g. '1990-01-01' const dob = userData.date_of_birth; // Calculate the date exactly 18 years ago const eighteenYearsAgo = new Date(); eighteenYearsAgo.setFullYear(eighteenYearsAgo.getFullYear() - 18); // Return true if the user's date of birth is before 18 years ago, false otherwise return new Date(dob) < eighteenYearsAgo;}
Here’s an example of a Computed Field called is_over_18, that takes the user’s date of birth, entered by the user into month, day and year dropdowns, and returns a boolean value indicating whether they are over 18.
Copy
Ask AI
// All Computed Fields must contain a function called result()function result(userData, helperFunctions, triggerContext) { // Get the user's date of birth from the User Data JSON object const day = userData.dob_day; const month = userData.dob_month; const year = userData.dob_year; // Calculate the date of birth from the day, month and year const dob = new Date(year, month - 1, day); // Calculate the date exactly 18 years ago const eighteenYearsAgo = new Date(); eighteenYearsAgo.setFullYear(eighteenYearsAgo.getFullYear() - 18); // Return true if the user's date of birth is before 18 years ago, false otherwise return dob < eighteenYearsAgo;}