Skip to content

Commit 50c4b09

Browse files
committed
fix(ios): fix trackAppLifecycleEvents
1 parent 09f0b28 commit 50c4b09

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

packages/core/ios/RNAnalytics/RNAnalytics.m

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import "RNAnalytics.h"
99

1010
#import <Analytics/SEGAnalytics.h>
11+
#import <React/RCTBridge.h>
1112

1213
static NSMutableSet* RNAnalyticsIntegrations = nil;
1314
static NSLock* RNAnalyticsIntegrationsLock = nil;
@@ -29,9 +30,11 @@ +(void)initialize {
2930

3031
RCT_EXPORT_MODULE()
3132

33+
@synthesize bridge = _bridge;
34+
3235
RCT_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

Comments
 (0)