OptimizelyConfig for the PHP SDK
Describes getting access to project configuration data within the datafile using OptimizelyConfig for the PHP 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.
$config = $optimizelyClient->getOptimizelyConfig();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 ensure that multiple SDK instances all instantiate from the same configuration (for example, 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:
/** * OptimizelyConfig is an object describing the current project configuration data being used by this SDK instance. * @typedef {string} environmentKey * @typedef {string} sdkKey * @typedef {string} revision * This experimentsMap 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. * @property {Object.<string, OptimizelyExperiment>}} experimentsMap * @property {Object.<string, OptimizelyFeature>}} featuresMap * @property [Object.<string, OptimizelyAttribute>}] attributes * @property [Object.<string, OptimizelyAudience>}] audiences * @property [Object.<string, OptimizelyEvent>}] events *//** * OptimizelyFeature is an object describing a feature flag * @typedef {Object} OptimizelyFeature * @property {string} id * @property {string} key * @property [Object.<string, OptimizelyExperiment>}] experimentRules * @property [Object.<string, OptimizelyExperiment>}] deliveryRules * Use experimentRules and deliverRules * @property {Object.<string, OptimizelyExperiment>}} experimentsMap * @property {Object.<string, OptimizelyVariable>}} variablesMap *//** * OptimizelyExperiment is an object describing an experiment * @typedef {Object} OptimizelyExperiment * @property {string} id * @property {string} key * @property {string} audiences * @property {Object.<string, OptimizelyVariation>}} variationMap *//** * OptimizelyVariation is an object describing a variation in an experiment * @typedef {Object} OptimizelyVariation * @property {string} id * @property {string} key * @property {boolean=} featureEnabled * @property {Object.<string, OptimizelyVariable>}} variablesMap *//** * OptimizelyVariable is an object describing a feature flag variable * @typedef {Object} OptimizelyVariable * @property {string} id * @property {string} key * @property {string} type * @property {string} value *//** * OptimizelyAttribute is an object describing an attribute * @typedef {Object} OptimizelyAttribute * @property {string} id * @property {string} key *//** * OptimizelyAudience is an object describing an audience * @typedef {Object} OptimizelyAudience * @property {string} id * @property {string} name * @property {string} conditions *//** * OptimizelyEvent is an object describing an event * @typedef {Object} OptimizelyEvent * @property {string} id * @property {string} key * @property {array} experimentIds */Examples
You can accessOptimizelyConfig from theOptimizelyClient (top-level) using the following code example:
$config = $optimizelyClient->getOptimizelyConfig();// get the revisionecho "[OptimizelyConfig] revision:" . $config->getRevision();// get the SDK keyecho "[OptimizelyConfig] SDKKey:" . $config->getSdkKey();// get the environment keyecho "[OptimizelyConfig] environmentKey:" . $config->getEnvironmentKey();// all attributesecho "[OptimizelyConfig] attributes:";$attributes = $config->getAttributes();foreach($attributes as $attribute){ echo "[OptimizelyAttribute] -- (id, key) = ((" . $attribute->getId(). "), (". $attribute->getKey() . "))";}// all audiencesecho "[OptimizelyConfig] audiences:";$audiences = $config->getAudiences();foreach($audiences as $audience){ echo "[OptimizelyAudience] -- (id, key, conditions) = ((" . $audience->getId(). "), (". $audience->getName() . "), (". $audience->getConditions() . "))";}// all eventsecho "[OptimizelyConfig] events:";$events = $config->getEvents();foreach($events as $event){ echo "[OptimizelyEvent] -- (id, key, experimentIds) = ((" . $event->getId(). "), (". $event->getKey() . "), (". $event->getExperimentIds() . "))";}// all flags$flags = array_values((array)$config->getFeaturesMap());foreach ($flags as $flag){ // Use experimentRules and deliverRules $experimentRules = $flag->getExperimentRules(); foreach ($experimentRules as $experimentRule) { echo "[OptimizelyExperiment] - experiment rule-key = " . $experimentRule->getKey(); echo "[OptimizelyExperiment] - experiment audiences = " . $experimentRule->getExperimentAudiences(); // all variations $variations = array_values((array)$experimentRule->getVariationsMap()); foreach ($variations as $variation) { echo "[OptimizelyVariation] -- variation = { key: " . $variation->getKey() . ", id: " . $variation->getId() . ", featureEnabled: " . $variation->getFeatureEnabled() . " }"; $variables = $variation->getVariablesMap(); foreach ($variables as $variable) { echo "[OptimizelyVariable] --- variable: " . $variable->getKey() . ", " . $variable->getId(); // use variable data here. } // use experimentRule data here. } } $deliveryRules = $flag->getDeliveryRules(); foreach ($deliveryRules as $deliveryRule) { echo "[OptimizelyExperiment] - delivery rule-key = " . $deliveryRule->getKey(); echo "[OptimizelyExperiment] - delivery audiences = " . $deliveryRule->getExperimentAudiences(); // use delivery rule data here... }}$optimizelyClient->notificationCenter->addNotificationListener( NotificationType::OPTIMIZELY_CONFIG_UPDATE, function () { $newConfig = $optimizelyClient->getOptimizelyConfig(); echo "[OptimizelyConfig] revision = " . $newConfig ? $newConfig->getRevision() : ""; });Updated 17 days ago