Skip to main content

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:

FieldRequiredMeaning
code, nameyescode is lowercase letters, digits and hyphens, and can't change later.
categoryyesWhen 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.
eventTypeCodeyesThe event type the rule listens to. It needs an event type configuration.
programIdsyesThe programs the rule belongs to. A customer must be enrolled in one of them.
eventExpressionyesThe condition. { "true": {} } matches every event of the type.
eventActionsExpressionyesThe actions, hung on a node. { "true": { "actions": [...] } } runs them whenever the rule applies.
processItemsIndividuallynotrue runs the rule once per line item; see Per-item rules. Defaults to false.
descriptionno{ "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:

PathWhat 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.
$.eventDescriptionThe 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" } }
]
}
}
OperatorFieldsMatches when
truenonealways
equals, notEqualspath, valuethe value at path is (or isn't) value
greaterThan, greaterThanEquals, lessThan, lessThanEqualspath, value (a number)the number at path compares to value
betweenpath, lowerBound, upperBound, includeLowerBound, includeUpperBoundthe number at path is in the range
in, notInpath, values (array)the value at path is (or isn't) one of values
containsAny, containsAllpath, values (array)the array at path holds any (or all) of values
startsWith, endsWith, matchespath, value, ignoreCasethe string at path starts with, ends with, or matches the regex
exists, notExistspaththe field is present (or absent)
empty, notEmptypaththe string or array at path is empty (or not)
currentTiertierCode, revalidateTierthe customer holds the tier
hasBadgebadgeId, revalidateBadgethe customer holds the badge
hasProgramCurrencyprogramCurrencyCode, comparisonAmountthe customer's balance is at least comparisonAmount
targetCompletedtargetId, revalidateTargetthe 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.

ActionDoesKey fields
issueProgramCurrencyAwards currencyprogramId or programCode, programCurrencyCode, then a fixed amount or an amount read from a path (below)
withdrawProgramCurrencyTakes currency backsame as issueProgramCurrency
issueCustomerTierGives a tiertierCode, issueLifetimeTier, issueTrialTier
issueCustomerBadgeGives a badgebadgeCode
issueCustomerCouponIssues a coupon from an offercouponOfferCode
useCustomerCoupon, holdCustomerCoupon, returnCustomerCoupon, cancelCustomerCouponChanges a coupon's statecouponCode or couponId
registerTarget, completeTargetDataCounts toward or completes a targettargetCode
addCustomerToAudience, removeCustomerFromAudienceChanges audience membershipcustomerAudienceCode
sendNotificationSends a member messagechannelCode, templateCode

How much currency​

issueProgramCurrency and withdrawProgramCurrency take their amount one of two ways:

  • A fixed amount: "fixedAmount": true and "defaultResult": 10 awards 10 every time.
  • An amount from the event: "path": "$.eventData.amount" awards the value at the path. defaultResult is 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-operationFields
add, subtract, multiply, divide, modulooperand (a number) or operandPath (a path), not both
length, sizenone: the length of a string or an array
subStringbeginIndex, endIndex
matchesCurrentDatetimeZone, 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.