Defining Segments
Segments group customers based on rules and attributes. Define segments in your config to create rule-based customer groups that can be used for content targeting, experiments, and analytics.
Segment Interface
interface ApplyConfigSegmentInput {
key: string; // Unique identifier (e.g., 'vip-customers')
name: string; // Display name
description?: string; // What this segment represents
rules?: Record<string, unknown>; // Matching rules
evaluationMode?: string; // 'realtime' or 'batch'
isActive?: boolean; // Whether the segment is evaluating (default: true)
}Basic Example
import { defineConfig, defineSegment } from '@eide/foir-cli/configs';
export default defineConfig({
key: 'my-app',
name: 'My App',
segments: [
defineSegment({
key: 'vip-customers',
name: 'VIP Customers',
description: 'Customers with total spend over $500',
rules: {
type: 'condition',
left: { type: 'field', path: 'totalSpend' },
operator: 'greaterThan',
right: { type: 'literal', value: 500 },
},
isActive: true,
}),
],
});Rules Structure
Segment rules use a condition expression format. Each rule specifies a left operand, an operator, and a right operand.
Simple Condition
rules: {
type: 'condition',
left: { type: 'field', path: 'orderCount' },
operator: 'greaterThan',
right: { type: 'literal', value: 5 },
}Combining Conditions
Use and or or to combine multiple conditions:
rules: {
type: 'and',
conditions: [
{
type: 'condition',
left: { type: 'field', path: 'totalSpend' },
operator: 'greaterThan',
right: { type: 'literal', value: 500 },
},
{
type: 'condition',
left: { type: 'field', path: 'orderCount' },
operator: 'greaterThan',
right: { type: 'literal', value: 5 },
},
],
}Available Operators
| Operator | Description |
|---|---|
equals | Exact match |
notEquals | Does not match |
greaterThan | Greater than a value |
lessThan | Less than a value |
greaterThanOrEqual | Greater than or equal |
lessThanOrEqual | Less than or equal |
contains | String contains value |
startsWith | String starts with value |
in | Value is in a list |
notIn | Value is not in a list |
Evaluation Modes
| Mode | Description | Use Case |
|---|---|---|
realtime | Evaluated on every content request | Targeting rules that need instant accuracy |
batch | Evaluated periodically in the background | Large segments where slight delay is acceptable |
segments: [
{
key: 'high-value',
name: 'High Value',
evaluationMode: 'realtime',
rules: { ... },
},
{
key: 'inactive-users',
name: 'Inactive Users',
evaluationMode: 'batch',
rules: { ... },
},
]Examples
Mobile Users
defineSegment({
key: 'mobile-users',
name: 'Mobile Users',
description: 'Customers who primarily use mobile devices',
rules: {
type: 'condition',
left: { type: 'field', path: 'primaryDevice' },
operator: 'equals',
right: { type: 'literal', value: 'mobile' },
},
evaluationMode: 'realtime',
isActive: true,
})High-Spend Repeat Buyers
defineSegment({
key: 'high-spend-repeat',
name: 'High-Spend Repeat Buyers',
description: 'Customers with 10+ orders and over $1000 total spend',
rules: {
type: 'and',
conditions: [
{
type: 'condition',
left: { type: 'field', path: 'totalSpend' },
operator: 'greaterThan',
right: { type: 'literal', value: 1000 },
},
{
type: 'condition',
left: { type: 'field', path: 'orderCount' },
operator: 'greaterThanOrEqual',
right: { type: 'literal', value: 10 },
},
],
},
isActive: true,
})Regional Segment
defineSegment({
key: 'eu-customers',
name: 'EU Customers',
description: 'Customers in European markets',
rules: {
type: 'condition',
left: { type: 'field', path: 'market' },
operator: 'in',
right: { type: 'literal', value: ['uk', 'de', 'fr', 'es', 'it', 'nl'] },
},
evaluationMode: 'batch',
isActive: true,
})Next Steps
- Segments Concepts — How segments work, evaluation, and membership
- Targeting — Use segments in variant targeting rules
- Configuration Reference — Full config API reference
Last updated on