Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Counting App Users For Billing #1603

Merged
merged 7 commits into from
May 20, 2015
1 change: 1 addition & 0 deletions platform/ios/MGLMapboxEvents.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#import <Foundation/Foundation.h>

extern NSString *const MGLEventTypeAppUserTurnstile;
extern NSString *const MGLEventTypeMapLoad;
extern NSString *const MGLEventTypeMapTap;
extern NSString *const MGLEventTypeMapDragEnd;
Expand Down
25 changes: 23 additions & 2 deletions platform/ios/MGLMapboxEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
static NSString *const MGLMapboxEventsUserAgent = @"MapboxEventsiOS/1.0";
static NSString *MGLMapboxEventsAPIBase = @"https://api.tiles.mapbox.com";

NSString *const MGLEventTypeAppUserTurnstile = @"appUserTurnstile";
NSString *const MGLEventTypeMapLoad = @"map.load";
NSString *const MGLEventTypeMapTap = @"map.click";
NSString *const MGLEventTypeMapDragEnd = @"map.dragend";
Expand Down Expand Up @@ -277,6 +278,9 @@ - (instancetype) init {
MGLMapboxEvents *strongSelf = weakSelf;
[strongSelf validate];
}];

// Turn the Mapbox Turnstile to Count App Users
[self pushEvent:MGLEventTypeAppUserTurnstile withAttributes:nil];
}
return self;
}
Expand Down Expand Up @@ -315,6 +319,7 @@ + (void)validate {
[[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];
Expand Down Expand Up @@ -430,9 +435,25 @@ - (void) pushEvent:(NSString *)event withAttributes:(NSDictionary *)attributeDic
__weak MGLMapboxEvents *weakSelf = self;

dispatch_async(_serialQueue, ^{

MGLMapboxEvents *strongSelf = weakSelf;

if ( ! strongSelf) return;


if ([MGLEventTypeAppUserTurnstile isEqualToString:event]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking my earlier feedback into account. Now that I look at this, I’m thinking it would be cleaner to have a separate, specialized method called -pushTurnstileEvent just for this chunk of code (and all the weak/strong/dispatch scaffolding above). You’d call that method from within -init. That way there are no special cases and no possibility of somehow accidentally turning the turnstile more than once.

Not critical though: I think this PR looks good overall.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually like that idea quite a bit. It further separates the Turnstile tick from the stock Events API conceptually as well as gets rid of the "special cases" which tbh wasn't really sitting well with me. 👍

// 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;
Expand Down Expand Up @@ -481,7 +502,7 @@ - (void) pushEvent:(NSString *)event withAttributes:(NSDictionary *)attributeDic
[_eventQueue addObject:finalEvent];

// Has Flush Limit Been Reached?
if (_eventQueue.count >= MGLMaximumEventsPerFlush) {
if (_eventQueue.count >= MGLMaximumEventsPerFlush || [MGLEventTypeAppUserTurnstile isEqualToString:event]) {
[strongSelf flush];
} else if (_eventQueue.count == 1) {
// If this is first new event on queue start timer,
Expand Down