Optimizely’s APIs

class optimizely.optimizely.Optimizely(datafile: Optional[str] = None, event_dispatcher: Optional[CustomEventDispatcher] = None, logger: Optional[_logging.Logger] = None, error_handler: Optional[BaseErrorHandler] = None, skip_json_validation: Optional[bool] = False, user_profile_service: Optional[UserProfileService] = None, sdk_key: Optional[str] = None, config_manager: Optional[BaseConfigManager] = None, notification_center: Optional[NotificationCenter] = None, event_processor: Optional[BaseEventProcessor] = None, datafile_access_token: Optional[str] = None, default_decide_options: Optional[list[str]] = None, event_processor_options: Optional[dict[str, Any]] = None, settings: Optional[OptimizelySdkSettings] = None)

Class encapsulating all SDK functionality.

__init__(datafile: Optional[str] = None, event_dispatcher: Optional[CustomEventDispatcher] = None, logger: Optional[_logging.Logger] = None, error_handler: Optional[BaseErrorHandler] = None, skip_json_validation: Optional[bool] = False, user_profile_service: Optional[UserProfileService] = None, sdk_key: Optional[str] = None, config_manager: Optional[BaseConfigManager] = None, notification_center: Optional[NotificationCenter] = None, event_processor: Optional[BaseEventProcessor] = None, datafile_access_token: Optional[str] = None, default_decide_options: Optional[list[str]] = None, event_processor_options: Optional[dict[str, Any]] = None, settings: Optional[OptimizelySdkSettings] = None) 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.BatchEventProcessor is used which batches events. To simply forward events to the event dispatcher configure and use optimizely.event.event_processor.ForwardingEventProcessor.

  • 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.

  • event_processor_options – Optional dict of options to be passed to the default batch event processor.

  • settings – Optional instance of OptimizelySdkSettings for sdk configuration.

activate(experiment_key: str, user_id: str, attributes: Optional[UserAttributes] = None) Optional[str]

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: str, attributes: Optional[UserAttributes] = None) Optional[optimizely.optimizely_user_context.OptimizelyUserContext]

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: str, user_id: str, attributes: Optional[UserAttributes] = None) Optional[dict[str, Any]]

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: str, attributes: Optional[UserAttributes] = None) list[str]

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: str, variable_key: str, user_id: str, attributes: Optional[UserAttributes] = None) Any

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: str, variable_key: str, user_id: str, attributes: Optional[UserAttributes] = None) Optional[bool]

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: str, variable_key: str, user_id: str, attributes: Optional[UserAttributes] = None) Optional[float]

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: str, variable_key: str, user_id: str, attributes: Optional[UserAttributes] = None) Optional[int]

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: str, variable_key: str, user_id: str, attributes: Optional[UserAttributes] = None) Optional[dict[str, Any]]

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: str, variable_key: str, user_id: str, attributes: Optional[UserAttributes] = None) Optional[str]

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: str, user_id: str) Optional[str]

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() Optional[optimizely.optimizely_config.OptimizelyConfig]

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: str, user_id: str, attributes: Optional[UserAttributes] = None) Optional[str]

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: str, user_id: str, attributes: Optional[UserAttributes] = None) bool

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.

send_odp_event(action: str, identifiers: dict[str, str], type: str = 'fullstack', data: Optional[dict[str, str | int | float | bool | None]] = None) None

Send an event to the ODP server.

Parameters
  • action – The event action name. Cannot be None or empty string.

  • identifiers – A dictionary for identifiers. The caller must provide at least one key-value pair.

  • type – The event type. Default ‘fullstack’.

  • data – An optional dictionary for associated data. The default event data will be added to this data

  • server. (before sending to the ODP) –

set_forced_variation(experiment_key: str, user_id: str, variation_key: Optional[str]) bool

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.

  • null (will be forced into. If) –

  • mapping. (then clear the existing experiment-to-variation) –

Returns

A boolean value that indicates if the set completed successfully.

track(event_key: str, user_id: str, attributes: Optional[UserAttributes] = None, event_tags: Optional[EventTags] = None) 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

class optimizely.event_dispatcher.EventDispatcher
static dispatch_event(event: optimizely.event_builder.Event) None

Dispatch the event being represented by the Event object.

Parameters

event – Object holding information about the request to be dispatched to the Optimizely backend.

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: int = 20)

Class providing log method which logs to stdout.

optimizely.logger.adapt_logger(logger: Union[logging.Logger, optimizely.logger.BaseLogger]) Union[logging.Logger, optimizely.logger.BaseLogger]

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: str, level: Optional[int] = None, handler: Optional[logging.Handler] = None) logging.Logger

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: str, experiment_bucket_map: Optional[dict[str, dict[str, Optional[str]]]] = None, **kwargs: Any)

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: str) Optional[str]

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: str, variation_id: str) None

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: str) dict[str, Any]

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: dict[str, Any]) None

Save the user profile dict sent to this method.

Parameters

user_profile – Dict representing the user’s profile.