The OptimizelyUserContext object allows you to make flag decisions and track events for a user context that you have already created using the Create User Context method.
Additionally, if you have the Advanced Audience Targeting integration between Optimizely Data Platform (ODP) and Optimizely Feature Experimentation enabled, you can evaluate if your user would qualify for a real-time audience segment.
OptimizelyUserContext is supported on SDK v3.8 and higher.
remove_all_forced_decisions() methods are supported on v3.10.0 and higher.
qualified_for?() methods are supported on v5.0.0 and higher.
The following code shows the object definition for OptimizelyUserContext:
# Create an instance of the Optimizely User Context. Pass in user id and optionally user attributes
def initialize(optimizely_client, user_id, user_attributes)
# set an attribute for the user
def set_attribute(attribute_key, attribute_value)
# get attributes for the user
# make a decision about which flag variation the user buckets into for the flag key
def decide(key, options = nil)
# make decisions about which flag variations the user buckets into for flag keys
def decide_for_keys(keys, options = nil)
# make decisions about which flag variations the user buckets into for all flags
def decide_all(options = nil)
# track user event
def track_event(event_key, event_tags = nil)
OptimizelyDecisionContext = Struct.new(:flag_key, :rule_key)
OptimizelyForcedDecision = Struct.new(:variation_key)
# Sets the forced decision (variation_key) for a given decision context
def set_forced_decision(context, decision)
# Returns the forced decision for a given decision context
# Removes the forced decision for a given decision context
# Removes all forced decisions bound to this user context
# The following methods require the Advanced Audience Targeting
# integration enabled. See note following the code sample.
# An array of segment names that the user is qualified for.
# The result of **fetch_qualified_segments()** will be saved here.
# Can be nil if not properly updated with fetch_qualified_segments().
# You can read and write directly to the qualified segments array.
# This allows for bypassing the remote fetching process from ODP
# or for utilizing your own fetching service.
# Fetch all qualified segments for the user context.
# If no callback is provided, this method will fetch the qualified segments
# and return a boolean signifying success.
# If a callback is provided, the method will fetch segments in a separate thread,
# invoke the provided callback when results are available, and return the thread handle.
def fetch_qualified_segments(options, &block)
# Check is the user qualified for the given segment.
You must first enable theAdvanced Audience Targeting integration to be able to run the
The following table shows attributes for the OptimizelyUserContext object:
|The ID of the user
|A map of custom key-value pairs specifying attributes for the user that are used for audience targeting. You can pass the map with the user ID when you create the user.
The following table shows methods for the OptimizelyUserContext object:
|Pass a custom user attribute as a key-value pair to the user context.
|Returns a decision result for a flag key for a user in an OptimizelyDecision object, which contains all data required to deliver the flag rule.
See Decide methods
|Returns a map of flag decisions for specified flag keys.
See Decide methods
|Returns decisions for all active (unarchived) flags for a user.
See Decide methods
|Tracks a conversion event for a user (an action a user takes) and logs an error message if the specified event key does not match any existing events.
See Track Event
|Forces a user into a specific variation.
See Set Forced Decision
|Returns what variation the user was forced into.
See Get Forced Decision
|Removes a user from a specific forced variation.
See Remove Forced Decision
|Removes a user from all forced variations.
See Remove All Forced Decisions
|Fetch all Optimizely Data Platform (ODP) real-time segments that the user context qualified for. See Advanced Audience Targeting segment qualification methods.
|Check if the user context qualified for a given ODP real-time segment. See Advanced Audience Targeting segment qualification methods.
** Requires the Advanced Audience Targeting integration.
The language and platform source files containing the implementation for Ruby are available on Github.com.
Updated about 1 month ago