• Home
  • Integrations
  • SDKs
  • Guides
  • API docs
No results for ""
EXPAND ALL

EDIT ON GITHUB

Identifying and changing users

Read time: 8 minutes
Last edited: Jul 29, 2022

Overview

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

Using identify to change users

Identify's behavior differs between client-side and server-side SDKs.

Client-side SDKs are configured to operate for one user at a time, identified or anonymous. In these SDKs, the identify feature allows customers to change the user context, such as if a user logs in or changes their user settings. Identifying users will cause LaunchDarkly to index them.

Server-side SDKs operate for multiple users concurrently. Unlike in client-side SDKs, server-side SDKs do not have a notion of "changing the user context" because users are directly passed to client method invocations for actions such as evaluating flag variations. In server-side SDKs, the only impact of identifying users is that they are added to the Users list. However, in most applications this is not needed because users are automatically indexed when used for a flag evaluation. Instead, you should provide the user 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 user 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
  • Server-side SDKs

Client-side SDKs

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

.NET (client-side)

Expand .NET (client-side) code sample

You can use the Identify or IdentifyAsync method to switch user contexts. This loads any saved flag values for the new user, and immediately triggers an update of the latest flags from LaunchDarkly.

You may want to wait until the flag values for the new user 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 updatedUser = LDUser.Builder("user-key")
.Email("user@example.com")
.Build();
// Synchronous method
client.Identify(updatedUser, TimeSpan.FromSeconds(5));
// Asynchronous method
await client.IdentifyAsync(updatedUser);

To learn more, read Identify and IdentifyAsync.

Android

Expand Android code sample

If multiple users use your app on a single device, you may want to change users and have separate flag settings for each one. To do this, the SDK stores the last five user contexts on a single device, with support for switching between different user contexts. You can use the identify method to switch user contexts. identify loads any saved flag values for the new user and immediately triggers an update of the latest flags from LaunchDarkly.

Here's how:

LDUser updatedUser = new LDUser.Builder(user)
.email("fake2@example.com")
.build();
client.identify(updatedUser);

The identify call loads any saved flag values for the new user 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.

C/C++ (client-side)

Expand C/C++ (server-side) code sample

If your app is used by multiple users on a single device, you may want to change users and have separate flag settings for each user. To achieve this, the SDK supports switching between different user contexts.

You can use the LDClientIdentify method to switch user contexts:

LDClientIdentify(client, newUser);

The 'identify' call will load 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, it should not be called at high frequency. The intended use case for switching user contexts is the login/logout flow.

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. The client always has one current user, because client-side SDKs are not designed for evaluating flags for different users at the same time.

For example, on a sign-in page in a single-page app, you can initialize the client with an anonymous user. But when the user logs in, they should receive the correct feature flag settings for their account.

If you provide a callback function, it is called with a map of flag keys and values after the flag values for the new user are available. After that point, variation() uses the new values. You can also use a Promise for the same purpose.

Here's how:

const newUser = { key: 'someone-else', name: 'John' };
client.identify(newUser, (newFlags) => {
console.log('value of flag for this user is: ' + newFlags['YOUR_FEATURE_KEY']);
console.log('this should be the same: ' + client.variation('YOUR_FEATURE_KEY'));
});
// or:
client.identify(newUser).then((newFlags) => {
// as above
});

Flutter

Expand Flutter code sample

If multiple users use your app on a single device, you may want to change users and have separate flag settings for each one. To do this, the SDK stores the last five user contexts on a single device, with support for switching between different user contexts. You can use the identify method to switch user contexts. identify loads any saved flag values for the new user and immediately triggers an update of the latest flags from LaunchDarkly.

Here's how:

LDUser updatedUser = LDUserBuilder('user key')
.email('fake2@example.com')
.build();
await LDClient.identify(updatedUser);

iOS

Expand iOS code sample

If multiple users use your app on a single device, you may want to change users and have separate flag settings for each one. To do this, the SDK stores the last five user contexts on a single device, with support for switching between different user contexts. You can use the identify method to switch user contexts.

When a new user is identified, the SDK temporarily goes offline while reconnecting on behalf of the new user. Upon successfully reconnecting, the SDK comes back online and calls the optional completion if one was provided.

If the client app does not identify an LDUser, LDClient creates an anonymous default user, which can affect which feature flags LaunchDarkly delivers to the LDClient. Client apps should follow the Apple Privacy Policy when collecting user information.

identify loads any saved flag values for the new user and immediately triggers an update of the latest flags from LaunchDarkly.

Here's how:

let newUser = LDUser(key: "NEW_USER_KEY")
LDClient.get()!.identify(user: newUser)
// identify can also be called with a completion
LDClient.get()!.identify(user: newUser) {
// Flags have been retrieved for the new user
}

JavaScript

Expand JavaScript code sample

The identify() method tells the client to change the current user and obtain the feature flag values for the new user. The client always has one current user, because client-side SDKs are not designed for evaluating flags for different users at the same time.

For example, on a sign-in page in a single-page app, you can initialize the client with an anonymous user. But when the user logs in, they should receive the correct feature flag settings for their account.

If you provide a callback function, it is called with a map of flag keys and values after the flag values for the new user 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(newUser, hash, function() {
console.log("New user's flags available");
});
You must use a hash parameter while in secure mode

The hash parameter is the hash for the new user, assuming that the user'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

You may want to change the user context dynamically and receive the new set of feature flags for that user or generate events for the new user. To do this, you can call the identify function. You can also pass a callback that is called when the new flags are in effect.

client.identify(newUser, () => {
console.log("New user's flags available");
});
// or, with a Promise:
client.identify(newUser).then(() => {
console.log("New user's flags available");
});

React

Expand React code sample

The identify() method tells the client to change the current user and obtain the feature flag values for the new user. The client always has one current user, because client-side SDKs are not designed for evaluating flags for different users at the same time. You can also pass a callback that is called when the new flags are in effect.

import { useLDClient } from 'launchdarkly-react-client-sdk';
let ldClient = useLDClient();
ldClient.identify(newUser, null, () => {
console.log("New user's flags available");
});

To learn more, read identify.

React Native

Expand React Native code sample

If multiple users use your app on a single device, you may want to change users and have separate flag settings for each one. To do this, the SDK stores the last five user contexts on a single device, with support for switching between different user contexts. You can use the identify method to switch user contexts. identify loads any saved flag values for the new user and immediately triggers an update of the latest flags from LaunchDarkly.

Here's how:

let user = {'key': 'minimal_user'};
await client.identify(user); //promise resolves to nil/null
Alert.alert('identify', 'success');

Roku

Expand Roku code sample

If multiple users use your app on a single device, you may want to change users and have separate flag settings for each one. To do this, the SDK stores the last five user contexts on a single device, with support for switching between different user contexts. You can use the identify method to switch user contexts. identify loads any saved flag values for the new user and immediately triggers an update of the latest flags from LaunchDarkly. Because this method re-fetches flag settings for the new user, do not call it frequently.

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)

Expand .NET (server-side) code sample

Identify creates or updates users on 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 creates 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);

Apex

Expand Apex code sample

Identify creates or updates users in LaunchDarkly, which makes them available for targeting and autocomplete on the Users list.

Apex variation methods do not automatically add users to 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);

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);

Erlang

Expand Erlang 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 creates 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:

ldclient:identify(#{key => <<"aa0ceb">>})

Go

Expand Go 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 method automatically creates 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)

Haskell

Expand Haskell code sample

In most cases, you do not need to call Identify. The Variation method automatically creates 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:

identify client user

Java

Expand Java 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 method automatically creates 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);

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 call automatically creates 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)

Node.js (server-side)

Expand Node.js (server-side) 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 call automatically creates 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);

PHP

Expand PHP 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 call automatically creates 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);

Python

Expand Python 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 call automatically creates 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:

ldclient.get().identify(user)

Ruby

Expand Ruby 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 call automatically creates 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)

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 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);