Product Segments
Dynamic product collections defined by layered rules. Product segments let tenants create named groups of products that automatically update when new products are ingested or when segment rules change.
Product segments are the product-side counterpart of user segments (Module 1). They use the same DSL, compute infrastructure, and visual builder — adapted for products instead of users.
Architecture#
Key difference from user segments: Product segment DSL compiler returns WHERE clause strings (not (select, having) tuples). No GROUP BY/HAVING needed because product attributes are direct properties, not event aggregates.
DSL Format#
Condition Types#
product_attribute#
Filters products via JSONExtract* functions on the data JSON column in ClickHouse.
| Field | Type | Required | Description |
|---|---|---|---|
property | string | yes | Schema field name (from ProductSchema.fields) |
operator | string | yes | See operator table below |
value | any | depends | The comparison value |
value2 | number | for between/not_between | Upper bound |
value_type | string | no | string, number, boolean, array, url, price |
Operators by value_type:
| value_type | Available operators |
|---|---|
string | eq, neq, contains, does_not_contain, starts_with, ends_with, in, not_in, is_set, is_not_set |
number, price | eq, neq, gt, gte, lt, lte, between, not_between, in, not_in, is_set, is_not_set |
boolean | is_true, is_false |
array | contains, does_not_contain, is_set (not empty), is_not_set (empty) |
url | eq, is_set, is_not_set |
user_behavior_signal#
Matches products that appear in behavioral events from users in a given user segment.
| Field | Type | Required | Description |
|---|---|---|---|
signal | string | yes | purchased_by_segment or viewed_by_segment |
segment_id | string | yes | ID of a user segment (Module 1) |
event_type | string | no | Defaults to order_completed / product_viewed |
product_id_property | string | no | Event payload field holding the product ID. Default: product_id |
Compiled to a subquery:
ai_visibility_metric#
Stub condition type. Always compiles to 1=0 (returns no matches) until M14 (AI Visibility Tracking) is live.
Models#
ProductSegment (apps/product_segments/models.py)#
| Field | Type | Description |
|---|---|---|
tenant | FK | Owning tenant |
name | string | Unique per tenant |
description | text | Optional |
definition | JSONField | DSL object (logic + conditions) |
is_active | bool | Active segments auto-recompute every 60s |
computed_count | int? | Number of matched products after last compute |
last_computed_at | datetime? | Timestamp of last successful compute |
refresh_interval | int | Seconds between recomputes (default 60) |
ClickHouse Table#
Compute Pipeline#
compute_product_segment_memberships(product_segment_id)Celery task- Loads
ProductSegment+ injectstenant_id/tenant_sluginto definition build_product_preview_query()compiles definition → SQL + parameters- Executes on ClickHouse → list of matching SKUs
- Inserts rows to
product_memberships - Updates
segment.computed_countandsegment.last_computed_atin Postgres
Scheduled: schedule_product_segment_recomputation fires every 60s (Celery beat), enqueues tasks for all active segments due for refresh.
UI#
/pim/segments— List all product segments with computed count, status, last computed time, inline Recompute button/pim/segments/new— Builder in create mode; redirects to/pim/segments/[id]after save/pim/segments/[id]— Builder in edit mode + members table showing matched products with schema fields
Integration with Other Modules#
- M13 (PIM): Reads
ProductSchema.fieldsto populate the attribute picker in the rule builder - M1 (User Segments):
user_behavior_signalconditions reference user segment IDs; membership data comes fromsegmentation.segment_memberships - M14 (AI Visibility, upcoming):
ai_visibility_metricconditions activate when M14 is live - M10 (Copilot):
list_product_segmentsandget_product_segment_detailtools expose segment data to the AI copilot
Code Paths#
| Concern | Path |
|---|---|
| DSL compiler | apps/api/apps/product_segments/preview.py |
| Celery tasks | apps/api/apps/product_segments/tasks.py |
| API views | apps/api/apps/product_segments/views.py |
| Frontend builder | apps/web/app/(app)/pim/segments/builder.tsx |
| Frontend list | apps/web/app/(app)/pim/segments/page.tsx |
| Frontend detail | apps/web/app/(app)/pim/segments/[id]/page.tsx |