Forced Decision methods for the Swift SDK
Describes the Forced Decision methods for the Swift SDK, which you can use to force users into a specific variation in Optimizely Feature Experimentation.
These methods will help test and debug various flows of your client applications by forcing users into a specific variation.
The Swift SDK will check forced decisions before making any decisions. If a matching item is found for the requested flag, the Swift SDK will return the forced decision immediately (audience conditions andtraffic allocations are ignored) before making normal decisions.
The following describes specific scenarios the Swift SDK will follow:
Flag-to-Decision
- SDK will look up at the beginning of anydecide 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.
❗️
WarningYou must associate your variation(s) to aflag 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 thedisableDecisionEvent option).
📘
NoteThese forced decisions are not persistent and will be cleared when theOptimizelyUserContext is re-initialized.
For more information about each method, click on the method name below:
OptimizelyDecisionContext
public struct OptimizelyDecisionContext { public let flagKey: String public let ruleKey: String?}OptimizelyForcedDecision
public struct OptimizelyForcedDecision { public let variationKey: String}Set Forced Decision Method - setForcedDecision()
Version
3.10.0
Description
Sets a forced decision (variationKey) for a givenOptimizelyDecisionContext.
Parameters
This table lists the required and optional parameters for the Swift SDK.
Parameter | Type | Description |
|---|---|---|
contextrequired | Struct | An instance of |
decision | Struct | An instance of |
Returns
A boolean value that indicates if setting the forced decision (variationKey) was completed successfully.
Example
See the full Swift SDK examplehere.
Get Forced Decision Method - getForcedDecision()
Version
3.10.0
Description
Returns the forced decision (variationKey) for a givenOptimizelyDecisionContext. Returns theOptimizelyForcedDecision instance or null if there is no matching item.
Parameters
This table lists the required and optional parameters for the Swift SDK.
Parameter | Type | Description |
|---|---|---|
contextrequired | struct | An instance of |
Returns
A forced decisionOptimizelyForcedDecision instance for the context or null if there is no matching item.
Example
See the full Swift SDK examplehere.
Remove Forced Decision Method - removeForcedDecision()
Version
3.10.0
Description
Removes the forced decision (variationKey) for a givenOptimizelyDecisionContext.
Parameters
This table lists the required and optional parameters for the Swift SDK.
Parameters | Type | Description |
|---|---|---|
contextrequired | struct | An instance of |
Returns
A success/failure boolean status if the forced decision (variationKey) was removed.
Example
See the full Swift SDK examplehere.
Remove All Forced Decisions Method - removeAllForcedDecisions()
Version
3.10.0
Description
Removes all forced decisions (variationKey) for the user context.
Parameters
This table lists the required and optional parameters for the Swift SDK.
| Parameters | Type | Description |
|---|---|---|
| None | N/A | N/A |
Returns
A success/failure boolean status.
Example
See the full Swift SDK examplehere.
Full Code Example
let optimizely = OptimizelyClient(sdkKey: “sdk-key”)optimizely.start(datafile: datafile)let user = optimizely.createUserContext(userId: “test-user”, attributes: attributes)let flagContext = OptimizelyDecisionContext(flagKey: "flag-1")let flagAndABTestContext = OptimizelyDecisionContext(flagKey: "flag-1", ruleKey: "ab-test-1")let flagAndDeliveryRuleContext = OptimizelyDecisionContext(flagKey: "flag-1", ruleKey: "delivery-1")let variationAForcedDecision = OptimizelyForcedDecision(variationKey: "variation-a")let variationBForcedDecision = OptimizelyForcedDecision(variationKey: "variation-b")let variationOnForcedDecision = OptimizelyForcedDecision(variationKey: "on") // set a forced decision for a flagvar success = user.setForcedDecision(context: flagContext, decision: variationAForcedDecision)decision = user.decide(key: "flag-1")// set a forced decision for an ab-test rulesuccess = user.setForcedDecision(context: flagAndABTestContext, decision: variationBForcedDecision)decision = user.decide(key: "flag-1")// set a forced variation for a delivery rulesuccess = user.setForcedDecision(context: flagAndDeliveryRuleContext, decision: variationOnForcedDecision)decision = user.decide(key: "flag-1")// get forced variationslet forcedDecision = user.getForcedDecision(context: flagContext)print("[ForcedDecision] variationKey = \(forcedDecision!.variationKey)")// remove forced variationssuccess = user.removeForcedDecision(context: flagAndABTestContext)success = user.removeAllForcedDecisions()See Also
Source files
The language/platform source files containing the implementation for Swift isOptimizelyClient.swift.
Updated 17 days ago