Targeting rules
Read time: 18 minutes
Last edited: Sep 14, 2023
Overview
This topic explains how to use targeting rules to target segments or contexts based on their attributes.
Each feature flag can include a combination of individual targets and targeting rules. To learn more about individual targets, read Individual targeting.
Each feature flag must include a default rule. To learn more, read Setting the default rule, below.
Understanding targeting rules
Targeting rules can have one or more conditions.
Each condition has three parts:
- A context kind and attribute, which defines the scope of the condition's impact, such as only targeting an email address for the selected context kind. To learn more, read Attributes.
- An operator, which sets differentiating characteristics of the attribute, such as limiting the condition to emails that end with certain extensions. If a condition specifies multiple values for the operator to track, the operator iterates over the array. To learn more, read Operators.
- A value, which identifies the attribute by a value you specify, such as
.edu
.
Here is an image of a targeting rule:

Creating targeting rules
To create a targeting rule:
- Click Add rules.
- Enter a name for the rule.
- Select an option from the Context kind menu:
- "Context kind" lets you choose one or more context kinds to target. If you choose "Context kind," skip to step 5.
- "is in segment" lets you target one or more specific segments. If you choose "is in segment," skip to step 6.
- "is not in segment" lets you target contexts that are not in one or more specific segments. If you choose "is in segment," skip to step 6.
- Choosing a specific context kind lets you target on attributes for contexts of that kind.
- Choose an attribute from the "Select an attribute" menu.
- Choose an operator from the "Select an operator" menu.
- Enter a value in the "Enter some values" field.
- Choose a variation from the "Rollout" menu.
- (Optional) If you need to undo the changes you just made, click the undo arrow icon to discard all changes:

- Click Review and save.
If a targeting rule references any context kinds or attributes with null
values, or that do not exist for a given context, then the flag skips that rule. For example, in a rule that checks "region is one of Canada," any context whose region attribute is not set or is set to null
does not match the rule. Similarly, in a rule that checks "region is not one of Canada," any context whose region attribute is not set or is set to null
does not match the rule. This behavior ensures that your rules only target contexts for which you explicitly have attribute information.
You can add multiple conditions to a rule. Here is how rules handle multiple conditions and values:
- Contexts must meet all the conditions in a rule to match the rule. If any of the conditions are not met, the context will not match the rule.
- If a condition has multiple values, LaunchDarkly considers the condition met if there is a match on any of the values.
- If an attribute has an array value, LaunchDarkly treats it as multiple values and allows any of the values within the array to match a rule.
You can also use the REST API: Update feature flag
Rule duplication
You can create new targeting rules by duplicating a flag's existing targeting rule and modifying the new rule.
To duplicate an existing targeting rule:
- Click on the flag rule's three-dot overflow menu and choose "Duplicate rule." A new rule appears.
- Make at least one change to the existing rule or the new rule.
- Click Review and save.
Here is an image of the "Duplicate rule" option in the rule menu:

The new rule appears below the original rule. You must make at least one change to either the existing rule or the new rule before saving your changes, to prevent two exact duplicate rules on the same flag.
Percentage rollouts
If you want only a portion of contexts matching a rule to receive a particular variation, you can serve a percentage rollout.
Here is an image of the percentage rollout section:

In this example, 50% of contexts will receive each variation.
To learn more, read Percentage rollouts.
Experimentation
Experimentation is available to customers on a Pro or Enterprise plan. To learn more, read about our pricing. To add Experimentation to your plan, contact Sales.
After you save a targeting rule, you can click the graph icon and select "Run an experiment on this rule" to create an experiment. To learn more, read Creating experiments.
Here is an image of the graph icon on a flag's targeting rule:

Attributes
LaunchDarkly allows you to create your own attributes. For instance, you might want to target contexts based on plan, group, role, or location.
Here is an example of a context with custom attribute values:
"context": {"kind": "user","name": "Sandy","email": "sandy@example.com","gymMember": "true"}
Using attributes, you could show some features to customers on your regular plan, and additional features to customers on your premium plan. Or you could roll out a new feature to 30% of end users at a particular location, rather than 30% of all end users. To learn more, read Context attributes.
In each targeting rule, you can choose an attribute specific to your chosen context kind using the "Attribute" menu.

If an attribute is an object, then in your targeting you can use /
as a delimiter to refer to specific object fields. For example, if you have an "address" attribute that includes "city," "state," and several other fields, then you can use /address/city
in your targeting.
From here, you can also select whether to include or exclude all contexts of a particular context kind based on whether they are part of a segment. To learn more, read Segments.
Operators
LaunchDarkly supports the following operators:
Operator | Attribute type | Meaning |
---|---|---|
is one of (=), is not one of (!=) | string, number, boolean, date | Exact match |
ends with, does not end with | string | String suffix match |
starts with, does not start with | string | String prefix match |
matches regex, does not match regex | string | Regular expression match |
contains, does not contain | string | Substring match |
greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=) | number | Numeric comparisons |
before, after | date | Date comparisons. Dates must be formatted in UNIX milliseconds or a string in RFC-3339 format. To learn more, read Representing data/time values. |
semantic version is one of (=), is not one of (!=), greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=) | string | Semantic version comparison. Valid string attributes must follow the semantic versioning specification, although LaunchDarkly allows you to omit the PATCH version. For example, |
Setting the default rule
LaunchDarkly defines a final default rule, sometimes called the "fallthrough" rule, for any contexts that don't match any of the previous targeting rules on the page. As with other rules, you can choose to serve a specific variation, or apply a percentage rollout to any remaining contexts.
Here is an image of a default rule:

Now, all contexts that have not been targeted by any other rules will receive false
.
Setting the default off variation
When the toggle is turned off, LaunchDarkly will serve the default off variation for your feature flag. For boolean flags, the default off variation is set to false
. For multivariate flags, you select one of your custom variations. You can customize the default off variation for both boolean and multivariate flags in the Targeting tab.
If you do not specify a default off variation, then the SDK will return the fallback value defined in your code.
Regardless of how you set up targeting for your feature flag, each time you evaluate a flag from the SDK, you must include a fallback value as one of the parameters. This fallback value is the value served to the context if the SDK cannot connect to LaunchDarkly. To learn more, read Evaluating flags.
Evaluation order
The Targeting tab evaluates flag prerequisites and rules from top to bottom.
This diagram represents rule matching behavior:

At each step in the process, if the context instance matches the rule, it receives the appropriate variation. If it does not match the rule, it moves on to the next rule.
Configure your SDK: Configuring variations
Here is how a context instance moves through a flag evaluation:
-
If your app can't connect to LaunchDarkly, the context receives the fallback value. Otherwise, it moves to the next step.
-
If the flag is off, the context receives the default off variation. Otherwise, it moves to the next step. To learn more, read Setting default values.
-
If the flag has prerequisites, and the context doesn't meet the prerequisites, it receives the default off variation. Otherwise, it moves to the next step. To learn more, read Flag prerequisites.
-
If the context is individually targeted, it receives the chosen variation in the individual targeting rule. Otherwise, it moves to the next step. To learn more, read Individual targeting.
-
If the context meets a flag rule, it receives the chosen variation. Otherwise, it moves to the next step. If there are multiple flag rules, the context moves through the rules from top to bottom.
-
If the context doesn't meet any of the previous criteria, it receives the default on variation. To learn more, read Setting default values.
Here is an image of two targeting rules:

In this example, the first rule (if the segment is not in Beta users
) is evaluated before the second rule (if the organization key is Beta
).
You can re-order rules by clicking and dragging them into different positions.
Converting rules into segments
You may require complex targeting rules to successfully perform feature launches. You can convert a targeting rule into a reusable standard segment from the flag's Targeting page.
To convert a rule into a segment:
- Navigate to the flag's Targeting tab.
- In the targeting section or the rule matching section, click the overflow menu of the rule you wish to convert. The overflow menu appears.
- Click "Convert to segment." The "Convert to segment" dialog appears.
- Give your segment a human-readable Name and a Key.
- (Optional) Add a Description.
- (Optional) Select a tag or create a new tag from the Tags menu.
- Click Save segment. A confirmation appears indicating that you've created a new segment.
A new rule targeting the newly created segment appears in the list of targeting rules.