Anonymous contexts

Overview

This topic explains how to configure contexts as anonymous in LaunchDarkly SDKs. These features are available for all SDKs.

Each SDK lets you designate anonymous contexts. Anonymous contexts 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. To learn more, read Multi-contexts and context instances.

In some 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.

To learn more, read Anonymous contexts.

Details about each SDK’s configuration are available in the SDK-specific sections below.

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.

Client-side SDKs

Here are the configuration options for anonymous contexts in client-side SDKs.

.NET (client-side)

To distinguish logged-in end users from anonymous end users in the SDK:

.NET SDK v3.0+ (C#)
1Context context = Context.Builder("context-key-123abc")
2 .Anonymous(true)
3 .Build();

To auto-generate a key for any context whose anonymous attribute is true:

1var config = Configuration
2 .Builder("mobile-key-123abc", ConfigurationBuilder.AutoEnvAttributes.Enabled)
3 .GenerateAnonymousKeys(true)
4 .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 “placeholder-key”, but it could be any non-empty string:

.NET SDK v3.0+ (C#)
1Context context = Context.Builder("placeholder-key")
2 .Anonymous(true)
3 .Build();

Android

To distinguish logged-in end users from anonymous end users in the SDK:

1LDContext context = LDContext.builder("context-key-123abc")
2 .anonymous(true)
3 .build();

If you set this option, you must still specify a non-null key as a placeholder when you construct the LDContext, because this 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 LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.

C++ (client-side)

To distinguish logged-in end users from anonymous end users in the SDK:

1auto context = ContextBuilder()
2 .Kind("user", "user-key-123abc")
3 .Anonymous(true)
4 .Build();

To learn more, read ContextBuilder.

Electron

To distinguish logged-in end users from anonymous end users in the SDK:

1const anonymousUser = { 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:

1const anonymousUser = { anonymous: true };

Flutter

To distinguish logged-in end users from anonymous end users in the SDK:

1final context = LDContextBuilder()
2 .kind('user', 'user-key-123abc')
3 .anonymous(true)
4 .build();

To learn more, read anonymous.

iOS

To distinguish logged-in end users from anonymous end users in the SDK:

1var contextBuilder = LDContextBuilder(key: "context-key-123abc")
2contextBuilder.anonymous(true)
3
4let 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:

1// Have the SDK use a device persistent key.
2// This sets `isAnonymous` by default.
3let context = try LDContextBuilder().build().get()

JavaScript

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:

1const anonymousUserContext = {
2 kind: 'user',
3 anonymous: true
4};
5
6// A multi-context can contain both anonymous and non-anonymous contexts.
7// Here, the organization is not anonymous.
8const multiContext = {
9 kind: 'multi',
10 user: anonymousUserContext,
11 org: {
12 key: 'org-key-123abc',
13 name: 'Acme, Inc.'
14 }
15}

Node.js (client-side)

To distinguish logged-in end users from anonymous end users in the SDK:

1const 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:

1const anonymousContext = { kind: 'user', anonymous: true };

React Native

To create an create an anonymous context, specify the anonymous property and set the context key to an empty string.

Here’s how:

1// This device context is anonymous
2const deviceContext = {
3 // The key attribute is required and should be empty
4 // The SDK will automatically generate a unique, stable key
5 key: '',
6 kind: 'device',
7 deviceId: '12345',
8 anonymous: true
9}
10
11// This user context is not anonymous
12const userContext = {
13 kind: 'user',
14 key: 'user-key-123abc'
15}
16
17// The multi-context contains one anonymous context
18// and one non-anonymous context
19const multiContext = {
20 kind: 'multi',
21 user: userContext,
22 device: deviceContext
23}

In version 10 of the SDK, you must include the key attribute when building the anonymous context. If you set the key to an empty string, the client will automatically set the key to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots. We strongly recommend having the client manage the key for anonymous contexts. If you set the key to a non-empty string, the client uses that value as the key. However, the key may not be stable across restarts or reboots.

The SDK gives a usage error if you omit the key attribute. It also gives a usage error if you set the key to an empty string and do not mark the context as anonymous.

In versions 7 through 9 of the SDK, you may omit the context key when building an anonymous context, and the client will automatically set it to a LaunchDarkly-specific, device-unique string that is consistent between app restarts and device reboots.

Using a shared key between anonymous contexts in React Native for Android



It is possible to use one, shared key between anonymous contexts. However, we do not recommend this. Using a shared key between anonymous contexts means that some features will be limited or will not work as expected. To learn more, read Use a shared key between anonymous contexts.

If you are using an older version of the React Native SDK on Android, there is some additional configuration required if you want to use a shared key between anonymous contexts.

If you do choose to use a shared key between anonymous contexts, and you:

  • have an Android application
  • are using the React Native SDK in versions 7.x through 9.x

then you must also explicitly configure the SDK to allow using a shared key between anonymous contexts. The generateAnonymousKeysAndroid configuration option defaults to true, which means that the SDK will automatically generate unique keys for anonymous contexts. If you need to use a shared key between anonymous contexts, then you must set this option to false.

Here’s how:

1import LDClient, { LDConfig } from 'launchdarkly-react-native-client-sdk';
2
3let config: LDConfig = {
4 mobileKey: 'mobile-key-123abc',
5 enableAutoEnvAttributes: true,
6 generateAnonymousKeysAndroid: false
7};
8
9let context: LDContext = {
10 kind: 'user',
11 key: 'shared-key-for-anonymous-users',
12 anonymous: true
13}
14
15await client.configure(config, context);

To learn more, read generateAnonymousKeys.

The React Native SDK for iOS generates a context key for anonymous contexts only if you do not supply one. No additional SDK configuration is required if you are using a shared key for anonymous contexts with the React Native SDK for iOS.

React Web

All context-related functionality provided by the JavaScript SDK is also available in the React Web SDK.

Roku

To distinguish logged-in end users from anonymous end users in the SDK:

1context = LaunchDarklyCreateContext({"key": "user-key-123abc", "kind": "user", "anonymous": true})

Server-side SDKs

Here are the configuration options for anonymous contexts in server-side SDKs:

.NET (server-side)

To distinguish logged-in end users from anonymous end users in the SDK:

.NET SDK v7.0 (C#)
1var context = Context.Builder("context-key-123abc")
2 .Anonymous(true)
3 .Build();

Apex

To distinguish logged-in end users from anonymous end users in the SDK:

Java
1LDUser user = new LDUser.Builder('abc123')
2 .setAnonymous(true)
3 .build();

C++ (server-side)

To distinguish logged-in end users from anonymous end users in the SDK:

1auto context = ContextBuilder()
2 .Kind("user", "user-key-123abc")
3 .Anonymous(true)
4 .Build();

To learn more, read ContextBuilder.

Erlang

To distinguish logged-in end users from anonymous end users in the SDK:

1 Context = ldclient_context:set(anonymous, true,
2 ldclient_context:new(<<"user-key-123abc">>))

Go

To distinguish logged-in end users from anonymous end users in the SDK:

Go SDK v6.0
1import (
2 "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
3)
4
5// Anonymous context with only a key
6context1 := ldcontext.NewBuilder("context-key-123abc").Anonymous(true)
7
8// Anonymous context with a key plus other attributes
9context2 := ldcontext.NewBuilder("context-key-456def").
10 Anonymous(true).
11 SetString("country", "Canada").
12 Build()

Anonymous contexts do not appear on the Contexts list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related events to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read EventsConfiguration.

Haskell

To distinguish logged-in end users from anonymous end users in the SDK:

1makeContext "user-key-123abc" "user"
2 & withAnonymous True

Anonymous contexts do not appear on the Contexts list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related events to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read configSetOmitAnonymousContexts.

Java

To distinguish logged-in end users from anonymous end users in the SDK:

Java SDK v6.0
1LDContext context = LDContext.builder("context-key-123abc")
2 .anonymous(true)
3 .build();

Lua

To distinguish logged-in end users from anonymous end users in the SDK:

1-- to create an anonymous user context
2local userContext = ld.makeContext({
3 user = {
4 key = "user-key-123abc",
5 anonymous = true
6 }
7})
8
9-- to create an anonymous context of a different kind
10local deviceContext = ld.makeContext({
11 device = {
12 key = "device-key-123abc",
13 anonymous = true
14 }
15})

To learn more, read makeContext.

Node.js (server-side)

To distinguish logged-in end users from anonymous end users in the SDK:

1import * as ld from '@launchdarkly/node-server-sdk';
2
3const context: ld.LDContext = {
4 kind: 'user',
5 key: 'user-key-123abc',
6 anonymous: true,
7}

PHP

To distinguish logged-in end users from anonymous end users in the SDK:

PHP SDK v5.0
1$context = LDContext::builder("context-key-123abc")->anonymous(true)->build();

Python

To distinguish logged-in end users from anonymous end users in the SDK:

1context = Context.builder("context-key-123abc").anonymous(True).build()

Anonymous contexts do not appear on the Contexts list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related events to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read omit_anonymous_contexts.

Ruby

To distinguish logged-in end users from anonymous end users in the SDK:

Ruby SDK v7.0
1context = { key: "context-key-123abc", anonymous: true }

Anonymous contexts do not appear on the Contexts list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related events to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read omit_anonymous_contexts.

Rust

To distinguish logged-in end users from anonymous end users in the SDK:

Rust SDK v1
1// Anonymous context with only a key
2let context = ContextBuilder::new("context-key-123abc").anonymous(true).build();
3
4// Anonymous context with a key plus other attributes
5let context = ContextBuilder::new("context-key-123abc").
6 anonymous(true).
7 set_value("country", "US".into()).
8 build();

Anonymous contexts do not appear on the Contexts list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related events to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read omit_anonymous_contexts.

Edge SDKs

Here are the configuration options for anonymous contexts in edge SDKs.

Akamai

To distinguish logged-in end users from anonymous end users in the SDK:

TypeScript
1import { LDContext } from '@launchdarkly/akamai-edgeworker-sdk-common';
2
3const anonymousContext: LDContext = { kind: 'user', key: 'user-key-123abc', anonymous: true };

Cloudflare

To distinguish logged-in end users from anonymous end users in the SDK:

TypeScript
1import type { LDContext } from '@launchdarkly/cloudflare-server-sdk';
2
3const anonymousContext: LDContext = { kind: 'user', key: 'user-key-123abc', anonymous: true };

Vercel

To distinguish logged-in end users from anonymous end users in the SDK:

TypeScript
1import type { LDContext } from '@launchdarkly/vercel-server-sdk';
2
3const anonymousContext: LDContext = { kind: 'user', key: 'user-key-123abc', anonymous: true };

AI SDKs

Here are the configuration options for anonymous contexts in AI SDKs.

.NET AI

To distinguish logged-in end users from anonymous end users in the SDK:

.NET AI SDK
1var context = Context.Builder("context-key-123abc")
2 .Anonymous(true)
3 .Build();

Go AI

To distinguish logged-in end users from anonymous end users in the SDK:

Go AI SDK
1import (
2 "github.com/launchdarkly/go-sdk-common/v3/ldcontext"
3)
4
5// Anonymous context with only a key
6context1 := ldcontext.NewBuilder("context-key-123abc").Anonymous(true)
7
8// Anonymous context with a key plus other attributes
9context2 := ldcontext.NewBuilder("context-key-456def").
10 Anonymous(true).
11 SetString("country", "Canada").
12 Build()

Node.js (server-side) AI

To distinguish logged-in end users from anonymous end users in the SDK:

1const context: LDContext = {
2 kind: 'user',
3 key: 'user-key-123abc',
4 anonymous: true,
5}

Anonymous contexts do not appear on the Contexts list.

Python AI

To distinguish logged-in end users from anonymous end users in the SDK:

Python AI SDK
1context = Context.builder("context-key-123abc").anonymous(True).build()

Anonymous contexts do not appear on the Contexts list. Optionally, you can configure the SDK to omit data from anonymous contexts when sending related events to LaunchDarkly. Depending on how your application uses contexts, this can significantly decrease the amount of data your application sends to LaunchDarkly. To learn more, read omit_anonymous_contexts.

Built with