Skip to main content

Documentation Index

Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt

Use this file to discover all available pages before exploring further.

Client-side SDKs

In client-side SDKs, you must enable an evaluation reason configuration option for this feature to work. The code samples below include this option. To learn more about configuration options, read Configuration.
This feature is available in the following client-side SDKs:

.NET (client-side)

The VariationDetail methods, such as BoolVariationDetail, work the same as Variation, but also provide additional “reason” information about how a flag value was calculated. For example, you can find out if the context was individually targeted for the flag or was matched by one of the flag’s rules. You can examine the “reason” data programmatically, or, if you capture detailed analytics events for flags, view it with Data Export.Here is an example:

      var config = Configuration
          .Builder("example-mobile-key", ConfigurationBuilder.AutoEnvAttributes.Enabled)
          .EvaluationReasons(true)
          .Build();
      LdClient client = LdClient.Init(config, context);

      EvaluationDetail<bool> detail =
          client.BoolVariationDetail("example-bool-flag-key", false);
          // or StringVariationDetail for a string-valued flag, and so on.

      bool value = detail.Value;
      int? index = detail.VariationIndex;
      EvaluationReason reason = detail.Reason;
To learn more about the VariationDetail methods, read EvaluationDetail and BoolVariationDetail. To learn more about the configuration option, read EvaluationReasons.

Android

The variationDetail methods, such as boolVariationDetail, work the same as variation. They also provide additional “reason” information about how a flag value was calculated, such as if the context matched a specific rule. You can examine the “reason” data programmatically, or, if you capture detailed analytics events for flags, view it with Data Export.Here is an example:
      LDConfig ldConfig = new LDConfig.Builder(AutoEnvAttributes.Enabled)
        .mobileKey("example-mobile-key")
        .evaluationReasons(true)
        .build();
      LDClient client = LDClient.init(this.getApplication(), ldConfig, context, secondsToBlock);

      EvaluationDetail<Boolean> detail =
        client.boolVariationDetail("example-flag-key", false);
        // or stringVariationDetail for a string-valued flag, etc.

      boolean value = detail.getValue();
      Integer index = detail.getVariationIndex();
      EvaluationReason reason = detail.getReason();
To learn more about the variationDetail methods, read EvaluationDetail and getVariationIndex. To learn more about the configuration option, read evaluationReasons.Here is an example of how to access the details of a reason object:
      void printReason(EvaluationReason reason) {
        switch (reason.getKind()) {
          case OFF:
            Timber.d("it's off");
            break;
          case FALLTHROUGH:
            Timber.d("fell through");
            break;
          case TARGET_MATCH:
            Timber.d("targeted");
            break;
          case RULE_MATCH:
            EvaluationReason.RuleMatch rm =
              (EvaluationReason.RuleMatch)reason;
            Timber.d("matched rule %d/%s",
                     rm.getRuleIndex(),
                     rm.getRuleId());
            break;
          case PREREQUISITE_FAILED:
            EvaluationReason.PrerequisiteFailed pf =
              (EvaluationReason.PrerequisiteFailed)reason;
            Timber.d("prereq failed: %s", pf.getPrerequisiteKey());
            break;
          case ERROR:
            EvaluationReason.Error e = (EvaluationReason.Error)reason;
            Timber.d("error: %s", e.getErrorKind());
        }
        // or, if all you want is a simple descriptive string:
        Timber.d(reason.toString());
      }
To learn more, read EvaluationReason.

C++ (client-side)

You can request and then programmatically inspect the reason for a particular feature flag evaluation.The detail.Reason() response is described in Evaluation reasons.Here is an example:
      auto detail = client.BoolVariationDetail("example-flag-key", false);
      if (detail.Value()) {
        std::cout << "Value was true!" << std::endl;
      } else {
        // it was false, let's find out why.
        if (auto reason = detail.Reason(); reason.has_value()) {
          // reason might not be present, so we have to check
          std::cout << "Value was false because of " << reason.value() << std::endl;
        } else {
          std::cout << "No reason provided to explain why flag was false!" << std::endl;
        }
      }
To learn more, read EvaluationDetail and BoolVariationDetail.

Electron

The variationDetail methods work the same as variation. They also provide additional “reason” information about how a flag value was calculated. For example, you can find out if the context was individually targeted for the flag or was matched by one of the flag’s rules. You can examine the “reason” data programmatically, or, if you capture detailed analytics events for flags, view it with Data Export.Here is an example:

      const { value, variationIndex, reason } = client.variationDetail('example-flag-key', false);
To learn more about the variationDetail methods, read LDEvaluationDetail and variationDetail. To learn more about the configuration option, read LDEvaluationReason.

Flutter

The variationDetail methods, such as boolVariationDetail, work the same as variation. They also provide additional “reason” information about how a flag value was calculated. For example, you can find out if the context was individually targeted for the flag or was matched by one of the flag’s rules. You can examine the “reason” data programmatically, or, if you capture detailed analytics events for flags, view it with Data Export.To enable this functionality, set the evaluationReasons configuration option to true when you initialize the client.Here is an example:
      final config = LDConfig(
        CredentialSource.fromEnvironment(),
        AutoEnvAttributes.enabled,
        dataSourceConfig: DataSourceConfig(
          evaluationReasons: true
        ),
      );

      // initialize client and context

      LDEvaluationDetail<bool> detail =
        client.boolVariationDetail('example-flag-key', false);
        // or stringVariationDetail for a string-valued flag, and so on.

      bool value = detail.value;
      int index = detail.variationIndex;
      LDEvaluationReason reason = detail.reason;
To learn more about the variationDetail methods, read LDEvaluationDetail and boolVariationDetail. To learn more about the configuration option, read evaluationReasons.Here is an example of how to access the details of a reason object:
      void printReason(LDEvaluationReason reason) {
        switch (reason.kind) {
          case LDKind.OFF:
            print("it's off");
            break;
          case LDKind.FALLTHROUGH:
            print('fell through');
            break;
          case LDKind.TARGET_MATCH:
            print('targeted');
            break;
          case LDKind.RULE_MATCH:
            print('matched rule: ${reason.ruleIndex} ${reason.ruleId}');
            break;
          case LDKind.PREREQUISITE_FAILED:
            print('prereq failed: ${reason.prerequisiteKey}');
            break;
          case LDKind.ERROR:
            print('error: ${reason.errorKind}');
            break;
          default: // LDKind.UNKNOWN
            print('unknown service kind');
        }
      }
To learn more, read LDEvaluationDetail.

iOS

The variationDetail methods, such as boolVariationDetail, work the same as the variation methods. They also provide additional “reason” information about how a flag value was calculated, such as if the user matched a specific rule. You can examine the “reason” data programmatically, or, if you capture detailed analytics events for flags, view it with Data Export.Here is an example:
      ldConfig.evaluationReasons = true
      LDClient.start(config: ldConfig, context: context)

      let detail = client.boolVariationDetail(forKey: "example-flag-key", defaultValue: false);

      let value: Bool = detail.value
      let variationIndex: Int? = detail.variationIndex
      let reason: [String: LDValue]? = detail.reason
To learn more about the variationDetail methods, read LDEvaluationDetail and boolVariationDetail. To learn more about the configuration option, read LDConfig.

Java

Script
The variationDetail method lets you evaluate a feature flag with the same parameters you would for variation. With variationDetail, you receive more information about how the value was calculated. In v4.x of the JavaScript SDK you can also use typed methods, for example, boolVariationDetail for boolean feature flags.The variation detail returns in an object containing both the result value and a “reason” object which tells you more information about the flag evaluation. For example, you can find out if the context was individually targeted for the flag or was matched by one of the flag’s rules. It also indicates if the flag returned the default value due to an error. You can examine the “reason” data programmatically, or, if you capture detailed analytics events for flags, view it with Data Export.Here is an example:
      const options = { evaluationReasons: true };
      // Create client
      const client = createClient('example-client-side-id', context, options);

      // Then start the client
      client.start();

      client.waitForInitialization({timeout: 5});

        if (result.status === 'complete') {
        // Client initialized successfully
        } else if (result.status === 'failed') {
        // Client failed to initialize
        console.error('Initialization failed:', result.error);
        } else if (result.status === 'timeout') {
        // Initialization timed out
        console.error('Initialization timed out');
        }

        // Note: Events still work if you prefer that approach
        client.on('ready', () => {
        // Client is ready (success or failure)
        });
        client.on('initialized', () => {
        // Client initialized successfully
        });
        client.on('failed', (err) => {
        // Client failed to initialize
        });
To learn more about the *variationDetail methods, read LDEvaluationDetail. To learn more about the configuration option, read evaluationReasons.

Node.js (client-side)

The variationDetail method lets you evaluate a feature flag with the same parameters you would for variation. With variationDetail, you receive more information about how the value was calculated.The variation detail returns in an object that contains both the result value and a “reason” object which tells you more information about the flag evaluation. For example, you can find out if the user was individually targeted for the flag or was matched by one of the flag’s rules. It also indicates if the flag returned the default value due to an error. You can examine the “reason” data programmatically, or, if you capture detailed analytics events for flags, view it with Data Export.Here is an example:
      const options = { evaluationReasons: true };
      const client = LDClient.initialize('example-client-side-id', user, options);

      const detail = client.variationDetail('example-flag-key', false);

      const value = detail.value;
      const index = detail.variationIndex;
      const reason = detail.reason;
To learn more about the variationDetail method, read LDEvaluationDetail and variationDetail. To learn more about the configuration option, read evaluationReasons.

React Native

The variationDetail methods work the same way as the variation methods, and also provide additional information about how a flag value was calculated. For example, you can find out if the context was individually targeted for the flag or was matched by one of the flag’s rules. You can examine the “reason” data programmatically, or, if you capture detailed analytics events for flags, view it with Data Export. To view this reason information, set the withReasons configuration option to true.In React Native, there is a variation detail method for each type, such as boolVariationDetail or stringVariationDetail. In the React Native SDK version 10, there is also a hook for each type, such as useBoolVariationDetail or useStringVariationDetail.Here is an example:
      const { reason, value, variationIndex } = useBoolVariationDetail('example-flag-key', false);
To learn more about the variationDetail methods, read LDEvaluationDetail, useBoolVariationDetail and boolVariationDetail.To learn more about the withReasons configuration option, read LDOptions.The SDK also includes an untyped method to determine the variation of a feature flag and provide information about how the flag value was calculated. To learn more, read variationDetail. We recommend using the strongly typed variation methods, such as boolVariationDetail, which perform type checks and handle type errors.

Roku

For each variation type there is also an associated version that returns the reason a particular value was returned.Here is an example:
      config.setUseEvaluationReasons(true)

      details = launchDarkly.intVariationDetail("example-flag-key", 123)

These variation methods return an object containing the keys value, reason, and variationIndex. The value field is the result of the evaluation. The reason field is an object that explains why the result happened, for example details about a rule match. The reason object will always contain a kind field. Lastly the variationIndex field contains the ID of the particular value returned. This field may be null.