Updating usage from older versions

Code examples

This section provides code examples for how we recommend leveraging our new Decision and Event Tracking APIs. All existing methods and implementation are still included and supported and will only be removed after deprecation marking and with a future Major version.

We recommend adopting the new Decide, Decide All and Track Event methods as more flexible and easier to use replacements for cases where you currently use isFeatureEnabled, getFeatureVariable, getAllFeatures or Track calls within your implementation.

Refer to an earlier version of the SDK reference guides for earlier methods.

The following are some examples of how to migrate older methods to newer 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.create_user_context("user_123", attributes)

# -------------------------------
# Is Feature Enabled
# ------------------------------
# old method
enabled = optimizely.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})

Activate method and A/B Tests

Like the existing Is Feature Enabled method, the Decide methods are based on feature flag keys, and do not support standalone A/B tests or Multi-armed Bandits. We are working to unify the data models and interfaces for our application longer term to mitigate the need to maintain multiple different access methods. In the meantime, you can still use the Activate and Get Variation methods for standalone A/B tests in your legacy projects alongside the Decide methods.