diff --git a/Analytics/Classes/Integrations/SEGIntegrationsManager.m b/Analytics/Classes/Integrations/SEGIntegrationsManager.m index 573bf81e1..dbc6f949b 100644 --- a/Analytics/Classes/Integrations/SEGIntegrationsManager.m +++ b/Analytics/Classes/Integrations/SEGIntegrationsManager.m @@ -417,7 +417,7 @@ + (BOOL)isTrackEvent:(NSString *)event enabledForIntegration:(NSString *)key inP if ([key isEqualToString:@"Segment.io"]) { return YES; } - + if (plan[@"track"][event]) { if ([plan[@"track"][event][@"enabled"] boolValue]) { return [self isIntegration:key enabledInOptions:plan[@"track"][event][@"integrations"]]; diff --git a/Analytics/Classes/Internal/SEGAnalyticsUtils.h b/Analytics/Classes/Internal/SEGAnalyticsUtils.h index 4b2c513de..8168525e0 100644 --- a/Analytics/Classes/Internal/SEGAnalyticsUtils.h +++ b/Analytics/Classes/Internal/SEGAnalyticsUtils.h @@ -8,6 +8,8 @@ NSString *GenerateUUIDString(void); // Date Utils NSString *iso8601FormattedString(NSDate *date); +void trimQueue(NSMutableArray *array, int size); + // Async Utils dispatch_queue_t seg_dispatch_queue_create_specific(const char *label, dispatch_queue_attr_t _Nullable attr); diff --git a/Analytics/Classes/Internal/SEGAnalyticsUtils.m b/Analytics/Classes/Internal/SEGAnalyticsUtils.m index 9701baa47..97a7e15fb 100644 --- a/Analytics/Classes/Internal/SEGAnalyticsUtils.m +++ b/Analytics/Classes/Internal/SEGAnalyticsUtils.m @@ -25,6 +25,20 @@ return [dateFormatter stringFromDate:date]; } +/** trim the queue so that it contains only upto `max` number of elements. */ +void trimQueue(NSMutableArray *queue, int max) +{ + if (queue.count < max) { + return; + } + + // Previously we didn't cap the queue. Hence there are cases where + // the queue may already be larger than 1000 events. Delete as many + // events as required to trim the queue size. + NSRange range = NSMakeRange(0, queue.count - max); + [queue removeObjectsInRange:range]; +} + // Async Utils dispatch_queue_t seg_dispatch_queue_create_specific(const char *label, diff --git a/Analytics/Classes/Internal/SEGSegmentIntegration.m b/Analytics/Classes/Internal/SEGSegmentIntegration.m index c785f6736..6ea6f892a 100644 --- a/Analytics/Classes/Internal/SEGSegmentIntegration.m +++ b/Analytics/Classes/Internal/SEGSegmentIntegration.m @@ -461,10 +461,9 @@ - (void)enqueueAction:(NSString *)action dictionary:(NSMutableDictionary *)paylo - (void)queuePayload:(NSDictionary *)payload { @try { - if (self.queue.count > 1000) { - // Remove the oldest element. - [self.queue removeObjectAtIndex:0]; - } + // We only queue upto 1000 items, so trim the queue to 1000-1=999 + // before we add a new element. + trimQueue(self.queue, 999); [self.queue addObject:payload]; [self persistQueue]; [self flushQueueByLength]; diff --git a/AnalyticsTests/AnalyticsUtilTests.swift b/AnalyticsTests/AnalyticsUtilTests.swift index deda55864..ba6f7d691 100644 --- a/AnalyticsTests/AnalyticsUtilTests.swift +++ b/AnalyticsTests/AnalyticsUtilTests.swift @@ -13,12 +13,12 @@ import Analytics class AnalyticsUtilTests: QuickSpec { override func spec() { - + it("format NSDate objects to RFC 3339 complaint string") { let date = Date(timeIntervalSince1970: 0) let formattedString = iso8601FormattedString(date) expect(formattedString) == "1970-01-01T00:00:00.000Z" - + var components = DateComponents() components.year = 1992 components.month = 8 @@ -33,7 +33,34 @@ class AnalyticsUtilTests: QuickSpec { let formattedString2 = iso8601FormattedString(date2) expect(formattedString2) == "1992-08-06T11:32:04.335Z" } - + + describe("trimQueue", { + it("does nothing when count < max") { + let queue = NSMutableArray(array: []) + for i in 1...4 { + queue.add(i) + } + trimQueue(queue, 5) + expect(queue) == [1, 2, 3, 4] + } + + it("trims when count > max") { + let queue = NSMutableArray(array: []) + for i in 1...10 { + queue.add(i) + } + trimQueue(queue, 5) + expect(queue) == [6, 7, 8, 9, 10] + } + + it("does not trim when count == max") { + let queue = NSMutableArray(array: []) + for i in 1...5 { + queue.add(i) + } + trimQueue(queue, 5) + expect(queue) == [1, 2, 3, 4, 5] + } + }) } - }