No results for ""
  • Home
  • API docs

Custom rules

Read time: 4 minutes
Last edited: Jun 07, 2024


This topic explains how to use custom rules to target contexts based on their attributes.

Target contexts with custom rules

You can create custom targeting rules using any context kinds and any context attributes.

To create a custom targeting rule:

  1. Click the + Add rule button, or + between existing rules, and select "Build a custom rule":
The "+ Add rule" menu, with "Build a custom rule" called out.
The "+ Add rule" menu, with "Build a custom rule" called out.
  1. (Optional) Enter a name for the rule.
  2. Select an option from the Context kind menu:
  • Choosing a specific context kind lets you target on attributes for contexts of that kind.
  • Choosing "Context kind" lets you target one or more context kinds. If you choose "Context kind," skip to step 5.
  1. In the Attribute menu, select one of this context's attributes.
  2. In the Operator menu, select the operator for your clause.
  3. In the Values menu, enter one or more values to check against.
  4. (Optional) Click the + to add additional clauses to your targeting rule.
  5. In the Rollout field, select the variation to serve, or set a percentage rollout.
  6. Click Review and save.
Custom targeting rules can include mobile application data

If you are using a mobile SDK that supports environment attributes, you can target your mobile applications based on details about your application, application version, and device using a mobile targeting rule. To create custom targeting rules using this same context information, use the ld_application and ld_device context kinds.

If you are using other mobile SDKs, you can target your mobile applications using any context attributes that you have created.

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.

Multiple conditions and values

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.

To reference this rule when working with other members of your organization, click the link icon to copy a link to this rule.

You can also use the REST API: Update feature flag

Convert 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:

  1. Navigate to the flag's targeting page.
  2. Find the custom targeting rule you want to convert and click the overflow menu. The overflow menu appears.
  3. Click Convert to segment. The "Convert targeting rule to segment" dialog appears.
  4. Give your segment a human-readable Name and a Key.
  5. (Optional) Add a Description.
  6. (Optional) Select a tag or create a new tag from the Tags menu.
  7. 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.