Anonymous contexts and users
Read time: 4 minutes
Last edited: Mar 16, 2023
Overview
This topic explains how to configure contexts as anonymous in LaunchDarkly SDKs. These features are 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.
Each SDK lets you designate anonymous contexts or users. Anonymous contexts or users don't appear on your Contexts list, so you can't search for them, and you can't search for or autocomplete by their keys. If you use multi-contexts, you can choose to make only some contexts anonymous.
In client-side SDKs, if you don't provide a key or set it to null, and set anonymous
to true
, then the SDK generates a random key for you.
If you generate keys for anonymous contexts, session IDs or UUIDs work best. If you want to reduce your client-side MAU, you can use the same key for every initialization and then replace that with a different key, unique to each context, when you know which person or entity the context represents. This way LaunchDarkly counts the initialization key only once against your client-side MAU, instead of every time you initialize. However, you cannot use this method if you use Experimentation, because all contexts you include in an experiment must have a unique key.
Details about each SDK's configuration are available in the SDK-specific sections below.
Client-side SDKs
Here are the configuration options for anonymous contexts and users in client-side SDKs.
- .NET (client-side)
- Android
- C/C++ (client-side)
- Electron
- Flutter
- iOS
- JavaScript
- Node.js (client-side)
- React Native
- React Web: The React SDK relies on the JavaScript SDK for context-related functionality.
- Roku
.NET (client-side)
Expand .NET (client-side) code sample
To distinguish logged-in end users from anonymous end users in the SDK:
Context context = Context.Builder("context-key-123abc").Anonymous(true).Build();
To auto-generate a key for any context whose anonymous
attribute is true:
Configuration config = Configuration.Builder("mobile-key-123abc").GenerateAnonymousKeys(true).Build();
If you set this option, you must still specify a non-null key as a placeholder when you construct the Context
, because the SDK does not allow a Context
to exist with a null key. When you pass this context to SDK methods like Init
or Identify
, the SDK replaces the placeholder key with a generated key.
In this example, the placeholder key is "unknown-context-key", but it could be any non-empty string:
Context context = Context.Builder("unknown-context-key").Anonymous(true).Build();
Android
Expand Android code sample
To distinguish logged-in end users from anonymous end users in the SDK:
LDContext context = LDContext.builder("context-key-123abc").anonymous(true).build();
When you mark the context as anonymous, you can leave the key parameter in the Builder null or make it an empty string. The client will automatically set it to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.
C/C++ (client-side)
Expand C/C++ (client-side) code sample
To distinguish logged-in end users from anonymous end users in the SDK:
struct LDUser *user = LDUserNew("user-key-123abc");LDUserSetAnonymous(user, true);
Electron
Expand Electron code sample
To distinguish logged-in end users from anonymous end users in the SDK:
const anonymousUser2 = { key: 'user-key-123abc', anonymous: true };
To create an anonymous user with an auto-generated key, specify the "anonymous" property and omit the "key" property. The LaunchDarkly client creates a unique key for this user and caches it locally:
const anonymousUser = { anonymous: true };
Flutter
Expand Flutter code sample
To distinguish logged-in end users from anonymous end users in the SDK:
LDUser user = LDUserBuilder('user key').anonymous(true).build();
iOS
Expand iOS code sample
To distinguish logged-in end users from anonymous end users in the SDK:
var contextBuilder = LDContextBuilder(key: "context-key-123abc")contextBuilder.anonymous(true)let context = contextBuilder.build().get()
Alternatively, you can omit the key parameter. The client will automatically set the isAnonymous
property for the context, and set the key to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.
Here's how:
// Have the SDK use a device persistent key.// This sets `isAnonymous` by default.let context = try LDContextBuilder().build().get()
JavaScript
Expand JavaScript code sample
To create an anonymous context, specify the anonymous
property and omit the key
property. The client will automatically set the key to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.
Here's how:
const anonymousUserContext = {kind: 'user',anonymous: true};// A multi-context can contain both anonymous and non-anonymous contexts.// Here, the organization is not anonymous.const multiContext = {kind: 'multi',user: anonymousUserContext,org: {key: 'org-key-123abc',name: 'Example organization name'}}
Node.js (client-side)
Expand Node.js (client-side) code sample
To distinguish logged-in end users from anonymous end users in the SDK:
const anonymousContext = { kind: 'user', key: 'user-key-123abc', anonymous: true };
You can also have the SDK generate the key for you. Specify the anonymous
property and omit the key
property. The client will automatically set the key to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.
Here's how:
const anonymousContext = { kind: 'user', anonymous: true };
React Native
Expand React Native code sample
To create an create an anonymous context, specify the anonymous
property. If you omit the context key when building an anonymous context, the client will automatically set it to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots. If you omit the context key and do not mark the context as anonymous, the SDK gives a usage error.
Here's how:
// This device context is anonymous// The key is omitted, and the SDK will automatically generate oneconst deviceContext = {kind: 'device',deviceId: '12345',anonymous: true}// The multi-context contains one anonymous context// and one non-anonymous contextconst multiContext = {kind: 'multi',user: userContext,device: deviceContext}
In the 6.x versions of the SDK, the user key is required when building anonymous users. To learn more, read Understanding changes to anonymous users in the React Native SDK 6.x to 7.0 migration guide.
React Web
All context-related functionality provided by the JavaScript SDK is also available in the React SDK.
Roku
Expand Roku code sample
To distinguish logged-in end users from anonymous end users in the SDK:
user.setAnonymous(Boolean)
Server-side SDKs
Here are the configuration options for anonymous contexts and users in 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
To distinguish logged-in end users from anonymous end users in the SDK:
var context = Context.Builder("context-key-123abc").Anonymous(true).Build();
Apex
Expand Apex code sample
To distinguish logged-in end users from anonymous end users in the SDK:
LDUser user = new LDUser.Builder('abc123').setAnonymous(true).build();
C/C++ (server-side)
Expand C/C++ (server-side) code sample
To distinguish logged-in end users from anonymous end users in the SDK:
LDUserSetAnonymous(user, true);
Erlang
Expand Erlang code sample
To distinguish logged-in end users from anonymous end users in the SDK:
Context = ldclient_context:set(anonymous, true,ldclient_context:new(<<"user-key-123abc">>))
Go
Expand Go code sample
To distinguish logged-in end users from anonymous end users in the SDK:
import ("github.com/launchdarkly/go-sdk-common/v3/ldcontext")// Anonymous context with only a keycontext1 := ldcontext.NewBuilder("context-key-123abc").Anonymous(true)// Anonymous context with a key plus other attributescontext2 := ldcontext.NewBuilder("context-key-456def").Anonymous(true).SetString("country", "Canada").Build()
Haskell
Expand Haskell code sample
To distinguish logged-in end users from anonymous end users in the SDK:
makeContext "user-key-123abc" "user"& withAnonymous True
Java
Expand Java code sample
To distinguish logged-in end users from anonymous end users in the SDK:
LDContext context = LDContext.builder("context-key-123abc").anonymous(true).build();
Lua
Expand Lua code sample
To distinguish logged-in end users from anonymous end users in the SDK:
local user = ld.makeUser({key = "user-key-123abc",anonymous = true})
Node.js (server-side)
Expand Node.js (server-side) code sample
To distinguish logged-in end users from anonymous end users in the SDK:
const context = {kind:'user', key:'user-key-123abc', anonymous: true};
PHP
Expand PHP code sample
To distinguish logged-in end users from anonymous end users in the SDK:
$context = LDContext::builder("context-key-123abc")->anonymous(true)->build();
Python
Expand Python code sample
To distinguish logged-in end users from anonymous end users in the SDK:
context = Context.builder("context-key-123abc").anonymous(True).build()
Ruby
Expand Ruby code sample
To distinguish logged-in end users from anonymous end users in the SDK:
context = { key: "context-key-123abc", anonymous: true }
Rust
Expand Rust code sample
To distinguish logged-in end users from anonymous end users in the SDK:
// Anonymous context with only a keylet context = ContextBuilder::new("context-key-123abc").anonymous(true).build();// Anonymous context with a key plus other attributeslet context = ContextBuilder::new("context-key-123abc").anonymous(true).set_value("country", "US".into()).build();