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 ReferenceChangelog
Dev guideAPI ReferenceRecipesChangelogUser GuideGitHubDev 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.

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 the datafile. 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.

Get OptimizelyConfig

You can access OptimizelyConfig through the OptimizelyClient (top-level) using this public API call.

$config = $optimizelyClient->getOptimizelyConfig();

getOptimizelyConfig returns an OptimizelyConfig 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)

📘

Note

When the system updates the datafile, you can add a OptimizelyConfigUpdate notification listener to get notified. After receiving the notification, call the method to get the updated OptimizelyConfig data.

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, see Sharing the datafile with multiple SDK implementations.

Object model

The following sample 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

You can access OptimizelyConfig from the OptimizelyClient (top-level) using the following code example:

$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() : "";
    }
);