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