1717#import " AppDelegate.h"
1818#import " VariationViewController.h"
1919#import " FailureViewController.h"
20+ #import " CustomLogger.h"
2021
2122@import Optimizely;
2223#if TARGET_OS_IOS
23- @import Amplitude_iOS;
24+ @import Amplitude_iOS;
2425#endif
2526
27+
28+ static NSString * const kOptimizelySdkKey = @" FCnSegiEkRry9rhVMroit4" ;
2629static NSString * const kOptimizelyDatafileName = @" demoTestDatafile" ;
2730static NSString * const kOptimizelyExperimentKey = @" background_experiment" ;
2831static NSString * const kOptimizelyEventKey = @" sample_conversion" ;
29- static NSString * const kOptimizelySdkKey = @" AqLkkcss3wRGUbftnKNgh2" ;
3032
3133@interface AppDelegate ()
3234@property (nonnull , strong , nonatomic ) NSString *userId;
@@ -36,12 +38,14 @@ @interface AppDelegate ()
3638
3739@implementation AppDelegate
3840
39-
4041- (BOOL )application : (UIApplication *)application didFinishLaunchingWithOptions : (NSDictionary *)launchOptions {
41-
42- self.userId = [NSString stringWithFormat: @" %d " , arc4random ()];
43- self.attributes = @{ @" browser_type" : @" safari" };
42+ // most of the third-party integrations only support iOS, so the sample code is only targeted for iOS builds
43+ #if TARGET_OS_IOS
4444
45+ #endif
46+
47+ self.userId = [NSString stringWithFormat: @" %d " , arc4random ()];
48+ self.attributes = @{ @" browser_type" : @" safari" , @" bool_attr" : @(false ) };
4549
4650 // initialize SDK in one of these two ways:
4751 // (1) asynchronous SDK initialization (RECOMMENDED)
@@ -50,34 +54,30 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
5054 // (2) synchronous SDK initialization
5155 // - initialize immediately with the given JSON datafile or its cached copy
5256 // - no network delay, but the local copy is not guaranteed to be in sync with the server experiment settings
53-
57+
5458 [self initializeOptimizelySDKAsynchronous ];
55- // [self initializeOptimizelySDKSynchronous];
56-
5759 return YES ;
5860}
5961
62+ // MARK: - Initialization Examples
63+
6064-(void )initializeOptimizelySDKAsynchronous {
6165 self.optimizely = [[OptimizelyManager alloc ] initWithSdkKey: kOptimizelySdkKey ];
62-
63- [self .optimizely initializeSDKWithCompletion: ^(NSData * _Nullable data, NSError * _Nullable error) {
66+
67+ [self .optimizely initializeSDKWithCompletion: ^(NSData *data, NSError *error) {
6468 if (error == nil ) {
6569 NSLog (@" Optimizely SDK initialized successfully!" );
6670 } else {
6771 NSLog (@" Optimizely SDK initiliazation failed: %@ " , error.localizedDescription );
6872 self.optimizely = nil ;
6973 }
7074
71- [self .optimizely.notificationCenter addActivateNotificationListenerWithActivateListener: ^(NSDictionary <NSString *,id > * _Nonnull experiment, NSString * _Nonnull userId, NSDictionary <NSString *,id > * _Nullable attributes, NSDictionary <NSString *,id > * _Nonnull variation, NSDictionary <NSString *,id > * _Nonnull event) {
72- NSLog (@" got activate with experiment" );
73- NSLog (@" %@ " , experiment[@" key" ]);
74- }];
75- [self startAppWithExperimentActivated ];
75+ [self startWithRootViewController ];
7676 }];
7777}
7878
7979-(void )initializeOptimizelySDKSynchronous {
80- NSString *localDatafilePath = [[NSBundle bundleForClass: self .classForCoder ] pathForResource: kOptimizelyDatafileName ofType: @" json" ];
80+ NSString *localDatafilePath = [[NSBundle mainBundle ] pathForResource: kOptimizelyDatafileName ofType: @" json" ];
8181 if (localDatafilePath == nil ) {
8282 NSAssert (false , @" Local datafile cannot be found" );
8383 self.optimizely = nil ;
@@ -86,11 +86,8 @@ -(void)initializeOptimizelySDKSynchronous {
8686
8787 self.optimizely = [[OptimizelyManager alloc ] initWithSdkKey: kOptimizelySdkKey ];
8888
89- // customization example (optional)
90- // TODO: add cutomization for ObjC
91-
9289 NSString *datafileJSON = [NSString stringWithContentsOfFile: localDatafilePath encoding: NSUTF8StringEncoding error: nil ];
93-
90+
9491 if (datafileJSON == nil ) {
9592 NSLog (@" Invalid JSON format" );
9693 self.optimizely = nil ;
@@ -105,72 +102,117 @@ -(void)initializeOptimizelySDKSynchronous {
105102 }
106103 }
107104
108- [self startAppWithExperimentActivated ];
109- }
110-
111- -(void )startAppWithExperimentActivated {
112- NSError *error;
113- NSString *variationKey = [self .optimizely activateWithExperimentKey: kOptimizelyExperimentKey
114- userId: self .userId
115- attributes: self .attributes
116- error: &error];
117-
118- if (variationKey == nil ) {
119- NSLog (@" Optimizely SDK activation failed: %@ " , error.localizedDescription );
120- self.optimizely = nil ;
121- }
122-
123-
124- [self setRootViewControllerWithOtimizelyManager: self .optimizely bucketedVariation: variationKey];
105+ [self startWithRootViewController ];
125106}
126107
127- -(void )setRootViewControllerWithOtimizelyManager : (OptimizelyManager*)manager bucketedVariation : (NSString *)variationKey {
128- dispatch_async (dispatch_get_main_queue (), ^{
108+ -(void )initializeOptimizelySDKWithCustomization {
109+ // customization example (optional)
110+
111+ CustomLogger *customLogger = [[CustomLogger alloc ] init ];
112+ // 30 sec interval may be too frequent. This is for demo purpose.
113+ // This should be should be much larger (default = 10 mins).
114+ NSNumber *customDownloadIntervalInSecs = @(30 );
115+
116+ self.optimizely = [[OptimizelyManager alloc ] initWithSdkKey: kOptimizelySdkKey
117+ logger: customLogger
118+ eventDispatcher: nil
119+ userProfileService: nil
120+ periodicDownloadInterval: customDownloadIntervalInSecs
121+ defaultLogLevel: OptimizelyLogLevelInfo];
122+
123+ NSNumber *notifId;
124+ notifId = [self .optimizely.notificationCenter addDecisionNotificationListenerWithDecisionListener: ^(NSString *type,
125+ NSString *userId,
126+ NSDictionary <NSString *,id > *attributes,
127+ NSDictionary <NSString *,id > *decisionInfo) {
128+ NSLog (@" Received decision notification: %@ %@ %@ %@ " , type, userId, attributes, decisionInfo);
129+ }];
130+
131+ notifId = [self .optimizely.notificationCenter addTrackNotificationListenerWithTrackListener: ^(NSString *eventKey,
132+ NSString *userId,
133+ NSDictionary <NSString *,id > *attributes, NSDictionary <NSString *,id > *eventTags, NSDictionary <NSString *,id > *event) {
134+ NSLog (@" Received track notification: %@ %@ %@ %@ %@ " , eventKey, userId, attributes, eventTags, event);
129135
130136#if TARGET_OS_IOS
131- UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @" iOSMain" bundle: nil ];
132- #else
133- UIStoryboard *storyboard = [UIStoryboard storyboardWithName: @" tvOSMain" bundle: nil ];
137+ // Amplitude example
138+ NSString *propertyKey = [NSString stringWithFormat: @" [Optimizely] %@ " , eventKey];
139+ AMPIdentify *identify = [[AMPIdentify alloc ] init ];
140+ [identify set: propertyKey value: userId];
141+ // Track event (optional)
142+ NSString *eventIdentifier = [NSString stringWithFormat: @" [Optimizely] %@ - %@ " , eventKey, userId];
143+ [Amplitude.instance logEvent: eventIdentifier];
134144#endif
135- UIViewController *rootViewController;
145+ }];
146+
147+ [self .optimizely initializeSDKWithCompletion: ^(NSData *data, NSError *error) {
148+ if (error == nil ) {
149+ NSLog (@" Optimizely SDK initialized successfully!" );
150+ } else {
151+ NSLog (@" Optimizely SDK initiliazation failed: %@ " , error.localizedDescription );
152+ self.optimizely = nil ;
153+ }
136154
137- if ((manager != nil ) && (variationKey != nil )) {
138- VariationViewController *vc = [storyboard instantiateViewControllerWithIdentifier: @" VariationViewController" ];
139-
140- vc.eventKey = kOptimizelyEventKey ;
141- vc.optimizely = manager;
142- vc.userId = self.userId ;
143- vc.variationKey = variationKey;
144-
145- rootViewController = vc;
155+ [self startWithRootViewController ];
156+ }];
157+ }
158+
159+ // MARK: - ViewControl
160+
161+ -(void )startWithRootViewController {
162+ dispatch_async (dispatch_get_main_queue (), ^{
163+ NSError *error;
164+ NSString *variationKey = [self .optimizely activateWithExperimentKey: kOptimizelyExperimentKey
165+ userId: self .userId
166+ attributes: self .attributes
167+ error: &error];
168+
169+ if (variationKey != nil ) {
170+ [self openVariationViewWithVariationKey: variationKey];
146171 } else {
147- rootViewController = [storyboard instantiateViewControllerWithIdentifier: @" FailureViewController" ];
172+ NSLog (@" Optimizely SDK activation failed: %@ " , error.localizedDescription );
173+ [self openFailureView ];
148174 }
149-
150- self.window .rootViewController = rootViewController;
151175 });
152176}
153177
178+ -(void )openVariationViewWithVariationKey : (nullable NSString *)variationKey {
179+ VariationViewController *variationViewController = [self .storyboard instantiateViewControllerWithIdentifier: @" VariationViewController" ];
180+
181+ variationViewController.optimizely = self.optimizely ;
182+ variationViewController.userId = self.userId ;
183+ variationViewController.variationKey = variationKey;
184+ variationViewController.eventKey = kOptimizelyEventKey ;
185+
186+ self.window .rootViewController = variationViewController;
187+ }
154188
189+ -(void )openFailureView {
190+ self.window .rootViewController = [self .storyboard instantiateViewControllerWithIdentifier: @" FailureViewController" ];
191+ }
155192
156- - (void )applicationWillResignActive : (UIApplication *)application {
193+ -(UIStoryboard*)storyboard {
194+ #if TARGET_OS_IOS
195+ return [UIStoryboard storyboardWithName: @" iOSMain" bundle: nil ];
196+ #else
197+ return [UIStoryboard storyboardWithName: @" tvOSMain" bundle: nil ];
198+ #endif
157199}
158200
201+ // MARK: - AppDelegate
159202
160- - (void )applicationDidEnterBackground : (UIApplication *)application {
203+ - (void )applicationWillResignActive : (UIApplication *)application {
161204}
162205
206+ - (void )applicationDidEnterBackground : (UIApplication *)application {
207+ }
163208
164209- (void )applicationWillEnterForeground : (UIApplication *)application {
165210}
166211
167-
168212- (void )applicationDidBecomeActive : (UIApplication *)application {
169213}
170214
171-
172215- (void )applicationWillTerminate : (UIApplication *)application {
173216}
174217
175-
176218@end
0 commit comments