Audience expression editor
Learn how to build audiences in the Loyalty Engine using the expression editor, based on complex criteria across a range of data points.
Using the expression editor to build audiences gives you greater flexibility than the query builder, allowing you to define complex criteria across a wide range of data points.
It supports advanced logic, including grouping conditions in custom ways and referencing calculated values across multiple events — for example, total spend over a specific time period.
Background data structure
The expression editor evaluates a JSON payload containing multiple data points related to the user. When an audience is refreshed, the Loyalty Engine checks each user against the defined criteria — if they match, they are added to the audience.
This payload includes seven arrays, each representing a different category of user data.
Writing expressions
The expression editor allows you to write JSONata expressions to query user data with advanced logic. You can learn more about JSONata here.
Each expression must return a true
or false
value. If the expression evaluates to true
, the user will join the audience. If it evaluates to false
, they will not.
You can find an example of the data structure and a sample expression here. You can adjust the data and expression to see how it affects the result.
Date criteria: within a specified time period
To check whether an event occurred within a certain number of days, calculate the time since the event and compare it to the time window you’re interested in.
The expression has two parts:
(activityData[$toMillis($now()) - $toMillis(reportedAtISOString)
Calculates how long ago the event occurred, in milliseconds.($days * 24 * 60 * 60 * 1000)
Converts your desired time period (in days) into milliseconds.
By comparing these values, you can determine if the event occurred within the specified time frame.
$exists(activityData[$toMillis($now()) - $toMillis(reportedAtISOString) < (30 * 24 * 60 * 60 * 1000)])
This expression checks whether the user has any events reported in the past 30 days. You can combine it with additional conditions (e.g. specific event types or payload values) to refine your criteria.
Date criteria: before or after a specific date
If you want to check whether an event occurred before or after a certain date, you can compare the ISO date string directly using comparison operators.
$exists(activityData[reportedAtISOString < "2025-01-01T00:00:00Z"])
This expression checks whether the user has any events reported before midnight on January 1st, 2025.
You can use the following comparison operators: =
, <
, >
, <=
and >=
.
As with the previous method, this can be combined with other conditions to filter for specific event types or payload values.
Last updated
Was this helpful?