Targeting Rules

Targeting rules let you enable features for specific user segments. Rules are evaluated in priority order, and the first matching rule determines the flag value.

How Targeting Works

  1. If the flag is disabled, return the default value
  2. Evaluate targeting rules in priority order
  3. If a rule matches the user context, return that rule's value
  4. If no rules match and a rollout percentage is set, use sticky bucketing
  5. Otherwise, return the default value

Creating Rules in the Dashboard

In the dashboard, each flag can have multiple targeting rules per environment. Each rule consists of:

  • Conditions - One or more attribute checks that must ALL pass
  • Value - The value to return when all conditions match
  • Priority - Rules are evaluated top to bottom

Available Operators

Operator Description Example
equals Exact match plan equals "pro"
not_equals Not an exact match plan not_equals "free"
contains String contains value email contains "@company.com"
not_contains String doesn't contain email not_contains "test"
gt Greater than age gt 18
gte Greater than or equal orders gte 10
lt Less than created_at lt "2024-01-01"
lte Less than or equal balance lte 100
in Value is in list country in ["GB","US","CA"]
not_in Value not in list country not_in ["CN","RU"]

Example: Beta Users

Enable a feature only for users on the beta plan:

Rule 1

  • Condition: plan equals "beta"
  • Value: true

Example: Internal Testing

Enable for employees first, then pro users, then 10% rollout:

Rule 1 (Priority: 1)

  • Condition: email contains "@yourcompany.com"
  • Value: true

Rule 2 (Priority: 2)

  • Condition: plan equals "pro"
  • Value: true

Rollout: 10%

Remaining users get 10% random (but sticky) rollout

Example: Regional Launch

Launch in UK first, then expand to EU:

Rule 1

  • Condition: country equals "GB"
  • Value: true

Rule 2

  • Condition: country in ["DE","FR","ES","IT","NL"]
  • Value: true

Percentage Rollouts

Gradually roll out features to a percentage of users:

  • Set the rollout percentage in the dashboard (0-100%)
  • Users are "bucketed" using a hash of their ID + flag key
  • The same user always gets the same result (sticky bucketing)
  • Increase the percentage to expand the rollout
// In your code, just check the flag normally
if (Feature::enabled('new-algorithm')) {
    // 25% of users will see this (set in dashboard)
}

Providing Context

For targeting to work, you need to provide user context:

// From a User model (uses config mapping)
Feature::for($user)->enabled('premium-feature');

// With explicit context
Feature::for([
    'id' => $user->id,
    'email' => $user->email,
    'plan' => $user->subscription->plan,
    'country' => $user->country_code,
    'created_at' => $user->created_at->toISOString(),
    'total_orders' => $user->orders()->count(),
])->enabled('premium-feature');

Multiple Conditions (AND)

All conditions in a rule must match:

VIP Access Rule

  • Condition 1: plan equals "pro"
  • Condition 2: total_orders gte 50
  • Value: true

Both conditions must be true for this rule to match.

Next Steps