Skip to content

Commit 3138efe

Browse files
committed
Tracking cold restarts
A cold restart is when an app is closed (swipe away) and relaunched within 30 seconds. In this scenario we currently won't call onSession. We want to track how often this occurs to know the impact of calling onSession in this scenario. We don't want to consider backgrounding in this case so we are using the registerUserFinished boolean to remove cases where we have already registered a session. We also only want to track this once per app launch so the trackedColdRestart boolean prevents it from being called multiple times.
1 parent e9c67df commit 3138efe

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,6 +1678,7 @@ + (BOOL)isRegisterUserSuccessful {
16781678
return _registerUserSuccessful || isOnSessionSuccessfulForCurrentState;
16791679
}
16801680

1681+
static BOOL _trackedColdRestart = false;
16811682
+ (BOOL)shouldRegisterNow {
16821683
// return if the user has not granted privacy permissions
16831684
if ([self shouldLogMissingPrivacyConsentErrorWithMethodName:nil])
@@ -1709,11 +1710,14 @@ + (BOOL)shouldRegisterNow {
17091710
// Make sure last time we closed app was more than 30 secs ago
17101711
const int minTimeThreshold = 30;
17111712
NSTimeInterval delta = now - lastTimeClosed;
1712-
if (delta < minTimeThreshold && appId) {
1713-
// https://api.onesignal.com/api/v1/track
1714-
// do track here
1715-
// OS-Usage-Data
1716-
NSLog(@"ECM tracking restart appId: %@", appId);
1713+
if (delta < minTimeThreshold && appId && !_registerUserFinished && !_trackedColdRestart) {
1714+
NSString *osUsageData = [NSString stringWithFormat:@"lib-name=OneSignal-iOS-SDK,lib-version=%@,lib-event=cold_restart", ONESIGNAL_VERSION];
1715+
_trackedColdRestart = true;
1716+
[[OneSignalClient sharedClient] executeRequest:[OSRequestTrackV1 trackUsageData:osUsageData appId:appId] onSuccess:^(NSDictionary *result) {
1717+
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"shouldRegisterNow:trackColdRestart: successfully tracked cold restart"];
1718+
} onFailure:^(NSError *error) {
1719+
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"shouldRegisterNow:trackColdRestart: Failed to track cold restart: %@", error.localizedDescription]];
1720+
}];
17171721
}
17181722
return delta >= minTimeThreshold;
17191723
}

0 commit comments

Comments
 (0)