Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions DemoSwiftApp/Samples/SamplesForAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,97 @@ class SamplesForAPI {

}

// MARK:- CMAB Example

static func CMAB_Basic() {
let SDK_KEY = "Q9LjjQmUn5pDCjKMc1jFC"
let FLAG_KEY = "cmab-flag"
let client = OptimizelyClient(
sdkKey: SDK_KEY
)

if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
Task {
try await client.start()
let user = OptimizelyUserContext(
optimizely: client,
userId: "USER_123",
attributes: ["country": "us"]
)
let options: [OptimizelyDecideOption] = [.ignoreCmabCache, .ignoreUserProfileService]
let decision = await user.decideAsync(key: FLAG_KEY, options: options)
print("CMAB decision: \(decision)")
}
} else {
guard let localDatafileUrl = Bundle.main.url(forResource: "demoTestDatafile", withExtension: "json"),
let localDatafile = try? Data(contentsOf: localDatafileUrl)
else {
fatalError("Local datafile cannot be found")
}

try? client.start(datafile: localDatafile)

let user = OptimizelyUserContext(
optimizely: client,
userId: "USER_123",
attributes: ["country": "us"]
)
let options: [OptimizelyDecideOption] = [.ignoreCmabCache, .ignoreUserProfileService]
user.decideAsync(key: FLAG_KEY, options: options, completion: { decision in
print("CMAB decision: \(decision)")
})
}

}

static func CMAB_Custom_Config() {
let SDK_KEY = "Q9LjjQmUn5pDCjKMc1jFC"
let FLAG_KEY = "cmab-flag"

var cmabConfig = CmabConfig(
cacheSize: 10,
cacheTimeoutInSecs: 120,
predictionEndpoint: "https://prediction.cmab.optimizely.com/predict/%@"
)

// Initialize client with custom cmab config
let client = OptimizelyClient(
sdkKey: SDK_KEY,
cmabConfig: cmabConfig
)

if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) {
Task {
try await client.start()
let user = OptimizelyUserContext(
optimizely: client,
userId: "USER_123",
attributes: ["country": "us"]
)
let options: [OptimizelyDecideOption] = [.ignoreCmabCache, .ignoreUserProfileService]
let decision = await user.decideAsync(key: FLAG_KEY, options: options)
print("CMAB decision: \(decision)")
}
} else {
guard let localDatafileUrl = Bundle.main.url(forResource: "demoTestDatafile", withExtension: "json"),
let localDatafile = try? Data(contentsOf: localDatafileUrl)
else {
fatalError("Local datafile cannot be found")
}

try? client.start(datafile: localDatafile)

let user = OptimizelyUserContext(
optimizely: client,
userId: "USER_123",
attributes: ["country": "us"]
)
let options: [OptimizelyDecideOption] = [.ignoreCmabCache, .ignoreUserProfileService]
user.decideAsync(key: FLAG_KEY, options: options, completion: { decision in
print("CMAB decision: \(decision)")
})
}

}

}
46 changes: 46 additions & 0 deletions DemoSwiftApp/SharedResources/Resources/demoTestDatafile.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,55 @@

},
"id": "12331371850"
},
{
"id": "9300002796873",
"key": "cmab-rule-1",
"status": "Running",
"layerId": "9300002051159",
"variations": [
{
"id": "1532445",
"key": "on",
"featureEnabled": true,
"variables": []
},
{
"id": "1532444",
"key": "off",
"featureEnabled": false,
"variables": []
}
],
"trafficAllocation": [],
"forcedVariations": {

},
"audienceIds": [
"5716609898119168"
],
"audienceConditions": [
"or",
"5716609898119168"
],
"cmab": {
"attributeIds": [
"6381652968996864"
],
"trafficAllocation": 10000
}
}
],
"audiences": [
{
"id": "12208130097",
"conditions": "[\"and\", [\"or\", [\"or\", {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"safari\"}, {\"name\": \"browser_type\", \"type\": \"custom_attribute\", \"value\": \"chrome\"}]]]",
"name": "safari"
},
{
"id": "5716609898119168",
"conditions": "[\"and\", [\"or\", [\"or\", {\"match\": \"exact\", \"name\": \"country\", \"type\": \"custom_attribute\", \"value\": \"us\"}], [\"or\", {\"match\": \"exact\", \"name\": \"country\", \"type\": \"custom_attribute\", \"value\": \"ru\"}]]]",
"name": "cmab-audience"
}
],
"groups": [
Expand All @@ -174,6 +216,10 @@
{
"id": "12126623630",
"key": "geo"
},
{
"id": "6381652968996864",
"key": "country"
}
],
"botFiltering": false,
Expand Down
Loading