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 Swift SDK

How to get access to project configuration data within the datafile using OptimizelyConfig for the Optimizely Feature Experimentation Swift 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.

public func getOptimizelyConfig() throws -> OptimizelyConfig

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

📘

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 share the same datafile between multiple SDK instances, such as 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:

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

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

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)")
   }
}