Conditions and segments

Conditions and segments

Conditions are typed expressions over profile fields, event payloads and the event log. Segments name a condition so it can be reused, measured and used as a trigger.

A condition is a question about a user: does their plan equal 'pro', did they purchase in the last 30 days, can they be reached by push. Conditions decide who enters a workflow, which branch they take, what a journey waits for and when it counts as a success. They are written as free functions (eq, gt, performed, and, ...) composed into an expression. Like the rest of the DSL, an expression is data: it compiles to a condition tree in the IR, which the engine evaluates and Studio can display.

ts
import { and, eq, gte, not, performed } from 'sendwend';

and(
  eq(u.plan, 'pro'),
  not(performed(e.purchase, (p) => gte(p.value, 100), { within: '30 days' }))
);

Where conditions are used

PlaceWhat it decides
A trigger's payload whereWhich occurrences of the event start the journey
A trigger's filterWhich users may enter
.branch([condition, flow])Which arm runs
.filter(condition)Whether the journey continues
.waitUntil(condition, ...)What the journey waits for
goal, exitWhenWhen the journey converts or ends
segment({ condition })Who is in a named audience

Operators

FunctionMeaning
eq, neEqual / not equal
gt, gte, lt, lteOrdered comparison (string or number fields)
between, notBetweenInclusive range (string or number fields)
inArray, notInArrayMembership in a list of values
exists, notExistsThe property is present / absent
contains, notContainsSubstring (string fields)
performed(e.x, where?, { within?, count? })Did the event, optionally matching, in a window, at least N times
reachable(channel)Push: has a registered device; other channels: has the address
and, or, notCombinators; nest freely
segment({ id, condition, name?, description? })A named condition: reusable, swept, usable as a trigger

"Did not do" is not(performed(...)); there is no separate notPerformed. Custom conditions written as functions do not exist, on purpose: routing stays declarative so a canvas can always explain why a user took a path. If an action needs to influence routing, it writes a property or emits an event, and the condition reads that.

Types come from the reference

Predicates take a reference as their first argument, and the reference's type decides what the rest of the call accepts:

  • A profile reference, u.plan, typed by the contract's profile.
  • Inside a where, a payload reference: (p) => gt(p.value, 100), typed by that event's payload.
  • A field of the triggering event, checkout.payload.value, valid only in the workflow that trigger starts.

Values are typed by the reference only. With platform: enum(['ios', 'android', 'web']) in the profile, eq(u.platform, 'ios ') does not compile, because the value cannot widen the type to fit a typo. Operator applicability is part of the signature too: gt accepts only string and number fields, contains only string ones, so contains(u.age, '3') is a type error.

Where each kind of reference may appear is checked when the bundle compiles. A payload reference outside a where, or a trigger payload reference in a segment or a trigger filter, is refused with the place named.

performed()

performed asks the event log. In its full form:

ts
performed(e.login);
performed(e.login, (p) =>
  and(eq(p.platform, 'web'), eq(p.tags.utm_source, 'meta'))
);
performed(e.purchase, (p) => gt(p.value, 100), { within: '30 days', count: 2 });
performed(e.purchase, { within: '30 days' }); // options without a where
  • The optional where narrows which occurrences count, using the ordinary predicates over the typed payload, nested fields included.
  • within counts only occurrences that recent. It must be a positive duration.
  • count asks for at least that many occurrences, a positive integer (1 by default).

Where it is evaluated decides where its count starts:

InsideCounts events fromWhy
goalthe moment the user entereda conversion must not be satisfied by earlier history
waitUntilthe moment the wait begana wake must be something that happened while waiting
anywhere elsethe user's full history, bounded by withinbranches, filters, exitWhen and segments ask about the user as a whole

Because every count and window reads the event log, a duplicated event silently changes what a journey decides. That is why each event should carry an id the producer can reproduce; see Delivery.

To react to the event that started the journey, read the trigger's payload. To react to an event that arrives during the journey, use performed() in a waitUntil or a branch.

reachable()

reachable(channel) asks whether a message on that channel could reach the user right now:

  • reachable('push'): the user has at least one registered device and has not opted out of all push.
  • reachable('email'): the profile has an email and the user has not opted out of all email. A user who opted out of one topic is still reachable, so exitWhen: not(reachable('email')) fires on "unsubscribed from everything", not on a topic.
  • SMS checks the phone property. In-app, Slack and Discord need no address and are reachable unless the channel is opted out.

Inside a waitUntil, reachable wakes the wait when a device is registered or removed, when the profile changes, and when the channel-wide opt-out changes. .prefer() is a branch on reachable for one push and one email template.

Segments

segment() names a condition and makes it a top-level asset:

ts
import { and, not, performed, segment } from 'sendwend';

export const dormant7d = segment({
  id: 'dormant_7d',
  name: 'Dormant (7d)',
  description: 'Logged in before, but not in the last 7 days.',
  condition: and(
    performed(e.login),
    not(performed(e.login, { within: '7 days' }))
  ),
});

export const purchaser = segment({
  id: 'purchaser',
  condition: performed(e.purchase, { within: '30 days' }),
});

A segment is a Condition, so it goes anywhere a condition does (branch, filter, a trigger's filter, and(...)). What naming it adds:

  • It is referenced by id in the IR, and its definition is stored once. A segment can be built on other segments; a cycle is refused.
  • Its membership is kept. The host maintains each segment's members and size, so Studio can show how many users it holds.
  • It can start a journey with trigger.enters(segment).
  • It is a live audience. A workflow that asks about a segment asks under the condition deployed now, so editing a segment changes every journey that refers to it, including those already running. See Versioning.

id is wire identity: membership is stored under it, so rename name instead. name and description are metadata, outside the hash. Every segment a workflow references must be listed in the config's segments, or the compile fails.

Segment or const

For reuse inside your own code, a plain const is enough:

ts
const bigSpender = performed(e.purchase, (p) => gte(p.value, 500), { within: '90 days' });

workflow({ ... }).branch([bigSpender, (w) => w.email(email.vip_offer)]);

The difference is what the engine knows. An anonymous const is inlined into each workflow that uses it: no id, no membership, no size, and it cannot be a trigger. Make it a segment() when you want the audience to be visible, measured or able to start a journey. A one-off check is just an inline expression.

Entering a segment is not being in it

trigger.enters(s) looks forward. Whoever already matches s when the trigger is first deployed (or when s's condition changes, or the condition of a segment it is built on) is recorded as a member and starts nothing. They did not enter; they were already there.

The sweep after the deploy does that recording, in a pass over every user: one sweep for most audiences, several for a large one. From the moment the pass completes, a user who newly matches enters. Until then, a user who matches through their own activity is recorded the same way.

So a deploy never starts journeys in bulk, and the outcome does not depend on whether the segment happened to exist before its trigger did. Without this rule, shipping a "dormant for 7 days" segment with its winback workflow would email every dormant user you have at the next sweep.

A segment named in a condition is a different question and has no such rule. A trigger's filter, a branch or a waitUntil on s asks each user whether they match now, long-standing members included:

ts
trigger.enters(dormant7d); // whoever goes quiet from here on
trigger.event(e.session_start, { filter: purchaser }); // every purchaser, at their next session
trigger.date('2026-11-27 09:00:00', { filter: purchaser }); // every purchaser, at that time

To reach everyone who is in a segment today, name it in a filter.

How membership is kept

Membership is re-evaluated in two ways. On each event or profile update for a user, their segment membership is re-evaluated. And the sweep, run by the host's cron (POST /segments/sweep on demand), catches what the clock alone changes: a user becomes "dormant for 7 days" without doing anything.

The sweep does not scan every user each time. For each page it runs one SQL statement, compiled from the segments' conditions, to find who may have changed, and then decides each of them with the same evaluator journeys use, so its cost follows what changed, not the size of the user base. It spends a bounded time per call (about 30 seconds on the segments) and keeps its place in the store, so the next sweep carries on where the last stopped.

Segments are about users: visitors who have not signed in are not swept and do not enter segment triggers.

  • Workflows: how triggers use where, filter and segments.
  • Steps: waits, branches and filters.
  • Re-entry: what counts as a fresh qualification for a segment trigger.