@@ -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}
0 commit comments