JSON targeting
Overview
This topic explains how to create and edit flag targeting rules using JSON.
Editing flag rules using JSON follows the same model as the JSON response from the Get feature flag endpoint in the REST API.
View JSON targeting rules
To view a flag’s targeting rules in JSON, navigate to the Targeting tab and click the icon in the “Targeting configuration” section:
Basic JSON structure
Below is an example of the basic JSON structure for a boolean feature flag:
This example flag:
- is toggled off
- has no prerequisites
- has no targeting rules besides the default rule
- has true (
0
) as the default rule - has false (
1
) as the off variation
Variation IDs
For boolean flags, the ID of the true
variation is always 0
, and the ID of the false
variation is always 1
.
For multivariate flags, the ID of the first variation is 0
, the ID of the second variation is 1
, and so on.
Flag on/off status
To toggle a flag on or off, set on
to true
or false
.
Here is what a flag toggle looks like in the user interface (UI):
Here is an example of how to toggle a flag on
:
To learn about toggling flags on and off in the UI, read Turning flags on and off.
Off variation
The off variation is the variation served to all contexts when a flag is off.
Here is an example of how to set the off variation:
To learn how to set the off variation in the UI, read The off variation.
Default rule or variation
The default, or fallthrough, rule is the rollout or variation that a flag serves when a context doesn’t match any other targeting rules on the flag.
Here is what a default rule looks like in the UI:
To set a default variation, use fallthrough
and set variation
to the ID of the flag variation you want to use as the default variation.
Here is an example of setting the default variation on a boolean flag:
You can also set a rollout, instead of a single variation, as the default rule. To learn how, read Rollouts.
To learn how to set the default rule in the UI, read The default rule.
Prerequisites
To set a prerequisite, use prerequisites
with the following key/value pairs:
key
: the prerequisite flag key.variation
: the ID of the prerequisite flag’s variation you want to require.
Here is what a prerequisite rule looks like in the UI:
Here is an example prerequisite rule requiring the prerequisite variation for the flag alt-sort-order
to be true
:
In the above example, if the prerequisite flag is on: true
, and a context encountering this flag is receiving the true (0
) variation of the prerequisite flag, then the context is subject to this flag’s targeting rules.
If the prerequisite flag is on: false
, or the context is receiving a variation other than true (0
), then the context will receive this flag’s offVariation
of false (1
).
To learn how to use prerequisite flags in the UI, read Flag prerequisites.
Individual targeting
Individual targeting lets you serve chosen variations to specific contexts.
Here is what an individual targeting rule looks like in the UI:
To individually target a context, use contextTargets
with the following key/value pairs:
variation
: the ID of the variation you want to serve.values
: the context keys you want to target.contextKind
: the context kind of the contexts you want to target.
Here is an example individual targeting rule for a user context:
To learn how to target individual contexts in the UI, read Individual targeting.
Target multiple context kinds
You can use multiple rules if you need to target multiple context kinds.
This example flag targets both user and organization contexts:
Target different variations
You can use multiple rules if some individually targeted contexts should receive different variations than others.
This example flag serves true to some contexts and false to others:
Segment targeting
Segment targeting rules let you serve chosen variations to specific LaunchDarkly segments.
Here is what a segment targeting rule looks like in the UI:
To target a segment, use clauses
with the following key/value pairs:
attribute
:segmentMatch
ornot-SegmentMatch
, depending on whether or not you want the rule to target contexts in the chosen segment, or not in the chosen segmentop
:segmentMatch
negate
: set tofalse
if you setattribute
tosegmentMatch
. Set totrue
if you setattribute
tonot-segmentMatch
.values
: the segment keys you want to target in the rule
To learn how to build segment targeting rules in the UI, read Segment targeting.
Mobile targeting
Mobile targeting rules let you control which mobile apps and devices receive a variation of a feature flag.
Here is an example of a mobile targeting rule in the UI:
To target mobile devices, use rules
with the following key/value pairs:
description
(optional): the description of the rule.variation
: the ID of the variation you want to serve.clauses
with the following key/value pairs:contextKind
: eitherld_application
orld_device
, depending on if you want to target mobile applications or mobile devices in this rule.attribute
: the context attribute you want to use in the rule.op
: the operators available depend on your context kind and attribute:- if your
contextKind
isld_application
, and yourattribute
isversion support status
, thenapplicationVersionSupported
is the only available operator. - otherwise, use the standard operator you want to use in the rule.
- if your
negate
: set totrue
to use the inverse of the operator. For example, ifop
is set toin
, use"negate": true
to use “is not in” as the operator. Otherwise, set tofalse
.values
: the attribute values you want to use in the rule.
To learn how to build mobile targeting rules in the UI, read Mobile targeting.
Custom targeting rules
Custom targeting rules let you target contexts based on their context kind and attributes.
Here is an example of a custom targeting rule in the UI:
To target custom context kinds and attributes, use rules
with the following key/value pairs:
description
(optional): the description of the rule.variation
: the ID of the variation you want to serve.values
: the context keys you want to target.clauses
, with the following key/value pairs:contextKind
: the context kind of the contexts you want to target.attribute
: the context attribute you want to use in the rule.op
: the standard operator you want to use in the rule.negate
: leave set totrue
, or set tofalse
to use the inverse of the operator. For example, ifop
is set toin
, use"negate": true
to use “is not in” as the operator.values
: the attribute values you want to use in the rule.
trackEvents
(optional):true
orfalse
depending on whether or not you want to send feature events to LaunchDarkly.
Here is an example custom targeting rule that targets customers with an email address that ends in .edu
:
Here is an example targeting rule that targets accounts for an Early Access Program (EAP):
To learn how to build custom targeting rules in the UI, read Custom rules.
Rollouts
When you create a custom rule or default rule, you can choose to serve a rollout instead of a single variation.
There are three kinds of rollouts:
- Manual percentage rollout
- Progressive rollout
- Guarded rollout
Manual percentage rollout
Percentage rollouts let you roll out your feature to a small percentage of contexts and, as you become more confident your feature is working as intended, manually increase the percentage over time.
Here is what a percentage rollout looks like in the UI:
To create a manual percentage rollout, use percentageRolloutConfig
with the following key/value pairs:
contextKind
(optional): the context kind you want to roll out by. Defaults to your default context kind.bucketBy
(optional): the attribute value you want to roll out by. Defaults tokey
.variations
, with the following key/value pairs:variation
: the ID of the variation you want to serve.weight
: the percentage of contexts you want to include in that variation. Include three decimal places in the percentage, with no.
or,
. Do not include leading0
s. For example:- to include 5.5% of contexts in a variation, set the
weight
to5500
. - to include 75.5% of contexts in a variation, set the
weight
to75500
. The weight must add up to 100% between all of the variations.
- to include 5.5% of contexts in a variation, set the
The below example shows how to rollout the true
variation to 50% of contexts and the false
variation to 50% of contexts on the default rule of a flag.
Here is how to set the rollout using JSON:
To learn how to create percentage rollouts in the UI, read Percentage rollouts.
Progressive rollout
A progressive rollout lets you serve a given flag variation to a specified percentage of contexts, and gradually increases that percentage over a specified time.
Here is what a progressive rollout looks like in the UI:
To create a progressive rollout, use progressiveRolloutConfig
with the following key/value pairs:
contextKind
: the context kind you want to roll out by. Defaults to your default context kind.controlVariation
: the variation ID of the variation you want to serve at the beginning of the rollout. For boolean flags, this variation is automatically set based on the value the flag is currently serving, usually the off variation or the default variation.endVariation
: the variation ID of the variation you want to roll out over time. For boolean flags, this variation is automatically set based on the variation not currently being served.steps
, with the following key/value pairs:rolloutWeight
: the percentage of contexts you want to include in that rollout step. Include three decimal places in the percentage, with no.
or,
. Do not include leading0
s. For example, to include 5.5% of contexts in a rollout step, set therolloutWeight
to5500
. To include 10.5% of contexts in a variation, set therolloutWeight
to10500
. The weight must add up to 100% between all of the rollout steps.duration
with the following key/value pairs:quantity
: the length of time you want that step in the rollout to last, in the units specified inunit
.unit
: the unit of time you want to use in the rollout. Options include:day
hour
minute
The below example shows a progressive rollout over 20 hours on the default rule of a flag.
Here is how to set the rollout using JSON:
To learn how to build progressive rollouts in the UI, read Progressive rollouts.
Guarded rollout
Guarded rollouts let you attach a metric to a rollout to monitor the performance of a flag over time, and to take action on the results.
Here is what a guarded rollout setup looks like in the UI:
To create a guarded rollout, use guardedRolloutConfig
with the following key/value pairs:
randomizationUnit
: the context kind you want to target by.stages
with the following key/value pairs:monitoringWindowMilliseconds
: the length of time you want that step in the rollout to last, in milliseconds.rolloutWeight
: the percentage of contexts you want to include in that rollout step. Include three decimal places in the percentage, with no.
or,
. Do not include leading0
s. For example, to include 5.5% of contexts in a rollout step, set therolloutWeight
to5500
. To include 10.5% of contexts in a variation, set therolloutWeight
to10500
. The weight must add up to 100% between all of the rollout steps.
metrics
with the following key/value pairs:metricKey
: the key of the metric you want to monitor.onRegression
with the following key/value pair:rollback
: whether or not you want LaunchDarkly to automatically roll back the release in the event of a regression. Options includetrue
orfalse
.
regressionThreshold
: the regression threshold you want for the metric.
The below example shows a guarded rollout over 24 hours on the default rule of a flag, using a metric called sentry-errors
.
Here is how to set the guarded rollout using JSON:
To learn how to build guarded rollouts in the UI, read Guarded rollouts.