Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Don't report two app open events at initial launch #494

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions Sources/Amplitude/Amplitude.m
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ - (instancetype)initWithInstanceName:(NSString *)instanceName {
[[[AnalyticsConnector getInstance:self.instanceName] eventBridge] setEventReceiver:^(AnalyticsEvent * _Nonnull event) {
[self logEvent:[event eventType] withEventProperties:[event eventProperties] withApiProperties:nil withUserProperties:[event userProperties] withGroups:nil withGroupProperties:nil withTimestamp:nil outOfSession:false];
}];

self.defaultTracking = [[AMPDefaultTrackingOptions alloc] init];

_initializerQueue = [[NSOperationQueue alloc] init];
Expand Down Expand Up @@ -447,11 +447,6 @@ - (void)handleAppStateUpdates:(NSNotification *)notification {
kAMPEventPropPreviousVersion: previousVersion ?: @"",
}];
}
[self logEvent:kAMPApplicationOpened withEventProperties:@{
kAMPEventPropBuild: currentBuild ?: @"",
kAMPEventPropVersion: currentVersion ?: @"",
kAMPEventPropFromBackground: @NO,
}];

// persist the build/version when changed
if (currentBuild ? ![currentBuild isEqualToString:previousBuild] : (previousBuild != nil)) {
Expand All @@ -461,12 +456,25 @@ - (void)handleAppStateUpdates:(NSNotification *)notification {
[_dbHelper insertOrReplaceKeyValue:APP_VERSION value:currentVersion];
}
} else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
BOOL fromBackground = NO;
UIApplication *sharedApplication = [AMPUtils getSharedApplication];
if (sharedApplication) {
switch (sharedApplication.applicationState) {
case UIApplicationStateActive:
case UIApplicationStateInactive:
fromBackground = NO;
break;
case UIApplicationStateBackground:
fromBackground = YES;
break;
}
}
NSString *currentBuild = [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];
NSString *currentVersion = [[NSBundle mainBundle] infoDictionary][@"CFBundleShortVersionString"];
[self logEvent:kAMPApplicationOpened withEventProperties:@{
kAMPEventPropBuild: currentBuild ?: @"",
kAMPEventPropVersion: currentVersion ?: @"",
kAMPEventPropFromBackground: @YES,
kAMPEventPropFromBackground: @(fromBackground),
}];
} else if ([notification.name isEqualToString:UIApplicationDidEnterBackgroundNotification]) {
[self logEvent:kAMPApplicationBackgrounded];
Expand Down Expand Up @@ -537,7 +545,7 @@ - (void)initializeApiKey:(NSString *)apiKey
if (self.initCompletionBlock != nil) {
self.initCompletionBlock();
}

#if !TARGET_OS_OSX && !TARGET_OS_WATCH
// Unlike other default events options that can be evaluated later, screenViews has to be evaluated during the actual initialization
if (self.defaultTracking.screenViews) {
Expand Down
23 changes: 11 additions & 12 deletions Tests/AmplitudeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -1592,12 +1592,8 @@ - (void)testObserveDidFinishLaunchingNotification {

[client flushQueue];

NSDictionary *event1 = [client getLastEventFromInstanceName:instanceName fromEnd: 1];
NSDictionary *event1 = [client getLastEventFromInstanceName:instanceName fromEnd: 0];
XCTAssertEqualObjects([event1 objectForKey:@"event_type"], kAMPApplicationInstalled);

NSDictionary *event2 = [client getLastEventFromInstanceName:instanceName fromEnd: 0];
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], kAMPApplicationOpened);
XCTAssertEqualObjects([[event2 objectForKey:@"event_properties"] objectForKey:kAMPEventPropFromBackground], @NO);
}

- (void)testObserveDidFinishLaunchingNotificationWithPreviousBuild {
Expand All @@ -1621,19 +1617,15 @@ - (void)testObserveDidFinishLaunchingNotificationWithPreviousBuild {
[client flushQueue];

// This check is not ideal, to avoid flaky test, we only check the first event prefix.
NSDictionary *event1 = [client getLastEventFromInstanceName:instanceName fromEnd: 1];
NSDictionary *event1 = [client getLastEventFromInstanceName:instanceName fromEnd: 0];
XCTAssertTrue([[event1 objectForKey:@"event_type"] hasPrefix:@"[Amplitude] Application"]);

NSDictionary *event2 = [client getLastEventFromInstanceName:instanceName fromEnd: 0];
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], kAMPApplicationOpened);
XCTAssertEqualObjects([[event2 objectForKey:@"event_properties"] objectForKey:kAMPEventPropFromBackground], @NO);
}

- (void)testObserveWillEnterForegroundNotification {
id mockApplication = [OCMockObject niceMockForClass:[UIApplication class]];
[[[mockApplication stub] andReturn:mockApplication] sharedApplication];
OCMStub([mockApplication applicationState]).andReturn(UIApplicationStateInactive);
OCMExpect([mockApplication applicationState]).andReturn(UIApplicationStateInactive);

NSString *instanceName = @"default_tracking_ObserveWillEnterForegroundNotification";
Amplitude *client = [Amplitude instanceWithName:instanceName];
client.defaultTracking.appLifecycles = YES;
Expand All @@ -1643,8 +1635,15 @@ - (void)testObserveWillEnterForegroundNotification {
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:UIApplicationWillEnterForegroundNotification object:app];

OCMExpect([mockApplication applicationState]).andReturn(UIApplicationStateBackground);
[center postNotificationName:UIApplicationWillEnterForegroundNotification object:app];

[client flushQueue];

NSDictionary *event1 = [client getLastEventFromInstanceName:instanceName fromEnd: 1];
XCTAssertEqualObjects([event1 objectForKey:@"event_type"], kAMPApplicationOpened);
XCTAssertEqualObjects([[event1 objectForKey:@"event_properties"] objectForKey:kAMPEventPropFromBackground], @NO);

NSDictionary *event2 = [client getLastEventFromInstanceName:instanceName fromEnd: 0];
XCTAssertEqualObjects([event2 objectForKey:@"event_type"], kAMPApplicationOpened);
XCTAssertEqualObjects([[event2 objectForKey:@"event_properties"] objectForKey:kAMPEventPropFromBackground], @YES);
Expand Down
Loading