Experimentation
This topic explains how to set up select client-side LaunchDarkly SDKs to use the Experimentation feature.
The configuration requirements for individual SDKs are included below.
Client-side SDKs
Code samples for this feature are available for the following client-side SDKs:
Android
Expand Android code sample
To use Experimentation with the Android SDK, you must configure the following SDK features:
-
Track: Track methods send custom events to LaunchDarkly using an event key when a user or other context takes an action in your app. You use the event key with LaunchDarkly metrics to measure signals during an experiment.
This example shows how to initialize the SDK with the track call included:
Android SDK initialization for Experimentation -
Variation: Variation methods evaluate a flag for a specific context and return the variation value. Variation methods also record an exposure event so LaunchDarkly knows this context was part of the experiment. Use typed variation methods such as
stringVariation()orboolVariation()that match your flag’s value type. Call a variation method afteridentify()has completed, and call it where the experience is rendered rather than inonCreate()or application startup. -
Identify: Identify methods update the context the SDK uses for flag evaluation. All flags re-evaluate against the new context when it completes. You need
identify()when a user or other context logs in, switches accounts, or their attributes change mid-session in a way that affects targeting, for example, an in-app purchase or plan upgrade. Always callidentify()off of the main thread. It is a network operation and will block the UI if called on the main thread. Wrap it in a coroutine:CoroutineScope(Dispatchers.IO).launch { client.identify(ctx).get() }. -
Flush: Flush methods force the SDK to send all queued events, including exposures and conversions, to LaunchDarkly immediately instead of waiting for the automatic send interval.
iOS
Expand iOS code sample
To use Experimentation with the iOS SDK, you must configure the following SDK features:
-
Track: Track methods send custom events to LaunchDarkly using an event key when a user or other context takes an action in your app.
This example shows how to initialize the SDK with the track call included:
iOS SDK initialization for Experimentation -
Variation: Variation methods evaluate a flag for a specific context and return the variation value. Variation methods also record an exposure event so LaunchDarkly knows this user or other context was part of the experiment.
-
Identify: Identify methods update the context the SDK uses for flag evaluation. All flags re-evaluate against the new context when it completes.
-
Flush: Flush methods force the SDK to send all queued events, including exposures and conversions, to LaunchDarkly immediately instead of waiting for the automatic send interval.
JavaScript
Expand JavaScript code sample
To use Experimentation with the JavaScript SDK, you must configure the following SDK features:
-
Track: Track methods send custom events to LaunchDarkly using an event key when a user or other context takes an action in your app.
This example shows how to initialize the SDK with the track call included:
JavaScript SDK initialization for Experimentation -
Variation: Variation methods evaluate a flag for a specific context and return the variation value. Variation methods also record an exposure event so LaunchDarkly knows this user or other context was part of the experiment. Call
ldClient.variation()at the exact moment the context encounters the experience, not at initialization, and not cached ahead of time. -
Identify: Identify methods update the context the SDK uses for flag evaluation. All flags re-evaluate against the new context when it completes. You need
identify()when the context’s identity changes after the SDK has already initialized, for example, when a user on a public page logs in, when identity loads asynchronously after SDK startup, or when a user completes a cookie consent flow. You do not need it if the user is already logged in when the page loads. Pass their context in the initial configuration instead. -
Flush: Flush methods force the SDK to send all queued events, including exposures and conversions, to LaunchDarkly immediately instead of waiting for the automatic send interval. Call
flush()aftertrack()on any event that causes navigation, such as a button click that redirects, a form submission, or a link, because the page may unload before the automatic flush fires. Also call it on single-page application (SPA) route changes where the user leaves the current view.
React Native
Expand React Native code sample
To use Experimentation with the React Native SDK, you must configure the following SDK features:
-
Track: Track methods send custom events to LaunchDarkly using an event key when a user or other context takes an action in your app.
This example shows how to initialize the SDK with the track call included:
React Native SDK initialization for Experimentation -
Variation: Variation methods evaluate a flag for a specific context and return the variation value. Variation methods also record an exposure event so LaunchDarkly knows this context was part of the experiment. Use the typed hooks such as
useBoolVariation(),useStringVariation(), anduseNumberVariation(). These hooks are reactive and return a typed value with a per-call-site default, so you get the right type and a fallback without having to check the result afterward. -
Identify: Identify methods update the context the SDK uses for flag evaluation. All flags re-evaluate against the new context when it completes. You need
identify()in the same scenarios as the JavaScript SDK, as well as when a user switches accounts on a shared device or completes onboarding and becomes a known entity mid-session. Always await the result before evaluating experiment flags, but await with a timeout.identify()is not guaranteed to complete on a slow or offline network, so waiting indefinitely can make your app unavailable during a network outage. -
Flush: Flush methods force the SDK to send all queued events, including exposures and conversions, to LaunchDarkly immediately instead of waiting for the automatic send interval. Call
flush()in anAppStatechange listener when state leavesactive, so queued events are sent before the app moves to the background.
React Web
Expand React Web code sample
To use Experimentation with the React Web SDK, you must configure the track, variation, identify, and flush methods.
This example shows how to initialize the SDK with the track call included:
The React Web SDK relies on the JavaScript SDK for the variation, identify, and flush methods. The same guidance applies.