Skip to content

Commit aee54d6

Browse files
use new notification listeners (#243)
* use new notification listeners * fix TVOS errors and rename notification listener methods that were incorrect
1 parent 2d2147f commit aee54d6

File tree

4 files changed

+52
-72
lines changed

4 files changed

+52
-72
lines changed

OptimizelyDemoApp/AppDelegate.swift

Lines changed: 32 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -59,55 +59,6 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
5959
Mixpanel.initialize(token:"MIXPANEL_TOKEN")
6060
Localytics.autoIntegrate("YOUR-LOCALYTICS-APP-KEY", launchOptions: launchOptions)
6161

62-
let defaultNotificationCenter = NotificationCenter.default
63-
defaultNotificationCenter.addObserver(forName: NSNotification.Name("OptimizelyExperimentActivated"), object: nil, queue: nil) { (note) in
64-
print("Received an activation notification: \n", note)
65-
if let userInfo : Dictionary<String, AnyObject> = note.userInfo as! Dictionary<String, AnyObject>? {
66-
if let experiment = userInfo["experiment"] as! OPTLYExperiment? {
67-
if let variation = userInfo["variation"] as! OPTLYVariation? {
68-
// ---- Amplitude ----
69-
let propertyKey : String! = "[Optimizely] " + experiment.experimentKey
70-
let identify : AMPIdentify = AMPIdentify()
71-
identify.set(propertyKey, value:variation.variationKey as NSObject!)
72-
// Track impression event (optional)
73-
let eventIdentifier : String = "[Optimizely] " + experiment.experimentKey + " - " + variation.variationKey
74-
Amplitude.instance().logEvent(eventIdentifier)
75-
// ---- Google Analytics ----
76-
let tracker : GAITracker? = GAI.sharedInstance().defaultTracker
77-
let action : String = "Experiment - " + experiment.experimentKey
78-
let label : String = "Variation - " + variation.variationKey
79-
// Build and send a non-interaction Event
80-
let builder = GAIDictionaryBuilder.createEvent(withCategory: "Optimizely", action: action, label: label, value: nil).build()
81-
tracker?.send(builder as [NSObject : AnyObject]!)
82-
// ---- Mixpanel ----
83-
let mixpanel : MixpanelInstance = Mixpanel.mainInstance()
84-
mixpanel.registerSuperProperties([propertyKey: variation.variationKey])
85-
mixpanel.people.set(property: propertyKey, to: variation.variationKey)
86-
mixpanel.track(event:eventIdentifier)
87-
}
88-
}
89-
}
90-
}
91-
92-
defaultNotificationCenter.addObserver(forName: NSNotification.Name("OptimizelyEventTracked"), object: nil, queue: nil) { (note) in
93-
print("Received a tracking notification: \n", note)
94-
if let userInfo : Dictionary<String, AnyObject> = note.userInfo as! Dictionary<String, AnyObject>? {
95-
// ---- Localytics ----
96-
let attributes : NSMutableDictionary = [:]
97-
if let userAttributes = userInfo["attributes"] as! Dictionary<String, AnyObject>? {
98-
attributes.addEntries(from: userAttributes)
99-
}
100-
if let userExperimentVariationMapping = userInfo["ExperimentVariationMapping"] as? Dictionary<String, OPTLYVariation>? {
101-
for (key,variation) in userExperimentVariationMapping! {
102-
attributes.setValue(key, forKey:variation.variationKey)
103-
}
104-
}
105-
// Tag custom event with attributes
106-
let event : String = userInfo["eventKey"] as! String
107-
let localyticsEventIdentifier : String = "[Optimizely] " + event
108-
Localytics.tagEvent(localyticsEventIdentifier)
109-
}
110-
}
11162
#endif
11263
// **************************************************
11364
// *********** Optimizely Initialization ************
@@ -137,6 +88,37 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
13788
// ---- 1. Asynchronous Initialization -----
13889
// initialize Optimizely Client from a datafile download
13990
optimizelyManager?.initialize(callback: { [weak self] (error, optimizelyClient) in
91+
#if os(iOS)
92+
optimizelyClient?.optimizely?.notificationCenter?.addActivateNotificationListener({ (experiment, userId, attributes, variation, event) in
93+
// ---- Amplitude ----
94+
let propertyKey : String! = "[Optimizely] " + experiment.experimentKey
95+
let identify : AMPIdentify = AMPIdentify()
96+
identify.set(propertyKey, value:variation.variationKey as NSObject!)
97+
// Track impression event (optional)
98+
let eventIdentifier : String = "[Optimizely] " + experiment.experimentKey + " - " + variation.variationKey
99+
Amplitude.instance().logEvent(eventIdentifier)
100+
// ---- Google Analytics ----
101+
let tracker : GAITracker? = GAI.sharedInstance().defaultTracker
102+
let action : String = "Experiment - " + experiment.experimentKey
103+
let label : String = "Variation - " + variation.variationKey
104+
// Build and send a non-interaction Event
105+
let builder = GAIDictionaryBuilder.createEvent(withCategory: "Optimizely", action: action, label: label, value: nil).build()
106+
tracker?.send(builder as [NSObject : AnyObject]!)
107+
// ---- Mixpanel ----
108+
let mixpanel : MixpanelInstance = Mixpanel.mainInstance()
109+
mixpanel.registerSuperProperties([propertyKey: variation.variationKey])
110+
mixpanel.people.set(property: propertyKey, to: variation.variationKey)
111+
mixpanel.track(event:eventIdentifier)
112+
})
113+
114+
optimizelyClient?.optimizely?.notificationCenter?.addTrackNotificationListener({ (eventKey, userId, attributes, eventTags, event) in
115+
// Tag custom event with attributes
116+
let event : String = eventKey
117+
let localyticsEventIdentifier : String = "[Optimizely] " + event
118+
Localytics.tagEvent(localyticsEventIdentifier)
119+
120+
})
121+
#endif
140122
let variation = optimizelyClient?.activate((self?.experimentKey)!, userId: (self?.userId)!, attributes: (self?.attributes))
141123
self?.setRootViewController(optimizelyClient: optimizelyClient, bucketedVariation:variation)
142124
})
@@ -200,9 +182,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
200182
func applicationWillResignActive(_ application: UIApplication) {
201183
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
202184
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
203-
let defaultNotificationCenter = NotificationCenter.default
204-
defaultNotificationCenter.removeObserver(self, name: NSNotification.Name("OptimizelyExperimentActivated"), object: nil)
205-
defaultNotificationCenter.removeObserver(self, name: NSNotification.Name("OptimizelyEventTracked"), object: nil)
185+
optimizelyClient?.optimizely?.notificationCenter?.clearAllNotificationListeners();
206186
}
207187

208188
func applicationDidEnterBackground(_ application: UIApplication) {

OptimizelySDKCore/OptimizelySDKCore/OPTLYNotificationCenter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,18 +74,18 @@ typedef NSMutableDictionary<NSNumber *, GenericListener > OPTLYNotificationHolde
7474
* @param notificationId the id passed back from add notification.
7575
* @return true if removed otherwise false (if the notification is already removed, it returns false).
7676
*/
77-
- (BOOL)removeNotification:(NSUInteger)notificationId;
77+
- (BOOL)removeNotificationListener:(NSUInteger)notificationId;
7878

7979
/**
8080
* Clear notification listeners by notification type.
8181
* @param type type of OPTLYNotificationType to remove.
8282
*/
83-
- (void)clearNotifications:(OPTLYNotificationType)type;
83+
- (void)clearNotificationListeners:(OPTLYNotificationType)type;
8484

8585
/**
8686
* Clear out all the notification listeners.
8787
*/
88-
- (void)clearAllNotifications;
88+
- (void)clearAllNotificationListeners;
8989

9090
//
9191
/**

OptimizelySDKCore/OptimizelySDKCore/OPTLYNotificationCenter.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ - (NSInteger)addTrackNotificationListener:(TrackListener)trackListener {
6363
return [self addNotification:OPTLYNotificationTypeTrack listener:(GenericListener)trackListener];
6464
}
6565

66-
- (BOOL)removeNotification:(NSUInteger)notificationId {
66+
- (BOOL)removeNotificationListener:(NSUInteger)notificationId {
6767
for (NSNumber *notificationType in _notifications.allKeys) {
6868
OPTLYNotificationHolder *notificationMap = _notifications[notificationType];
6969
if (notificationMap != nil && [notificationMap.allKeys containsObject:@(notificationId)]) {
@@ -74,13 +74,13 @@ - (BOOL)removeNotification:(NSUInteger)notificationId {
7474
return NO;
7575
}
7676

77-
- (void)clearNotifications:(OPTLYNotificationType)type {
77+
- (void)clearNotificationListeners:(OPTLYNotificationType)type {
7878
[_notifications[@(type)] removeAllObjects];
7979
}
8080

81-
- (void)clearAllNotifications {
81+
- (void)clearAllNotificationListeners {
8282
for (NSNumber *notificationType in _notifications.allKeys) {
83-
[self clearNotifications:[notificationType unsignedIntegerValue]];
83+
[self clearNotificationListeners:[notificationType unsignedIntegerValue]];
8484
}
8585
}
8686

OptimizelySDKCore/OptimizelySDKCoreTests/OPTLYNotificationCenterTest.m

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ - (void)tearDown {
8181
[super tearDown];
8282

8383
// clean up all notifications
84-
[_notificationCenter clearAllNotifications];
84+
[_notificationCenter clearAllNotificationListeners];
8585
}
8686

8787
- (void)testAddAndRemoveNotificationListener {
@@ -92,11 +92,11 @@ - (void)testAddAndRemoveNotificationListener {
9292
XCTAssertEqual(1, _notificationCenter.notificationsCount);
9393

9494
// Verify that callback removed successfully.
95-
XCTAssertEqual(YES, [_notificationCenter removeNotification:notificationId]);
95+
XCTAssertEqual(YES, [_notificationCenter removeNotificationListener:notificationId]);
9696
XCTAssertEqual(0, _notificationCenter.notificationsCount);
9797

9898
//Verify return false with invalid ID.
99-
XCTAssertEqual(NO, [_notificationCenter removeNotification:notificationId]);
99+
XCTAssertEqual(NO, [_notificationCenter removeNotificationListener:notificationId]);
100100

101101
// Verify that callback added successfully and return right notification ID.
102102
XCTAssertEqual(_notificationCenter.notificationId, [_notificationCenter addActivateNotificationListener:_activateNotification]);
@@ -124,16 +124,16 @@ - (void)testClearNotifications {
124124
XCTAssertEqual(3, _notificationCenter.notificationsCount);
125125

126126
// Verify that only decision callbacks are removed.
127-
[_notificationCenter clearNotifications:OPTLYNotificationTypeActivate];
127+
[_notificationCenter clearNotificationListeners:OPTLYNotificationTypeActivate];
128128
XCTAssertEqual(1, _notificationCenter.notificationsCount);
129129

130130
// Verify that ClearNotifications does not break on calling twice for same type.
131-
[_notificationCenter clearNotifications:OPTLYNotificationTypeActivate];
132-
[_notificationCenter clearNotifications:OPTLYNotificationTypeActivate];
131+
[_notificationCenter clearNotificationListeners:OPTLYNotificationTypeActivate];
132+
[_notificationCenter clearNotificationListeners:OPTLYNotificationTypeActivate];
133133

134134
// Verify that ClearNotifications does not break after calling ClearAllNotifications.
135-
[_notificationCenter clearAllNotifications];
136-
[_notificationCenter clearNotifications:OPTLYNotificationTypeTrack];
135+
[_notificationCenter clearAllNotificationListeners];
136+
[_notificationCenter clearNotificationListeners:OPTLYNotificationTypeTrack];
137137
}
138138

139139
- (void)testClearAllNotifications {
@@ -149,13 +149,13 @@ - (void)testClearAllNotifications {
149149
XCTAssertEqual(3, _notificationCenter.notificationsCount);
150150

151151
// Verify that ClearAllNotifications remove all the callbacks.
152-
[_notificationCenter clearAllNotifications];
152+
[_notificationCenter clearAllNotificationListeners];
153153
XCTAssertEqual(0, _notificationCenter.notificationsCount);
154154

155155
// Verify that ClearAllNotifications does not break on calling twice or after ClearNotifications.
156-
[_notificationCenter clearNotifications:OPTLYNotificationTypeActivate];
157-
[_notificationCenter clearAllNotifications];
158-
[_notificationCenter clearAllNotifications];
156+
[_notificationCenter clearNotificationListeners:OPTLYNotificationTypeActivate];
157+
[_notificationCenter clearAllNotificationListeners];
158+
[_notificationCenter clearAllNotificationListeners];
159159
}
160160

161161
- (void)testSendNotifications {
@@ -193,7 +193,7 @@ - (void)testSendNotifications {
193193

194194
// Verify that after clearing notifications, SendNotification should not call any notification
195195
// which were previously registered.
196-
[_notificationCenter clearAllNotifications];
196+
[_notificationCenter clearAllNotificationListeners];
197197

198198
[_notificationCenter sendNotifications:OPTLYNotificationTypeActivate args:@[experiment, userId, attributes, variation, event]];
199199

0 commit comments

Comments
 (0)