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

EDIT ON GITHUB

Evaluating flags

Read time: 7 minutes
Last edited: May 13, 2022

Overview

This topic explains how to use the flag evaluation feature to serve different feature flag variations to users. This feature is available for all of our SDKs.

Configuring variations

Feature flag variations determine what a user receives when they encounter a feature flag. Every flag has at least two variations, one for when the flag is off, and one for when it's on. To learn more, read Creating flag variations.

Flags also have fallback values. The fallback value only returns if an error occurs. For example, the SDK serves the fallback value if LaunchDarkly is unreachable, the feature flag key doesn't exist, or the user doesn't have a key specified.

The flag evaluation feature adds a user to the Users dashboard if a user with the same key does not already exist. However, each SDK evaluates flags based only on the user object you provide in the evaluation call. The SDK does not automatically use the attributes shown on the dashboard, and user attributes are not synchronized across SDK instances. You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

You do not need to create users manually, but if you want to, you can with the identify feature. To learn more, read Identifying and changing users.

Details about each SDK's configuration are available in the SDK-specific sections below:

  • Client-side SDKs
  • Server-side SDKs

Client-side SDKs

This feature is available for all of our client-side SDKs:

.NET (client-side)

Expand .NET (client-side) code sample

The Variation method determines whether a flag is enabled or not for a specific user. In the client-side .NET SDK, there is a variation method for each type, such as BoolVariation or StringVariation.

variation calls take the feature flag key and a default value.

Here is an example:

client.BoolVariation("your.feature.key", false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Android

Expand Android code sample

The variation method determines whether or not a flag is enabled for a specific user. variation calls take the feature flag key and a fallback value. When LDClient is initialized for the first time at app launch, users receive the feature flag fallback values until an initial connection to LaunchDarkly completes.

In Android, there is a variation method for each type, such as boolVariation or stringVariation.

Here is an example:

boolean variationResult = client.boolVariation(flagKey, false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

C/C++ (client-side)

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

The variation methods determine whether or not a flag is enabled for a specific user. The variation method signatures take the feature flag key and a fallback value. When the client is initialized for the first time, users receive the feature flag fallback values until an initial connection to LaunchDarkly completes.

There are variation methods for each type, such as LDBoolVariation/boolVariation or LDStringVariation/stringVariation.

Here is an example:

bool show_feature = LDBoolVariation(client, "YOUR_FLAG_KEY", false);
if (show_feature) {
// application code to show the feature
} else {
// the code to run if the feature is off
}

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Electron

Expand Electron code sample

To evaluate any feature flag for the current user, call variation:

const flagValue = client.variation('YOUR_FEATURE_KEY', false);
// proceed based on flag value, for example:
if (flagValue) {
// feature flag is on
} else {
// feature flag is off
}

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

The return value of variation is always either one of the variations you defined for your flag in the flag dashboard, or the default value. The default value is the second parameter to variation.

You can also fetch all flags for the current user.

Here is an example:

const flags = client.allFlags();
const flagValue = flags['YOUR_FEATURE_KEY'];

This returns a key-value map of all your feature flags. It contains null values for any flags that could not be evaluated.

Both of these methods are synchronous. The client always has the last known flag values in memory, so retrieving them does not involve any input/output (I/O).

Flutter

Expand Flutter code sample

The variation methods determine the flag value for the current user. variation calls take the feature flag key and a fallback value. When LDClient is initialized for the first time at app launch, users receive the feature flag fallback values until an initial connection to LaunchDarkly completes.

In Flutter, there is a variation method for each type, such as boolVariation or stringVariation.

Here is an example:

bool variationResult = await LDClient.boolVariation(flagKey, false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

iOS

Expand iOS code sample

The variation functions determine the flag value for the current user. Variation functions take the feature flag key and a default value as parameters. If the flag does not exist, cannot be cast to the correct return type, or the LDClient is not started, the function returns the default value.

In the iOS SDK, there is a variation method for each type, such as boolVariation or jsonVariation.

Here is an example:

let boolFlagValue = LDClient.get()!.boolVariation(forKey: "bool-flag-key", defaultValue: false)
let jsonFlagValue = LDClient.get()!.jsonVariation(forKey: "json-flag-key", defaultValue: ["enabled": false, "special": "none"])

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

JavaScript

Expand JavaScript code sample

The variation method determines which variation of a feature flag a user receives. variation calls take the feature flag key and a default value.

Here is an example of the variation method:

client.variation('your.feature.key', false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Node.js (client-side)

Expand Node.js (client-side) code sample

The variation method determines which variation of a feature flag a user receives. variation calls take the feature flag key and a default value.

Here is an example:

const value = client.variation('YOUR_FEATURE_KEY', false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

React Native

Expand React Native code sample

The variation method determines whether a flag is enabled or not for a specific user. In React Native, there is a variation method for each type, such as boolVariation or stringVariation.

Variation calls take the feature flag key and a fallback value. When LDClient is initialized for the first time at app launch, users receive feature flag fallback values until an initial connection to LaunchDarkly is completed for the first time.

Here is an example:

let boolResult = await client.boolVariation('YOUR_BOOLEAN_FLAG_KEY', false);
let numResult = await client.numberVariation('YOUR_NUMERIC_FLAG_KEY', 2);
let stringResult = await client.stringVariation('YOUR_STRING_FLAG_KEY', '');
let jsonResult = await client.jsonVariation('YOUR_JSON_FLAG_KEY', {});

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Roku

Expand Roku code sample

The *variation methods determine which variation of a feature flag a user receives. *variation calls take a feature flag key and a default value.

Here is an example:

REM typed variations
myInt = launchDarkly.intVariation("MY_FLAG_KEY", 123)
myBool = launchDarkly.boolVariation("MY_FLAG_KEY", false)
myString = launchDarkly.stringVariation("MY_FLAG_KEY", "hello world!")
myObjectOrArray = launchDarkly.jsonVariation("MY_FLAG_KEY", {"value": 123})
REM generic variation
myValue = launchDarkly.variation("MY_FLAG_KEY", false)

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Server-side SDKs

This feature is available for all of our server-side SDKs:

.NET (server-side)

Expand .NET (server-side) code sample

The Variation methods determine whether a flag is enabled or not for a specific user. In .NET, there is a Variation method for each type:

Variation calls take the feature flag key, a User, and a default value.

Here is an example:

var value = client.BoolVariation("your.feature.key", user, false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Apex

Expand Apex code sample

The variation family of methods determine the flag value for a specific user. In Apex, there is a variation method for each type, such as boolVariation or stringVariation. The functions take an LDUser, feature flag key, and a default value.

Apex variation methods do not automatically add users to the dashboard

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 dashboard.

We recommend calling client.identify(user) with each of your users to generate user indexing events and populate your Users dashboard. To learn more, read Identifying and changing users.

Here is an example:

Boolean value = client.boolVariation(user, 'your.feature.key', false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

C/C++ (server-side)

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

The variation family of functions determine whether a flag is enabled or not for a specific user. In C, there is a variation function for each type, such as LDBoolVariation or LDStringVariation.

The functions take an LDClient, LDUser, feature flag key, default value, and optional LDDetails struct for an evaluation explanation.

Here is an example:

bool value = LDBoolVariation(client, user, "your.feature.key", false, NULL);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Erlang

Expand Erlang code sample

The variation function determines whether a flag is enabled or not for a specific user. The functions take a flag key, user, default value and an instance tag. The instance tag is optional.

Flag = ldclient:variation(<<"my-bool-key">>, #{key => <<"aa0ceb">>,}, false, my_instance)

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Go

Expand Go code sample

The Variation methods determine whether a flag is enabled or not for a specific user. In Go, there is a Variation method for each type:

Variation methods take the feature flag key, a User, and a default value. In the example below, the default value is false.

The Variation call generates analytics events to tell LaunchDarkly about the flag evaluation and the user properties, so the user will be added to the Users dashboard if a user with that key doesn't exist already.

Here is an example:

result, _ := client.BoolVariation("your.feature.key", user, false)
// result is now true or false depending on the setting of this boolean feature flag

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Haskell

Expand Haskell code sample

The variation family of functions determine whether a flag is enabled or not for a specific user. The functions take a Client, User, feature flag key, and a default value.

Here is an example:

myBoolVariation <- boolVariation client user "YOUR_FEATURE_KEY" False

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Java

Expand Java code sample

The variation methods determine which variation of a feature flag a user receives. In Java, there is a variation method for each type: boolVariation, intVariation, doubleVariation, stringVariation and jsonValueVariation, which can be of any JSON type.

The variation methods take the feature flag key, an LDUser, and a default value.

Here is an example:

boolean value = client.boolVariation("your.feature.key", user, false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Lua

Expand Lua code sample

The variation family of functions determine whether a flag is enabled or not for a specific user. In Lua, there is a variation function for each type, such as boolVariation or stringVariation.

To learn more about configuration options, read the API docs.

Here is an example:

local value = client:boolVariation(user, "YOUR_FEATURE_KEY", false)

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Node.js (server-side)

Expand Node.js (server-side) code sample

The variation method determines which variation of a feature flag a user receives.

variation calls take the feature flag key, an LDUser, and a default value.

Here is an example:

client.variation('your.feature.key', user, false,
(err, value) => {
// check value and proceed accordingly
});

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Node.js (Cloudflare)

This SDK uses the same code as the Node.js server-side SDK.

PHP

Expand PHP code sample

The variation method determines which variation of a feature flag a user receives. variation calls take the feature flag key, an LDUser, and a default value.

Here is an example:

$value = $client->variation($key, $user, false);

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Python

Expand Python code sample

The variation method determines which variation of a feature flag a user receives. variation calls take the feature flag key, an LDUser, and a default value.

Here is an example:

show_feature = ldclient.get().variation("your.feature.key", user, False)

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Ruby

Expand Ruby code sample

The variation method determines which variation of a feature flag a user receives.

variation calls take the feature flag key, a user, and a default value.

Here is an example:

value = client.variation("your.feature.key", user, false)

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.

Rust

Expand Rust code sample

The variation methods determine whether a flag is enabled or not for a specific user. In Rust, there is a variation method for each type:

variation methods take a User, the feature flag key, and a default value. In the example below, the default value is false.

Here is an example:

let result = client.bool_variation(&user, "your.feature.key", false);
// result is now true or false depending on the setting of this boolean feature flag

You must provide all relevant user attributes for each evaluation for your targeting rules to apply correctly.