OptimizelyConfig for the Java SDK
How to get access to project configuration data within the datafile using OptimizelyConfig for the Optimizely Feature Experimentation Java 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.
public OptimizelyConfig 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 multiple SDK instances use the same configuration, such as in a client or server scenario, pass a JSON string of the config (the datafile) between them. To get the datafile, use theOptimizelyConfig object'sgetDatafile 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 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 flagpublic 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 experimentpublic class OptimizelyExperiment { String id; String key; String audiences; Map<String, OptimizelyVariation> variationsMap;}// OptimizelyVariation is an object describing a variationpublic class OptimizelyVariation { String id; String key; Boolean featureEnabled; Map<String, OptimizelyVariable> variablesMap;}// OptimizelyVariable is an object describing a Variablepublic class OptimizelyVariable { String id; String key; String type; String value;}// OptimizelyAttribute is an object describing an Attributepublic class OptimizelyAttribute { String id; String key;}// OptimizelyAudience is an object describing an Audiencepublic class OptimizelyAudience { String id; String name; String conditions;}// OptimizelyEvent is an object describing an Eventpublic class OptimizelyEvent { String id; String key; List<String> experimentIds;}Examples
You can accessOptimizelyConfig from theOptimizelyClient (top-level) using the following code example:
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 featuresfor (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...}NotificationHandler<UpdateConfigNotification> handler = message -> optimizelyClient.getOptimizelyConfig();optimizelyClient.addUpdateConfigNotificationHandler(handler);Updated 17 days ago