diff options
Diffstat (limited to 'platform/ios/src/MGLMapboxEvents.m')
-rw-r--r-- | platform/ios/src/MGLMapboxEvents.m | 32 |
1 files changed, 25 insertions, 7 deletions
diff --git a/platform/ios/src/MGLMapboxEvents.m b/platform/ios/src/MGLMapboxEvents.m index 2dc7fee280..5ddf4e2b57 100644 --- a/platform/ios/src/MGLMapboxEvents.m +++ b/platform/ios/src/MGLMapboxEvents.m @@ -123,7 +123,7 @@ const NSTimeInterval MGLFlushInterval = 180; @property (nonatomic) MGLLocationManager *locationManager; @property (nonatomic) NSTimer *timer; @property (nonatomic) NSDate *instanceIDRotationDate; -@property (nonatomic) NSDate *turnstileSendDate; +@property (nonatomic) NSDate *nextTurnstileSendDate; @end @@ -136,7 +136,7 @@ const NSTimeInterval MGLFlushInterval = 180; NSBundle *bundle = [NSBundle mainBundle]; NSNumber *accountTypeNumber = [bundle objectForInfoDictionaryKey:@"MGLMapboxAccountType"]; [[NSUserDefaults standardUserDefaults] registerDefaults:@{ - @"MGLMapboxAccountType": accountTypeNumber ? accountTypeNumber : @0, + @"MGLMapboxAccountType": accountTypeNumber ?: @0, @"MGLMapboxMetricsEnabled": @YES, @"MGLMapboxMetricsDebugLoggingEnabled": @NO, }]; @@ -144,8 +144,12 @@ const NSTimeInterval MGLFlushInterval = 180; } + (BOOL)isEnabled { +#if TARGET_OS_SIMULATOR + return NO; +#else return ([[NSUserDefaults standardUserDefaults] boolForKey:@"MGLMapboxMetricsEnabled"] && [[NSUserDefaults standardUserDefaults] integerForKey:@"MGLMapboxAccountType"] == 0); +#endif } - (BOOL)debugLoggingEnabled { @@ -311,7 +315,7 @@ const NSTimeInterval MGLFlushInterval = 180; } - (void)pushTurnstileEvent { - if (self.turnstileSendDate && [[NSDate date] timeIntervalSinceDate:self.turnstileSendDate] < 0) { + if (self.nextTurnstileSendDate && [[NSDate date] timeIntervalSinceDate:self.nextTurnstileSendDate] < 0) { return; } @@ -338,11 +342,25 @@ const NSTimeInterval MGLFlushInterval = 180; return; } [strongSelf writeEventToLocalDebugLog:turnstileEventAttributes]; - NSTimeInterval twentyFourHourTimeInterval = 24 * 3600; - strongSelf.turnstileSendDate = [[NSDate date] dateByAddingTimeInterval:twentyFourHourTimeInterval]; + [strongSelf updateNextTurnstileSendDate]; }]; } +- (void)updateNextTurnstileSendDate { + // Find the time a day from now (sometime tomorrow) + NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; + NSDateComponents *dayComponent = [[NSDateComponents alloc] init]; + dayComponent.day = 1; + NSDate *sometimeTomorrow = [calendar dateByAddingComponents:dayComponent toDate:[NSDate date] options:0]; + + // Find the start of tomorrow and use that as the next turnstile send date. The effect of this is that + // turnstile events can be sent as much as once per calendar day and always at the start of a session + // when a map load happens. + NSDate *startOfTomorrow = nil; + [calendar rangeOfUnit:NSCalendarUnitDay startDate:&startOfTomorrow interval:nil forDate:sometimeTomorrow]; + self.nextTurnstileSendDate = startOfTomorrow; +} + + (void)pushEvent:(NSString *)event withAttributes:(MGLMapboxEventAttributes *)attributeDictionary { [[MGLMapboxEvents sharedManager] pushEvent:event withAttributes:attributeDictionary]; } @@ -356,7 +374,7 @@ const NSTimeInterval MGLFlushInterval = 180; [self pushTurnstileEvent]; } - if ([self isPaused]) { + if (self.paused) { return; } @@ -446,7 +464,7 @@ const NSTimeInterval MGLFlushInterval = 180; // Called implicitly from public use of +flush. // - (void)postEvents:(NS_ARRAY_OF(MGLMapboxEventAttributes *) *)events { - if ([self isPaused]) { + if (self.paused) { return; } |