Update from older versions of the Python SDK
Describes how to update from older versions of the Optimizely Full Stack (Legacy) Python SDK to Optimizely Feature Experimentation.
This section provides code examples for how Optimizely recommends leveraging the new Decision and Event Tracking APIs. All existing Full Stack (Legacy) methods and implementation are still supported. See Optimizely Feature Experimentation - application & migration documentation for the latest updates.
Optimizely recommends adopting the new Decide
, Decide All
, and Track Event
methods as a more flexible and easier-to-use replacement where you currently use isFeatureEnabled
, getFeatureVariable
, getAllFeatures
, or Track
calls within your implementation.
The following are examples of how to migrate the Full Stack (Legacy) methods to Feature Experimentation methods:
from optimizely import optimizely
from optimizely.decision.optimizely_decide_option import OptimizelyDecideOption
# -------------------------------
# Prereq for new methods: create a user
# ------------------------------
attributes = {"is_logged_in": True}
user = optimizely_client.create_user_context("user_123", attributes)
# -------------------------------
# Is Feature Enabled
# ------------------------------
# old method
enabled = optimizely_client.is_feature_enabled("flag_1","user_123", attributes)
# new method
decision = user.decide("flag_1")
enabled = decision.enabled
# -------------------------------
# Activate & Get Variation
# ------------------------------
# old method
variation_key = optimizely.activate('experiment_key', 'user_123', attributes)
# new method
variation_key = decision.variation_key
# -------------------------------
# Get All Feature Variables
# ------------------------------
# old method
all_var_values = optimizely.get_all_feature_variables('my_feature_key', 'user_123', attributes)
# new method
all_var_values = decision.variables
# -------------------------------
# Get Enabled Features
# ------------------------------
# old method
enabled_features = optimizely.get_enabled_features("user_123")
# new method
decisions = user.decide_all([OptimizelyDecideOption.ENABLED_FLAGS_ONLY])
enabled_flags = decisions.keys
# -------------------------------
# Track
# ------------------------------
# old method
optimizely.track('my_purchase_event_key', 'user_123', attributes, {"purchase_count": 2})
# new method
user.track_event('my_purchase_event_key', {"purchase_count": 2})
Updated 12 months ago