From b163e9949918c9d495b92765d694c8ed7049b260 Mon Sep 17 00:00:00 2001 From: Jesse Bounds Date: Fri, 18 Mar 2016 14:09:03 -0700 Subject: [iOS] Send turnstile event as much as once per day Previously, the post of the turnstile event was a side effect of the map.load event and would happen when the host app was started from a terminated state at the first initialization of MGLMapView. The turnstile event post was guarded with dispatch_once so that it would only ever be sent once during the lifecycle of the application after it started from the terminated state. This changes that behavior by: - Allowing a new turnstile event to be posted as much as once per day - Only triggering a map.view event when a map is initialized if the map is loaded outside of the 'background' application state - Sending a map.view event whenever a MGLMapView is instantiated and the application did become active or enters foreground iOS turnstile event sending behavior is now: - A map.view event is generated whenever a map is loaded and also when a map is shown again when the host app becomes active and moves into the foreground - Turnstile events are still triggered by mapviews and will be sent as frequently as once per day for a host app that is not terminated and also whenever a host app is terminated and restarted w --- platform/ios/src/MGLMapView.mm | 6 +++- platform/ios/src/MGLMapboxEvents.m | 63 +++++++++++++++++++------------------- 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/platform/ios/src/MGLMapView.mm b/platform/ios/src/MGLMapView.mm index 2def0c54f0..adfb635312 100644 --- a/platform/ios/src/MGLMapView.mm +++ b/platform/ios/src/MGLMapView.mm @@ -439,7 +439,9 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) _pendingLongitude = NAN; _targetCoordinate = kCLLocationCoordinate2DInvalid; - [MGLMapboxEvents pushEvent:MGLEventTypeMapLoad withAttributes:@{}]; + if ([UIApplication sharedApplication].applicationState != UIApplicationStateBackground) { + [MGLMapboxEvents pushEvent:MGLEventTypeMapLoad withAttributes:@{}]; + } } - (void)createGLView @@ -982,6 +984,8 @@ mbgl::Duration MGLDurationInSeconds(NSTimeInterval duration) _displayLink.paused = NO; [self validateLocationServices]; + + [MGLMapboxEvents pushEvent:MGLEventTypeMapLoad withAttributes:@{}]; } } diff --git a/platform/ios/src/MGLMapboxEvents.m b/platform/ios/src/MGLMapboxEvents.m index f5628bc6fb..13540bfd08 100644 --- a/platform/ios/src/MGLMapboxEvents.m +++ b/platform/ios/src/MGLMapboxEvents.m @@ -122,7 +122,8 @@ const NSTimeInterval MGLFlushInterval = 180; @property (nonatomic) dispatch_queue_t debugLogSerialQueue; @property (nonatomic) MGLLocationManager *locationManager; @property (nonatomic) NSTimer *timer; -@property (nonatomic) NSDate *lastInstanceIDRotationDate; +@property (nonatomic) NSDate *instanceIDRotationDate; +@property (nonatomic) NSDate *turnstileSendDate; @end @@ -233,13 +234,13 @@ const NSTimeInterval MGLFlushInterval = 180; } - (NSString *)instanceID { - if (self.lastInstanceIDRotationDate && [[NSDate date] timeIntervalSinceDate:self.lastInstanceIDRotationDate] >= 0) { + if (self.instanceIDRotationDate && [[NSDate date] timeIntervalSinceDate:self.instanceIDRotationDate] >= 0) { _instanceID = nil; } if (!_instanceID) { _instanceID = [[NSUUID UUID] UUIDString]; NSTimeInterval twentyFourHourTimeInterval = 24 * 3600; - self.lastInstanceIDRotationDate = [[NSDate date] dateByAddingTimeInterval:twentyFourHourTimeInterval]; + self.instanceIDRotationDate = [[NSDate date] dateByAddingTimeInterval:twentyFourHourTimeInterval]; } return _instanceID; } @@ -314,38 +315,36 @@ const NSTimeInterval MGLFlushInterval = 180; } - (void)pushTurnstileEvent { - __weak MGLMapboxEvents *weakSelf = self; + if (self.turnstileSendDate && [[NSDate date] timeIntervalSinceDate:self.turnstileSendDate] < 0) { + return; + } - static dispatch_once_t onceToken; - dispatch_once(&onceToken, ^{ - MGLMapboxEvents *strongSelf = weakSelf; - - if (!strongSelf) { - return; - } - - NSString *vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; - if (!vendorID) { - return; - } - - NSDictionary *turnstileEventAttributes = @{MGLEventKeyEvent: MGLEventTypeAppUserTurnstile, - MGLEventKeyCreated: [strongSelf.rfc3339DateFormatter stringFromDate:[NSDate date]], - MGLEventKeyVendorID: vendorID, - MGLEventKeyEnabledTelemetry: @([[strongSelf class] isEnabled])}; - - if ([MGLAccountManager accessToken] == nil) { + NSString *vendorID = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; + if (!vendorID) { + return; + } + + NSDictionary *turnstileEventAttributes = @{MGLEventKeyEvent: MGLEventTypeAppUserTurnstile, + MGLEventKeyCreated: [self.rfc3339DateFormatter stringFromDate:[NSDate date]], + MGLEventKeyVendorID: vendorID, + MGLEventKeyEnabledTelemetry: @([[self class] isEnabled])}; + + if ([MGLAccountManager accessToken] == nil) { + return; + } + + __weak __typeof__(self) weakSelf = self; + [self.apiClient postEvent:turnstileEventAttributes completionHandler:^(NSError * _Nullable error) { + __strong __typeof__(weakSelf) strongSelf = weakSelf; + if (error) { + [strongSelf pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"Network error", + @"error": error}]; return; } - [strongSelf.apiClient postEvent:turnstileEventAttributes completionHandler:^(NSError * _Nullable error) { - if (error) { - [strongSelf pushDebugEvent:MGLEventTypeLocalDebug withAttributes:@{MGLEventKeyLocalDebugDescription: @"Network error", - @"error": error}]; - return; - } - [strongSelf writeEventToLocalDebugLog:turnstileEventAttributes]; - }]; - }); + [strongSelf writeEventToLocalDebugLog:turnstileEventAttributes]; + NSTimeInterval twentyFourHourTimeInterval = 24 * 3600; + strongSelf.turnstileSendDate = [[NSDate date] dateByAddingTimeInterval:twentyFourHourTimeInterval]; + }]; } + (void)pushEvent:(NSString *)event withAttributes:(MGLMapboxEventAttributes *)attributeDictionary { -- cgit v1.2.1