Skip to content

docs: Context values for Segments - #5654

Merged
khvn26 merged 10 commits into
mainfrom
docs/context-values-for-segments
Jul 23, 2025
Merged

docs: Context values for Segments#5654
khvn26 merged 10 commits into
mainfrom
docs/context-values-for-segments

Conversation

@khvn26

@khvn26 khvn26 commented Jun 25, 2025

Copy link
Copy Markdown
Member

Thanks for submitting a PR! Please check the boxes below:

  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

Closes #5615.

This PR outlines the Context values for Segments functionality primarily aimed to solve the use case outlined in #5557, open the door for solving #136, #5635, #3971, and more.

Context values are used as Segment rule properties to enable more flexible segmentation. Currently, Context values include the Environment Name, Identity Identifier, and Identity Traits. The % Split operator, previously locked to Identity Identifier, can now be used with any Context value just as any other operator.

Recording 2025-06-25 at 18 35 21

In the future, we'll be able to add more useful predefined Context values like:

  • User-defined contexts, for proper multi-context support.
  • Feature evaluation results, should allow us to implement dependent flags.
  • User application names/versions and user agents from the SDK metrics tracking (being added in feat: Backend for SDK metrics #5623).
  • Current timestamp, to segment by datetime (do we need such a convenient shortcut for scheduled flags? 😄)

The PR contains the following changes:

  • Documentation for Context values. I'll be gathering feedback on that to more closely understand how to scope out the feature further.
  • Mocked-up (not actually working) changes to Segment creation UI to exhibit an idea how the feature should look for end users.
  • The Evaluation context schema now includes the Environment Name Context value. Rather than using it for the SDKs as initially planned, I'm looking to repurpose the schema for the next major version of the Flagsmith evaluation engine, as it clearly defines dependencies for flag evaluation.

How did you test this code?

N/A

@vercel

vercel Bot commented Jun 25, 2025

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 23, 2025 9:30am
2 Skipped Deployments
Name Status Preview Comments Updated (UTC)
flagsmith-frontend-preview ⬜️ Ignored (Inspect) Visit Preview Jul 23, 2025 9:30am
flagsmith-frontend-staging ⬜️ Ignored (Inspect) Visit Preview Jul 23, 2025 9:30am

@github-actions github-actions Bot added front-end Issue related to the React Front End Dashboard docs Documentation updates and removed docs Documentation updates labels Jun 25, 2025

@matthewelwell matthewelwell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added one minor comment to a specific line I wasn't sure about.

On the whole though, it looks good to me. I think that one thing that I'd love to see (whether they make it into the docs for release or not) is some example use cases listed out and how to achieve them.

For example,

"A multi-tenant platform where we want to roll-out a feature by percentage such that all users in a given tenancy get a consistent experience"

  • I think this can be achieved using this functionality + transient traits

I'm sure there are other use cases that we've received feedback about that we're trying to achieve with this too. What about different device types, are there use cases there?

Also, on a general functionality point of view, should we consider being able to define logical context values somehow? Like a hash of their identifier, and some other trait value for example? I'm not suggesting this is part of v1, but does it need to be considered as part of the data-model?

Comment thread docs/docs/basic-features/segments.md Outdated

@emyller emyller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks promising!

Comment thread docs/docs/basic-features/segments.md Outdated
Comment thread docs/docs/basic-features/segments.md
@github-actions github-actions Bot added docs Documentation updates and removed docs Documentation updates labels Jun 27, 2025
@khvn26

khvn26 commented Jun 27, 2025

Copy link
Copy Markdown
Member Author

@matthewelwell

On the whole though, it looks good to me. I think that one thing that I'd love to see (whether they make it into the docs for release or not) is some example use cases listed out and how to achieve them.

I've added 2 use cases in 08b9330 that made sense to me, let me know your thoughts.

Also, on a general functionality point of view, should we consider being able to define logical context values somehow?

I agree this might be a worthwhile endeavour. I can see two ways to achieve that:

  1. Add more operators, and build proper UX around nested conditions.
  2. Think on supporting some DSL for conditions (jsonlogic?)

If we go route 2, we might want to think early on data accessor format to use for Context values, so the answer to

does it need to be considered as part of the data-model?

should be "Yes".

@khvn26

khvn26 commented Jun 27, 2025

Copy link
Copy Markdown
Member Author

Some additional thoughts as to opportunities unlocked by Context values:

  • We'll be able to nuke MV features backend by preserving existing MV UX around environment-scope segments.
  • Per-identity overrides can be condensed to environment == x, identifier IN y[] type segments, which should save us a lot of storage and memory, and improve the local evaluation experience, especially in conjunction with Spike: Removing segment / feature / overrides limits from Edge projects #3763.

@khvn26

khvn26 commented Jun 30, 2025

Copy link
Copy Markdown
Member Author

For reference, these are free logical operations we receive with jsonpath-ng extensions:

https://github.com/h2non/jsonpath-ng?tab=readme-ov-file#extensions

@khvn26

khvn26 commented Jun 30, 2025

Copy link
Copy Markdown
Member Author

Thinking we could use jq syntax as well, if we prove the jq bindings to be performant enough.

jq is Turing-complete so this could be an overkill, or just the right solution depending on how flexible we want to allow this feature to be.

@emyller

emyller commented Jul 1, 2025

Copy link
Copy Markdown
Contributor

https://jmespath.org/ is worth mentioning — it's adopted by the AWS CLI.

Seems abandoned in GitHub.

@matthewelwell matthewelwell left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed on our call earlier today, in answer to the questions here, we plan to go ahead with JSONPath for context values in segment conditions.

We verified that there are currently no conditions in our SaaS DB that begin with $. so we can (fairly) safely assume that we can rely on that to determine if a condition is context value or a trait (although thinking about it, the user has to make a decision in defining the segment, so why don't we just store them separately in the data model or add a flag to the model?).

Comment thread docs/docs/basic-features/segments.md Outdated
@github-actions github-actions Bot added docs Documentation updates and removed docs Documentation updates labels Jul 8, 2025
matthewelwell
matthewelwell previously approved these changes Jul 8, 2025
@khvn26

khvn26 commented Jul 15, 2025

Copy link
Copy Markdown
Member Author

One more thought: Looks like we can ditch the FeatureSegment model as well if we come up with a performant enough implementation for the $.feature.name context value.

@khvn26
khvn26 force-pushed the docs/context-values-for-segments branch from 24ea414 to 3405ef0 Compare July 22, 2025 17:32
@khvn26
khvn26 marked this pull request as ready for review July 22, 2025 17:32
@khvn26
khvn26 requested a review from a team as a code owner July 22, 2025 17:32
@khvn26
khvn26 requested review from robertnorrie and removed request for a team July 22, 2025 17:32
emyller
emyller previously approved these changes Jul 22, 2025

@emyller emyller left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

Zaimwa9
Zaimwa9 previously approved these changes Jul 23, 2025

@Zaimwa9 Zaimwa9 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving because it's already good as of but I have 2 minor comments that I let you decide if worth

Comment thread docs/docs/basic-features/segments.md Outdated
Comment on lines +125 to +126
To avoid persisting the `organisation_name` trait on the user identity, mark it as
[transient](../advanced-use/transient-traits.md).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of clarity, I would add just a sentence before explaining the default behavior that would store the organisation_name and why that transient trait would be preferable in some situations

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to avoid duplicating the trait docs tbh. I think the word "persisting" gives a strong hint here.

Co-authored-by: Zaimwa9 <wadii.zaim@flagsmith.com>
Zaimwa9
Zaimwa9 previously approved these changes Jul 23, 2025
Comment thread docs/docs/basic-features/segments.md Outdated
Co-authored-by: Matthew Elwell <matthew.elwell@flagsmith.com>
@khvn26
khvn26 merged commit 126ed64 into main Jul 23, 2025
11 checks passed
@khvn26
khvn26 deleted the docs/context-values-for-segments branch July 23, 2025 13:03
khvn26 added a commit that referenced this pull request Jul 23, 2025
Co-authored-by: Zaimwa9 <wadii.zaim@flagsmith.com>
Co-authored-by: Matthew Elwell <matthew.elwell@flagsmith.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation updates front-end Issue related to the React Front End Dashboard

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Spike: Generate user documentation for functionality #5557

4 participants