Identifying and changing contexts
Read time: 12 minutes
Last edited: Feb 27, 2023
Overview
This topic explains how to use the identify feature in LaunchDarkly SDKs. Identify is available for both client-side and server-side SDKs.
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. Contexts replace another data object in LaunchDarkly: "users." To learn more, read Contexts and segments.
Creating contexts and evaluating flags based on them is supported in the latest major versions of most of our SDKs. For these SDKs, the code samples on this page include the two most recent versions.
Using identify to change contexts and users
The identify feature's behavior differs between client-side and server-side SDKs.
Client-side SDKs are configured to operate for one end user at a time, identified or anonymous. In these SDKs, the identify feature allows customers to change the context or user such as when an end user logs in or changes their settings. Identifying contexts and users causes LaunchDarkly to index them.
Server-side SDKs operate for multiple end users concurrently. Unlike in client-side SDKs, server-side SDKs do not have a notion of "changing the user context" because contexts or users are directly passed to client method invocations for actions such as evaluating flag variations. In server-side SDKs, the only impact of identifying contexts and users is that they are added to the Contexts and Users lists, respectively. However, in most applications this is not needed because they are automatically indexed when used for a flag evaluation. Instead, you should provide the evaluation object in a variation or all flags call to get the expected flag evaluation. To learn more, read Evaluating flags and Getting all flags.
To learn more about the differences in how SDKs identify and use users and contexts, read Client-side and server-side SDKs.
Details about each SDK's identify feature are available in the SDK-specific sections below.
Client-side SDKs
This feature is available in the following client-side SDKs:
- .NET (client-side)
- Android
- C/C++ (client-side)
- Electron
- Flutter
- iOS
- JavaScript
- Node.js (client-side)
- React
- React Native
- Roku
.NET (client-side)
Expand .NET (client-side) code sample
You can use the Identify
or IdentifyAsync
method to switch contexts. This loads any saved flag values for the next context, and tells the client to change the current context and obtain the feature flag values for the new context.
In some situations, the new context may be an updated version of the existing context. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. After the person logs in, you can call Identify()
or IdentifyAsync()
so that the person receives the correct feature flag settings for their account.
In other situations, the new context may be a multi-context, that is, a set of several different contexts that you want to evaluate together. For example, as soon as an end user visits your app, you may initialize the client with a context using a context kind of "device." When the end user logs in, you now also have their user and organization information. You can call Identify()
with a multi-context that contains the "device," "user," and "organization" contexts for the end user.
You may want to wait until the flag values for the new context have been loaded before proceeding. You can do this either by calling the synchronous method Identify
with a timeout, or by calling the asynchronous method IdentifyAsync
and awaiting the result.
Here's how:
var updatedContext = Context.Builder("context-key-123abc").Set("email", "sandy@example.com").Build();// Synchronous methodclient.Identify(updatedContext, TimeSpan.FromSeconds(5));// Asynchronous methodawait client.IdentifyAsync(updatedContext);
To learn more, read Identify
and IdentifyAsync
.
Android
Expand Android code sample
The identify()
method tells the client to change the current context and obtain the feature flag values for the new context.
In some situations, the new context may be an updated version of the existing context. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. You can initialize the client with an anonymous context with a context kind of "user." After the person logs in, you can update the user context and call identify()
so that the person receives the correct feature flag settings for their account.
In other situations, the new context may be a multi-context, that is, a set of several different contexts that you want to evaluate together. For example, as soon as an end user visits your app, you may initialize the client with a context using a context kind of "device." When the end user logs in, you now also have their user and organization information. You can call identify()
with a multi-context that contains the "device," "user," and "organization" contexts for the end user.
Here's how:
LDContext updatedContext = LDContext.builderFromContext(context).email("sandy@example.com").build();client.identify(updatedContext);
The identify()
call loads any saved flag values for the new user context and immediately triggers an update of the latest flags from LaunchDarkly.
identify()
returns a Future to indicate completion. If you want to be sure subsequent code is using the latest values from the server, you can wait for the Future using get
.
To learn more, read identify
.
C/C++ (client-side)
Expand C/C++ (server-side) code sample
The Identify()
method tells the client to change the current user and obtain the feature flag values for the new user.
In some situations, the new user may be an updated version of the existing user. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. After the person logs in, you can call Identify
so that the person receives the correct feature flag settings for their account.
You can use the LDClientIdentify
method to switch users:
LDClientIdentify(client, newUser);
The LDClientIdentify()
call loads any saved flag values for the new user and immediately trigger an update of the latest flags from LaunchDarkly. Because this method re-fetches flag settings for the new user, you should should not call it at high frequency. The intended use case for switching user contexts is the login/logout flow. To learn more, read LDClientIdentify
.
Electron
Expand Electron code sample
The identify()
method tells the client to change the current user and obtain the feature flag values for the new user.
In some situations, the new user may be an updated version of the existing user. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. After the person logs in, you can call identify()
so that the person receives the correct feature flag settings for their account.
Here's how:
const newUser = { key: 'user-key-123abc', name: 'Sandy' };client.identify(newUser, (newFlags) => {console.log('value of flag for this user is: ' + newFlags['flag-key-123abc']);console.log('this should be the same: ' + client.variation('flag-key-123abc'));});// or:client.identify(newUser).then((newFlags) => {// as above});
To learn more, read identify
.
Flutter
Expand Flutter code sample
The identify()
method tells the client to change the current user and obtain the feature flag values for the new user.
In some situations, the new user may be an updated version of the existing user. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. After the person logs in, you can call identify()
so that the person receives the correct feature flag settings for their account.
Here's how:
LDUser updatedUser = LDUserBuilder('user key').email('sandy@example.com').build();await LDClient.identify(updatedUser);
To learn more, read identify method.
iOS
Expand iOS code sample
The identify()
method tells the client to change the current context and obtain the feature flag values for the new context.
In some situations, the new context may be an updated version of the existing context. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. You can initialize the client with an anonymous context with a context kind of "user." After the person logs in, you can update the user context and call identify()
so that the person receives the correct feature flag settings for their account.
In other situations, the new context may be a multi-context, that is, a set of several different contexts that you want to evaluate together. For example, as soon as an end user visits your app, you may initialize the client with a context using a context kind of "device." When the end user logs in, you now also have their user and organization information. You can call identify()
with a multi-context that contains the "device," "user," and "organization" contexts for the end user.
If the client app does not identify an LDContext
, LDClient
creates an anonymous default context, which can affect which feature flags LaunchDarkly delivers to the LDClient
. Client apps should follow the Apple Privacy Policy when collecting end user information.
Here's how:
let newContext = try LDContextBuilder(key: "context-key-123abc").build().get();// You can also call identify with a completionLDClient.get()!.identify(context: newContext) {// Flags have been retrieved for the new context}
To learn more, read identify
.
JavaScript
Expand JavaScript code sample
The identify()
method tells the client to change the current context and obtain the feature flag values for the new context.
In some situations, the new context may be an updated version of the existing context. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. You can initialize the client with an anonymous context with a context kind of "user." After the person logs in, you can update the user context and call identify()
so that the person receives the correct feature flag settings for their account.
In other situations, the new context may be a multi-context, that is, a set of several different contexts that you want to evaluate together. For example, as soon as an end user visits your app, you may initialize the client with a context using a context kind of "device." When the end user logs in, you now also have their user and organization information. You can call identify()
with a multi-context that contains the "device," "user," and "organization" contexts for the end user.
If you provide a callback function, it is called with a map of flag keys and values after the flag values for the new context are available. After that point, variation()
uses the new values. You can also use a Promise for the same purpose.
Here's how:
client.identify(newContext, hash, function() {console.log("New context's flags available");});
To learn more, read identify
.
If your contexts are extraordinarily large, you may need to configure the JavaScript SDK to send the evaluation context as a request body. To learn more, read Using REPORT in the JavaScript SDK.
The hash parameter is the hash for the new context, assuming that the context's key has changed. The hash parameter is only required in secure mode. If secure mode is not enabled, pass in null
for the hash.
Node.js (client-side)
Expand Node.js (client-side) code sample
The identify()
method tells the client to change the current user and obtain the feature flag values for the new user.
In some situations, the new user may be an updated version of the existing user. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. After the person logs in, you can call identify()
so that the person receives the correct feature flag settings for their account.
In other situations, the new context may be a multi-context, that is, a set of several different contexts that you want to evaluate together. For example, as soon as an end user visits your app, you may initialize the client with a context using a context kind of "device." When the end user logs in, you now also have their user information. You can call identify()
with a multi-context that contains the "device" and "user" contexts for the end user.
Here's how:
client.identify(newContext, () => {console.log("New context's flags available");});// or, with a Promise:client.identify(newContext).then(() => {console.log("New context's flags available");});
To learn more, read identify
.
React
Expand React code sample
The identify()
method tells the client to change the current context and obtain the feature flag values for the new context.
In some situations, the new context may be an updated version of the existing context. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. You can initialize the client with an anonymous context with a context kind of "user." After the person logs in, you can update the user context and call identify()
so that the person receives the correct feature flag settings for their account.
In other situations, the new context may be a multi-context, that is, a set of several different contexts that you want to evaluate together. For example, as soon as an end user visits your app, you may initialize the client with a context using a context kind of "device." When the end user logs in, you now also have their user and organization information. You can call identify()
with a multi-context that contains the "device," "user," and "organization" contexts for the end user.
Here's how:
import { useLDClient } from 'launchdarkly-react-client-sdk';let ldClient = useLDClient();ldClient.identify(newContext, null, () => {console.log("New context's flags available");});
To learn more, read identify
.
React Native
Expand React Native code sample
If multiple customers use your app on a single device, you may want to change contexts and have separate flag settings for each one. To do this, the SDK stores the last five user contexts on a single device, and supports switching between different user contexts. You can use the identify
method to switch user contexts. identify
loads any saved flag values for the new context and immediately triggers an update of the latest flags from LaunchDarkly.
In other situations, the new context may be a multi-context, that is, a set of several different contexts that you want to evaluate together. For example, as soon as an end user visits your app, you may initialize the client with a context using a context kind of "device." When the end user logs in, you now also have their user and organization information. You can call identify()
with a multi-context that contains the "device," "user," and "organization" contexts for the end user.
Here's how:
let context = {'key': 'user-key-123abc', 'kind': 'user'};await client.identify(context); // promise resolves to nullAlert.alert('identify', 'success');
Roku
Expand Roku code sample
The identify()
method tells the client to change the current user and obtain the feature flag values for the new user.
In some situations, the new user may be an updated version of the existing user. For example, on a sign-in page in a single-page app, you could represent the same person as an anonymous user before they log in, and a different user after they log in. After the person logs in, you can call identify()
so that the person receives the correct feature flag settings for their account.
To do this:
user = LaunchDarklyUser("abc")launchDarkly.identify(user)
Server-side SDKs
This feature is available in the following server-side SDKs:
- .NET (server-side)
- Apex
- C/C++ (server side)
- Erlang
- Go
- Haskell
- Java
- Lua
- Node.js (server side)
- PHP
- Python
- Ruby
- Rust
.NET (server-side)
Expand .NET (server-side) code sample
Identify
creates or updates contexts on LaunchDarkly, which makes them available for targeting and autocomplete on the Contexts list.
In most cases, you do not need to call Identify
. The Variation
methods automatically create contexts on the Contexts list for you. Identify
is useful if you want to pre-populate your Contexts list before you launch any features.
To use Identify
:
client.Identify(context);
To learn more, read Identify
.
Apex
Expand Apex code sample
identify
creates or updates users in LaunchDarkly, which makes them available for targeting and autocomplete on the Users list.
Variation methods like boolVariation
or stringVariation
do not automatically generate index
events due to limitations with the Apex platform. This means that the SDK does not automatically populate the Users list.
We recommend calling client.identify(user)
with each of your users to generate user indexing events and populate your Users list.
To use identify
:
client.identify(user);
To learn more, read Other methods.
C/C++ (server-side)
Expand C/C++ (server-side) code sample
LDClientIdentify
creates or updates users in LaunchDarkly, which makes them available for targeting and autocomplete on the Users list.
In most cases, you do not need to call LDClientIdentify
. The variation
call automatically creates users on the Users list for you. LDClientIdentify
is useful if you want to pre-populate your Users list before you launch any features.
To use identify
:
LDClientIdentify(client, user);
To learn more, read LDClientIdentify
.
Erlang
Expand Erlang code sample
identify
creates or updates contexts in LaunchDarkly, which makes them available for targeting and autocomplete on the Contexts list.
In most cases, you do not need to call identify
. The variation
methods automatically create contexts on the Contexts list for you. identify
is useful if you want to pre-populate your Contexts list before you launch any features.
To use identify
:
ldclient:identify(#{key => <<"context-key-123abc">>})
To learn more, read identify
.
Go
Expand Go code sample
Identify
creates or updates contexts in LaunchDarkly, which makes them available for targeting and autocomplete on the Contexts list.
In most cases, you do not need to call Identify
. The Variation
method automatically creates contexts on the Contexts list for you. Identify
is useful if you want to pre-populate your Contexts list before you launch any features.
To use Identify
:
client.Identify(context)
To learn more, read Identify
.
Haskell
Expand Haskell code sample
identify
creates or updates contexts in LaunchDarkly, which makes them available for targeting and autocomplete on the Contexts list.
In most cases, you do not need to call identify
. The Variation
methods automatically create contexts on the Contexts list for you. identify
is useful if you want to pre-populate your Contexts list before you launch any features.
To use identify
:
identify client context
To learn more, read identify
.
Java
Expand Java code sample
identify
creates or updates contexts in LaunchDarkly, which makes them available for targeting and autocomplete on the Contexts list.
In most cases, you do not need to call identify
. The variation
methods automatically create contexts on the Contexts list for you. identify
is useful if you want to pre-populate your Contexts list before you launch any features.
To use identify
:
client.identify(context);
To learn more, read identify
.
Lua
Expand Lua code sample
identify
creates or updates users in LaunchDarkly, which makes them available for targeting and autocomplete on the Users list.
In most cases, you do not need to call identify
. The variation
methods automatically create users on the Users list for you. identify
is useful if you want to pre-populate your Users list before you launch any features.
To use identify
:
client:identify(user)
To learn more, read identify
.
Node.js (server-side)
Expand Node.js (server-side) code sample
identify
creates or updates contexts in LaunchDarkly, which makes them available for targeting and autocomplete on the Contexts list.
In most cases, you do not need to call identify
. The variation
call automatically creates users on the Contexts list for you. identify
is useful if you want to pre-populate your Contexts list before you launch any features.
To use identify
:
client.identify(context);
To learn more, read identify
.
PHP
Expand PHP code sample
identify
creates or updates users in LaunchDarkly, which makes them available for targeting and autocomplete on the Contexts list.
In most cases, you do not need to call identify
. The variation
call automatically creates contexts on the Contexts list for you. identify
is useful if you want to pre-populate your Contexts list before you launch any features.
To use identify
:
$client->identify($context);
To learn more, read identify
.
Python
Expand Python code sample
identify
creates or updates contexts in LaunchDarkly, which makes them available for targeting and autocomplete on the Contexts list.
In most cases, you do not need to call identify
. The variation
method automatically creates contexts on the Contexts list for you. identify
is useful if you want to pre-populate your Contexts list before you launch any features.
To use identify
:
ldclient.get().identify(context)
To learn more, read identify
.
Ruby
Expand Ruby code sample
identify
creates or updates contexts in LaunchDarkly, which makes them available for targeting and autocomplete on the Contexts list.
In most cases, you do not need to call identify
. The variation
call automatically creates contexts on the Contexts list for you. identify
is useful if you want to pre-populate your Contexts list before you launch any features.
To use identify
:
client.identify(context)
To learn more, read identify
.
Rust
Expand Rust code sample
identify
creates or updates users in LaunchDarkly, which makes them available for targeting and autocomplete on the Users list.
In most cases, you do not need to call identify
. The variation
methods automatically create users or contexts on the Users or Contexts list for you. identify
is useful if you want to pre-populate your Users or Contexts list before you launch any features.
To use identify
:
client.identify(context);
To learn more, read identify
.