Targeting Rules

Control who sees which flag variant

Targeting rules determine which variant a user receives based on their context.

Evaluation Context

Context is data you pass when evaluating a flag:

const context = {
  targetingKey: 'user-123',      // Required: unique user identifier
  email: 'user@example.com',
  plan: 'premium',
  country: 'SE',
  version: '2.1.0'
};

const value = await client.getBooleanValue('feature', false, context);
MutableContext context = new MutableContext("user-123");  // Required: unique user identifier
context.add("email", "user@example.com");
context.add("plan", "premium");
context.add("country", "SE");
context.add("version", "2.1.0");

boolean value = client.getBooleanValue("feature", false, context);
context = EvaluationContext(
    targeting_key="user-123",      # Required: unique user identifier
    attributes={
        "email": "user@example.com",
        "plan": "premium",
        "country": "SE",
        "version": "2.1.0",
    },
)

value = client.get_boolean_value("feature", False, context)
evalCtx := openfeature.NewEvaluationContext(
    "user-123",  // Required: unique user identifier
    map[string]interface{}{
        "email":   "user@example.com",
        "plan":    "premium",
        "country": "SE",
        "version": "2.1.0",
    },
)

value, _ := client.BooleanValue(ctx, "feature", false, evalCtx)

The targetingKey is required - it identifies the user for consistent rollouts.

Rule Structure

Each rule has:

  1. Conditions - Criteria that must match
  2. Variant - The value to return if conditions match
  3. Priority - Order of evaluation (first match wins)
Rule 1 (priority 1): IF email ends with "@company.com" THEN return "enabled"
Rule 2 (priority 2): IF plan equals "enterprise" THEN return "enabled"
Default: return "disabled"

Rules are evaluated in priority order. First match wins.

Operators

String Operators

OperatorDescriptionExample
equalsExact match (case-sensitive)email equals "admin@company.com"
equals (ignore case)Exact match (case-insensitive)country equals (ignore case) "se"
containsSubstring matchemail contains "test"
starts withPrefix matchemail starts with "admin"
ends withSuffix matchemail ends with "@company.com"
matches regexRegular expressionemail matches regex ".*@(alpha|beta)\\.com"

List Operators

OperatorDescriptionExample
inValue in listcountry in ["SE", "NO", "DK"]
not inValue not in listplan not in ["free", "trial"]

Numeric Operators

OperatorDescriptionExample
= (equals)Equal toage = 25
> (greater than)Greater thanage > 18
>= (greater or equal)Greater than or equalversion >= 2.0
< (less than)Less thanversion < 3.0
<= (less or equal)Less than or equalseats <= 10

Combining Conditions

Rules can have multiple conditions combined with AND:

IF email ends with "@company.com"
   AND plan equals "enterprise"
   AND seats >= 10
THEN return "enabled"

All conditions must match for the rule to apply.

Gradual Rollouts

Rollouts release a feature to a percentage of users:

10% -> variant-a
20% -> variant-b
70% -> control

How it works:

  1. User's targetingKey is hashed
  2. Hash maps to a bucket (0-99)
  3. Bucket determines which variant they receive

Properties:

  • Deterministic - Same user always gets the same variant
  • Consistent - Increasing percentage doesn't reassign existing users
  • Statistically valid - Even distribution for A/B tests

Percentages must sum to 100%. If they don't, the last variant receives the remainder.

Combining Rules and Rollouts

Targeting rules and gradual rollouts work together with a clear priority:

  1. Targeting rules are evaluated first - if any rule matches, its value is returned immediately
  2. Rollout applies when no rules match - only users who don't match any targeting rule enter the gradual rollout

Example setup:

Targeting Rules:
  Rule 1: IF user in segment "internal" THEN return "enabled"
  Rule 2: IF user in segment "beta-testers" THEN return "enabled"

Gradual Rollout (for everyone else):
  10% -> "enabled"
  90% -> "disabled"

This pattern lets you:

  • Give 100% access to internal users and beta testers (via rules)
  • Gradually roll out to remaining users (via gradual rollout)

On this page