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.
Get OptimizelyConfig
OptimizelyConfig can be accessed from OptimizelyClient (top-level) with this public API call:
public func getOptimizelyConfig() throws -> OptimizelyConfig
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 Sharing the datafile with multiple SDK implementations.
Object model
The following shows the object model for OptimizelyConfig.
public protocol OptimizelyConfig {
var environmentKey: String { get }
var sdkKey: String { get }
var revision: String { get }
// 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.
var experimentsMap: [String: OptimizelyExperiment] { get }
var featuresMap: [String: OptimizelyFeature] { get }
var attributes: [OptimizelyAttribute] { get }
var audiences: [OptimizelyAudience] { get }
var events: [OptimizelyEvent] { get }
}
public protocol OptimizelyExperiment {
var id: String { get }
var key: String { get }
var audiences: String { get }
var variationsMap: [String: OptimizelyVariation] { get }
}
public protocol OptimizelyFeature {
var id: String { get }
var key: String { get }
var experimentRules: [OptimizelyExperiment] { get }
var deliveryRules: [OptimizelyExperiment] { get }
var variablesMap: [String: OptimizelyVariable] { get }
@available(*, deprecated, message: "Use experimentRules and deliveryRules instead")
var experimentsMap: [String: OptimizelyExperiment] { get }
}
public protocol OptimizelyVariation {
var id: String { get }
var key: String { get }
var featureEnabled: Bool? { get }
var variablesMap: [String: OptimizelyVariable] { get }
}
public protocol OptimizelyVariable {
var id: String { get }
var key: String { get }
var type: String { get }
var value: String { get }
}
public protocol OptimizelyAttribute {
var id: String { get }
var key: String { get }
}
public protocol OptimizelyAudience {
var id: String { get }
var name: String { get }
var conditions: String { get }
}
public protocol OptimizelyEvent {
var id: String { get }
var key: String { get }
var experimentIds: [String] { get }
}
Examples
OptimizelyConfig can be accessed from OptimizelyClient (top-level) like this:
let config = try! optimizelyClient.getOptimizelyConfig()
print("[OptimizelyConfig] revision = \(config.revision)")
print("[OptimizelyConfig] sdkKey = \(config.sdkKey)")
print("[OptimizelyConfig] environmentKey = \(config.environmentKey)")
print("[OptimizelyConfig] attributes:")
config.attributes.forEach { attribute in
print("[OptimizelyAttribute] -- (id, key) = (\(attribute.id), \(attribute.key))")
}
print("[OptimizelyConfig] audiences:")
config.audiences.forEach { audience in
print("[OptimizelyAudience] -- (id, name, conditions) = (\(audience.id), \(audience.name), \(audience.conditions))")
}
print("[OptimizelyConfig] events:")
config.events.forEach { event in
print("[OptimizelyEvent] -- (id, key, experimentIds) = (\(event.id), \(event.key), \(event.experimentIds))")
}
// all flags
let flags = config.featuresMap.values
let flagKeys = config.featuresMap.keys
for flagKey in flagKeys {
let flag = config.featuresMap[flagKey]!
let experimentRules = flag.experimentRules
let deliveryRules = flag.deliveryRules
// use experiment rules and delivery rules and other flag data here...
experimentRules.forEach { experiment in
print("[OptimizelyExperiment] - experiment rule-key = \(experiment.key)")
print("[OptimizelyExperiment] - experiment audiences = \(experiment.audiences)")
let variationsMap = experiment.variationsMap
let variationKeys = variationsMap.keys
variationKeys.forEach { varKey in
let variation = variationsMap[varKey]!
print("[OptimizelyVariation] -- variation = { key: \(varKey), id: \(variation.id), featureEnabled: \(String(describing: variation.featureEnabled)) }")
let variablesMap = variationsMap[varKey]!.variablesMap
let variableKeys = variablesMap.keys
variableKeys.forEach { variableKey in
let variable = variablesMap[variableKey]!
print("[OptimizelyVariable] --- variable: \(variableKey), \(variable)")
}
}
}
deliveryRules.forEach { delivery in
print("[OptimizelyExperiment] - delivery rule-key = \(delivery.key)")
print("[OptimizelyExperiment] - delivery audiences = \(delivery.audiences)")
// use delivery rule data here...
}
}
// listen to NotificationType.datafileChange to get updated data
_ = optimizelyClient.notificationCenter?.addDatafileChangeNotificationListener { _ in
if let newOptConfig = try? self.optimizely.getOptimizelyConfig() {
print("[OptimizelyConfig] revision = \(newOptConfig.revision)")
}
}
Updated almost 3 years ago