Skip to main content

Transient Traits and Identities

By default, Flagsmith stores all Traits associated with an Identity to evaluate flags. This default behavior works for most use cases. However, there are scenarios where you may want more control over which Traits or Identities are stored. Using the Flagsmith API and SDKs, you can mark Traits and Identities as transient — meaning they are used for flag evaluation but are not stored.

Overview and Use Cases

Transient Identities and Traits are particularly useful when you need to run flag evaluations without storing specific data in Flagsmith. Below are common scenarios where transient Traits or Identities come in handy.

Skipping Personal Identifiable Information (PII)

You can mark sensitive Traits, such as email, phoneNumber, or locality, as transient to enable segmentation without storing the actual data. For example, you may want to target users based on email domains while avoiding the storage of email addresses.

Considering you have a Segment condition where "email" ends with "example.com". To get flags for this Segment:

flagsmith.updateContext({
identity: {
identifier: 'test-user-with-transient-email',
traits: {
email: { value: 'alice@example.com', transient: true },
},
},
});

After making the SDK call, if you check the Identities tab, you'll see the test-user-with-transient-email Identity without the email Trait being stored.

Anonymous Identities

If you choose to provide a blank ("") or null identifier, Flagsmith will automatically treat the Identity as transient. In this case, an auto-generated identifier is returned, based on a hash of the provided Traits. This ensures consistent flag evaluations without persisting the Identity in the Flagsmith dashboard.

If no Traits and a null/blank identifier provided, a random UUIDv4 identifier is generated and used for flag evaluation.

flagsmith.init({
evaluationContext: {
identity: {
identifier: null,
traits: { paymentPreference: 'cash' },
},
},
});
let identifierToUseLater = flagsmith.getContext().identity.identifier;

This approach is useful for scenarios such as A/B testing in e-commerce, where you want to include users who haven't registered or logged in.

info

Transient identifiers generated by Flagsmith API are consistent across flag evaluations, meaning the same identifier will be returned for identical sets of Traits.

To generate truly unique identifiers, include additional unique Trait values.

Ephemeral Contexts

In some cases, you may need to temporarily override certain Traits for a session or device without overwriting the stored value. For example, imagine you’ve persisted the screenOrientation Trait as landscape for a user:

flagsmith.init({
evaluationContext: {
identity: {
identifier: 'my-user',
traits: { screenOrientation: 'landscape' },
},
},
});

If you want to switch the screenOrientation to portrait for a specific session without overwriting the existing value, you can mark the Trait as transient:

flagsmith.setTrait('screenOrientation', { value: 'portrait', transient: true });

Until you set another Trait value, the screenOrientation used for flag evaluations will be portrait in this specific SDK instance. The landscape value will remain stored in Flagsmith for other evaluations.

Usage

Server-side SDKs

Mark a Trait as transient:

curl --request POST 'https://edge.api.flagsmith.com/api/v1/identities/' \
--header 'X-Environment-Key: <Your Env Key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"identifier":"identifier_5",
"traits": [
{
"trait_key": "my_trait_key",
"trait_value": 123.5,
"transient": true
},
{
"trait_key": "my_other_key",
"trait_value": true
}
]
}'

Mark the whole Identity as transient:

curl --request POST 'https://edge.api.flagsmith.com/api/v1/identities/' \
--header 'X-Environment-Key: <Your Env Key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"identifier":"identifier_5",
"traits": [
{
"trait_key": "my_trait_key",
"trait_value": 123.5
},
{
"trait_key": "my_other_key",
"trait_value": true
}
],
"transient": true
}'

Client-side SDKs

info

Transient Traits and Identities are supported starting from JavaScript SDK version 5.0.0.

Mark a Trait as transient:

flagsmith.setTrait('my_trait_key', { value: 123.5, transient: true });

Mark the whole Identity as transient:

flagsmith.setContext({
identity: {
identifier: 'my-user',
transient: true,
traits: { my_trait_key: 123.5, my_other_key: true },
},
});