OptimizelyConfig for the Python SDK
How to get access to project configuration data within the datafile using OptimizelyConfig for the Optimizely Feature Experimentation Python SDK.
Optimizely Feature Experimentation SDKs open a well-defined set of public APIs, hiding implementation details. However, you may need access to project configuration data within thedatafile. The following section extends the public APIs to define data models and access methods, which you can use to access project configuration data.
OptimizelyConfig API
A public configuration data model (OptimizelyConfig) is defined as a structured format of static Optimizely Project data.
GetOptimizelyConfig
OptimizelyConfigYou can accessOptimizelyConfig through theOptimizelyClient (top-level) using this public API call.
def get_optimizely_config(self)getOptimizelyConfig returns anOptimizelyConfig instance, which includes the following:
- Environment key
- SDK key
- Datafile revision number
- Experiments mapped by key values
- All attributes
- All audiences
- All events
- Feature flags mapped by key values
- Function to retrieve the project configuration (the datafile)
📘
NoteWhen the system updates the datafile, you can add aOptimizelyConfigUpdate notification listener to get notified. After receiving the notification, call the method to get the updated
OptimizelyConfigdata.
Get datafile
To share the same datafile between multiple SDK instances, such as in a client and server scenario, you can pass a JSON string representation of the config (the datafile) between the instances. To get the datafile, use the OptimizelyConfig object's getDatafile method. For more information, seeSharing the datafile with multiple SDK implementations.
Object model
The following sample shows the object model forOptimizelyConfig:
class OptimizelyConfig(object): def __init__(self, revision, experiments_map, features_map, sdk_key=None, environment_key=None, attributes=None, events=None, audiences=None): self.revision = revision # This experiments_map is for experiments of legacy projects only. # For flag projects, experiment keys are not guaranteed to be unique # across multiple flags, so this map may not include all experiments # when keys conflict. self.experiments_map = experiments_map self.features_map = features_map self.sdk_key = sdk_key or '' self.environment_key = environment_key or '' self.attributes = attributes or [] self.events = events or [] self.audiences = audiences or []class OptimizelyExperiment(object): def __init__(self, id, key, variations_map, audiences=''): self.id = id self.key = key self.variations_map = variations_map self.audiences = audiencesclass OptimizelyFeature(object): self.id = id self.key = key self.variables_map = variables_map self.delivery_rules = [] self.experiment_rules = [] # Deprecated. Use experiment_rules and delivery_rules. self.experiments_map = experiments_mapclass OptimizelyVariation(object): def __init__(self, id, key, feature_enabled, variables_map): self.id = id self.key = key self.feature_enabled = feature_enabled self.variables_map = variables_mapclass OptimizelyVariable(object): def __init__(self, id, key, variable_type, value): self.id = id self.key = key self.type = variable_type self.value = valueclass OptimizelyAttribute(object): def __init__(self, id, key): self.id = id self.key = keyclass OptimizelyEvent(object): def __init__(self, id, key, experiment_ids): self.id = id self.key = key self.experiment_ids = experiment_idsclass OptimizelyAudience(object): def __init__(self, id, name, conditions): self.id = id self.name = name self.conditions = conditionsExamples
You can accessOptimizelyConfig from theOptimizelyClient (top-level) using the following code example:
from optimizely import optimizelyoptimizely_client = optimizely.Optimizely(sdk_key='<YOUR_SDK_KEY')config = optimizely_client.get_optimizely_config()print('REVISION ', config.revision)print('SDK KEY ', config.sdk_key)print('ENV KEY ', config.environment_key)print("[OptimizelyConfig] revision = ", config.revision)print("[OptimizelyConfig] sdk_key = ", config.sdk_key)print("[OptimizelyConfig] environment_key = ", config.environment_key)print("[OptimizelyConfig] attributes:")for attribute in config.attributes: print('[optimizelyConfig] - (id, key) ', attribute.id, attribute.key)print("[OptimizelyConfig] audiences:")for audience in config.audiences: print('[OptimizelyConfig] - (id, name, conditions) ', audience.id, audience.name, audience.conditions)print("[OptimizelyConfig] events:")for event in config.events: print("[OptimizelyConfig] - (id, key, experimentIds) ", event.id, event.key, event.experiment_ids)# all flagsflags = config.features_map.values()print('[OptimizelyConfig] - flags ', flags)flag_keys = config.features_map.keys() # swiftprint('[OptimizelyConfig] - flag keys ', flag_keys)for flag_key in flag_keys: flag = config.features_map[flag_key] experiment_rules = flag.experiment_rules delivery_rules = flag.delivery_rules print(experiment_rules) print(delivery_rules) # use experiment rules and delivery rules and other flag data here... for experiment in experiment_rules: print("[OptimizelyConfig] - experiment rule-key = ", experiment.key) print("[OptimizelyConfig] - experiment audiences = ", experiment.audiences) print("[OptimizelyConfig] - experiment variations map = ", experiment.variations_map) variations_map = experiment.variations_map variation_keys = variations_map.keys() for variation_key in variation_keys: print("[OptimizelyConfig] - variation = ", variation_key) # not the same as in swift! map_of_variables = variations_map[variation_key].variables_map variable_keys = map_of_variables.keys() for variable_key in variable_keys: variable = map_of_variables[variable_key] print('[OptimizelyConfig] - variable = ', variable_key, variable.value) for delivery in delivery_rules: print("[OptimizelyConfig] - delivery rule-key = ", delivery.key) print("[OptimizelyConfig] - delivery audiences = ", delivery.audiences)# listen to OPTIMIZELY_CONFIG_UPDATE to get updated datadef on_config_update_listener(*args): config = optimizely_client.get_optimizely_config()optimizely_client.notification_center.add_notification_listener( enums.NotificationTypes.OPTIMIZELY_CONFIG_UPDATE, on_config_update_listener)Updated 17 days ago