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

Commit

Permalink
[iOS] Generate and send a turnstile event when map view event is queued
Browse files Browse the repository at this point in the history
This change adds logic to push a turnstile event whenever a map view
event is queued. It guards against sending the turnstile event more
than once per session.

Note that `pushTurnstileEvent` is actually a "force push" in that it
flushes the queue and, since it enqueues a turnstile event, the
flush's call to `postEvents` will attempt to send any queued events
even if telemetry is paused. This has the side effect of defeating
the other queue / timing / flush logic in place whenever map load
(and now implicitly turnstile) events are pushed.
  • Loading branch information
boundsj committed Feb 15, 2016
1 parent 14f81db commit 9891d0e
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions platform/ios/src/MGLMapboxEvents.m
Original file line number Diff line number Diff line change
Expand Up @@ -275,11 +275,6 @@ - (instancetype) init {
MGLMapboxEvents *strongSelf = weakSelf;
[strongSelf validate];
}];

// Turn the Mapbox Turnstile to Count App Users
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
[self pushTurnstileEvent];
});
}
return self;
}
Expand Down Expand Up @@ -429,38 +424,42 @@ - (void)startUpdatingLocation {
}
}


- (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];

if (!vid) return;

NSDictionary *vevt = @{@"event" : MGLEventTypeAppUserTurnstile,
@"created" : [strongSelf.rfc3339DateFormatter stringFromDate:[NSDate date]],
@"appBundleId" : strongSelf.appBundleId,
@"vendorId": vid,
@"version": @(version),
@"instance": strongSelf.instanceID};

// Add to Queue
[_eventQueue addObject:vevt];

// Flush
[strongSelf flush];

if ([strongSelf debugLoggingEnabled]) {
[strongSelf writeEventToLocalDebugLog:vevt];
}


static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dispatch_async(_serialQueue, ^{

MGLMapboxEvents *strongSelf = weakSelf;

if ( ! strongSelf) return;

// Build only IDFV event
NSString *vid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];

if (!vid) return;

NSDictionary *vevt = @{@"event" : MGLEventTypeAppUserTurnstile,
@"created" : [strongSelf.rfc3339DateFormatter stringFromDate:[NSDate date]],
@"appBundleId" : strongSelf.appBundleId,
@"vendorId": vid,
@"version": @(version),
@"instance": strongSelf.instanceID};

// Add to Queue
[_eventQueue addObject:vevt];

// Flush
[strongSelf flush];

if ([strongSelf debugLoggingEnabled]) {
[strongSelf writeEventToLocalDebugLog:vevt];
}

});
});
}

Expand All @@ -484,13 +483,18 @@ - (void) pushEvent:(NSString *)event withAttributes:(MGLMapboxEventAttributes *)
MGLMapboxEvents *strongSelf = weakSelf;

if ( ! strongSelf) return;

if (!event) return;

// If it's a map load event then turn the Mapbox Turnstile to count app users
if ([event isEqualToString:MGLEventTypeMapLoad]) {
[self pushTurnstileEvent];
}

// Metrics Collection Has Been Paused
if (_paused) {
return;
}

if (!event) return;

MGLMutableMapboxEventAttributes *evt = [MGLMutableMapboxEventAttributes dictionaryWithDictionary:attributeDictionary];
// mapbox-events stock attributes
Expand Down Expand Up @@ -520,6 +524,7 @@ - (void) pushEvent:(NSString *)event withAttributes:(MGLMapboxEventAttributes *)

// Put On The Queue
[_eventQueue addObject:finalEvent];


// Has Flush Limit Been Reached?
if (_eventQueue.count >= MGLMaximumEventsPerFlush) {
Expand Down

0 comments on commit 9891d0e

Please sign in to comment.