Decide methods

Use the Decide methods to return a flag decision for a user. The flag decision includes flag enabled/disabled status and flag variation.

This page describes the following Decide methods:

Decide

Version

SDK 3.8 and higher

Description

Returns a decision result for a flag key for a user. The decision result is returned in an OptimizelyDecision object and contains all data required to deliver the flag rule.

Decide is a method of the UserContext object. See OptimizelyUserContext for details.

See the OptimizelyDecision for details of the returned decision object.

Parameters

The following table describes parameters for the Decide method:

ParameterTypeDescription
flagKeyStringThe key of the feature flag
options (optional)ArrayArray of OptimizelyDecideOption enums. See following table.

OptimizelyDecideOption

The following example shows how you can set options individually on any Decide method, or as global defaults when you instantiate the Optimizely client. See Initialize SDK

from optimizely import optimizely
from optimizely.decision.optimizely_decide_option import OptimizelyDecideOption

# set global default decide options when initializing the client
optimizely_client = optimizely.Optimizely(
  sdk_key = sdk_key,
  options = [OptimizelyDecideOption.DISABLE_DECISION_EVENT])


# set additional options in a decide call
user = optimizely.create_user_context("user123")
decision = user.decide_all(
  options = [OptimizelyDecideOption.ENABLED_FLAGS_ONLY,
             OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE])

The following table shows details for the OptimizelyDecideOption.

# set global default decide options when initializing the client
ProjectConfigManager configManager = HttpProjectConfigManager.builder()
            .withSdkKey(sdkKey)
            .build();
List<OptimizelyDecideOption> options = Arrays.asList(OptimizelyDecideOption.DISABLE_DECISION_EVENT);

Optimizely optimizely = Optimizely.builder()
            .withConfigManager(configManager)
            .withDefaultDecideOptions(options)
            .build();


# set additional options in a decide call
OptimizelyUserContext user = optimizely.createUserContext("user123");
List<OptimizelyDecideOption> options = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY, OptimizelyDecideOption.DISABLE_DECISION_EVENT);

Map<String, OptimizelyDecision> decisions = user.decideAll(options);

OptimizelyDecideOption enum

If set:

OptimizelyDecideOption.DISABLE_DECISION_EVENT

Prevents the visitor from firing an impression while still being served the variation, which disables displaying results of the Decide method on the Optimizely application's Results page.

This setting can be why the Decision Event Dispatched enum is false in the returned OptimizelyDecision object or the DECIDE notification listener payload.

OptimizelyDecideOption.ENABLED_FLAGS_ONLY

Return decisions for enabled flags only. This is a valid option only for methods that decide multiple flags, like the Decide All method. This option is ignored if it is invalid. When this option is not set, the SDK returns all decisions regardless of whether the flag is enabled or not.

OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE

When set, the SDK bypasses UPS (both lookup and save) for the decision.

When this option is not set, UPS overrides audience targeting, traffic allocation, and experiment mutual exclusion groups.

OptimizelyDecideOption.INCLUDE_REASONS

Return log messages in the Reasons field of OptimizelyDecision object. Note that unlike info or debug messages, critical error messages are always returned, regardless of this setting.

OptimizelyDecideOption.EXCLUDE_VARIABLES

Exclude flag variable values from the decision result. Use this option to minimize the returned decision by skipping large JSON variables.

Returns

The Decide method returns an OptimizelyDecision object. For more information, see OptimizelyDecision.

If the method encounters a critical error (SDK not ready, invalid flag key, etc), then it returns a decision with a null Variation Key field and populates the Reasons field with error messages (regardless of the Include Reasons option).

Example Decision

The following is an example of calling the Decide method and accessing the returned OptimizelyDecision object:

// create the user and decide which flag rule & variation they bucket into 
Map<String, Object> attributes = new HashMap<>();
attributes.put("logged_in", true);
OptimizelyUserContext user = optimizely.createUserContext(userId, attributes);

OptimizelyDecision decision = user.decide("product_sort");

// variation. if null, decision fail with a critical error
String variationKey = decision.getVariationKey();

// flag enabled state
boolean enabled = decision.getEnabled();

// all variable values
OptimizelyJSON variables = decision.getVariables();

// String variable value
String vs = null;
try {
    vs = variables.getValue("sort_method", String.class);
} catch (JsonParseException e) {
  e.printStackTrace();
}

// An alternative way to access a variable value
Boolean vb = (Boolean) variables.toMap().get("k_boolean");

// flag key for which for which decision was made
String flagKey = decision.getFlagKey();

// a copy of the user context for which the decision was made
OptimizelyUserContext userContext = decision.getUserContext();
   
// reasons for the decision
List<String> reasons = decision.getReasons();

Side Effects

Invokes the DECISION notification listener if this listener is enabled.

Decide All

Returns decisions for all active (unarchived) flags for a user.

See OptimizelyDecision for details.

Version

SDK v3.8 and higher

Description

Use the Decide All method to return a map of flag decisions for a user.

Parameters

The following table describes parameters for the Decide All method:

ParameterTypeDescription
options (optional)ArrayArray of OptimizelyDecideOption enums. See Decide Parameters.

Returns

The Decide All method returns a map of OptimizelyDecision objects. For more information, see OptimizelyDecision.

If the method fails for all flags (for example, the SDK isn't ready or the user context is invalid), then it returns an empty map. If the method detects an error for a specific flag, it returns error messages in the Reasons field of the decision for that flag.

Examples

The following is an example of getting flags for the user with the Decide All call:

import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;

// make decisions for all active (unarchived) flags in the project for a user
List<OptimizelyDecideOption> options = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY);
Map<String, OptimizelyDecision> decisions = user.decideAll(options);

Set<String> allKeys = decisions.keySet();
OptimizelyDecision decisionForFlag1 = decisions.get("flag_1");

Side effects

Invokes the DECISION notification listener for each decision, if this listener is enabled.

Decide For Keys

The Decide For Keys method returns a map of flag decisions for specified flag keys.

Version

SDK v3.8 and higher

Description

Get a map of flag decisions for an array of flag keys.

Parameters

The following table describes parameters for the Decide for Keys method:

ParameterTypeDescription
keysArrayArray of string flag keys.
options (optional)ArrayArray of OptimizelyDecideOption enums. See OptimizelyDecideOption.

Returns

Returns a map of OptimizelyDecisions. For more information, see OptimizelyDecision.

If the method fails for all flags (for example, the SDK isn't ready or the user context is invalid), then it returns an empty map. If the method detects an error for a specific flag, it returns error messages in the Reasons field of the decision for that flag.

Example

The following is an example of getting specified flags for the user:

// make decisions for specific flags
List<String> keys = Arrays.asList("flag-1", "flag-2");
Map<String, OptimizelyDecision> decisions = user.decideForKeys(keys);
        
OptimizelyDecision decision1 = decisions.get("flag-1");
OptimizelyDecision decision2 = decisions.get("flag-2");

Side effects

Invokes the DECISION notification listener for each decision, if this listener is enabled.

Source files

The language/platform source files containing the implementation for Java are at Optimizely.java.


Did this page help you?