Skip to content

Commit 3850038

Browse files
authored
fix samples for optimizely-config-v2 (#428)
1 parent 967546e commit 3850038

File tree

3 files changed

+58
-80
lines changed

3 files changed

+58
-80
lines changed

DemoSwiftApp/AppDelegate.swift

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ import Optimizely
2020
@UIApplicationMain
2121
class AppDelegate: UIResponder, UIApplicationDelegate {
2222
let logLevel = OptimizelyLogLevel.debug
23-
23+
2424
let sdkKey = "FCnSegiEkRry9rhVMroit4"
2525
let datafileName = "demoTestDatafile"
2626
let featureKey = "decide_demo"
2727
let experimentKey = "background_experiment_decide"
2828
let eventKey = "sample_conversion"
29-
29+
3030
let userId = String(Int.random(in: 0..<100000))
3131
let attributes: [String: Any] = ["location": "NY",
3232
"bool_attr": false,
3333
"semanticVersioning": "1.2"]
34-
34+
3535
var window: UIWindow?
3636
var optimizely: OptimizelyClient!
3737
var user: OptimizelyUserContext!
@@ -43,9 +43,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
4343
return UIStoryboard(name: "tvOSMain", bundle: nil)
4444
#endif
4545
}
46-
46+
4747
func applicationDidFinishLaunching(_ application: UIApplication) {
48-
48+
4949
// initialize SDK in one of these two ways:
5050
// (1) asynchronous SDK initialization (RECOMMENDED)
5151
// - fetch a JSON datafile from the server
@@ -56,14 +56,14 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5656

5757
initializeOptimizelySDKWithCustomization()
5858
}
59-
59+
6060
// MARK: - Initialization Examples
61-
61+
6262
func initializeOptimizelySDKAsynchronous() {
6363
optimizely = OptimizelyClient(sdkKey: sdkKey, defaultLogLevel: logLevel)
64-
64+
6565
addNotificationListeners()
66-
66+
6767
optimizely.start { result in
6868
switch result {
6969
case .failure(let error):
@@ -73,20 +73,20 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
7373
@unknown default:
7474
print("Optimizely SDK initiliazation failed with unknown result")
7575
}
76-
76+
7777
self.startWithRootViewController()
7878
}
7979
}
80-
80+
8181
func initializeOptimizelySDKSynchronous() {
8282
guard let localDatafilePath = Bundle.main.path(forResource: datafileName, ofType: "json") else {
8383
fatalError("Local datafile cannot be found")
8484
}
85-
85+
8686
optimizely = OptimizelyClient(sdkKey: sdkKey, defaultLogLevel: logLevel)
8787

8888
addNotificationListeners()
89-
89+
9090
do {
9191
let datafileJSON = try String(contentsOfFile: localDatafilePath, encoding: .utf8)
9292
try optimizely.start(datafile: datafileJSON)
@@ -95,29 +95,29 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
9595
} catch {
9696
print("Optimizely SDK initiliazation failed: \(error)")
9797
}
98-
98+
9999
startWithRootViewController()
100100
}
101-
101+
102102
func initializeOptimizelySDKWithCustomization() {
103103
// customization example (optional)
104-
104+
105105
// You can enable background datafile polling by setting periodicDownloadInterval (polling is disabled by default)
106106
// 60 sec interval may be too frequent. This is for demo purpose. (You can set this to nil to use the recommended value of 600 secs).
107107
let downloadIntervalInSecs: Int? = 60
108-
108+
109109
// You can turn off event batching with 0 timerInterval (this means that events are sent out immediately to the server instead of saving in the local queue for batching)
110110
let eventDispatcher = DefaultEventDispatcher(timerInterval: 0)
111-
111+
112112
// customize logger
113113
let customLogger = CustomLogger()
114114

115115
optimizely = OptimizelyClient(sdkKey: sdkKey,
116-
logger: customLogger,
117-
eventDispatcher: eventDispatcher,
118-
periodicDownloadInterval: downloadIntervalInSecs,
119-
defaultLogLevel: logLevel)
120-
116+
logger: customLogger,
117+
eventDispatcher: eventDispatcher,
118+
periodicDownloadInterval: downloadIntervalInSecs,
119+
defaultLogLevel: logLevel)
120+
121121
addNotificationListeners()
122122

123123
// initialize SDK
@@ -141,15 +141,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
141141
func addNotificationListeners() {
142142
// notification listeners
143143
let notificationCenter = optimizely.notificationCenter!
144-
144+
145145
_ = notificationCenter.addDecisionNotificationListener(decisionListener: { (type, userId, attributes, decisionInfo) in
146146
print("Received decision notification: \(type) \(userId) \(String(describing: attributes)) \(decisionInfo)")
147147
})
148148

149149
_ = notificationCenter.addTrackNotificationListener(trackListener: { (eventKey, userId, attributes, eventTags, event) in
150150
print("Received track notification: \(eventKey) \(userId) \(String(describing: attributes)) \(String(describing: eventTags)) \(event)")
151151
})
152-
152+
153153
_ = notificationCenter.addDatafileChangeNotificationListener(datafileListener: { _ in
154154
DispatchQueue.main.async {
155155
#if os(iOS)
@@ -161,7 +161,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
161161
#else
162162
print("Datafile changed")
163163
#endif
164-
164+
165165
if let controller = self.window?.rootViewController as? VariationViewController {
166166
let decision = self.user.decide(key: "show_coupon")
167167
controller.showCoupon = decision.enabled
@@ -177,9 +177,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
177177
print("Received logEvent notification: \(url) \(event)")
178178
})
179179
}
180-
180+
181181
// MARK: - ViewControl
182-
182+
183183
func startWithRootViewController() {
184184
DispatchQueue.main.async {
185185
self.user = self.optimizely.createUserContext(userId: self.userId,
@@ -194,49 +194,49 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
194194
}
195195
}
196196
}
197-
197+
198198
func openVariationView(variationKey: String?) {
199199
if let variationViewController = storyboard.instantiateViewController(withIdentifier: "VariationViewController")
200200
as? VariationViewController {
201-
201+
202202
let decision = self.user.decide(key: "show_coupon")
203203
variationViewController.showCoupon = decision.enabled
204-
204+
205205
variationViewController.optimizely = optimizely
206206
variationViewController.userId = userId
207207
variationViewController.variationKey = variationKey
208208
variationViewController.eventKey = eventKey
209-
209+
210210
window?.rootViewController = variationViewController
211211
}
212212
}
213-
213+
214214
func openFailureView() {
215215
window?.rootViewController = storyboard.instantiateViewController(withIdentifier: "FailureViewController")
216216
}
217-
217+
218218
// MARK: - AppDelegate
219-
219+
220220
func applicationWillResignActive(_ application: UIApplication) {
221221
}
222-
222+
223223
func applicationDidEnterBackground(_ application: UIApplication) {
224224
}
225-
225+
226226
func applicationWillEnterForeground(_ application: UIApplication) {
227227
}
228-
228+
229229
func applicationDidBecomeActive(_ application: UIApplication) {
230230
}
231-
231+
232232
func applicationWillTerminate(_ application: UIApplication) {
233233
}
234-
234+
235235
func application(_ application: UIApplication,
236236
performFetchWithCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
237-
237+
238238
// add background fetch task here
239-
239+
240240
completionHandler(.newData)
241241
}
242242
}

DemoSwiftApp/Samples/SamplesForAPI.swift

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -158,40 +158,10 @@ class SamplesForAPI {
158158
print("[OptimizelyConfig] -- (id, key, experimentIds) = (\(event.id), \(event.key), \(event.experimentIds))")
159159
}
160160

161-
//let experiments = optConfig.experimentsMap.values
162-
let experimentKeys = optConfig.experimentsMap.keys
163-
print("[OptimizelyConfig] all experiment keys = \(experimentKeys)")
164-
165161
//let features = optConfig.featureFlagsMap.values
166162
let featureKeys = optConfig.featuresMap.keys
167163
print("[OptimizelyConfig] all feature keys = \(featureKeys)")
168164

169-
// enumerate all experiments (variations, and associated variables)
170-
171-
experimentKeys.forEach { expKey in
172-
print("[OptimizelyConfig] experimentKey = \(expKey)")
173-
174-
let experiment = optConfig.experimentsMap[expKey]!
175-
176-
let variationsMap = experiment.variationsMap
177-
let variationKeys = variationsMap.keys
178-
179-
variationKeys.forEach { varKey in
180-
print("[OptimizelyConfig] - variationKey = \(varKey)")
181-
182-
let variation = variationsMap[varKey]!
183-
184-
let variablesMap = variation.variablesMap
185-
let variableKeys = variablesMap.keys
186-
187-
variableKeys.forEach { variableKey in
188-
let variable = variablesMap[variableKey]!
189-
190-
print("[OptimizelyConfig] -- variable: \(variableKey), \(variable)")
191-
}
192-
}
193-
}
194-
195165
// enumerate all features (experiments, variations, and assocated variables)
196166

197167
featureKeys.forEach { featKey in
@@ -201,13 +171,14 @@ class SamplesForAPI {
201171

202172
let feature = optConfig.featuresMap[featKey]!
203173

204-
let experimentsMap = feature.experimentsMap
205-
let experimentKeys = experimentsMap.keys
174+
let experimentRules = feature.experimentRules
175+
let deliveryRules = feature.deliveryRules
206176

207-
experimentKeys.forEach { expKey in
208-
print("[OptimizelyConfig] - experimentKey = \(expKey)")
209-
210-
let variationsMap = experimentsMap[expKey]!.variationsMap
177+
experimentRules.forEach { experiment in
178+
print("[OptimizelyConfig] - experiment rule-key = \(experiment.key)")
179+
print("[OptimizelyConfig] - experiment audiences = \(experiment.audiences)")
180+
181+
let variationsMap = experiment.variationsMap
211182
let variationKeys = variationsMap.keys
212183

213184
variationKeys.forEach { varKey in
@@ -225,6 +196,13 @@ class SamplesForAPI {
225196
}
226197
}
227198

199+
deliveryRules.forEach { delivery in
200+
print("[OptimizelyConfig] - delivery rule-key = \(delivery.key)")
201+
print("[OptimizelyConfig] - delivery audiences = \(delivery.audiences)")
202+
203+
// use delivery rule data here...
204+
}
205+
228206
// enumerate all feature-variables
229207

230208
let variablesMap = optConfig.featuresMap[featKey]!.variablesMap

Sources/Optimizely/OptimizelyConfig.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import Foundation
2020

2121
public protocol OptimizelyConfig {
2222
var environmentKey: String { get }
23-
var revision: String { get }
2423
var sdkKey: String { get }
24+
var revision: String { get }
2525
var experimentsMap: [String: OptimizelyExperiment] { get }
2626
var featuresMap: [String: OptimizelyFeature] { get }
2727
var attributes: [OptimizelyAttribute] { get }
@@ -82,8 +82,8 @@ public protocol OptimizelyEvent {
8282

8383
struct OptimizelyConfigImp: OptimizelyConfig {
8484
var environmentKey: String = ""
85-
var revision: String = ""
8685
var sdkKey: String = ""
86+
var revision: String = ""
8787
var experimentsMap: [String: OptimizelyExperiment] = [:]
8888
var featuresMap: [String: OptimizelyFeature] = [:]
8989
var attributes: [OptimizelyAttribute] = []

0 commit comments

Comments
 (0)