Performing math operations in journeys
Overview
You can use liquid syntax in the update field shapes in your journeys to perform dynamic math operations. This is particularly useful if you want to automatically adjust numerical fields based on specific events or conditions, such as adding reward points, updating scores, or tracking numerical milestones. It can also be useful for updating date fields if you need to extend or reduce a trial period, for example.
Accessing the update field shape
To access the update field shape, go to an existing Journey > Click the + symbol to add a new shape > Actions > Update field.
Supported field types
The following field types are supported for math operations:
- Text
- Number
- Decimal
- Currency
- Date
Supported liquid filters
The following liquid filters are supported for dynamic math operations:
Liquid filters
Plus
The Plus liquid filter allows adding a number to an existing field value. You can use either a static value or the value from another field dynamically.
The liquid syntax for performing a math operation with static values in an update field shape is as follows:
{{ Liquid field id | liquid filter: value }}
EX:
{{ people.custom.points | plus: 1 }}
In this example:
Liquid field id:people.custom.points
Liquid filter:plus
Value:1
This liquid will add 1 to the points custom field.
The liquid syntax for performing a math operation between two fields in an update field shape is as follows:
{{ Liquid id of the first field | liquid filter: Liquid id of the second field }}
EX:
{{ people.custom.total-spent | plus: people.custom.total-tax }}
In this example:
Liquid id of the first field:people.custom.total-spent
Liquid filter:plus
Liquid id of the second field:people.custom.total-tax
This liquid will add the first field to the second field and add the result to the field you're updating in the shape.
TIP: You don’t necessarily have to use the field you're updating in the update field shape for these math operations. It works with any supported fields. For example, you can update Custom Field A with the result of a math operation involving Custom Field B and Custom Field C.
Minus
The Minus liquid filter allows subtracting a number from an existing field value. You can use either a static value or the value from another field dynamically.
The liquid syntax for performing a math operation with static values in an update field shape is as follows:
{{ Liquid field id | liquid filter: value }}
EX:
{{ people.custom.points | minus: 10 }}
In this example:
Liquid field id:people.custom.points
Liquid filter:minus
Value:10
This liquid will subtract 10 from the points custom field.The liquid syntax for performing a math operation between two fields in an update field shape is as follows:
{{ Liquid id of the first field | liquid filter: Liquid id of the second field }}
EX:
{{ people.custom.total-due | minus: people.custom.total-paid }}
In this example:
Liquid id of the first field:people.custom.total-due
Liquid filter:minus
Liquid id of the second field:people.custom.total-paid
This liquid will subtract the first field from the second field and add the result to the field you're updating in the shape.
NOTE: Negative values are supported. If the result of the math operation is a negative value, the field will be updated accordingly.
TIP: You don’t necessarily have to use the field you're updating in the update field shape for these math operations. It works with any supported fields. For example, you can update Custom Field A with the result of a math operation involving Custom Field B and Custom Field C.
Times
The Times liquid filter allows multiplying a number by another number. You can use either a static value or the value from another field dynamically.
The liquid syntax for performing a math operation with static values in an update field shape is as follows:
{{ Liquid field id | liquid filter: value }}
EX:
{{ people.custom.points | times: 2 }}
In this example:
Liquid field id:people.custom.points
Liquid filter:times
Value:2
This liquid will multiply the points custom field by 2.The liquid syntax for performing a math operation between two fields in an update field shape is as follows:
EX:
{{ people.custom.units-sold | times: people.custom.price-per-unit }}
In this example:
Liquid id of the first field:people.custom.units-sold
Liquid filter:times
Liquid id of the second field:people.custom.price-per-unit
This liquid will multiply the first field by the second field and add the result to the field you're updating in the shape.
TIP: You don’t necessarily have to use the field you're updating in the update field shape for these math operations. It works with any supported fields. For example, you can update Custom Field A with the result of a math operation involving Custom Field B and Custom Field C.
Divided_by
The Divided_by liquid filter allows dividing a number by another number. You can use either a static value or the value from another field dynamically.
The liquid syntax for performing a math operation with static values in an update field shape is as follows:
{{ Liquid field id | liquid filter: value }}
EX:
{{ people.custom.points | divided_by: 2 }}
In this example:
Liquid field id:people.custom.points
Liquid filter:divided_by
Value:2
This liquid will divide the points custom field by 2.The liquid syntax for performing a math operation between two fields in an update field shape is as follows:
EX:
{{ people.custom.total-spent | divided_by: people.custom.number-orders }}
In this example:
Liquid id of the first field:people.custom.total-spent
Liquid filter:divided_by
Liquid id of the second field:people.custom.number-orders
This liquid will divide the first field by the second field and add the result to the field you're updating in the shape.
NOTE: The result will always be rounded down to the nearest whole number, with no decimal places for text and number fields. For example, if your custom field is set to 50 and you use divided_by: 1.5, the output will be 33 instead of 33.333333.
TIP: You don’t necessarily have to use the field you're updating in the update field shape for these math operations. It works with any supported fields. For example, you can update Custom Field A with the result of a math operation involving Custom Field B and Custom Field C.
Decimal and currency fields
Ortto treats decimal and currency fields as integers, multiplied by 1,000 in order to maintain accuracy when calculations are done on these fields. This means that a field displayed as 10.00 in the UI represents 10,000 on the backend.
So for math operations involving these fields, you need to multiply the number by 1,000.
EX: Let's say we have a currency field called Total spent:
Total spent currency field is set to $10.00. To update the field to 11.00 we need to provide 1,000 in the liquid syntax:{{ people.custom.total-spent | plus: 1000 }}
To update the field to 20.00, we need to provide 10,000 in the liquid syntax:{{ people.custom.total-spent | plus: 10000 }}
Date fields
It’s also possible to perform math operations with date fields. This is helpful if, for example, you want to add or subtract a specific number of days to or from an existing date.
To do this, you need to:
Declare a variable and assign it the liquid field id. Convert the days you want to add or subtract into seconds.Then use the following liquid syntax:
{% assign varname = liquid field id | date: "%s" %} {{ varname | liquid filter: days in seconds | date: "%Y-%m-%d" }}
EX:
{% assign vartimestamp = people.custom.event-date | date: "%s" %} {{ vartimestamp | plus: 345600 | date: "%Y-%m-%d" }}
In this example:
Variable name:vartimestamp
Liquid field id:people.custom.event-date
Liquid filter:plus
4 days converted to seconds:345600
This liquid will add 4 days to the event date custom field.
NOTE: Variable names must start with the prefix var.