Skip to content

Commit db736bd

Browse files
feat: add CMAB Example and CMAB Custom Config functions and update datafile JSON structure (#611)
1 parent b7b8435 commit db736bd

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed

DemoSwiftApp/Samples/SamplesForAPI.swift

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,4 +427,97 @@ class SamplesForAPI {
427427

428428
}
429429

430+
// MARK:- CMAB Example
431+
432+
static func CMAB_Basic() {
433+
let SDK_KEY = "Q9LjjQmUn5pDCjKMc1jFC"
434+
let FLAG_KEY = "cmab-flag"
435+
let client = OptimizelyClient(
436+
sdkKey: SDK_KEY
437+
)
438+
439+
if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
440+
Task {
441+
try await client.start()
442+
let user = OptimizelyUserContext(
443+
optimizely: client,
444+
userId: "USER_123",
445+
attributes: ["country": "us"]
446+
)
447+
let options: [OptimizelyDecideOption] = [.ignoreCmabCache, .ignoreUserProfileService]
448+
let decision = await user.decideAsync(key: FLAG_KEY, options: options)
449+
print("CMAB decision: \(decision)")
450+
}
451+
} else {
452+
guard let localDatafileUrl = Bundle.main.url(forResource: "demoTestDatafile", withExtension: "json"),
453+
let localDatafile = try? Data(contentsOf: localDatafileUrl)
454+
else {
455+
fatalError("Local datafile cannot be found")
456+
}
457+
458+
try? client.start(datafile: localDatafile)
459+
460+
let user = OptimizelyUserContext(
461+
optimizely: client,
462+
userId: "USER_123",
463+
attributes: ["country": "us"]
464+
)
465+
let options: [OptimizelyDecideOption] = [.ignoreCmabCache, .ignoreUserProfileService]
466+
user.decideAsync(key: FLAG_KEY, options: options, completion: { decision in
467+
print("CMAB decision: \(decision)")
468+
})
469+
}
470+
471+
}
472+
473+
static func CMAB_Custom_Config() {
474+
let SDK_KEY = "Q9LjjQmUn5pDCjKMc1jFC"
475+
let FLAG_KEY = "cmab-flag"
476+
477+
var cmabConfig = CmabConfig(
478+
cacheSize: 10,
479+
cacheTimeoutInSecs: 120,
480+
predictionEndpoint: "https://prediction.cmab.optimizely.com/predict/%@"
481+
)
482+
483+
// Initialize client with custom cmab config
484+
let client = OptimizelyClient(
485+
sdkKey: SDK_KEY,
486+
cmabConfig: cmabConfig
487+
)
488+
489+
if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
490+
Task {
491+
try await client.start()
492+
let user = OptimizelyUserContext(
493+
optimizely: client,
494+
userId: "USER_123",
495+
attributes: ["country": "us"]
496+
)
497+
let options: [OptimizelyDecideOption] = [.ignoreCmabCache, .ignoreUserProfileService]
498+
let decision = await user.decideAsync(key: FLAG_KEY, options: options)
499+
print("CMAB decision: \(decision)")
500+
}
501+
} else {
502+
guard let localDatafileUrl = Bundle.main.url(forResource: "demoTestDatafile", withExtension: "json"),
503+
let localDatafile = try? Data(contentsOf: localDatafileUrl)
504+
else {
505+
fatalError("Local datafile cannot be found")
506+
}
507+
508+
try? client.start(datafile: localDatafile)
509+
510+
let user = OptimizelyUserContext(
511+
optimizely: client,
512+
userId: "USER_123",
513+
attributes: ["country": "us"]
514+
)
515+
let options: [OptimizelyDecideOption] = [.ignoreCmabCache, .ignoreUserProfileService]
516+
user.decideAsync(key: FLAG_KEY, options: options, completion: { decision in
517+
print("CMAB decision: \(decision)")
518+
})
519+
}
520+
521+
}
522+
430523
}

DemoSwiftApp/SharedResources/Resources/demoTestDatafile.json

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,55 @@
150150

151151
},
152152
"id": "12331371850"
153+
},
154+
{
155+
"id": "9300002796873",
156+
"key": "cmab-rule-1",
157+
"status": "Running",
158+
"layerId": "9300002051159",
159+
"variations": [
160+
{
161+
"id": "1532445",
162+
"key": "on",
163+
"featureEnabled": true,
164+
"variables": []
165+
},
166+
{
167+
"id": "1532444",
168+
"key": "off",
169+
"featureEnabled": false,
170+
"variables": []
171+
}
172+
],
173+
"trafficAllocation": [],
174+
"forcedVariations": {
175+
176+
},
177+
"audienceIds": [
178+
"5716609898119168"
179+
],
180+
"audienceConditions": [
181+
"or",
182+
"5716609898119168"
183+
],
184+
"cmab": {
185+
"attributeIds": [
186+
"6381652968996864"
187+
],
188+
"trafficAllocation": 10000
189+
}
153190
}
154191
],
155192
"audiences": [
156193
{
157194
"id": "12208130097",
158195
"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"safari\"}, {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"chrome\"}]]]",
159196
"name": "safari"
197+
},
198+
{
199+
"id": "5716609898119168",
200+
"conditions": "[\"and\", [\"or\", [\"or\", {\"match\": \"exact\", \"name\": \"country\", \"type\": \"custom_attribute\", \"value\": \"us\"}], [\"or\", {\"match\": \"exact\", \"name\": \"country\", \"type\": \"custom_attribute\", \"value\": \"ru\"}]]]",
201+
"name": "cmab-audience"
160202
}
161203
],
162204
"groups": [
@@ -174,6 +216,10 @@
174216
{
175217
"id": "12126623630",
176218
"key": "geo"
219+
},
220+
{
221+
"id": "6381652968996864",
222+
"key": "country"
177223
}
178224
],
179225
"botFiltering": false,

0 commit comments

Comments
 (0)