[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
This commit is contained in:
Jesse Bounds
2016-03-18 14:09:03 -07:00
parent fb4d9d083d
commit b163e99499
2 changed files with 36 additions and 33 deletions

View File

@@ -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:@{}];
}
}

View File

@@ -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) {
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;
}
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) {
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 {