Rule Expressions
An event rule is two expression trees on one event type. eventExpression decides whether the rule applies to an event. eventActionsExpression decides what it does: each node can carry actions, and a node's actions run when that node matches. The same grammar drives audiences and flow conditions.
Every node is a JSON object with exactly one key, the operator, whose value holds the operator's fields:
{ "greaterThan": { "path": "$.eventData.amount", "value": 0 } }
This page covers the operators, actions and pre-operations most rules use. The Expression, ExpressionAction and PreOperation schemas in the API Reference list every one.
The rule body
POST /event-rules takes:
| Field | Required | Meaning |
|---|---|---|
code, name | yes | code is lowercase letters, digits and hyphens, and can't change later. |
category | yes | When the rule runs relative to others: BASE, TIER, PROMOTION, TIER_AFTER_PROMOTION, ADJUSTMENT, REDEMPTION, COMMUNICATION, PARTNER, ORGANIZATION_DEFINED. A base earn rule is BASE; a bonus on top of it is PROMOTION. |
eventTypeCode | yes | The event type the rule listens to. It needs an event type configuration. |
programIds | yes | The programs the rule belongs to. A customer must be enrolled in one of them. |
eventExpression | yes | The condition. { "true": {} } matches every event of the type. |
eventActionsExpression | yes | The actions, hung on a node. { "true": { "actions": [...] } } runs them whenever the rule applies. |
processItemsIndividually | no | true runs the rule once per line item; see Per-item rules. Defaults to false. |
description | no | { "en": "..." }. |
A new rule is published as soon as it's saved. Every path, code and id it names is checked then: a rule that reads a field the event type doesn't declare, or names a currency, tier or badge that doesn't exist, is refused with a message listing the valid values. Change a published rule by creating a new version with POST /event-rules/{id}/versions.
Paths
Conditions and actions read values with a JSONPath that starts at one of these roots:
| Path | What it reads |
|---|---|
$.eventData.<field> | A field of the event. The fields an event can carry are the ones its event type's data model declares, and you send them at the top level of the POST /events body. $.eventData.amount reads "amount": 42.5. |
$.eventDescription | The event's description. |
$.eventLineItem.<field> | The line item being scored, in a rule with processItemsIndividually: true. |
$.eventTarget.<field> | The customer the event is for. |
$.currentTier.<field> | The customer's current tier. |
$.programCurrencies.<code> | The customer's balance of a currency. |
$.targets.<field> | The customer's progress on targets. |
Conditions
Combine conditions with and, or and not, each taking a children array:
{
"and": {
"children": [
{ "greaterThanEquals": { "path": "$.eventData.amount", "value": 20 } },
{ "equals": { "path": "$.eventData.channel", "value": "online" } }
]
}
}
| Operator | Fields | Matches when |
|---|---|---|
true | none | always |
equals, notEquals | path, value | the value at path is (or isn't) value |
greaterThan, greaterThanEquals, lessThan, lessThanEquals | path, value (a number) | the number at path compares to value |
between | path, lowerBound, upperBound, includeLowerBound, includeUpperBound | the number at path is in the range |
in, notIn | path, values (array) | the value at path is (or isn't) one of values |
containsAny, containsAll | path, values (array) | the array at path holds any (or all) of values |
startsWith, endsWith, matches | path, value, ignoreCase | the string at path starts with, ends with, or matches the regex |
exists, notExists | path | the field is present (or absent) |
empty, notEmpty | path | the string or array at path is empty (or not) |
currentTier | tierCode, revalidateTier | the customer holds the tier |
hasBadge | badgeId, revalidateBadge | the customer holds the badge |
hasProgramCurrency | programCurrencyCode, comparisonAmount | the customer's balance is at least comparisonAmount |
targetCompleted | targetId, revalidateTarget | the customer has completed the target |
A comparison on a field the event doesn't carry is false, so a rule that reads an optional field simply doesn't match an event without it. A leaf can transform the value before comparing with preOperation (see below).
Actions
Actions sit in an actions array on any node. When the node matches, each action runs in order.
| Action | Does | Key fields |
|---|---|---|
issueProgramCurrency | Awards currency | programId or programCode, programCurrencyCode, then a fixed amount or an amount read from a path (below) |
withdrawProgramCurrency | Takes currency back | same as issueProgramCurrency |
issueCustomerTier | Gives a tier | tierCode, issueLifetimeTier, issueTrialTier |
issueCustomerBadge | Gives a badge | badgeCode |
issueCustomerCoupon | Issues a coupon from an offer | couponOfferCode |
useCustomerCoupon, holdCustomerCoupon, returnCustomerCoupon, cancelCustomerCoupon | Changes a coupon's state | couponCode or couponId |
registerTarget, completeTargetData | Counts toward or completes a target | targetCode |
addCustomerToAudience, removeCustomerFromAudience | Changes audience membership | customerAudienceCode |
sendNotification | Sends a member message | channelCode, templateCode |
How much currency
issueProgramCurrency and withdrawProgramCurrency take their amount one of two ways:
- A fixed amount:
"fixedAmount": trueand"defaultResult": 10awards 10 every time. - An amount from the event:
"path": "$.eventData.amount"awards the value at the path.defaultResultis what it awards when the field is missing (0 if you leave it out).
Round a computed amount with decimalPlaces and roundingAlgorithm (ROUND_DOWN, ROUND_UP, ROUND_HALF_UP, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_CEILING, ROUND_FLOOR). Scale it with a preOperation:
| Pre-operation | Fields |
|---|---|
add, subtract, multiply, divide, modulo | operand (a number) or operandPath (a path), not both |
length, size | none: the length of a string or an array |
subString | beginIndex, endIndex |
matchesCurrentDate | timeZone, pattern: compares a date to today |
Examples
These are rules a coffee shop's Cafe Rewards program might run on its purchase event type, whose data model declares amount.
A flat award
10 beans for every purchase, whatever it costs:
{
"code": "visit-bonus",
"name": "10 beans per purchase",
"category": "BASE",
"eventTypeCode": "purchase",
"programIds": ["prog_123"],
"eventExpression": { "true": {} },
"eventActionsExpression": {
"true": {
"actions": [
{
"issueProgramCurrency": {
"programId": "prog_123",
"programCurrencyCode": "beans",
"fixedAmount": true,
"defaultResult": 10
}
}
]
}
}
}
An award based on spend
One bean per dollar, rounded down, on purchases over $0. A $42.50 purchase earns 42:
{
"code": "earn-on-spend",
"name": "1 bean per dollar",
"category": "BASE",
"eventTypeCode": "purchase",
"programIds": ["prog_123"],
"eventExpression": { "greaterThan": { "path": "$.eventData.amount", "value": 0 } },
"eventActionsExpression": {
"true": {
"actions": [
{
"issueProgramCurrency": {
"programId": "prog_123",
"programCurrencyCode": "beans",
"path": "$.eventData.amount",
"decimalPlaces": 0,
"roundingAlgorithm": "ROUND_DOWN"
}
}
]
}
}
}
For 3 beans per dollar, add "preOperation": { "multiply": { "operand": 3 } } to the action.
Per-item rules
With processItemsIndividually: true, the rule runs once for each line item, and $.eventLineItem is the item being scored. This promotion gives 2 beans per dollar spent on coffee beans, and nothing on the rest of the basket:
{
"code": "double-on-beans",
"name": "2 beans per dollar on coffee beans",
"category": "PROMOTION",
"eventTypeCode": "purchase",
"programIds": ["prog_123"],
"processItemsIndividually": true,
"eventExpression": { "equals": { "path": "$.eventLineItem.category", "value": "coffee-beans" } },
"eventActionsExpression": {
"true": {
"actions": [
{
"issueProgramCurrency": {
"programId": "prog_123",
"programCurrencyCode": "beans",
"path": "$.eventLineItem.amount",
"preOperation": { "multiply": { "operand": 2 } },
"decimalPlaces": 0,
"roundingAlgorithm": "ROUND_DOWN"
}
}
]
}
}
}
Send the items in the event's lineItems array:
{
"eventTypeCode": "purchase",
"eventReference": "order-5007",
"description": "Order 5007",
"amount": 30,
"lineItems": [
{
"lineItemReference": "1",
"sku": "BEANS-1",
"category": "coffee-beans",
"quantity": 1,
"unitPrice": 18,
"amount": 18
},
{
"lineItemReference": "2",
"sku": "MUG-1",
"category": "mugs",
"quantity": 1,
"unitPrice": 12,
"amount": 12
}
]
}
With all three rules live, this order earns 30 (the spend rule), 10 (the flat award) and 36 (2 × $18 of beans). A rule that reads $.eventLineItem without processItemsIndividually: true is refused when you save it.
Actions on a condition
Actions can hang on any node, not only the root, so one rule can award different amounts in different cases. This promotion adds 20% on purchases over $100, on top of the base rule. A $200 purchase earns 40 more:
{
"code": "big-basket",
"name": "20% more over $100",
"category": "PROMOTION",
"eventTypeCode": "purchase",
"programIds": ["prog_123"],
"eventExpression": { "true": {} },
"eventActionsExpression": {
"and": {
"children": [{ "greaterThan": { "path": "$.eventData.amount", "value": 100 } }],
"actions": [
{
"issueProgramCurrency": {
"programCode": "cafe-rewards",
"programCurrencyCode": "beans",
"path": "$.eventData.amount",
"preOperation": { "multiply": { "operand": 0.2 } }
}
}
]
}
}
}
A tier bonus works the same way, with { "currentTier": { "tierCode": "gold", "revalidateTier": true } } as the child. A condition node with no actions of its own awards nothing, so put the actions on the and that holds it.
Check a rule before it goes live
- Rule tracing records why each rule matched or failed for an event.
- Cart preview runs an event through your rules without recording anything (
POST /events?simulate=true). - The API reference lists every field of Create an event rule.