Skip to content

Commit f5c58fb

Browse files
committed
only track 1 out of 100 cold restarts
1 parent 20902ee commit f5c58fb

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,14 +1710,24 @@ + (BOOL)shouldRegisterNow {
17101710
// Make sure last time we closed app was more than 30 secs ago
17111711
const int minTimeThreshold = 30;
17121712
NSTimeInterval delta = now - lastTimeClosed;
1713+
1714+
// Tracking cold starts within 30 seconds of last close.
1715+
// Depending on the results of our tracking we will change this case
1716+
// from a tracking request to return true
17131717
if (delta < minTimeThreshold && appId && !_registerUserFinished && !_trackedColdRestart) {
1714-
NSString *osUsageData = [NSString stringWithFormat:@"lib-name=OneSignal-iOS-SDK,lib-version=%@,lib-event=cold_restart", ONESIGNAL_VERSION];
1718+
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"shouldRegisterNow:coldRestart"];
1719+
// Set to true even if it doesn't pass the sample check
17151720
_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-
}];
1721+
// Sample /track calls to avoid hitting our endpoint too hard
1722+
int randomSample = arc4random_uniform(100);
1723+
if (randomSample == 99) {
1724+
NSString *osUsageData = [NSString stringWithFormat:@"lib-name=OneSignal-iOS-SDK,lib-version=%@,lib-event=cold_restart", ONESIGNAL_VERSION];
1725+
[[OneSignalClient sharedClient] executeRequest:[OSRequestTrackV1 trackUsageData:osUsageData appId:appId] onSuccess:^(NSDictionary *result) {
1726+
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"shouldRegisterNow:trackColdRestart: successfully tracked cold restart"];
1727+
} onFailure:^(NSError *error) {
1728+
[OneSignal onesignal_Log:ONE_S_LL_ERROR message:[NSString stringWithFormat:@"shouldRegisterNow:trackColdRestart: Failed to track cold restart: %@", error.localizedDescription]];
1729+
}];
1730+
}
17211731
}
17221732
return delta >= minTimeThreshold;
17231733
}

0 commit comments

Comments
 (0)