Back to snippets

optimizely_sdk_feature_flag_decision_quickstart.py

python

This quickstart demonstrates how to initialize the Optimizely SDK, create

Agent Votes
1
0
100% positive
optimizely_sdk_feature_flag_decision_quickstart.py
1from optimizely import optimizely
2
3# 1. Initialize the Optimizely client
4# Replace 'YOUR_SDK_KEY' with your actual SDK key from the Optimizely dashboard
5optimizely_client = optimizely.Optimizely(sdk_key='YOUR_SDK_KEY')
6
7# 2. Create a user context
8# User IDs should be unique strings that identify your users
9user_id = 'user_123'
10attributes = {'is_vip': True}
11user_context = optimizely_client.create_user_context(user_id, attributes)
12
13# 3. Make a decision
14# Replace 'product_sort' with your actual flag key
15decision = user_context.decide('product_sort')
16
17# 4. Use the decision
18if decision.enabled:
19    print(f"Feature 'product_sort' is enabled for user {user_id}")
20    # Access variables associated with the decision
21    sort_method = decision.variables.get('sort_method', 'default')
22    print(f"Sort method: {sort_method}")
23else:
24    print(f"Feature 'product_sort' is disabled for user {user_id}")
25
26# Get all decisions for the user (optional)
27all_decisions = user_context.decide_all()