Skip to content

Commit

Permalink
Record entire context for every event.
Browse files Browse the repository at this point in the history
Previously, some pieces of context were set on the batch (static
context such as library info, device info, etc.).

This had the advantage of reducing the payload size (send it once
instead of 20 times in 20 event batch). But by adding gzip support we
don't really need to do this as much anymore.

So now we store the entire context with every payload, which represents the
context at the time of the event (rather than upload) more accurately.
  • Loading branch information
f2prateek committed Jun 30, 2016
1 parent bad7259 commit 98a4672
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions Pod/Classes/Internal/SEGSegmentIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static BOOL GetAdTrackingEnabled()
@interface SEGSegmentIntegration ()

@property (nonatomic, strong) NSMutableArray *queue;
@property (nonatomic, strong) NSDictionary *context;
@property (nonatomic, strong) NSDictionary *cachedStaticContext;
@property (nonatomic, strong) NSArray *batch;
@property (nonatomic, strong) SEGAnalyticsRequest *request;
@property (nonatomic, assign) UIBackgroundTaskIdentifier flushTaskID;
Expand All @@ -79,7 +79,7 @@ - (id)initWithAnalytics:(SEGAnalytics *)analytics
self.bluetooth = [[SEGBluetooth alloc] init];
self.reachability = [SEGReachability reachabilityWithHostname:@"google.com"];
[self.reachability startNotifier];
self.context = [self staticContext];
self.cachedStaticContext = [self staticContext];
self.flushTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(flush) userInfo:nil repeats:YES];
self.serialQueue = seg_dispatch_queue_create_specific("io.segment.analytics.segmentio", DISPATCH_QUEUE_SERIAL);
self.flushTaskID = UIBackgroundTaskInvalid;
Expand Down Expand Up @@ -192,8 +192,6 @@ - (NSDictionary *)liveContext
{
NSMutableDictionary *context = [[NSMutableDictionary alloc] init];

[context addEntriesFromDictionary:self.context];

context[@"locale"] = [NSString stringWithFormat:
@"%@-%@",
[NSLocale.currentLocale objectForKey:NSLocaleLanguageCode],
Expand Down Expand Up @@ -346,7 +344,7 @@ - (void)registeredForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
for (NSUInteger i = 0; i < deviceToken.length; i++) {
[token appendString:[NSString stringWithFormat:@"%02lx", (unsigned long)buffer[i]]];
}
[self.context[@"device"] setObject:[token copy] forKey:@"token"];
[self.cachedStaticContext[@"device"] setObject:[token copy] forKey:@"token"];
}

#pragma mark - Queueing
Expand Down Expand Up @@ -384,11 +382,13 @@ - (void)enqueueAction:(NSString *)action dictionary:(NSMutableDictionary *)paylo

[payload setValue:[self integrationsDictionary:integrations] forKey:@"integrations"];

NSDictionary *defaultContext = [self liveContext];
NSDictionary *staticContext = self.cachedStaticContext;
NSDictionary *liveContext = [self liveContext];
NSDictionary *customContext = context;
NSMutableDictionary *context = [NSMutableDictionary dictionaryWithCapacity:customContext.count + defaultContext.count];
[context addEntriesFromDictionary:defaultContext];
[context addEntriesFromDictionary:customContext]; // let the custom context override ours
NSMutableDictionary *context = [NSMutableDictionary dictionaryWithCapacity:(staticContext.count + liveContext.count + customContext.count)];
[context addEntriesFromDictionary:staticContext];
[context addEntriesFromDictionary:liveContext];
[context addEntriesFromDictionary:customContext];
[payload setValue:[context copy] forKey:@"context"];

SEGLog(@"%@ Enqueueing action: %@", self, payload);
Expand All @@ -402,7 +402,6 @@ - (void)queuePayload:(NSDictionary *)payload
[self.queue addObject:payload];
[[self.queue copy] writeToURL:[self queueURL] atomically:YES];
[self flushQueueByLength];

}
@catch (NSException *exception) {
SEGLog(@"%@ Error writing payload: %@", self, exception);
Expand All @@ -415,7 +414,6 @@ - (void)queuePayloadFromArray:(NSArray *)payloadArray
[self.queue addObjectsFromArray:payloadArray];
[[self.queue copy] writeToURL:[self queueURL] atomically:YES];
[self flushQueueByLength];

}
@catch (NSException *exception) {
SEGLog(@"%@ Error writing payload: %@", self, exception);
Expand Down Expand Up @@ -447,7 +445,6 @@ - (void)flushWithMaxSize:(NSUInteger)maxBatchSize
NSMutableDictionary *payloadDictionary = [[NSMutableDictionary alloc] init];
[payloadDictionary setObject:self.configuration.writeKey forKey:@"writeKey"];
[payloadDictionary setObject:iso8601FormattedString([NSDate date]) forKey:@"sentAt"];
[payloadDictionary setObject:self.context forKey:@"context"];
[payloadDictionary setObject:self.batch forKey:@"batch"];

SEGLog(@"Flushing payload %@", payloadDictionary);
Expand Down

0 comments on commit 98a4672

Please sign in to comment.