Expressions

Introduction to expressions:

When setting up conditions and effects for rules in Campaigns, you may find that some situations are not covered by premade values. However, you are not limited to using these values. In conditions, you can find a condition called "Expressions."
You can set the value of such an expression. It can be set as a combination of attributes and operators using the meta language created by Open Loyalty. It allows you to create a fully customizable condition that meets the rules of your planned loyalty program. Once you get to know it, it may be faster to set rules for a campaign using it.
For example, you can create a campaign that is triggered when a customer buys a product from a certain category. You can do it in two ways:
Set a premade condition:
Or use an expression:
In this document, you will find a description of the meta language together with all the supported syntaxes, which is based on the expression syntax of Symfony Expression.

The component supports:

  • strings - single and double quotes (e.g. 'hello')
  • numbers - integers (e.g. 103), decimals (e.g. 9.95), decimals without leading zeros (e.g. .99, equivalent to 0.99); all numbers support optional underscores as separators to improve readability (e.g. 1_000_000, 3.14159_26535)
  • arrays - using JSON-like notation (e.g. [1, 2])
  • hashes - using JSON-like notation (e.g. { foo: 'bar' })
  • booleans - true and false
  • null - null
  • exponential - also known as scientific (e.g. 1.99E+3 or 1e-2)

Meta language attributes and operators:

Below you can find a list of all the meta language objects with supported triggers and operators you can use while configuring your Campaign in Open Loyalty.
Context object
Transaction
Internal event
Custom event
customer.firstName
customer.lastName
customer.email
customer.phone
customer.birthDate
customer.address.street
customer.address.address1
customer.address.address2
customer.address.city
customer.address.postal
customer.address.province
customer.address.country
customer.loyaltyCardNumber
customer.legalConsent
customer.marketingConsent
customer.dataProcessingConsent
customer.gender
customer.registeredDate
customer.level
customer.firstTransactionDate
customer.lastTransactionDate
customer.levelAchievementDate
customer.numberOfPurchases
customer.averagePurchaseAmount
customer.availablePoints
customer.usedPoints
customer.totalEarnedPoints
customer.lockedPoints
customer.blockedPoints
customer.expiredPoints
customer.labels
transaction.grossValue
transaction.documentNumber
transaction.purchasedAt
transaction.purchasePlace
transaction.shippingCity
transaction.sku(‘SKU123’).qty
transaction.sku(‘SKU123’).grossValue
transaction.category(‘shoes’).qty
transaction.category(‘shoes’).grossValue
transaction.maker(‘Nike’).qty
transaction.maker(‘Nike’).grossValue
transaction.itemLabel(‘SKU123’).qty
transaction.itemLabel(‘SKU123’).grossValue
transaction.itemLabel(‘SKU123’, ‘Value’).qty
transaction.itemLabel(‘SKU123’, ‘Value’).grossValue
transaction.itemLabels
transaction.labels
transaction.qty
transaction.channelId
event.body.sampleatributename
Operators

Examples:

From the list of Condition type choose "Expression"
Then based on the meta language, you can build custom expression conditions. You can find some examples below:
Type
Example
Has label
{
"operator": "expression",
"data": "agg(transaction.labels).matchLabel('k1')"
}
{
"operator": "expression",
"data": "agg(customer.labels).matchLabel('k1', 'v1')"
}
Has item label
{
"operator": "expression",
"data": "transaction.itemLabel('k1').qty > 2"
}
Has item label with value
{
"operator": "expression",
"data": "transaction.itemLabel('k1', 'v1').qty > 2"
}
Sum by
{
"operator": "expression",
"data": "agg(transaction.items).sumBy('qty') > 2"
}
Transaction item by
{
"operator": "expression",
"data": "transaction.maker('Company').qty > 0
}
{
"operator": "expression",
"data": "transaction.category('C1').qty > 0
}
{
"operator": "expression",
"data": "transaction.sku('AB1122').qty > 0
}
Excluding users from campaigns
There are pre-made conditions that let you target campaigns to a specific group of users - different tiers, members’ segments, or members with set custom attributes.
However, there is no predefined condition to exclude a group of users from the campaign. For example, you have a custom attribute called Debt, with two possible values set: true or false. Let it be that you want to set a campaign for all users, but not for those who are in debt with your app. The best way to do so is to create a condition using an expression. How to do it? To include a customer with a specific custom value, you would use an expression that looks like this: agg(customer.labels).matchLabel('debt', 'true') To exclude those users, you have to set the opposite of it, which you can accomplish by adding “!” at the beginning of it: !agg(customer.labels).matchLabel('debt', 'true')
This will allow you to create a campaign that affects all users except for those with the custom attribute Debt equal true.
Campaigns based on passing time
One way of keeping the customers engaged with the loyalty program is to occasionally reward them for taking some actions. The way to do it in Open Loyalty is to use an expression timestamp, which allows you to measure how much time has passed between certain events.
For example, you want to reward a user for purchasing items in your store in the first week after registering.
To do so, you can create the following expression:
timestamp(transaction.purchasedAt) - timestamp(customer.registeredDate) <= 86400 * 7
The first part of the expression:
timestamp(transaction.purchasedAt) - timestamp(customer.registeredDate)
This means that you want to define the time passed between those two events: transaction and registration. In the second part of the expression, you define how much time passed in seconds. 86400 seconds is the number of seconds in a day, so <= 86400 * 7 means that less than 7 days passed between two events.
Birthday campaigns for members that registered on their birthday
Normally, birthday campaigns do not trigger if a member is registered on his birthday. There is a way to create such a campaign using the following expression in conditions:
customer.birthDate.format("d-m") === event.eventDate.format("d-m")
This way, a reward will be given to the members who registered on their birthday. To give rewards to other members on their birthdays, you still have to create a regular birthday campaign.
Copy link
On this page
Introduction to expressions:
The component supports:
Meta language attributes and operators:
Examples: