Decide methods for the Android SDK
Overview of the decide methods which you can use to return a flag decision for a user in Optimizely Feature Experimentation.
Use the decide methods to return feature flag decisions for a user. Each decision includes whether the flag is enabled and which variation the user receives.
Decide
Returns a decision for a specific feature flag and user.
Minimum SDK version – v3.9+
Description
The decide method handles the variation decision for an individual experiment. It returns a decision result for a flag key for a user in anOptimizelyDecision object and contains the data required to deliver the flag rule.
This method ensures that Feature Experimentation properly buckets the user in experiment analytics, and you should use it if there is no need for pre-rendering decisions.
📘
NoteDecide is a method of the
UserContextobject. SeeOptimizelyUserContext for details.
See theOptimizelyDecision documentation for details of the returned decision object.
Key features
- Single experiment decision – Returns the variation for a specific experiment.
- Bucketing in results – Ensures Feature Experimentation records the user in the experiment results page for proper analysis.
- Complement to decide all method – If you use thedecide all method to pre-fetch variations, you should also call the decide method for each experiment that the user interacts with. This dual approach guarantees that Feature Experimentation accurately tracks the user's participation and that the correct variation is served through caching.
Parameters
The following table describes the parameters for the decide method:
| Parameter | Type | Description |
|---|---|---|
flagKey | String | The key of the feature flag |
options (optional) | Array | Array ofOptimizelyDecideOption enums. |
Returns
The decide method returns anOptimizelyDecision object. For information, seeOptimizelyDecision.
If the method encounters a critical error (SDK not ready, invalid flag key, and so on), then it returns a decision with anullvariationKey field and populates thereasons field with error messages (regardless of theOptimizelyDecideOption.INCLUDE_REASONS option).
Exampledecide
decide// Create the user and decide which flag rule and variation they bucket intoval attributes: MutableMap<String, Any> = HashMap()attributes["logged_in"] = trueval user = optimizelyClient.createUserContext("user123", attributes)val decision = user!!.decide("product_sort")// Variation key. If null, the decision failed due to a critical error.val variationKey = decision.variationKey// The flag enabled stateval enabled = decision.enabled// All variable valuesval variables = decision.variables// String variable valuevar vs: String? = nulltry { vs = variables.getValue("sort_method", String::class.java)} catch (e: JsonParseException) { e.printStackTrace()}// Boolean variable valueval vb = variables.toMap()!!["k_boolean"] as Boolean?// Flag key for which decision was madeval flagKey = decision.flagKey// User for which the decision was madeval userContext = decision.userContext// Reasons for the decisionval reasons = decision.reasons// Create the user and decide which flag rule and variation they bucket into Map<String, Object> attributes = new HashMap<>();attributes.put("logged_in", true);OptimizelyUserContext user = optimizelyClient.createUserContext("user123", attributes);OptimizelyDecision decision = user.decide("product_sort");// Variation key. If null, the decision failed due to a critical error.String variationKey = decision.getVariationKey();// Flag enabled stateboolean enabled = decision.getEnabled();// All variable valuesOptimizelyJSON variables = decision.getVariables();// String variable valueString vs = null;try {vs = variables.getValue("sort_method", String.class);} catch (JsonParseException e) { e.printStackTrace();}// Boolean variable valueBoolean vb = (Boolean) variables.toMap().get("k_boolean");// Flag key for which decision was madeString flagKey = decision.getFlagKey();// User for which the decision was madeOptimizelyUserContext userContext = decision.getUserContext(); // Reasons for the decisionList<String> reasons = decision.getReasons();Side effect
Invokes thedecision notification listener if this listener is enabled.
Minimum SDK version – v3.7+
Description
Use the decide all method to retrieve decisions for all active flags before rendering content. This is particularly useful when you need to serve the correct cached version of your page or component based on the user's variation. For example when using anedge worker orcloud function.
Key features
- Pre-rendering decision – Lets you know all variation assignments ahead of time.
- Cache control – Lets you serve the correct cached content based on the user's pre-determined variations.
- Delay experiment tracking – Use
DISABLE_DECISION_EVENTto prevent the SDK from recording a decision before the user sees the feature. This ensures that participation is only tracked when the experience is delivered.
Parameters
The following table describes the parameters for the decide all method:
| Parameter | Type | Description |
|---|---|---|
options (optional) | Array | Array ofOptimizelyDecideOption enums. |
Returns
Returns a map ofOptimizelyDecisions.
If the method fails for all flags (for example, the SDK is not ready or the user context is invalid), it returns an empty map. If the method detects an error for a specific flag, it returns error messages in thereasons field of the decision for that flag.
ExampledecideAll
decideAll// make decisions for all active (unarchived) flags in the project for a userval options = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY)val decisions = user!!.decideAll(options)val allKeys: Set<String> = decisions.keysval decisionForFlag1 = decisions["flag_1"]// make decisions for all active (unarchived) flags in the project for a userList<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 effect
Invokes thedecision notification listener for each decision, if this listener is enabled.
Minimum SDK version – v3.7+
Parameters
The following table describes parameters for the decide for keys method:
| Parameter | Type | Description |
|---|---|---|
keys | Array | Array of string flag keys. |
options (optional) | Array | Array ofOptimizelyDecideOption enums. |
Returns
Returns a map ofOptimizelyDecisions. For information, seeOptimizelyDecision.
If the method fails for all flags (for example, the SDK is not ready or the user context is invalid), it returns an empty map. If the method detects an error for a specific flag, it returns error messages in thereasons field of the decision for that flag.
ExampledecideForKeys
decideForKeys// make decisions for specific flagsval keys = Arrays.asList("flag-1", "flag-2")val decisions = user!!.decideForKeys(keys)val decision1 = decisions["flag-1"]val decision2 = decisions["flag-2"]// make decisions for specific flagsList<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 effect
Invokes thedecision notification listener for each decision, if this listener is enabled.
OptimizelyDecideOption
OptimizelyDecideOptionThe following table lists theOptimizelyDecideOption enum with an explanation what happens if you set them. In addition to setting these options individually for a decide method, you can also set them as global defaults when you instantiate the Optimizely client. SeeInitialize the Android SDK.
enum | If set |
|---|---|
| Prevents the SDK from dispatching animpression event when serving a variation. This disables decision tracking on theOptimizely Experiment Results page and thedecision notification listener. |
| Returns decisions only for flags that are currently enabled. Used with thedecide all method anddecide for keys method. When this option is not set, the Android SDK returns all decisions regardless of whether the flag is enabled. |
| Bypasses the user profile service (both lookup and save) for the decision. When this option is not set, user profile service overrides audience targeting, traffic allocation, and experiment mutual exclusion groups. |
| Adds log messages to the reasons field of the decision. Critical errors are always returned, even if this option is not set. |
| Excludes flag variable values from the decision result. Use this option to minimize the returned decision by skipping large JSON variables. |
The following code sample shows how to implement theOptimizelyDecideOption as a global default and locally in a decide method call.
// Set global default decide options when initializing the clientvar options: List<OptimizelyDecideOption>options = Arrays.asList(OptimizelyDecideOption.DISABLE_DECISION_EVENT)val optimizelyManager = OptimizelyManager.builder() .withSDKKey("YOUR_SDK_KEY") // Replace with your SDK key .withDefaultDecideOptions(options) .build(context)// Set additional options in a decide callval optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile)val user = optimizelyClient.createUserContext("user123")options = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY, OptimizelyDecideOption.EXCLUDE_VARIABLES)val decisions = user!!.decideAll(options)// Set global default decide options when initializing the clientList<OptimizelyDecideOption> options;options = Arrays.asList(OptimizelyDecideOption.DISABLE_DECISION_EVENT);OptimizelyManager optimizelyManager = OptimizelyManager.builder() .withSDKKey("YOUR_SDK_KEY") // Replace with your SDK key .withDefaultDecideOptions(options) .build(context);// Set additional options in a decide callOptimizelyClient optimizelyClient = optimizelyManager.initialize(context, R.raw.datafile);OptimizelyUserContext user = optimizelyClient.createUserContext("user123");options = Arrays.asList(OptimizelyDecideOption.ENABLED_FLAGS_ONLY, OptimizelyDecideOption.EXCLUDE_VARIABLES);Map<String, OptimizelyDecision> decisions = user.decideAll(options);Best practices
- Early and late invocation
- Use the decide all method early in your application's rendering process to load the correct cached content. For example, on theCDN with an Edge SDK.
- Use the decide method at the point of user interaction or when you must ensure Feature Experimentation records the decision in your experiment analytics.
- Combining methods – When using the decide and decide all methods, always pair the decide all method with the decide method for each experiment the user encounters. This prevents discrepancies between served content and analytics data.
- Parameter management – Ensure you use the
DISABLE_DECISION_EVENToption with the decide all method to avoid premature bucketing, then use the decide method to handle the decision event when the user experiences the content.
Source files
The language and platform source files containing the implementation for the Android SDK are available onGitHub.
Updated 17 days ago