Skip to main content

Segments

A segment is a subset of identities, defined by a set of rules that match identity traits. An identity always belongs to a single environment and can belong to any number of segments.

Overview

Once you have defined a segment, you can create segment overrides for features within an environment. A segment override allows you to control the state of a feature only for identities that belong to a specific segment. This is similar to how identity overrides let you control the state of features for an explicit set of identities that is known in advance.

Because segments are driven by identity traits, your application must identify the user when retrieving flags in order for segment overrides to be applied. If your user is not identified, no overrides will be applied and all flags will be returned exactly how they are defined in the current environment.

Click to see common implementation scenarios
// Set up internal user traits
flagsmith.identify('user_123');
flagsmith.setTrait('email', 'employee@company.com');
flagsmith.setTrait('department', 'engineering');

// Segment Rule: email Contains @company.com AND department = engineering

Common uses:

  • Internal feature testing
  • Beta testing with employees
  • QA environment simulation

Security and privacy

The Flagsmith API to set user traits, e.g. the setTraits method from the JavaScript SDK, does not require authentication or credentials. This means that users can change their own traits, which could be a security problem if you are using segments for authorisation or access control.

Security Recommendations

Segment definitions are:

  • Not included in remote evaluation mode responses
  • Only served to clients in local evaluation mode
  • Never exposed through SDK flag retrieval methods

Creating Segments

Project-wide Segments

  1. Navigate to Segments page in dashboard
  2. Create segment with rules
  3. Apply segment override to any feature

Feature-specific Segments

  1. Go to specific feature
  2. Open Segment Overrides tab
  3. Click "Create Feature-Specific Segment"
  4. Define segment rules

Note: Project segments cannot be converted to feature-specific segments or vice versa.

Rule Evaluation

Order of Rules

Rules are evaluated top-to-bottom. Example impact:

// Scenario 1:
// 1. 10% percentage split
// 2. is_subscriber = true
// Result: 10% of ALL users, then filter for subscribers

// Scenario 2:
// 1. is_subscriber = true
// 2. 10% percentage split
// Result: ALL subscribers, then take 10%

Flag evaluation precedence

Trait Data Types

Supported trait types:

  • String
  • Boolean
  • Integer
  • Float

Type Coercion

  • Segment rule values stored as strings
  • Coerced to match trait type during evaluation
  • Provides backwards compatibility

Examples:

// Boolean trait
flagsmith.setTrait('accepted_cookies', true);

// String trait - both work
flagsmith.setTrait('accepted_cookies', 'true');
flagsmith.setTrait('accepted_cookies', 'partial');

These string values evaluate to true:

  • "True"
  • "true"
  • "1"

Rule Operators Reference

Comparison Operators

tip

All rule operators are case-sensitive.

OperatorDescriptionExample
Exactly Matches (=)Trait value equals rule valueage = 21
Does not match (!=)Trait value not equal to rule valuecountry != US
>Trait value greater than rule valuelogins > 10
>=Trait value greater than/equal to rule valuespent >= 100
<Trait value less than rule valueage < 18
<=Trait value less than/equal to rule valueversion <= 2.1

String Operators

OperatorDescriptionExample
ContainsRule value is substring of trait valueemail Contains @company.com
Does not containRule value not substring of trait valueplan Does not contain free
Matches regexTrait matches regular expressionemail Matches .*@gmail\.com
InTrait equals any value in comma-separated listcountry In US,UK,CA

Special Operators

OperatorDescriptionExample
% SplitIdentity in percentage bucket% Split = 25
Is setTrait exists for identitypremium_until Is set
Is not setTrait doesn't exist for identitytrial_ended Is not set
SemVerSemantic version comparisonversion SemVer >= 2.1.0
ModuloRemainder after division`user_id % 2

Operator Details

The In operator lets you match a trait value against a comma-separated list of values. For example, the segment rule value might read 21,682,8345. This would match against a trait value of 682 but not against a trait value of 683 or 834.

The In operator can be useful to build segments that represent a specific set of tenants in your application. For example, you could create a segment with the following rule: tenant_id In tenant_1,tenant_2,tenant_3

Performance Considerations

When using local evaluation mode, segments provide optimal performance:

  • Sub-millisecond evaluation time
  • No network requests needed
  • Ideal for high-traffic scenarios

Best practices:

  • Keep segment rules focused and minimal
  • Avoid large regex patterns or extensive IN lists
  • Monitor segment evaluation times
  • Consider complexity vs performance tradeoffs

Minimum SDK Versions

When running in local evaluation mode, SDK clients evaluate segment rules locally, which means they must be updated to support the latest operators.

If an SDK client tries to evaluate a segment rule that has an unrecognised operator, that rule will silently evaluate to false. The table below lists the minimum required SDK version required by each operator:

ModuloIn
Python2.3.03.3.0
Java5.1.07.1.0
.NET4.2.05.0.0
Node.js2.4.02.5.0
Ruby3.1.03.2.0
PHP3.1.04.1.0
Go2.2.03.1.0
Rust0.2.01.3.0
Elixir1.1.02.0.0

Limits

These are the default limits for segments and rules:

  • 100 segments per project
  • 100 segment overrides per environment
  • 100 rules per segment override
  • 1000 bytes per segment rule value

See the documentation on System Limits for more details.

Custom fields

Optional or required custom fields can be defined when creating or updating segments. Learn more