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: trueEvaluate 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
How to Set Environment-Specific Feature Flag ValuesGive a feature flag different values per environment so it can be on in staging and off in production, with C…
How to Do a Percentage Rollout With Feature FlagsEnable a feature for a growing percentage of users with a feature flag rollout, bucketing by a stable key so…