Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

Forced Decision methods for the Android SDK

Describes the Forced Decision methods, which lets you force users into a specific variation in Optimizely Feature Experimentation.

These methods help test and debug various flows of your client applications by forcing users into a specific variation.

The Android SDK checks if any Forced Decision methods were implemented before making any decisions. If a matching item is found for the requested flag, the SDK will return the forced decision immediately (audience conditions and traffic allocations are ignored) before making normal decisions.

The following describes specific scenarios the Android SDK follows:

Flag-to-Decision

  • Android SDK will look up at the beginning of any decide call for the given flag. If a matching Flag-to-Decision forced decision is found for the flag, it returns the decision.

Experiment-Rule-to-Decision

  • Android SDK will look up at the beginning of the decision for the given experiment rule (of the flag key). If a matching Experiment-Rule-to-Decision forced decision is found for the flag, it returns the decision.

Delivery-Rule-to-Decision

  • Android SDK will look up at the beginning of the decision for the given delivery rule (of the flag key). If a matching Delivery-Rule-to-Decision forced decision is found, it returns the decision.

❗️

Warning

You must associate your variations to a flag rule before calling any Forced Decision methods.

On forced decisions, the Android SDK will fire impression events and notifications just like other normal decisions (unless disabled by the disableDecisionEvent.

📘

Note

These forced decisions are not persistent and are cleared when the OptimizelyUserContext is re-initialized.

For information about each Forced Decision method, click on the method name

OptimizelyDecisionContext

data class OptimizelyDecisionContext(val flagKey: String, val ruleKey: String?)
public class OptimizelyDecisionContext {
   public OptimizelyDecisionContext(@Nonnull String flagKey, @Nullable String ruleKey);
  
   public String getFlagKey();
  
   public String getRuleKey();
}

OptimizelyForcedDecision

data class OptimizelyForcedDecision(val variationKey: String)
public class OptimizelyForcedDecision { 
   public OptimizelyForcedDecision(@Nonnull String variationKey);
  
   public String getVariationKey();
}

Set Forced Decision method – setForcedDecision()

Version

3.9.1

Description

Sets a forced decision (variationKey) for a given OptimizelyDecisionContext.

Parameters

This table lists the required and optional parameters for the Android SDK.

ParameterTypeDescription
context
required
StructAn instance of OptimizelyDecisionContext with the required flagKey and optional ruleKey for the forced decision you want to set.
decision
required
StructAn instance of OptimizelyForcedDecision with the required variationKey for the forced decision you want to set.

Returns

A boolean value that indicates if setting the forced decision (variationKey) was completed successfully.

Example

See the full Android SDK example in the Full code example section.

Get Forced Decision method – getForcedDecision()

Versionretruns

3.9.1

Description

Returns the forced decision (variationKey) for a given OptimizelyDecisionContext. Returns the OptimizelyForcedDecision instance or null if there is no matching item.

Parameters

This table lists the required and optional parameters for the Android SDK.

ParameterTypeDescription
context
required
structAn instance of OptimizelyDecisionContext with the required flagKey and optional ruleKey for the forced decision you want to get.

Returns

A forced decision OptimizelyForcedDecision instance for the context or null if there is no matching item.

Example

See the full Android SDK example in the Full code example section.

Remove Forced Decision method – removeForcedDecision()

Version

3.9.1

Description

Removes the forced decision (variationKey) for a given OptimizelyDecisionContext.

Parameters

This table lists the required and optional parameters for the Android SDK.

ParametersTypeDescription
context
required
classAn instance of OptimizelyDecisionContext with the required flagKey and optional ruleKey for the forced decision you want to remove.

Returns

A success or failure boolean status if the forced decision (variationKey) was removed.

Example

See the full Android SDK example in the Full code example section.

Remove All Forced Decisions method – removeAllForcedDecisions()

Version

3.9.1

Description

Removes all forced decisions (variationKey) for the user context.

Parameters

This table lists the required and optional parameters for the Android SDK.

ParametersTypeDescription
NoneN/AN/A

Returns

A success or failure boolean status.

Example

See the full Android SDK example in the Full code example section.

Full code example

// Create the OptimizelyUserContext, passing in the UserId and Attributes
val user = optimizelyClient.createUserContext("user-id")

val flagContext = OptimizelyDecisionContext("flag-1", null)
val flagAndABTestContext = OptimizelyDecisionContext("flag-1", "exp-1")
val flagAndDeliveryRuleContext = OptimizelyDecisionContext("flag-1", "delivery-1")
val variationAForcedDecision = OptimizelyForcedDecision("variation-a")
val variationBForcedDecision = OptimizelyForcedDecision("variation-b")
val variationOnForcedDecision = OptimizelyForcedDecision("on")

// set a forced decision for a flag
var success = user!!.setForcedDecision(flagContext, variationAForcedDecision)
var decision = user.decide("flag-1")

// set a forced decision for an ab-test rule
success = user.setForcedDecision(flagAndABTestContext, variationBForcedDecision)
decision = user.decide("flag-1")

// set a forced variation for a delivery rule
success = user.setForcedDecision(flagAndDeliveryRuleContext, variationOnForcedDecision)
decision = user.decide("flag-1")

// get forced variations
val forcedDecision = user.getForcedDecision(flagContext)
println("[ForcedDecision] variationKey = " + forcedDecision!!.variationKey)

// remove forced variations
success = user.removeForcedDecision(flagAndABTestContext)
success = user.removeAllForcedDecisions()
/* Create the OptimizelyUserContext, passing in the UserId and Attributes */
OptimizelyUserContext user = optimizelyClient.createUserContext("user-id");

OptimizelyDecisionContext flagContext = new OptimizelyDecisionContext("flag-1", null);
OptimizelyDecisionContext flagAndABTestContext = new OptimizelyDecisionContext("flag-1", "exp-1");
OptimizelyDecisionContext flagAndDeliveryRuleContext = new OptimizelyDecisionContext("flag-1", "delivery-1");
OptimizelyForcedDecision variationAForcedDecision = new OptimizelyForcedDecision("variation-a");
OptimizelyForcedDecision variationBForcedDecision = new OptimizelyForcedDecision("variation-b");
OptimizelyForcedDecision variationOnForcedDecision = new OptimizelyForcedDecision("on");
        
// set a forced decision for a flag
Boolean success = user.setForcedDecision(flagContext, variationAForcedDecision);
OptimizelyDecision decision = user.decide("flag-1");

// set a forced decision for an ab-test rule
success = user.setForcedDecision(flagAndABTestContext, variationBForcedDecision);
decision = user.decide("flag-1");

// set a forced variation for a delivery rule
success = user.setForcedDecision(flagAndDeliveryRuleContext, variationOnForcedDecision);
decision = user.decide("flag-1");

// get forced variations
OptimizelyForcedDecision forcedDecision = user.getForcedDecision(flagContext);
System.out.println("[ForcedDecision] variationKey = " + forcedDecision.getVariationKey()); 

// remove forced variations
success = user.removeForcedDecision(flagAndABTestContext);
success = user.removeAllForcedDecisions();