HomeGuidesAPI Reference
Submit Documentation FeedbackJoin Developer CommunityOptimizely GitHubOptimizely NuGetLog In

OptimizelyUserContext

This topic describes the OptimizelyUserContext Object which allows you to make flag decisions and track events for a user context for the Optimizely Feature Experimentation Python SDK.

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.

OptimizelyUserContext version

OptimizelyUserContext is supported on SDK v3.8.0 and higher.

Forced decision methods version

set_forced_decision(), get_forced_decision(), remove_forced_decision() and remove_all_forced_decision() methods are supported on 4.0.0 and higher.

OptimizelyUserContext definition

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):

Properties

The following table shows attributes for the OptimizelyUserContext object:

AttributeTypeComment
user_idStringThe ID of the user
(optional) attributesMapA 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.

Methods

The following table shows methods for the OptimizelyUserContext object:

MethodComment
set_attributePass a custom user attribute as a key-value pair to the user context.
decideReturns a decision result for a flag key for a user. The decision result is returned in an OptimizelyDecision object, and contains all data required to deliver the flag rule.
See Decide methods
decide_for_keysReturns a map of flag decisions for specified flag keys.
See Decide methods
decide_allReturns decisions for all active (unarchived) flags for a user.
See Decide methods
track_eventTracks a conversion event (in other words, an action a user takes) for a user. Logs an error message if the specified event key doesn't match any existing events.
See Track Event
set_forced_decisionForces a user into a specific variation.
See Set Forced Decision
get_forced_decisionReturns what variation the user was forced into.
See Get Forced Decision
remove_forced_decisionRemoves a user from a specific forced variation.
See Remove Forced Decision
remove_all_forced_decisionsRemoves a user from all forced variations.
See Remove All Forced Decisions

See Also

Create User Context

Source files

The language/platform source files containing the implementation for Python is optimizely.py.