Skip to Content
FeaturesTargeting & Context

Targeting & Context

Targeting rules determine which content variant to show to each visitor. Build rules based on device type, authentication status, geographic market, user segments, and custom context dimensions you define.

Overview

Foir resolves content variants by evaluating targeting rules against request context. When a visitor requests content:

  1. Foir extracts context values (device, market, locale, etc.) from the request
  2. The targeting rules of each variant’s catalog entry are evaluated
  3. Conditions are checked against the context — “is the user on mobile?”, “is the market UK?”
  4. Among the variants whose rules match, the one with the highest priority is returned
  5. If no rules match, the default variant is shown

Key Concepts

  • Context dimensions are the values you can target against (device, market, locale, custom dimensions)
  • Targeting rules are conditions that compare context values to expected values
  • Priority ranks the matches: among the variants whose rules match, the highest priority wins
  • Rules and priority live on the catalog entry a variant references, in the Variant Catalog

Building Rules

Simple Conditions

A condition compares a context value against a target:

  • Device equals mobile
  • Market equals UK
  • Segment “VIP” is true

Combining Conditions

Use AND/OR to create complex rules:

AND (all must be true):

  • Device is mobile AND Market is UK

OR (any can be true):

  • Market is UK OR Market is US OR Market is Canada

Nested groups:

  • Segment is VIP AND (Device is mobile OR Market is UK)

Available Comparisons

TypeOperators
Equalityequals, does not equal
Numbersgreater than, less than, greater than or equal, less than or equal
Textcontains, starts with, ends with
Listsis in, is not in
Existenceis empty, is not empty
Booleanis true, is false

System Contexts

Four built-in contexts are always available without configuration, and two more say where the visitor is:

ContextValuesWhen omittedDescription
devicedesktop, mobile, tabletNone. Absent unless you pass itDevice type
platformweb, ios, androidNone. Absent unless you pass itPlatform the request came from
authStatusauthenticated or anonymousNot yours to pass. The platform sets it from the user’s tokenWhether the user is logged in
localeProject’s configured localesNone. Absent unless you pass itLanguage/locale for content

authStatus is determined automatically from the user’s authentication token and cannot be set manually via headers. Forward a user token on the read and it is authenticated; send no token and it is anonymous. A rule can name either, so a signed-out visitor can be served a variant of their own. Authentication covers which credentials produce which principal.

Country and location

Two more are built in, and they say where the visitor is:

ContextValueDescription
countryAn ISO 3166-1 alpha-2 code, such as GBThe visitor’s country
locationA latitude and a longitudeWhere the visitor is. Compare it with within_radius and a lat,lng,radiusKm value: 48.86,2.35,50 is within 50 km of Paris

Where a visitor’s location comes from

It depends on who is calling, and there are two paths:

  • A browser or an app calling Foir on a publishable key. The caller is the visitor, so Foir places them itself and you pass nothing. A visitor Foir cannot place has no country and no location.
  • A server rendering your pages. The caller is your server, and where your server is says nothing about the visitor. So the server says where the visitor is, in contexts.country and contexts.location, from what its host tells it. That takes a key holding visitors:forward, which a server key can and a publishable key cannot. The SDK attaches both to every read from the client’s visitor: see A server rendering your pages.
query { page(naturalKey: "homepage", contexts: { country: "FR", location: { latitude: 48.86, longitude: 2.35 } }) { _id } }

The same read through the data client. A client given a visitor attaches the place to every read, so it is written on one read only to say something else:

const page = await foir.page.findUnique({ naturalKey: 'homepage', contexts: { country: 'FR', location: { latitude: 48.86, longitude: 2.35 } }, });

Through a key holding visitors:forward, the place is what the read says or nothing: Foir never uses where the caller is. A read that says none is answered as a visitor nobody could place, and a place said on one read of a request stands for the reads of that request that say none. A key that does not hold the permission is refused both fields, naming it.

A server that calls on a publishable key is placed where the server is, because Foir cannot tell it is a server. Give a rendering server a server key.

A visitor with no country or location is absent from those two dimensions, as for any dimension left out. A rule that requires a country does not match, and a rule that excludes one does.

Custom Context Dimensions

A custom context dimension is a value your application sends with a read: a market, a city, a storefront. It is a context dimension with sourceType: 'request', backed by a model whose records are the values it can take.

Creating a Custom Context

  1. Create a model for the values (for example market) and add a record for each one. The record’s natural key is the value: us, uk, eu
  2. Go to Settings > Context Dimensions and click Create
  3. Configure:
    • Key: unique identifier (e.g., market)
    • Name: display name (e.g., “Market”)
    • Type: Value sent with the request
    • Source Model: the model that provides the values (e.g., market)
    • Default Value: optional, and stored with the dimension. Resolution does not substitute it, so pass the dimension on the request when you want to target on it
  4. Click Save, then release the draft

The source model’s records have to belong to the project as a whole, because their keys are published in your project’s schema as the dimension’s values. A model whose records are owned by users or organizations is refused, and so is an inline model, which holds no records. A project can define as many of these dimensions as it needs.

A dimension cannot take a name the resolution context already uses: device, platform, locale, authStatus, country, location, date, hour, dayOfWeek, user, segment, userId, preview, viewerKey, localeStrict or resolutionTrace. Names are compared ignoring case and punctuation, so auth-status is taken too.

The same dimension in foir.config.ts:

defineContextDimension({ key: 'market', name: 'Market', sourceType: 'request', sourceModelKey: 'market', })

Passing Context

Context is passed as the typed contexts argument on any per-model read query (singular or plural). There is no context request header or query parameter — the platform reads targeting context only from this argument.

curl https://api.foir.dev/graphql \ -H "x-api-key: foir_pub_your_publishable_key" \ -H "Content-Type: application/json" \ -d '{"query": "{ page(naturalKey: \"homepage\", locale: \"en-GB\", contexts: { device: MOBILE }) { _id title } }"}'

Context dimensions

  • device — built-in enum: DESKTOP, MOBILE, TABLET
  • platform — built-in enum: WEB, IOS, ANDROID
  • locale — BCP-47 string (also accepted as the top-level locale query argument)
  • country and location — where the visitor is, from a key holding visitors:forward only. See where a visitor’s location comes from
  • Custom dimensions you configure (e.g. market) appear as additional fields on contexts, typed as an enum of their configured values

authStatus and user segment membership are not client inputs: the platform derives them server-side from the user access token.

Omitted dimensions

A dimension you leave out of contexts is absent from targeting rather than filled in with a default value. A rule that requires it to equal a particular value does not match, so a variant targeted at locale equals en-US is not selected by a query that says nothing about locale.

Omitting locale also means translatable fields come back with the values they were authored in. A locale you do pass is a different matter: it resolves down an ordered list that ends at your project’s default. See Localization for that list and how to configure it.

In the console

Creating Targeting Rules

Targeting rules and priority are set on a variant catalog entry, not on a record’s variant:

  1. Go to Settings > Variant Catalog and open an entry, or click New Entry
  2. Under Targeting Rules, build your rule:
    • Add a condition (e.g., Device equals mobile)
    • Add more conditions and set logic to AND or OR
    • Nest groups for complex rules
  3. Set the entry’s Priority (the highest priority among matching entries wins)
  4. Save, then release the draft
  5. On the record, add a variant that references the entry

Managing Context Dimensions

Go to Settings > Context Dimensions to create, edit, or delete custom contexts.

Testing Rules

Preview Panel

  1. Open any content with variants
  2. Click Preview
  3. Use the context selector to simulate different devices, markets, and segments
  4. See which variant would display

Via the CLI

Context dimensions are managed with foir context-dimensions (aliased foir context-dims). The similarly named foir context command is something else: it chooses which project and tenant the CLI is pointed at.

# List the project's context dimensions foir context-dimensions list # Create a dimension whose value the request sends foir context-dims create --data '{ "key": "market", "name": "Market", "sourceType": "request", "sourceModelKey": "market" }' # Release the draft foir release publish context_dimension:market

Via the API

Resolving Content with Context

Pass contexts on the per-model query; the matching variant’s values come back directly on the typed fields:

query GetPage { page( naturalKey: "homepage" locale: "en-GB" contexts: { device: MOBILE } ) { _id title hero { heading } } }

Resolution happens server-side and the resolved values arrive on the typed fields. The record also carries resolution metadata: _variant (the selected variant’s key, ungated) and — for a key holding targeting:debug — _resolvedBy and _segments. See _segments for the shape and the gating.

The same read through the data client — locale and contexts are ordinary arguments there too:

const page = await foir.page.findUnique({ naturalKey: 'homepage', locale: 'en-GB', contexts: { device: 'MOBILE' }, });

contexts is the same input object the GraphQL argument takes, one key per dimension, with enum dimensions written as their name in a string — the value travels as a GraphQL variable typed from the manifest, so the executor coerces the enum rather than the client guessing at it. Resolution and the resolution metadata are identical, because the client prints the query above.

Best Practices

  • Start with device targeting — mobile optimization is usually the first need.
  • Always have a default variant — ensure content displays when no rules match.
  • Set clear priorities — more specific rules should have higher priority.
  • Use descriptive keys — user-tier is clearer than ctx-1.
  • Pass every dimension your rules read — an omitted dimension is absent, so a rule that tests it cannot match and the variant behind it never shows.
  • Keep rules simple — complex rules are harder to maintain and debug.

Common Patterns

Mobile First

Priority 10: Device is mobile -> Mobile variant Priority 0: Default -> Desktop variant

Regional Content

Priority 10: Market is UK -> UK variant Priority 10: Market is US -> US variant Priority 0: Default -> International variant

VIP Experience

Priority 20: Segment includes VIP -> VIP variant Priority 10: Device is mobile -> Mobile variant Priority 0: Default -> Standard variant

Authenticated Users

Priority 10: Auth status is authenticated -> Member variant Priority 0: Default -> Public variant
Last updated on