The OptimizelyUserContext object allows you to make flag decisions and track events for a user context 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.0 and higher.
remove_all_forced_decision() methods are supported on 4.0.0 and higher.
is_qualified_for() methods are supported on version TBD and higher. They also require enabling the Advanced Audience Targeting integration.
Advanced Audience Targeting and the
is_qualified_for()methods are currently beta. Contact your Customer Success Manager for more information or register now on Optimizely.com.
The following code shows the object definition for OptimizelyUserContext:
class OptimizelyUserContext(object): # set an attribute for the user def set_attribute(self, attribute_key, attribute_value): # get attributes for the user def get_user_attributes(self): # make a decision about which flag variation the user buckets into for the flag key def decide(self, key, options=None): # make decisions about which flag variations the user buckets into for flag keys def decide_for_keys(self, keys, options=None): # make decisions about which flag variations the user buckets into for all flags def decide_all(self, options=None): # track user event def track_event(self, event_key, event_tags=None): # OptimizelyDecisionContext class OptimizelyDecisionContext(object): def __init__(self, flag_key, rule_key): # OptimizelyForcedDecision class OptimizelyForcedDecision(object): def __init__(self, variation_key): # Sets the forced decision (variation_key) for a given decision context def set_forced_decision(self, OptimizelyDecisionContext, OptimizelyForcedDecision): # Returns the forced decision for a given decision context def get_forced_decision(self, OptimizelyDecisionContext): # Removes the forced decision for a given decision context def remove_forced_decision(self, OptimizelyDecisionContext): # Removes all forced decisions bound to this user context def remove_all_forced_decisions(self): # The following methods require the Audience targeting with Optimizely Data Platform # integration enabled. See note below code sample. # Return the saved results of **fetch_qualified_segments()**. # Can be None if not properly updated with fetch_qualified_segments(). def get_qualified_segments(self): # Overwrite the qualified segments array. # This allows for bypassing the remote fetching process from ODP # or for utilizing your own fetching service. def set_qualified_segments(self, segments): # 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(callback=None, options=None): # Check is the user qualified for the given segment. def is_qualified_for(self, segment):
You must first enable the Optimizely Data Platform Advanced Audience Targeting integration to be able to access the
is_qualified_for()methods. Refer to Advanced Audience Targeting for more information.
The following table shows attributes for the OptimizelyUserContext object:
|user_id||String||The ID of the user|
|(optional) attributes||Dict||A dictionary of custom key-value pairs specifying attributes for the user that are used for audience targeting. You can pass the dictionary with the user ID when you create the user.|
The following table shows methods for the OptimizelyUserContext object:
|set_attribute||Pass a custom user attribute as a key-value pair to the user context.|
|decide||Returns a decision result for a flag key for a user. The method returns the decision result in an OptimizelyDecision object, which contains all data required to deliver the flag rule. --comment check my rewording|
See Decide methods
|decide_for_keys||Returns a dictionary of flag decisions for specified flag keys.|
See Decide methods
|decide_all||Returns decisions for all active (unarchived) flags for a user.|
See Decide methods
|track_event||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
|set_forced_decision||Forces a user into a specific variation.|
See Set Forced Decision
|get_forced_decision||Returns what variation the user was forced into.|
See Get Forced Decision
|remove_forced_decision||Removes a user from a specific forced variation.|
See Remove Forced Decision
|remove_all_forced_decisions||Removes a user from all forced variations.|
See Remove All Forced Decisions
|get_qualified_segments **||Returns the qualified segments that were saved by fetch_qualified_segments. See Advanced Audience Targeting segment qualification methods.|
|set_qualified_segments **||Overwrites the qualified segments that were saved by fetch_qualified_segments. See Advanced Audience Targeting segment qualification methods.|
|fetch_qualified_segments **||Fetch all ODP real-time segments that the user context qualified for. Has a synchronous and asynchronous implementation. See Advanced Audience Targeting segment qualification methods.|
|is_qualified_for **||Checks 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.
Create User Context --comment not sure this needs to be a separate heading, I think you could just say "To learn more, go to Create User Context"
The language/platform source files containing the implementation for Python is optimizely.py.
Updated about 2 months ago