Defining Context Dimensions (Segments)
Segments are context dimensions — named signals in your project’s targeting
context. A segment dimension carries a rule that is evaluated fresh on every
request into segment.<key> (true/false); a model dimension designates a
user-owned model whose record hydrates the user.* targeting namespace.
Define them in your config to manage them as code: a foir push creates and
updates the context dimensions its config declares, and prunes the ones it no
longer declares when the push prunes (see Ownership & Reconcile).
Migrating from
segments:— Segments used to be a separate resource with asegments:block and per-segmentevaluationMode. They are now context dimensions (sourceType: 'segment'), always evaluated in real time. Replace anysegments:/defineSegmentin your config withcontextDimensions:/defineContextDimensionas shown below.
Context Dimension Interface
interface ApplyConfigContextDimensionInput {
key: string; // Unique identifier (e.g., 'vip-users')
name: string; // Display name
description?: string; // What this dimension represents
sourceType: string; // 'segment' (rule-based) | 'model' (a user's own context) | 'request' (a value sent with the read)
evaluationRules?: Record<string, unknown>; // Rule for sourceType 'segment'
sourceModelKey?: string; // The backing model: a user-owned singleton ('model'), or a model of values ('request')
defaultRecordKey?: string; // Optional default record (sourceType 'model')
}Basic Example
import { defineConfig, defineContextDimension } from '@foir/cli/configs';
export default defineConfig({
key: 'my-app',
name: 'My App',
contextDimensions: [
defineContextDimension({
key: 'vip-users',
name: 'VIP Users',
description: 'Users with total spend over $500',
sourceType: 'segment',
evaluationRules: {
type: 'condition',
left: { type: 'context', path: 'user.totalSpend' },
operator: 'greater_than',
right: { type: 'literal', value: 500 },
},
}),
],
});A user matches this segment when the rule evaluates true against the
request’s targeting context. Reference it in variant / content targeting rules
as segment.vip-users.
Rules Structure
evaluationRules uses the same condition-expression format as variant targeting.
Each rule specifies a left operand, an operator, and a right operand.
A segment rule reads the request’s targeting context, so its operands are context operands: user.<field> for the signed-in user’s context record, a dimension key such as device, or segment.<key>. A field operand reads nothing here.
Simple Condition
evaluationRules: {
type: 'condition',
left: { type: 'context', path: 'user.orderCount' },
operator: 'greater_than',
right: { type: 'literal', value: 5 },
}Combining Conditions
Use a group node with logicalOperator set to 'AND' or 'OR' to combine
multiple conditions:
evaluationRules: {
type: 'group',
logicalOperator: 'AND',
conditions: [
{
type: 'condition',
left: { type: 'context', path: 'user.totalSpend' },
operator: 'greater_than',
right: { type: 'literal', value: 500 },
},
{
type: 'condition',
left: { type: 'context', path: 'user.orderCount' },
operator: 'greater_than',
right: { type: 'literal', value: 5 },
},
],
}Segment Chains
A segment rule can reference another segment (segment.gold == true). Segments
are evaluated in topological order, so a dependent segment sees the
already-evaluated result of the segments it references — no extra work required.
Available Operators
| Operator | Description |
|---|---|
equals | Exact match |
not_equals | Does not match |
greater_than | Greater than a value |
greater_than_equals | Greater than or equal |
less_than | Less than a value |
less_than_equals | Less than or equal |
contains | String contains value |
not_contains | String does not contain value |
starts_with | String starts with value |
ends_with | String ends with value |
matches | Left side matches a regular expression |
not_matches | Left side does not match a regular expression |
in | The left side is one of the items in the list on the right |
not_in | The left side is not one of them |
includes | The left side is the list, and it contains the value on the right |
not_includes | The left side is a list that does not contain it |
within_radius | A geopoint on the left is within a radius of the point on the right |
is_empty / is_not_empty | Value is / isn’t empty |
is_null / is_not_null | Value is / isn’t null |
is_true / is_false | Boolean value is true / false |
in and includes are not synonyms, and the difference is which side holds the list.
inasks is this one value somewhere in that list? — the list is the right operand.{ left: country, op: 'in', right: ['UK', 'IE'] }.includesasks does this list contain that value? — the list is the left operand.{ left: tags, op: 'includes', right: 'vip' }.
Get them the wrong way round and the rule does not error; it evaluates to false for everyone, which reads as a segment nobody is in rather than as a mistake.
Ownership & Reconcile
Context dimensions declared in your config are owned by that config (a
config_id is stamped on create). A foir push:
- creates dimensions your config declares that don’t exist yet,
- updates ones it already owns, and
- deletes config-owned dimensions you removed from the config, when the push prunes. An interactive session push prunes; a
FOIR_API_KEYpush (the usual CI setup) only does with--prune, and outside a terminal it needs--allow-deletetoo. See Push and Remove.
A new or changed dimension is a draft until it is released, with foir release publish or foir push --publish.
Dimensions you author in the console (no config) are never touched by a push, and vice-versa — so config-as-code and the UI coexist safely.
User-Context Source (sourceType: 'model')
Designate a user-owned singleton model as the source of the user.*
targeting namespace:
defineContextDimension({
key: 'userContext',
name: 'User Context',
sourceType: 'model',
sourceModelKey: 'user_profile',
})Request Dimension (sourceType: 'request')
A value your application sends in the contexts argument of a read, such as a
market or a city. It names a model, and each record of that model is one value
the dimension can take:
defineContextDimension({
key: 'market',
name: 'Market',
sourceType: 'request',
sourceModelKey: 'market',
})The model’s records must belong to the project as a whole, because their keys
are published in the project’s schema as an enum. A read then passes
contexts: { market: uk }, and a variant rule tests market. See
Targeting & Context.
Examples
Mobile Users
defineContextDimension({
key: 'mobile-users',
name: 'Mobile Users',
sourceType: 'segment',
evaluationRules: {
type: 'condition',
left: { type: 'context', path: 'device' },
operator: 'equals',
right: { type: 'literal', value: 'mobile', valueType: 'string' },
},
})High-Spend Repeat Buyers
defineContextDimension({
key: 'high-spend-repeat',
name: 'High-Spend Repeat Buyers',
sourceType: 'segment',
evaluationRules: {
type: 'group',
logicalOperator: 'AND',
conditions: [
{
type: 'condition',
left: { type: 'context', path: 'user.totalSpend' },
operator: 'greater_than',
right: { type: 'literal', value: 1000 },
},
{
type: 'condition',
left: { type: 'context', path: 'user.orderCount' },
operator: 'greater_than_equals',
right: { type: 'literal', value: 10 },
},
],
},
})Next Steps
- Segments Concepts — how segments/context dimensions evaluate
- Configuration Reference — full config API reference
- Manage dimensions imperatively with
foir context-dimensions(see the CLI commands)