Forced Decision methods for the React SDK
Describes the Forced Decision methods, which you can use to 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 React SDK checks forced decisions before making any decisions. If a matching item is found for the requested flag, the SDK returns the forced decision immediately (audience conditions and traffic allocations are ignored) before making normal decisions.
The React SDK checks for forced decisions at the start of each decision process. If a matching forced decision is found, it returns the decision immediately.
-
Flag-to-Decision – The SDK checks at the start of any decide call for the given flag.
-
Experiment-Rule-to-Decision – The SDK checks at the start of the decision for the given experiment rule of the flag key.
-
Delivery-Rule-to-Decision – The SDK checks at the start of the decision for the given delivery rule of the flag key.
WarningYou must associate your variations to a flag rule before calling any Forced Decision methods.
On forced decisions, the React SDK fires impression events and notifications just like other normal decisions (unless disabled by the disableDecisionEvent option).
NoteThese forced decisions are not persistent and are cleared when the user is updated.
For more information about each method, click on the method name.
If you are calling forced decision methods right after initialization, ensure you wait for the user to be ready using the onUserUpdate promise.
const optimizely = createInstance({
sdkKey: '<Your_SDK_Key>',
});
optimizely.onUserUpdate(() => {
optimizely.setForcedDecision(
{
flagKey: "product_sort",
ruleKey: "experiment",
},
{ variationKey: "treatment" }
);
});OptimizelyDecisionContext
export interface OptimizelyDecisionContext {
flagKey: string;
ruleKey?: string;
}OptimizelyForcedDecision
export interface OptimizelyForcedDecision {
variationKey: string;
}Set Forced Decision Method - setForcedDecision()
Version
2.8.0
Description
Sets a forced decision (variationKey) for a given OptimizelyDecisionContext.
Parameters
This table lists the required and optional parameters for the React SDK.
Parameter | Type | Description |
|---|---|---|
decisionContext required | Interface | An instance of |
decision | Interface | An instance of |
Returns
A boolean value that indicates if setting the forced decision (variationKey) was completed successfully.
Example
optimizely.setForcedDecision(
{
flagKey: "product_sort",
ruleKey: "experiment",
},
{ variationKey: "treatment" }
);Get Forced Decision Method - getForcedDecision()
Version
2.8.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 React SDK.
Parameter | Type | Description |
|---|---|---|
decisionContext required | Interface | An instance of |
Returns
A forced decision OptimizelyForcedDecision instance for the context or null if there is no matching item.
Example
var forcedDecision = optimizely.getForcedDecision({
flagKey: "product_sort",
ruleKey: "experiment",
});Remove Forced Decision Method - removeForcedDecision()
Version
2.8.0
Description
Removes the forced decision (variationKey) for a given OptimizelyDecisionContext.
Parameters
This table lists the required and optional parameters for the React SDK.
Parameters | Type | Description |
|---|---|---|
decisionContext required | Interface | An instance of |
Returns
A success/failure boolean status if the forced decision (variationKey) was removed.
Example
var success = optimizely.removeForcedDecision({
flagKey: "product_sort",
ruleKey: "experiment",
})Remove All Forced Decisions Method - removeAllForcedDecisions()
Version
2.8.0
Description
Removes all forced decisions (variationKey) for the user context.
Parameters
This table lists the required and optional parameters for the React SDK.
| Parameters | Type | Description |
|---|---|---|
| None | N/A | N/A |
Returns
A success/failure boolean status.
Example
var success = optimizely.removeAllForcedDecisions();Full Code Example
import React from "react";
import {
createInstance,
OptimizelyProvider,
useDecision,
withOptimizely,
} from "@optimizely/react-sdk";
const optimizely = createInstance({
sdkKey: "<YOUR_SDK_KEY_HERE>",
});
optimizely.onUserUpdate(() => {
// Wait for the user to be available before using forced decision methods.
optimizely.setForcedDecision({ flagKey: "product_sort" }, { variationKey: "treatment" });
});
const PurchaseButton = withOptimizely((props: any) => {
const handleClick = () => {
const { optimizely } = props;
optimizely.track("purchased");
};
return <div><button onClick={handleClick}>Purchase</button></div>;
});
const RemoveForcedDecision = withOptimizely((props: any) => {
const handleClick = () => {
const { optimizely } = props;
optimizely.removeForcedDecision({ flagKey: 'product_sort' });
};
return <div><button onClick={handleClick}>Remove Forced Decision</button></div>;
});
const RemoveAllForcedDecisions = withOptimizely((props: any) => {
const handleClick = () => {
const { optimizely } = props;
optimizely.removeAllForcedDecisions();
};
return <div><button onClick={handleClick}>Remove All Forced Decisions</button></div>;
});
function ProductSort() {
const [decision] = useDecision("product_sort", {
autoUpdate: true
});
const variationKey = decision.variationKey;
const isEnabled = decision.enabled;
const sortMethod = decision.variables["sort_method"];
return (
<div>
{/* If variation is null, display error */}
{variationKey === null && <div className="error">{decision.reasons}</div>}
{/* If feature is enabled, do something with the Sort Method variable */}
{isEnabled && <div>The Sort method is {sortMethod}</div>}
{/* Show relevant component based on the variation key */}
{variationKey === "control" && <div>Control Component</div>}
{variationKey === "treatment" && <div>Treatment Component</div>}
{/* Show Purchase Button to track Purchase event */}
<PurchaseButton />
<RemoveForcedDecision />
<RemoveAllForcedDecisions />
</div>
);
}
function App() {
return (
<OptimizelyProvider
optimizely={optimizely}
user={{
id: "user123",
}}
>
<ProductSort />
</OptimizelyProvider>
);
}
export default App;Updated about 11 hours ago
