Skip to content

Commit 126ed64

Browse files
khvn26Zaimwa9matthewelwell
authored
docs: Context values for Segments (#5654)
Co-authored-by: Zaimwa9 <[email protected]> Co-authored-by: Matthew Elwell <[email protected]>
1 parent 9115f16 commit 126ed64

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

docs/docs/basic-features/segments.md

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';
77
# Segments
88

99
A segment is a subset of [identities](/basic-features/managing-identities.md), defined by a set of rules that match
10-
identity [traits](managing-identities.md#identity-traits). An identity always belongs to a single environment and can
11-
belong to any number of segments.
10+
identity [traits](managing-identities.md#identity-traits) or other [context values](#context-values). An identity always
11+
belongs to a single environment and can belong to any number of segments.
1212

1313
Once you have defined a segment, you can create **segment overrides** for features within an environment. A segment
1414
override allows you to control the state of a feature only for identities that belong to a specific segment. This is
@@ -69,14 +69,14 @@ Segment rules are evaluated in order, i.e. from top to bottom when viewed in the
6969

7070
For example, consider the following segment:
7171

72-
1. 10% percentage split
72+
1. 10% percentage split over identifier
7373
2. `is_subscriber = true`
7474

7575
This segment would first select 10% of _all_ identities, and then choose subscribers from that cohort. Instead, if we
7676
used the opposite order:
7777

7878
1. `is_subscriber = true`
79-
2. 10% percentage split
79+
2. 10% percentage split over identifier
8080

8181
This would first select all subscriber identities, and then randomly choose 10% of them.
8282

@@ -94,6 +94,60 @@ flag is:
9494
2. Segment overrides
9595
3. Default value for the current environment
9696

97+
## Context values
98+
99+
:::warning
100+
101+
Currently, context values are only available for remote evaluation. In local evaluation, rules using context values will evaluate to `false`.
102+
103+
:::
104+
105+
In addition to identity [traits](managing-identities.md#identity-traits), you can use the following context values as
106+
Segment rule properties:
107+
108+
- **Identifier**: a unique identifier for an identity, used for segment and multivariate feature flag targeting, and displayed in the Flagsmith UI. Useful for bucketing via the
109+
[`% Split`](?operators=percent#operator-details) operator, or managing a list of users via the
110+
[`In`](?operators=in#operator-details) operator.
111+
- **Identity Key**: a key used when selecting a value for a multivariate feature, or for % split segmentation. Set to an internal identifier or a composite value based on the environment key and identifier, depending on Flagsmith implementation.
112+
- **Environment Name**: an environment's human-readable name. Useful for restricting Segments to certain environments.
113+
114+
Context values can be used to control your targeting more precisely:
115+
116+
### Gradual rollout across tenants
117+
118+
Suppose your application supports users who can belong to multiple organisations. Now, you want to roll out a new
119+
feature to 20% of your organisations, ensuring that users only have access to the feature when they are part of an organisation included in that 20%. If they switch to an organisation outside this group, they will no longer have access to the feature.
120+
121+
Here's how you might define a segment to achieve this:
122+
123+
| Rule Order | Property | Operator | Value |
124+
| ---------- | ------------------------- | -------- | ----- |
125+
| 1 | `organisation_name` Trait | % Split | 20 |
126+
127+
This setup, along with a segment override, instructs the evaluation engine to enable the feature if the user's current organisation belongs to consistent 20% of all organisations.
128+
129+
:::info Use transient traits for multi-tenancy
130+
131+
To avoid persisting the `organisation_name` trait on the user identity, mark it as
132+
[transient](../advanced-use/transient-traits.md).
133+
134+
:::
135+
136+
### Restrict evaluation to select environments
137+
138+
Segments and segment overrides are defined at the project level. This means they apply across all environments, so you
139+
may want to restrict your targeting to specific environments — for example, to avoid exposing features in production
140+
while still running complex evaluations in staging or development.
141+
142+
Here’s how you could define such a segment:
143+
144+
| Rule Order | Property | Operator | Value |
145+
| ---------- | ---------------- | ------------------- | ------------ |
146+
| 1 | Environment Name | Does Not Match (!=) | `production` |
147+
| 2 | Identifier | % Split | 20 |
148+
149+
This setup will enable the feature for 20% of users, but only in non-production environments.
150+
97151
## Trait data types
98152

99153
Each individual trait value is always stored as one of the following data types:
@@ -193,16 +247,15 @@ Versions are compared as defined by the [Semantic Versioning specification](http
193247
</TabItem>
194248
<TabItem value="percent" label="Percentage Split">
195249

196-
Percentage Split is the only operator that does not require a trait. You can use it to drive
197-
[A/B tests](/advanced-use/ab-testing) and
250+
You can use Percentage Split to drive [A/B tests](/advanced-use/ab-testing) and
198251
[staged feature rollouts](/guides-and-examples/staged-feature-rollouts#creating-staged-rollouts).
199252

200-
Percentage Split deterministically assigns a "bucket" to each identity solely based on its ID and not any traits,
201-
meaning that Segment overrides that use Percentage Split will always result in the same feature value for a given
202-
identity.
253+
Percentage Split deterministically assigns an Identity to a bucket based on a provided [context value](#context-values)
254+
or a trait. This means that Segment overrides that use Percentage Split will always result in the same feature state and value for
255+
a given identity.
203256

204-
If you create a Segment with a single Percentage Split rule, Identities who are members of that split when the split
205-
value is set to, say, 10% will be guaranteed to also be in that split if it is changed to a value higher than 10%.
257+
If you create a Segment with a single Percentage Split rule of 10% over identifier, Identities who are members of that
258+
split will be guaranteed to also be in that split if it is changed to a value higher than 10%.
206259

207260
If the Percentage Split is reduced in value, some Identities will be removed from that Percentage Split to maintain the
208261
balance. The algorithm is fairly simple and good to understand - it is

docs/docs/guides-and-examples/staged-feature-rollouts.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ Note that you can include the "% Split" rule alongside other Segment rules if yo
3131

3232
## How does it work
3333

34-
Each Identity and Segment has a unique identifier. These two pieces of data are merged then hashed, and a floating point
35-
value between 0.0 and 1.0 is generated from this hash. This value is then evaluated against the "% Split" rule.
34+
Flagsmith merges the Segment unique identifier and a context value chosen for the `% Split` rule — usually an identifier
35+
or a trait — then hashes the result. A floating point value between 0.0 and 1.0 is generated from this hash. This value
36+
is then evaluated against the threshold set for the `% Split` rule.
3637

3738
### An Example
3839

0 commit comments

Comments
 (0)