Skip to content
Latchkey

How to Write Feature Flag Targeting Rules

Targeting rules match on context attributes (plan, region, user id) so a flag enables only for the audience you choose.

A targeting rule enables a variation when the evaluation context matches conditions. Pass a consistent context (user key plus attributes) so rules resolve the same way in app and CI.

Steps

  • Decide the attributes rules will match on (plan, country, user id).
  • Write ordered rules; the first match wins, else the default.
  • Pass those attributes in the evaluation context everywhere.

Targeting rules

flags.yaml
flags:
  new-checkout:
    defaultVariation: false
    rules:
      - if: { attribute: plan, op: in, values: [enterprise] }
        variation: true
      - if: { attribute: country, op: equals, value: CA }
        variation: true

Evaluate with context

Terminal
const on = await client.getBooleanValue('new-checkout', false, {
  targetingKey: user.id,
  plan: user.plan,
  country: user.country,
});

Gotchas

  • Missing attributes make a rule fall through to the default; always pass the full context.
  • Rule order matters; put the most specific rules first.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →