From f3d7212137021860b653a6e56fab4677407c68fb Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Tue, 19 May 2015 17:02:54 -0500 Subject: [PATCH 1/7] #1595 - Adding App User count event to be triggered and immediately sent when Access Token is set. --- platform/ios/MGLAccountManager.m | 3 +++ platform/ios/MGLMapboxEvents.h | 1 + platform/ios/MGLMapboxEvents.m | 3 ++- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/platform/ios/MGLAccountManager.m b/platform/ios/MGLAccountManager.m index a49433777..e0999ba29 100644 --- a/platform/ios/MGLAccountManager.m +++ b/platform/ios/MGLAccountManager.m @@ -53,6 +53,9 @@ // Update MGLMapboxEvents // NOTE: This is (likely) the initial setup of MGLMapboxEvents [MGLMapboxEvents sharedManager]; + + // Count App Users + [MGLMapboxEvents pushEvent:MGLEventTypeAppUserCount withAttributes:nil]; } + (NSString *) accessToken { diff --git a/platform/ios/MGLMapboxEvents.h b/platform/ios/MGLMapboxEvents.h index 94aa8be78..174c35514 100644 --- a/platform/ios/MGLMapboxEvents.h +++ b/platform/ios/MGLMapboxEvents.h @@ -1,5 +1,6 @@ #import +extern NSString *const MGLEventTypeAppUserCount; extern NSString *const MGLEventTypeMapLoad; extern NSString *const MGLEventTypeMapTap; extern NSString *const MGLEventTypeMapDragEnd; diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 98bfe21cf..2d41bc240 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -16,6 +16,7 @@ static NSString *const MGLMapboxEventsUserAgent = @"MapboxEventsiOS/1.0"; static NSString *MGLMapboxEventsAPIBase = @"https://api.tiles.mapbox.com"; +NSString *const MGLEventTypeAppUserCount = @"appUserCount"; NSString *const MGLEventTypeMapLoad = @"map.load"; NSString *const MGLEventTypeMapTap = @"map.click"; NSString *const MGLEventTypeMapDragEnd = @"map.dragend"; @@ -481,7 +482,7 @@ const NSTimeInterval MGLFlushInterval = 60; [_eventQueue addObject:finalEvent]; // Has Flush Limit Been Reached? - if (_eventQueue.count >= MGLMaximumEventsPerFlush) { + if (_eventQueue.count >= MGLMaximumEventsPerFlush || [MGLEventTypeAppUserCount isEqualToString:event]) { [strongSelf flush]; } else if (_eventQueue.count == 1) { // If this is first new event on queue start timer, From 58b967605aa882f93d0db220cae29a49444cc842 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 20 May 2015 11:32:57 -0500 Subject: [PATCH 2/7] Implementing suggestions to tighten up MGLAccountManager and rename MGLEventTypeUserCount to MGLEventTypeAppUserTurnstile --- platform/ios/MGLAccountManager.m | 8 +++----- platform/ios/MGLMapboxEvents.h | 2 +- platform/ios/MGLMapboxEvents.m | 5 +++-- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/platform/ios/MGLAccountManager.m b/platform/ios/MGLAccountManager.m index e0999ba29..1a9bbe47a 100644 --- a/platform/ios/MGLAccountManager.m +++ b/platform/ios/MGLAccountManager.m @@ -51,11 +51,9 @@ [[MGLAccountManager sharedManager] setAccessToken:accessToken]; // Update MGLMapboxEvents - // NOTE: This is (likely) the initial setup of MGLMapboxEvents - [MGLMapboxEvents sharedManager]; - - // Count App Users - [MGLMapboxEvents pushEvent:MGLEventTypeAppUserCount withAttributes:nil]; + // NOTE: This is (likely) the initial setup of MGLMapboxEvents (via implicit call to +[MGLMapboxEvents sharedManager]) + // Turn the Mapbox Turnstile to Count App Users + [MGLMapboxEvents pushEvent:MGLEventTypeAppUserTurnstile withAttributes:nil]; } + (NSString *) accessToken { diff --git a/platform/ios/MGLMapboxEvents.h b/platform/ios/MGLMapboxEvents.h index 174c35514..00de64a8b 100644 --- a/platform/ios/MGLMapboxEvents.h +++ b/platform/ios/MGLMapboxEvents.h @@ -1,6 +1,6 @@ #import -extern NSString *const MGLEventTypeAppUserCount; +extern NSString *const MGLEventTypeAppUserTurnstile; extern NSString *const MGLEventTypeMapLoad; extern NSString *const MGLEventTypeMapTap; extern NSString *const MGLEventTypeMapDragEnd; diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 2d41bc240..002e38d9d 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -16,7 +16,7 @@ static NSString *const MGLMapboxEventsUserAgent = @"MapboxEventsiOS/1.0"; static NSString *MGLMapboxEventsAPIBase = @"https://api.tiles.mapbox.com"; -NSString *const MGLEventTypeAppUserCount = @"appUserCount"; +NSString *const MGLEventTypeAppUserTurnstile = @"appUserTurnstile"; NSString *const MGLEventTypeMapLoad = @"map.load"; NSString *const MGLEventTypeMapTap = @"map.click"; NSString *const MGLEventTypeMapDragEnd = @"map.dragend"; @@ -316,6 +316,7 @@ const NSTimeInterval MGLFlushInterval = 60; [[MGLMapboxEvents sharedManager] validate]; } +// Used to determine if Mapbox Metrics should be collected at any given point in time - (void)validate { MGLAssertIsMainThread(); BOOL enabledInSettings = [[self class] isEnabled]; @@ -482,7 +483,7 @@ const NSTimeInterval MGLFlushInterval = 60; [_eventQueue addObject:finalEvent]; // Has Flush Limit Been Reached? - if (_eventQueue.count >= MGLMaximumEventsPerFlush || [MGLEventTypeAppUserCount isEqualToString:event]) { + if (_eventQueue.count >= MGLMaximumEventsPerFlush || [MGLEventTypeAppUserTurnstile isEqualToString:event]) { [strongSelf flush]; } else if (_eventQueue.count == 1) { // If this is first new event on queue start timer, From a5bd2fa3c66991ff31ba5d4091eb8400f6fc4397 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 20 May 2015 12:31:20 -0500 Subject: [PATCH 3/7] #1595 - Adding exception so that Turnstile event will always be sent in --- platform/ios/MGLMapboxEvents.m | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 002e38d9d..364a05180 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -432,9 +432,25 @@ const NSTimeInterval MGLFlushInterval = 60; __weak MGLMapboxEvents *weakSelf = self; dispatch_async(_serialQueue, ^{ + + if ([MGLEventTypeAppUserTurnstile isEqualToString:event]) { + // Build only IDFV event + NSString *vid = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; + NSDictionary *vevt = @{@"vendorId": vid}; + + // Add to Queue + [_eventQueue addObject:vevt]; + + // Flush + [MGLMapboxEvents flush]; + + return; + } + MGLMapboxEvents *strongSelf = weakSelf; + if ( ! strongSelf) return; - + // Metrics Collection Has Been Paused if (_paused) { return; From 44caf2c7a97bc3b1314cc88dbf2c66ddaf5da2b0 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 20 May 2015 14:18:39 -0500 Subject: [PATCH 4/7] #1595 - Improving stability by moving MGLEventTypeAppUserTurnstile after the strongSelf check --- platform/ios/MGLMapboxEvents.m | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 364a05180..6b15df145 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -433,6 +433,10 @@ const NSTimeInterval MGLFlushInterval = 60; dispatch_async(_serialQueue, ^{ + MGLMapboxEvents *strongSelf = weakSelf; + + if ( ! strongSelf) return; + if ([MGLEventTypeAppUserTurnstile isEqualToString:event]) { // Build only IDFV event NSString *vid = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; @@ -442,15 +446,11 @@ const NSTimeInterval MGLFlushInterval = 60; [_eventQueue addObject:vevt]; // Flush - [MGLMapboxEvents flush]; + [self flush]; return; } - MGLMapboxEvents *strongSelf = weakSelf; - - if ( ! strongSelf) return; - // Metrics Collection Has Been Paused if (_paused) { return; From a29587726cb1cedd5dc95532b731c4d4b6d7932b Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 20 May 2015 14:32:16 -0500 Subject: [PATCH 5/7] #1595 - Moving MGLEventTypeAppUserTurnstile event to end of MGLMapboxEvents init method to ensure it only gets called once --- platform/ios/MGLAccountManager.m | 5 ++--- platform/ios/MGLMapboxEvents.m | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/platform/ios/MGLAccountManager.m b/platform/ios/MGLAccountManager.m index 1a9bbe47a..a49433777 100644 --- a/platform/ios/MGLAccountManager.m +++ b/platform/ios/MGLAccountManager.m @@ -51,9 +51,8 @@ [[MGLAccountManager sharedManager] setAccessToken:accessToken]; // Update MGLMapboxEvents - // NOTE: This is (likely) the initial setup of MGLMapboxEvents (via implicit call to +[MGLMapboxEvents sharedManager]) - // Turn the Mapbox Turnstile to Count App Users - [MGLMapboxEvents pushEvent:MGLEventTypeAppUserTurnstile withAttributes:nil]; + // NOTE: This is (likely) the initial setup of MGLMapboxEvents + [MGLMapboxEvents sharedManager]; } + (NSString *) accessToken { diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index 6b15df145..b6daaab3b 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -278,6 +278,9 @@ const NSTimeInterval MGLFlushInterval = 60; MGLMapboxEvents *strongSelf = weakSelf; [strongSelf validate]; }]; + + // Turn the Mapbox Turnstile to Count App Users + [self pushEvent:MGLEventTypeAppUserTurnstile withAttributes:nil]; } return self; } From cbcc3a3e4912077f78e63ea4e470342f894e1630 Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 20 May 2015 15:05:56 -0500 Subject: [PATCH 6/7] #1595 - Separating pushTurnstileEvent from primary pushEvent infrastructure. Adding event, created, and appBundleId to posted Turnstile Event data model. --- platform/ios/MGLMapboxEvents.m | 45 ++++++++++++++++++++++------------ 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index b6daaab3b..db5949a31 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -280,7 +280,9 @@ const NSTimeInterval MGLFlushInterval = 60; }]; // Turn the Mapbox Turnstile to Count App Users - [self pushEvent:MGLEventTypeAppUserTurnstile withAttributes:nil]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ + [self pushTurnstileEvent]; + }); } return self; } @@ -419,6 +421,31 @@ const NSTimeInterval MGLFlushInterval = 60; } } +- (void) pushTurnstileEvent { + + __weak MGLMapboxEvents *weakSelf = self; + + dispatch_async(_serialQueue, ^{ + + MGLMapboxEvents *strongSelf = weakSelf; + + if ( ! strongSelf) return; + + // Build only IDFV event + NSString *vid = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; + NSDictionary *vevt = @{@"event" : MGLEventTypeAppUserTurnstile, + @"created" : [strongSelf.rfc3339DateFormatter stringFromDate:[NSDate date]], + @"appBundleId" : strongSelf.appBundleId, + @"vendorId": vid}; + + // Add to Queue + [_eventQueue addObject:vevt]; + + // Flush + [self flush]; + }); +} + // Can be called from any thread. Can be called rapidly from // the UI thread, so performance is paramount. // @@ -440,20 +467,6 @@ const NSTimeInterval MGLFlushInterval = 60; if ( ! strongSelf) return; - if ([MGLEventTypeAppUserTurnstile isEqualToString:event]) { - // Build only IDFV event - NSString *vid = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; - NSDictionary *vevt = @{@"vendorId": vid}; - - // Add to Queue - [_eventQueue addObject:vevt]; - - // Flush - [self flush]; - - return; - } - // Metrics Collection Has Been Paused if (_paused) { return; @@ -502,7 +515,7 @@ const NSTimeInterval MGLFlushInterval = 60; [_eventQueue addObject:finalEvent]; // Has Flush Limit Been Reached? - if (_eventQueue.count >= MGLMaximumEventsPerFlush || [MGLEventTypeAppUserTurnstile isEqualToString:event]) { + if (_eventQueue.count >= MGLMaximumEventsPerFlush) { [strongSelf flush]; } else if (_eventQueue.count == 1) { // If this is first new event on queue start timer, From a47438d42adddad9838048cb4c5996a3eadb1c6b Mon Sep 17 00:00:00 2001 From: Brad Leege Date: Wed, 20 May 2015 15:12:48 -0500 Subject: [PATCH 7/7] #1595 - Changing to strongSelf --- platform/ios/MGLMapboxEvents.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/ios/MGLMapboxEvents.m b/platform/ios/MGLMapboxEvents.m index db5949a31..011040097 100644 --- a/platform/ios/MGLMapboxEvents.m +++ b/platform/ios/MGLMapboxEvents.m @@ -442,7 +442,7 @@ const NSTimeInterval MGLFlushInterval = 60; [_eventQueue addObject:vevt]; // Flush - [self flush]; + [strongSelf flush]; }); }