Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev guideRecipesAPI Reference
Dev guideAPI ReferenceUser GuideLegal TermsGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
Dev guide

OptimizelyConfig for the PHP SDK

Describes getting access to project configuration data within the datafile using OptimizelyConfig for the PHP SDK.

Overview

Feature Experimentation SDKs open a well-defined set of public APIs, hiding all implementation details. However, some clients may need access to project configuration data within the datafile.

In this document, we extend our public APIs to define data models and access methods, which clients can use to access project configuration data.

OptimizelyConfig API

A public configuration data model (OptimizelyConfig) is defined below as a structured format of static Optimizely Project data.

Get OptimizelyConfig

OptimizelyConfig can be accessed from OptimizelyClient (top-level) with this public API call:

$config = $optimizelyClient->getOptimizelyConfig();

getOptimizelyConfig returns an OptimizelyConfig instance, which includes

  • environment key
  • SDK key
  • the datafile revision number
  • all experiments mapped by their key values
  • all attributes
  • all audiences
  • all events
  • feature flags mapped by their key values
  • function to retrieve the project configuration (the datafile)

📘

Note

When the SDK datafile is updated (the client can add a notification listener for OPTIMIZELY_CONFIG_UPDATE to get notified), the client is expected to call the method to get the updated OptimizelyConfig data. See examples below.

Get datafile

To ensure that multiple SDK instances all instantiate from the same configuration (for example, in a client/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, see Sharing the datafile with multiple SDK implementations.

Object model

The following shows the object model for OptimizelyConfig.

/**
 * 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

OptimizelyConfig can be accessed from OptimizelyClient (top-level) like this:

$config = $optimizelyClient->getOptimizelyConfig();
// get the revision
echo "[OptimizelyConfig] revision:" . $config->getRevision();

// get the SDK key
echo "[OptimizelyConfig] SDKKey:" . $config->getSdkKey();

// get the environment key
echo "[OptimizelyConfig] environmentKey:" . $config->getEnvironmentKey();

// all attributes
echo "[OptimizelyConfig] attributes:";
$attributes = $config->getAttributes();
foreach($attributes as $attribute)
{
    echo "[OptimizelyAttribute]   -- (id, key) = ((" . $attribute->getId(). "), (". $attribute->getKey() . "))";
}

// all audiences
echo "[OptimizelyConfig] audiences:";
$audiences = $config->getAudiences();
foreach($audiences as $audience)
{
    echo "[OptimizelyAudience]   -- (id, key, conditions) = ((" . $audience->getId(). "), (". $audience->getName() . "), (". $audience->getConditions() . "))";
}

// all events
echo "[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() : "";
    }
);