HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

Forced Decision methods

This topic describes the Forced Decision methods, which you can use to force users into a specific variation in Optimizely Full Stack.

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

The JavaScript (Browser) SDK will check forced decisions before making any decisions.

If a matching item is found for the requested flag, audience conditions and traffic allocation will be ignored, and the SDK will return the forced decision immediately before making normal decisions.

The following describes specific scenarios the JavaScript (Browser) SDK will follow:

Flag-to-Decision

  • 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

  • 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

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

❗️

You must associate your variation(s) to a flag rule before calling any Forced Decision methods.

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

📘

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

For more information about each method click on the method name below:

OptimizelyDecisionContext

export interface OptimizelyDecisionContext {
  flagKey: string;
  ruleKey?: string;
}

OptimizelyForcedDecision

export interface OptimizelyForcedDecision {
  variationKey: string;
}

Set Forced Decision Method - setForcedDecision()

Version

4.9.0

Description

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

Parameters

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

ParameterTypeDescription
context
required
InterfaceAn instance of OptimizelyDecisionContext with the required flagKey and optional ruleKey for the forced decision you want to set.
decision
required
InterfaceAn 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 Javascript SDK example here.

Get Forced Decision Method - getForcedDecision()

Version

4.9.0

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 Javascript SDK.

ParameterTypeDescription
context
required
InterfaceAn 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 Javascript SDK example here.

Remove Forced Decision Method - removeForcedDecision()

Version

4.9.0

Description

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

Parameters

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

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

Returns

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

Example

See the full Javascript SDK example here.

Remove All Forced Decisions Method - removeAllForcedDecisions()

Version

4.9.0

Description

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

Parameters

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

ParametersTypeDescription
NoneN/AN/A

Returns

A success/failure boolean status.

Example

See the full Javascript SDK example here.

Full Code Example

const optimizely = require('@optimizely/optimizely-sdk');
const optimizelyClientInstance = optimizely.createInstance({
  sdkKey: 'sdk_key'// Provide the sdkKey of your desired environment here
});

var user = optlyInstance.createUserContext("test_user", attributes)

// set a forced decision for a flag
var success = user.setForcedDecision({ 'flag-1', null }, 'variation-a');
var decision = user.decide('flag-1');

// set a forced decision for an ab-test rule
var success = user.setForcedDecision({ 'flag-1', 'ab-test-1' }, 'variation-b');
var decision = user.decide('flag-1');

// set a forced variation for a delivery rule
var success = user.setForcedDecision({ 'flag-1', 'delivery-1' }, 'on');
var decision = user.decide('flag-1');

// get forced variations
var forcedDecision = user.getForcedDecision($flagContext);
console.log("[ForcedDecision] variationKey = "+forcedDecision);

// remove forced variations
var success = user.removeForcedDecision({ 'flag-1', 'ab-test-1' });
var success = user.removeAllForcedDecisions();

See Also

OptimizelyUserContext