Identifying and changing contexts

Overview

This topic explains how to use the identify feature in LaunchDarkly SDKs. Identify is available for client-side and server-side SDKs.

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 you to change the context, such as when an end user logs in or changes their settings. Identifying contexts and users causes LaunchDarkly to index them.

To associate two contexts with each other, such as your representations of an end user before and after they log in, call identify with a multi-context that contains both contexts. The association between the two contexts that comprise your multi-context doesn’t persist between sessions. You must call identify with the multi-context each time.

Here is an example of a multi-context, though each SDK sends context data to LaunchDarkly in a slightly different format:

Example multi-context
1{
2 "kind": "multi",
3 "user": {
4 "key": "user-key-123abc",
5 "name": "Sandy",
6 "email": "sandy@example.com",
7 },
8 "device": {
9 "key": "device-key-123abc",
10 "type": "iPhone",
11 "deviceId": 12345
12 }
13}

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 list. 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, server-side, and edge SDKs.

Newer versions of LaunchDarkly SDKs replace users with contexts

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.

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.

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)

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. The identify method tells the client to change the current context and obtain the feature flag values for the new context. In the .NET SDK, you can use either the Identify or IdentifyAsync method to switch contexts.

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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 general, however, we recommend composing the new context as a multi-context based on multiple identifiers, rather than only pre- and post-login contexts. 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:

1var updatedContext = Context.Builder("context-key-123abc")
2 .Set("email", "sandy@example.com")
3 .Build();
4
5// Synchronous method
6client.Identify(updatedContext, TimeSpan.FromSeconds(5));
7
8// Asynchronous method
9await client.IdentifyAsync(updatedContext);

To learn more, read Identify and IdentifyAsync.

Android

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. 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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 based on multiple identifiers, rather than only pre- and post-login contexts. 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:

1LDContext updatedContext = LDContext.builderFromContext(context)
2 .email("sandy@example.com")
3 .build();
4
5client.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++ (client-side)

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. 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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 based on multiple identifiers, rather than only pre- and post-login contexts. 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 to update your initial context when you have additional information:

C++ SDK v3 (native)
1/* before end user logs in */
2auto context = ContextBuilder()
3 .Kind("device", "device-key-123abc")
4 .Build();
5
6/* after end user logs in */
7auto updated_context = ContextBuilder(context)
8 .Kind("user", "user-key-123abc")
9 .Name("Sandy")
10 .Kind("organization", "org-key-123abc")
11 .Name("Global Health Services")
12 .Build();

After you update the context, you must call identify with the updated context in order to evaluate flags based on the updated information.

Here’s how to use the identify method to switch contexts:

1client.IdentifyAsync(updated_context);

You can also examine the result to determine if the identification succeeded. Here’s how:

1auto identify_result = client.IdentifyAsync(updated_context);
2auto status = identify_result.wait_for(maxwait);
3
4if (status == std::future_status::ready) {
5 /* The client's attempt to identify succeeded or failed in the specified amount of time. */
6 if (identify_result.get()) {
7 /* Identification succeeded */
8 } else {
9 /* Identification failed */
10 }
11} else {
12 /* The specified timeout was reached, but the client is still identifying. */
13}

The IdentifyAsync call loads any saved flag values for the new context and immediately triggers an update of the latest flags from LaunchDarkly. Because this method re-fetches flag settings for the updated context, you should not call it at high frequency. The intended use case for switching contexts is the login/logout flow. To learn more, read IdentifyAsync.

Electron

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. 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 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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 based on multiple identifiers, rather than only pre- and post-login contexts. 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:

1const newUser = { key: 'user-key-123abc', name: 'Sandy' };
2
3client.identify(newUser, (newFlags) => {
4 console.log('value of flag for this user is: ' + newFlags['flag-key-123abc']);
5 console.log('this should be the same: ' + client.variation('flag-key-123abc'));
6});
7
8// or:
9client.identify(newUser).then((newFlags) => {
10 // as above
11});

To learn more, read identify.

Flutter

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. 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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 based on multiple identifiers, rather than only pre- and post-login contexts. 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:

1final updatedContext = LDContextBuilder()
2 .kind('user', 'user-key-123abc')
3 .setString('email', 'sandy@example.com'))
4 .build();
5
6await client.identify(updatedContext);

To learn more, read identify.

iOS

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. 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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 based on multiple identifiers, rather than only pre- and post-login contexts. 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:

1let newContext = try LDContextBuilder(key: "context-key-123abc").build().get();
2
3// You can also call identify with a completion
4LDClient.get()!.identify(context: newContext) {
5 // Flags have been retrieved for the new context
6}

To learn more, read identify.

JavaScript

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. 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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 based on multiple identifiers, rather than only pre- and post-login contexts. 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:

1client.identify(newContext, hash, function() {
2 console.log("New context's flags available");
3});

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.

You must use a hash parameter while in secure mode

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)

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. 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 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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 based on multiple identifiers, rather than only pre- and post-login contexts. 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:

1client.identify(newContext, () => {
2 console.log("New context's flags available");
3});
4
5// or, with a Promise:
6client.identify(newContext).then(() => {
7 console.log("New context's flags available");
8});

To learn more, read identify.

React

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. 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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 based on multiple identifiers, rather than only pre- and post-login contexts. 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:

JavaScript
1import { useLDClient } from 'launchdarkly-react-client-sdk';
2
3let ldClient = useLDClient();
4
5ldClient.identify(newContext, null, () => {
6 console.log("New context's flags available");
7});

To learn more, read identify.

React Native

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. The identify method tells the client to change the current context and obtain the feature flag values for the new context.

Version 10 requires an identify call

In the React Native SDK version 10, you do not provide a context when you initialize the ReactNativeLDClient. You must call identify before you can evaluate a feature flag for a specific context. End users will receive fallback values until you call specify a context by calling identify.

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 contexts on a single device using react-native-async-storage, and supports switching between different contexts. You can use the identify method to switch contexts. identify loads any saved flag values for the new context and immediately triggers an update of the latest flags from LaunchDarkly.

You might also call identify when your app gathers additional information about the end user. 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. Then you can evaluate flags using attributes from all three context kinds together.

Here’s how:

1import { useLDClient } from '@launchdarkly/react-native-client-sdk';
2
3const client = useLDClient();
4const context: LDContext = {'key': 'user-key-123abc', 'kind': 'user'};
5 client
6 .identify(context)
7 .catch((e: any) => console.error(`error identifying ${context.key}: ${e}`));

To learn more, read identify.

Roku

Client-side SDKs are configured to operate for one context at a time, whether identified or anonymous. 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 a multi-context that combines a pre-login anonymous context and a post-login context with the “user” kind. 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 based on multiple identifiers, rather than only pre- and post-login contexts. 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.

To do this:

1' before the end user logs in
2device = {
3 "kind": "device",
4 "key": "device-key-123abc",
5 "type": "tablet"
6}
7context = LaunchDarklyCreateContext(device)
8
9' after the end user logs in
10multi = {
11 "kind": "multi",
12 "device": device,
13 "user": {"key": "user-key-123abc", "name": "Sandy"},
14 "organization": {"key": "org-key-123abc", "name": "Acme, Inc."}
15}
16
17launchDarkly.identify(multi)

Server-side SDKs

This feature is available in the following server-side SDKs:

.NET (server-side)

In server-side SDKs, 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, using the context you pass to each Variation method. Identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use Identify:

1client.Identify(context);

To learn more, read Identify.

Apex

In server-side SDKs, identify creates or updates users in LaunchDarkly, which makes them available for targeting and autocomplete on the Users list.

To use identify:

Apex
1client.identify(user);

To learn more, read Other methods.

C++ (server-side)

In server-side SDKs, 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, using the evaluation context you pass to each variation method. Identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use Identify:

1client.Identify(context);

To learn more, read Identify() in Client.

Erlang

In server-side SDKs, 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, using the context you pass to each variation method. identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use identify:

1ldclient:identify(#{key => <<"context-key-123abc">>})

To learn more, read identify.

Go

In server-side SDKs, 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, using the context you pass to each Variation method. Identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use Identify:

1client.Identify(context)

To learn more, read Identify.

Haskell

In server-side SDKs, 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, using the context you pass to each variation method. identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use identify:

1identify client context

To learn more, read identify.

Java

In server-side SDKs, 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, using the context you pass to each variation method. identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use identify:

1client.identify(context);

To learn more, read identify.

Lua

In server-side SDKs, 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, using the context you pass to each variation method. identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use identify:

1client:identify(context)

To learn more, read identify.

Node.js (server-side)

In server-side SDKs, 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, using the context you pass to each variation method. identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use identify:

1client.identify(context);

To learn more, read identify.

PHP

In server-side SDKs, 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, using the context you pass to each variation call. identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use identify:

1$client->identify($context);

To learn more, read identify.

Python

In server-side SDKs, 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, using the context you pass to each variation method. identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use identify:

1ldclient.get().identify(context)

To learn more, read identify.

Ruby

In server-side SDKs, 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, using the context you pass to each variation method. identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use identify:

1client.identify(context)

To learn more, read identify.

Rust

In server-side SDKs, 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 methods automatically create users or contexts on the Contexts list for you, using the context you pass to each Variation method. identify is useful if you want to pre-populate your Contexts list before you launch any features.

To use identify:

1client.identify(context);

To learn more, read identify.

Built with