Skip to content

Commit

Permalink
Fix: Don't report two app open events at initial launch
Browse files Browse the repository at this point in the history
  • Loading branch information
crleona committed Apr 19, 2024
1 parent f780c12 commit 527c963
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
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
21 changes: 10 additions & 11 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 @@ -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;
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

0 comments on commit 527c963

Please sign in to comment.