From 527c963c49b0f8fe45765f73279dd296b67e413d Mon Sep 17 00:00:00 2001 From: Chris Leonavicius Date: Fri, 19 Apr 2024 16:30:53 -0700 Subject: [PATCH] Fix: Don't report two app open events at initial launch --- Sources/Amplitude/Amplitude.m | 24 ++++++++++++++++-------- Tests/AmplitudeTests.m | 21 ++++++++++----------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/Sources/Amplitude/Amplitude.m b/Sources/Amplitude/Amplitude.m index ddae15d7..a80738c9 100644 --- a/Sources/Amplitude/Amplitude.m +++ b/Sources/Amplitude/Amplitude.m @@ -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]; @@ -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)) { @@ -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]; @@ -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) { diff --git a/Tests/AmplitudeTests.m b/Tests/AmplitudeTests.m index ac06753a..88ea29c9 100644 --- a/Tests/AmplitudeTests.m +++ b/Tests/AmplitudeTests.m @@ -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 { @@ -1623,17 +1619,13 @@ - (void)testObserveDidFinishLaunchingNotificationWithPreviousBuild { // This check is not ideal, to avoid flaky test, we only check the first event prefix. NSDictionary *event1 = [client getLastEventFromInstanceName:instanceName fromEnd: 1]; 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; @@ -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);