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 Ruby SDK

Describes the Forced Decision methods, 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 Ruby SDK will check forced decisions before making any decisions. If a matching item is found for the requested flag, the Ruby SDK will return the forced decision immediately (audience conditions and traffic allocations are ignored) before making normal decisions.

The following describes specific scenarios the Ruby 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.

❗️

Warning

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 DISABLE_DECISION_EVENT option).

📘

Note

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

OptimizelyDecisionContext = Struct.new(:flag_key, :rule_key)

OptimizelyForcedDecision

OptimizelyForcedDecision = Struct.new(:variation_key)

Set Forced Decision Method - set_forced_decision()

Version

3.10.0

Description

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

Parameters

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

ParameterTypeDescription
context
required
StructAn instance of OptimizelyDecisionContext with the required flag_key and optional rule_key for the forced decision you want to set.
decision
required
StructAn instance of OptimizelyForcedDecision with the required variation_key for the forced decision you want to set.

Returns

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

Example

See the full Ruby SDK example here.

Get Forced Decision Method - get_forced_decision()

Version

3.10.0

Description

Returns the forced decision (variation_key) for a given OptimizelyDecisionContext. Returns the null if there is no matching item.

Parameters

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

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

Returns

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

Example

See the full Ruby SDK example here.

Remove Forced Decision Method - remove_forced_decision()

Version

3.10.0

Description

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

Parameters

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

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

Returns

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

Example

See the full Ruby SDK example here.

Remove All Forced Decisions Method - remove_all_forced_decisions()

Version

3.10.0

Description

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

Parameters

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

ParametersTypeDescription
NoneN/AN/A

Returns

A success/failure boolean status.

Example

See the full Ruby SDK example here.

Full Code Example

require 'optimizely'
require 'optimizely/optimizely_factory'

optimizely = Optimizely::OptimizelyFactory.default_instance('sdk_key')

user = optimizely.create_user_context('test_user', attributes)

flag_context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new('flag-1', nil)
flag_and_ab_test_context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new('flag-1','ab-test-1')
flag_and_delivery_rule_context = Optimizely::OptimizelyUserContext::OptimizelyDecisionContext.new('flag-1','delivery-1')
variation_a_forced_decision = Optimizely::OptimizelyUserContext::OptimizelyForcedDecision.new('variation-a')
variation_b_forced_decision = Optimizely::OptimizelyUserContext::OptimizelyForcedDecision.new('variation-b')
variation_on_forced_decision = Optimizely::OptimizelyUserContext::OptimizelyForcedDecision.new('on')

# set a forced decision for a flag
success = user.set_forced_decision(flag_context, variation_a_forced_decision)
decision = user.decide('flag-1')

# set a forced decision for an ab-test rule
success = user.set_forced_decision(flag_and_ab_test_context, variation_b_forced_decision)
decision = user.decide('flag-1')

# set a forced variation for a delivery rule
success = user.set_forced_decision(flag_and_delivery_rule_context, variation_on_forced_decision)
decision = user.decide('flag-1')

# get forced variations
forced_decision = user.get_forced_decision(flag_context)
puts "[ForcedDecision] variation_key = #{forced_decision}"

# remove forced variations
success = user.remove_forced_decision(flag_and_ab_test_context)
success = user.remove_all_forced_decisions

See Also

OptimizelyUserContext