88#import " RNAnalytics.h"
99
1010#import < Analytics/SEGAnalytics.h>
11+ #import < React/RCTBridge.h>
1112
1213static NSMutableSet * RNAnalyticsIntegrations = nil ;
1314static NSLock * RNAnalyticsIntegrationsLock = nil ;
@@ -29,9 +30,11 @@ +(void)initialize {
2930
3031RCT_EXPORT_MODULE ()
3132
33+ @synthesize bridge = _bridge;
34+
3235RCT_EXPORT_METHOD (setup:(NSDictionary *)options) {
3336 SEGAnalyticsConfiguration* config = [SEGAnalyticsConfiguration configurationWithWriteKey: options[@" ios" ][@" writeKey" ]];
34-
37+
3538 config.recordScreenViews = [options[@" recordScreenViews" ] boolValue ];
3639 config.trackApplicationLifecycleEvents = [options[@" trackAppLifecycleEvents" ] boolValue ];
3740 config.trackAttributionData = [options[@" trackAttributionData" ] boolValue ];
@@ -44,6 +47,19 @@ +(void)initialize {
4447
4548 [SEGAnalytics debug: [options[@" debug" ] boolValue ]];
4649 [SEGAnalytics setupWithConfiguration: config];
50+
51+ // On iOS we use method swizzling to intercept lifecycle events
52+ // However, React-Native calls our library after applicationDidFinishLaunchingWithOptions: is called
53+ // We fix this by manually calling this method at setup-time
54+ // TODO(fathyb): We should probably implement a dedicated API on the native part
55+ if (config.trackApplicationLifecycleEvents ) {
56+ SEL selector = @selector (_applicationDidFinishLaunchingWithOptions: );
57+
58+ if ([SEGAnalytics.sharedAnalytics respondsToSelector: selector]) {
59+ [SEGAnalytics.sharedAnalytics performSelector: selector
60+ withObject: _bridge.launchOptions];
61+ }
62+ }
4763}
4864
4965#define withContext (context ) @{@" context" : context}
0 commit comments