17
17
#import " AppDelegate.h"
18
18
#import " VariationViewController.h"
19
19
#import " FailureViewController.h"
20
+ #import " CustomLogger.h"
20
21
21
22
@import Optimizely;
22
23
#if TARGET_OS_IOS
23
- @import Amplitude_iOS;
24
+ @import Amplitude_iOS;
24
25
#endif
25
26
27
+
28
+ static NSString * const kOptimizelySdkKey = @" FCnSegiEkRry9rhVMroit4" ;
26
29
static NSString * const kOptimizelyDatafileName = @" demoTestDatafile" ;
27
30
static NSString * const kOptimizelyExperimentKey = @" background_experiment" ;
28
31
static NSString * const kOptimizelyEventKey = @" sample_conversion" ;
29
- static NSString * const kOptimizelySdkKey = @" AqLkkcss3wRGUbftnKNgh2" ;
30
32
31
33
@interface AppDelegate ()
32
34
@property (nonnull , strong , nonatomic ) NSString *userId;
@@ -36,12 +38,14 @@ @interface AppDelegate ()
36
38
37
39
@implementation AppDelegate
38
40
39
-
40
41
- (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
44
44
45
+ #endif
46
+
47
+ self.userId = [NSString stringWithFormat: @" %d " , arc4random ()];
48
+ self.attributes = @{ @" browser_type" : @" safari" , @" bool_attr" : @(false ) };
45
49
46
50
// initialize SDK in one of these two ways:
47
51
// (1) asynchronous SDK initialization (RECOMMENDED)
@@ -50,34 +54,30 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(
50
54
// (2) synchronous SDK initialization
51
55
// - initialize immediately with the given JSON datafile or its cached copy
52
56
// - no network delay, but the local copy is not guaranteed to be in sync with the server experiment settings
53
-
57
+
54
58
[self initializeOptimizelySDKAsynchronous ];
55
- // [self initializeOptimizelySDKSynchronous];
56
-
57
59
return YES ;
58
60
}
59
61
62
+ // MARK: - Initialization Examples
63
+
60
64
-(void )initializeOptimizelySDKAsynchronous {
61
65
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) {
64
68
if (error == nil ) {
65
69
NSLog (@" Optimizely SDK initialized successfully!" );
66
70
} else {
67
71
NSLog (@" Optimizely SDK initiliazation failed: %@ " , error.localizedDescription );
68
72
self.optimizely = nil ;
69
73
}
70
74
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 ];
76
76
}];
77
77
}
78
78
79
79
-(void )initializeOptimizelySDKSynchronous {
80
- NSString *localDatafilePath = [[NSBundle bundleForClass: self .classForCoder ] pathForResource: kOptimizelyDatafileName ofType: @" json" ];
80
+ NSString *localDatafilePath = [[NSBundle mainBundle ] pathForResource: kOptimizelyDatafileName ofType: @" json" ];
81
81
if (localDatafilePath == nil ) {
82
82
NSAssert (false , @" Local datafile cannot be found" );
83
83
self.optimizely = nil ;
@@ -86,11 +86,8 @@ -(void)initializeOptimizelySDKSynchronous {
86
86
87
87
self.optimizely = [[OptimizelyManager alloc ] initWithSdkKey: kOptimizelySdkKey ];
88
88
89
- // customization example (optional)
90
- // TODO: add cutomization for ObjC
91
-
92
89
NSString *datafileJSON = [NSString stringWithContentsOfFile: localDatafilePath encoding: NSUTF8StringEncoding error: nil ];
93
-
90
+
94
91
if (datafileJSON == nil ) {
95
92
NSLog (@" Invalid JSON format" );
96
93
self.optimizely = nil ;
@@ -105,72 +102,117 @@ -(void)initializeOptimizelySDKSynchronous {
105
102
}
106
103
}
107
104
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 ];
125
106
}
126
107
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);
129
135
130
136
#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];
134
144
#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
+ }
136
154
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];
146
171
} else {
147
- rootViewController = [storyboard instantiateViewControllerWithIdentifier: @" FailureViewController" ];
172
+ NSLog (@" Optimizely SDK activation failed: %@ " , error.localizedDescription );
173
+ [self openFailureView ];
148
174
}
149
-
150
- self.window .rootViewController = rootViewController;
151
175
});
152
176
}
153
177
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
+ }
154
188
189
+ -(void )openFailureView {
190
+ self.window .rootViewController = [self .storyboard instantiateViewControllerWithIdentifier: @" FailureViewController" ];
191
+ }
155
192
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
157
199
}
158
200
201
+ // MARK: - AppDelegate
159
202
160
- - (void )applicationDidEnterBackground : (UIApplication *)application {
203
+ - (void )applicationWillResignActive : (UIApplication *)application {
161
204
}
162
205
206
+ - (void )applicationDidEnterBackground : (UIApplication *)application {
207
+ }
163
208
164
209
- (void )applicationWillEnterForeground : (UIApplication *)application {
165
210
}
166
211
167
-
168
212
- (void )applicationDidBecomeActive : (UIApplication *)application {
169
213
}
170
214
171
-
172
215
- (void )applicationWillTerminate : (UIApplication *)application {
173
216
}
174
217
175
-
176
218
@end
0 commit comments