• HOME
  • INTEGRATIONS
  • SDKS
  • GUIDES
  • API DOCS
No results for ""
EXPAND ALL
launchdarkly.com

EDIT ON GITHUB

Identifying and changing users

Read time: 7 minutes
Last edited: Jun 18, 2021

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 indexed in the LaunchDarkly service. However, in most applications this is not needed because users are automatically indexed when used for flag evaluation.

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:

Android

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:

1LDUser updatedUser = new LDUser.Builder(user)
2 .email("fake2@example.com")
3 .build();
4
5ldClient.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)

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:

1LDClientIdentify(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. Since 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

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 see 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:

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

Flutter

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:

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

iOS

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:

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

JavaScript

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 see 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:

1ldclient.identify(newUser, hash, function() {
2 console.log("New user's flags available");
3});
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)

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.

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

React Native

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:

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

Roku

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:

1user = LaunchDarklyUser("abc")
2
3launchDarkly.identify(user)

Server-side SDKs

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

.NET

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

In most cases, you do not need to call Identify. The Variation methods automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1ldClient.Identify(user);

Apex

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

In most cases, you do not need to call Identify. The Variation methods automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1client.identify(user);

C/C++ (server-side)

LDClientIdentify creates or updates users in LaunchDarkly, which makes them available for targeting and autocomplete on the dashboard.

In most cases, you do not need to call LDClientIdentify. The variation call automatically creates users on the dashboard for you. LDClientIdentify is useful if you want to pre-populate your dashboard before you launchd any features.

1LDClientIdentify(client, user);

Erlang

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

In most cases, you do not need to call Identify. The Variation methods automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

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

Go

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

In most cases, you do not need to call Identify. The Variation method automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1ldClient.Identify(user)

Haskell

In most cases, you do not need to call Identify. The Variation method automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1identify client user

Java

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

In most cases, you do not need to call Identify. The Variation method automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1ldClient.identify(user);

Lua

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

In most cases, you do not need to call Identify. The Variation call automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1client:identify(user)

Node.js (server-side)

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

In most cases, you do not need to call Identify. The Variation call automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1ldClient.identify(user);

PHP

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

In most cases, you do not need to call Identify. The Variation call automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1$client->identify(user);

Python

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

In most cases, you do not need to call Identify. The Variation call automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1ldclient.get().identify(user)

Ruby

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

In most cases, you do not need to call Identify. The Variation call automatically creates users on the dashboard for you. Identify is useful if you want to pre-populate your dashboard before you launch any features.

1ld_client.identify(user)