OptimizelyConfig
This topic describes how to get access to project configuration data within the datafile using OptimizelyConfig.
Overview
Full Stack 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.
OptimizelyConfig can be accessed from OptimizelyClient (top-level) with this public API call:
public OptimizelyConfig 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
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 share the same datafile between multiple SDK instances (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 Multiple SDK implementations.
Object model
The following shows the object model for OptimizelyConfig.
// OptimizelyConfig is an object describing the current project configuration data
public class OptimizelyConfig {
// 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.
Map<String, OptimizelyExperiment> experimentsMap;
Map<String, OptimizelyFeature> featuresMap;
List<OptimizelyAttribute> attributes;
List<OptimizelyEvent> events;
List<OptimizelyAudience> audiences;
String revision;
String sdkKey;
String environmentKey;
}
// OptimizelyFeature is an object describing a feature flag
public class OptimizelyFeature {
String id;
String key;
/**
* @deprecated use {@link #experimentRules} and {@link #deliveryRules} instead
*/
@Deprecated
Map<String, OptimizelyExperiment> experimentsMap;
Map<String, OptimizelyVariable> variablesMap;
List<OptimizelyExperiment> experimentRules;
List<OptimizelyExperiment> deliveryRules;
}
// OptimizelyExperiment is an object describing an experiment
public class OptimizelyExperiment {
String id;
String key;
String audiences;
Map<String, OptimizelyVariation> variationsMap;
}
// OptimizelyVariation is an object describing a variation
public class OptimizelyVariation {
String id;
String key;
Boolean featureEnabled;
Map<String, OptimizelyVariable> variablesMap;
}
// OptimizelyVariable is an object describing a Variable
public class OptimizelyVariable {
String id;
String key;
String type;
String value;
}
// OptimizelyAttribute is an object describing an Attribute
public class OptimizelyAttribute {
String id;
String key;
}
// OptimizelyAudience is an object describing an Audience
public class OptimizelyAudience {
String id;
String name;
String conditions;
}
// OptimizelyEvent is an object describing an Event
public class OptimizelyEvent {
String id;
String key;
List<String> experimentIds;
}
Examples
OptimizelyConfig can be accessed from OptimizelyClient (top-level) like this:
OptimizelyConfig config = optimizelyClient.getOptimizelyConfig();
System.out.println("[OptimizelyConfig] revision = " + config.getRevision());
System.out.println("[OptimizelyConfig] sdkKey = " + config.getSdkKey());
System.out.println("[OptimizelyConfig] environmentKey = " + config.getEnvironmentKey());
System.out.println("[OptimizelyConfig] attributes:");
for (OptimizelyAttribute attribute: config.getAttributes()) {
System.out.println("[OptimizelyAttribute] -- (id, key) = " + attribute.getId() + ", " + attribute.getKey());
}
System.out.println("[OptimizelyConfig] audiences:");
for (OptimizelyAudience audience: config.getAudiences()) {
System.out.println("[OptimizelyAudience] -- (id, name, conditions) = " + audience.getId() + ", " + audience.getName() + ", " + audience.getConditions());
}
System.out.println("[OptimizelyConfig] events:");
for (OptimizelyEvent event: config.getEvents()) {
System.out.println("[OptimizelyEvent] -- (id, key, experimentIds) = " + event.getId() + ", " + event.getKey() + ", " + Arrays.toString(event.getExperimentIds().toArray()));
}
// all features
for (String flagKey: config.getFeaturesMap().keySet()) {
OptimizelyFeature flag = config.getFeaturesMap().get(flagKey);
for (OptimizelyExperiment experiment: flag.getExperimentRules()) {
System.out.println("[OptimizelyExperiment] -- Experiment Rule Key: " + experiment.getKey());
System.out.println("[OptimizelyExperiment] -- Experiment Audiences: " + experiment.getAudiences());
Map<String, OptimizelyVariation> variationsMap = experiment.getVariationsMap();
for (String variationKey: variationsMap.keySet()) {
OptimizelyVariation variation = variationsMap.get(variationKey);
System.out.println("[OptimizelyVariation] -- variation = { key: " + variationKey + ", id: " + variation.getId() + ", featureEnabled: " + variation.getFeatureEnabled() + " }");
// use variation data here...
Map<String, OptimizelyVariable> optimizelyVariableMap = variation.getVariablesMap();
for (String variableKey: optimizelyVariableMap.keySet()) {
OptimizelyVariable variable = optimizelyVariableMap.get(variableKey);
System.out.println("[OptimizelyVariable] -- variable = key: " + variableKey + ", value: " + variable.getValue());
// use variable data here...
}
}
}
for (OptimizelyExperiment delivery: flag.getDeliveryRules()) {
System.out.println("[OptimizelyExperiment] -- Delivery Rule Key: " + delivery.getKey());
System.out.println("[OptimizelyExperiment] -- Delivery Audiences: " + delivery.getAudiences());
}
Map<String, OptimizelyExperiment> experimentsMap = flag.getExperimentsMap();
// feature flag experiments
Set<String> experimentKeys = experimentsMap.keySet();
// use experiments and other feature flag data here...
}
// listen to OPTIMIZELY_CONFIG_UPDATE to get updated data
optimizelyClient.getNotificationCenter().addNotificationHandler(UpdateConfigNotification.class, handler -> {
OptimizelyConfig newConfig = optimizelyClient.getOptimizelyConfig();
});
Updated over 2 years ago