Optimizely’s APIs¶
-
class
optimizely.optimizely.
Optimizely
(datafile=None, event_dispatcher=None, logger=None, error_handler=None, skip_json_validation=False, user_profile_service=None, sdk_key=None, config_manager=None, notification_center=None, event_processor=None, datafile_access_token=None, default_decide_options=None)¶ Class encapsulating all SDK functionality.
-
__init__
(datafile=None, event_dispatcher=None, logger=None, error_handler=None, skip_json_validation=False, user_profile_service=None, sdk_key=None, config_manager=None, notification_center=None, event_processor=None, datafile_access_token=None, default_decide_options=None)¶ Optimizely init method for managing Custom projects.
- Parameters
datafile – Optional JSON string representing the project. Must provide at least one of datafile or sdk_key.
event_dispatcher – Provides a dispatch_event method which if given a URL and params sends a request to it.
logger – Optional component which provides a log method to log messages. By default nothing would be logged.
error_handler – Optional component which provides a handle_error method to handle exceptions. By default all exceptions will be suppressed.
skip_json_validation – Optional boolean param which allows skipping JSON schema validation upon object invocation. By default JSON schema validation will be performed.
user_profile_service – Optional component which provides methods to store and manage user profiles.
sdk_key – Optional string uniquely identifying the datafile corresponding to project and environment combination. Must provide at least one of datafile or sdk_key.
config_manager – Optional component which implements optimizely.config_manager.BaseConfigManager.
notification_center – Optional instance of notification_center.NotificationCenter. Useful when providing own config_manager.BaseConfigManager implementation which can be using the same NotificationCenter instance.
event_processor – Optional component which processes the given event(s). By default optimizely.event.event_processor.ForwardingEventProcessor is used which simply forwards events to the event dispatcher. To enable event batching configure and use optimizely.event.event_processor.BatchEventProcessor.
datafile_access_token – Optional string used to fetch authenticated datafile for a secure project environment.
default_decide_options – Optional list of decide options used with the decide APIs.
-
activate
(experiment_key, user_id, attributes=None)¶ Buckets visitor and sends impression event to Optimizely.
- Parameters
experiment_key – Experiment which needs to be activated.
user_id – ID for user.
attributes – Dict representing user attributes and values which need to be recorded.
- Returns
Variation key representing the variation the user will be bucketed in. None if user is not in experiment or if experiment is not Running.
-
create_user_context
(user_id, attributes=None)¶ We do not check for is_valid here as a user context can be created successfully even when the SDK is not fully configured.
- Parameters
user_id – string to use as user id for user context
attributes – dictionary of attributes or None
- Returns
UserContext instance or None if the user id or attributes are invalid.
-
get_all_feature_variables
(feature_key, user_id, attributes=None)¶ Returns dictionary of all variables and their corresponding values in the context of a feature.
- Parameters
feature_key – Key of the feature whose variable’s value is being accessed.
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
Feature key is invalid.
- Return type
Dictionary mapping variable key to variable value. None if
-
get_enabled_features
(user_id, attributes=None)¶ Returns the list of features that are enabled for the user.
- Parameters
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
A list of the keys of the features that are enabled for the user.
-
get_feature_variable
(feature_key, variable_key, user_id, attributes=None)¶ Returns value for a variable attached to a feature flag.
- Parameters
feature_key – Key of the feature whose variable’s value is being accessed.
variable_key – Key of the variable whose value is to be accessed.
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
Feature key is invalid.
Variable key is invalid.
- Return type
Value of the variable. None if
-
get_feature_variable_boolean
(feature_key, variable_key, user_id, attributes=None)¶ Returns value for a certain boolean variable attached to a feature flag.
- Parameters
feature_key – Key of the feature whose variable’s value is being accessed.
variable_key – Key of the variable whose value is to be accessed.
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
Feature key is invalid.
Variable key is invalid.
Mismatch with type of variable.
- Return type
Boolean value of the variable. None if
-
get_feature_variable_double
(feature_key, variable_key, user_id, attributes=None)¶ Returns value for a certain double variable attached to a feature flag.
- Parameters
feature_key – Key of the feature whose variable’s value is being accessed.
variable_key – Key of the variable whose value is to be accessed.
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
Feature key is invalid.
Variable key is invalid.
Mismatch with type of variable.
- Return type
Double value of the variable. None if
-
get_feature_variable_integer
(feature_key, variable_key, user_id, attributes=None)¶ Returns value for a certain integer variable attached to a feature flag.
- Parameters
feature_key – Key of the feature whose variable’s value is being accessed.
variable_key – Key of the variable whose value is to be accessed.
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
Feature key is invalid.
Variable key is invalid.
Mismatch with type of variable.
- Return type
Integer value of the variable. None if
-
get_feature_variable_json
(feature_key, variable_key, user_id, attributes=None)¶ Returns value for a certain JSON variable attached to a feature.
- Parameters
feature_key – Key of the feature whose variable’s value is being accessed.
variable_key – Key of the variable whose value is to be accessed.
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
Feature key is invalid.
Variable key is invalid.
Mismatch with type of variable.
- Return type
Dictionary object of the variable. None if
-
get_feature_variable_string
(feature_key, variable_key, user_id, attributes=None)¶ Returns value for a certain string variable attached to a feature.
- Parameters
feature_key – Key of the feature whose variable’s value is being accessed.
variable_key – Key of the variable whose value is to be accessed.
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
Feature key is invalid.
Variable key is invalid.
Mismatch with type of variable.
- Return type
String value of the variable. None if
-
get_forced_variation
(experiment_key, user_id)¶ Gets the forced variation for a given user and experiment.
- Parameters
experiment_key – A string key identifying the experiment.
user_id – The user ID.
- Returns
The forced variation key. None if no forced variation key.
-
get_optimizely_config
()¶ Gets OptimizelyConfig instance for the current project config.
- Returns
OptimizelyConfig instance. None if the optimizely instance is invalid or project config isn’t available.
-
get_variation
(experiment_key, user_id, attributes=None)¶ Gets variation where user will be bucketed.
- Parameters
experiment_key – Experiment for which user variation needs to be determined.
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
Variation key representing the variation the user will be bucketed in. None if user is not in experiment or if experiment is not Running.
-
is_feature_enabled
(feature_key, user_id, attributes=None)¶ Returns true if the feature is enabled for the given user.
- Parameters
feature_key – The key of the feature for which we are determining if it is enabled or not for the given user.
user_id – ID for user.
attributes – Dict representing user attributes.
- Returns
True if the feature is enabled for the user. False otherwise.
-
set_forced_variation
(experiment_key, user_id, variation_key)¶ Force a user into a variation for a given experiment.
- Parameters
experiment_key – A string key identifying the experiment.
user_id – The user ID.
variation_key – A string variation key that specifies the variation which the user.
be forced into. If null, then clear the existing experiment-to-variation mapping. (will) –
- Returns
A boolean value that indicates if the set completed successfully.
-
track
(event_key, user_id, attributes=None, event_tags=None)¶ Send conversion event to Optimizely.
- Parameters
event_key – Event key representing the event which needs to be recorded.
user_id – ID for user.
attributes – Dict representing visitor attributes and values which need to be recorded.
event_tags – Dict representing metadata associated with the event.
-
Event Dispatcher¶
Logger¶
-
class
optimizely.logger.
BaseLogger
¶ Class encapsulating logging functionality. Override with your own logger providing log method.
-
class
optimizely.logger.
NoOpLogger
¶ Class providing log method which logs nothing.
-
class
optimizely.logger.
SimpleLogger
(min_level=20)¶ Class providing log method which logs to stdout.
-
optimizely.logger.
adapt_logger
(logger)¶ Adapt our custom logger.BaseLogger object into a standard logging.Logger object.
- Adaptations are:
NoOpLogger turns into a logger with a single NullHandler.
SimpleLogger turns into a logger with a StreamHandler and level.
- Parameters
logger – Possibly a logger.BaseLogger, or a standard python logging.Logger.
Returns: a standard python logging.Logger.
-
optimizely.logger.
reset_logger
(name, level=None, handler=None)¶ Make a standard python logger object with default formatter, handler, etc.
- Defaults are:
level == logging.INFO
handler == logging.StreamHandler()
- Parameters
name – a logger name.
level – an optional initial log level for this logger.
handler – an optional initial handler for this logger.
Returns: a standard python logger with a single handler.
User Profile¶
UserProfile
¶
-
class
optimizely.user_profile.
UserProfile
(user_id, experiment_bucket_map=None, **kwargs)¶ Class encapsulating information representing a user’s profile.
user_id: User’s identifier. experiment_bucket_map: Dict mapping experiment ID to dict consisting of the
variation ID identifying the variation for the user.
-
get_variation_for_experiment
(experiment_id)¶ Helper method to retrieve variation ID for given experiment.
- Parameters
experiment_id – ID for experiment for which variation needs to be looked up for.
- Returns
Variation ID corresponding to the experiment. None if no decision available.
-
save_variation_for_experiment
(experiment_id, variation_id)¶ Helper method to save new experiment/variation as part of the user’s profile.
- Parameters
experiment_id – ID for experiment for which the decision is to be stored.
variation_id – ID for variation that the user saw.
-
UserProfileService
¶
-
class
optimizely.user_profile.
UserProfileService
¶ Class encapsulating user profile service functionality. Override with your own implementation for storing and retrieving the user profile.
-
lookup
(user_id)¶ Fetch the user profile dict corresponding to the user ID.
- Parameters
user_id – ID for user whose profile needs to be retrieved.
- Returns
Dict representing the user’s profile.
-
save
(user_profile)¶ Save the user profile dict sent to this method.
- Parameters
user_profile – Dict representing the user’s profile.
-