From fcc084b40759e9433c5d701903a9e21cb80ace38 Mon Sep 17 00:00:00 2001 From: Fathy Boundjadj Date: Fri, 13 Jul 2018 15:48:31 +0200 Subject: [PATCH 1/2] Add a `flushInterval` configuration option Ref: LIB-443 --- .../Classes/Internal/SEGSegmentIntegration.m | 6 ++++- Analytics/Classes/SEGAnalyticsConfiguration.h | 4 ++++ Analytics/Classes/SEGAnalyticsConfiguration.m | 1 + AnalyticsTests/AnalyticsTests.swift | 24 +++++++++++++++++++ AnalyticsTests/Utils/TestUtils.swift | 6 +++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Analytics/Classes/Internal/SEGSegmentIntegration.m b/Analytics/Classes/Internal/SEGSegmentIntegration.m index 6ea6f892a..ba4c745ac 100644 --- a/Analytics/Classes/Internal/SEGSegmentIntegration.m +++ b/Analytics/Classes/Internal/SEGSegmentIntegration.m @@ -119,7 +119,11 @@ - (id)initWithAnalytics:(SEGAnalytics *)analytics httpClient:(SEGHTTPClient *)ht - (void)setupFlushTimer { - self.flushTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(flush) userInfo:nil repeats:YES]; + self.flushTimer = [NSTimer scheduledTimerWithTimeInterval:self.configuration.flushInterval + target:self + selector:@selector(flush) + userInfo:nil + repeats:YES]; } /* diff --git a/Analytics/Classes/SEGAnalyticsConfiguration.h b/Analytics/Classes/SEGAnalyticsConfiguration.h index 43c1a236e..da8fac9f6 100644 --- a/Analytics/Classes/SEGAnalyticsConfiguration.h +++ b/Analytics/Classes/SEGAnalyticsConfiguration.h @@ -61,6 +61,10 @@ typedef NSMutableURLRequest *_Nonnull (^SEGRequestFactory)(NSURL *_Nonnull); */ @property (nonatomic, assign) NSUInteger flushAt; +/** + * The amount of time to wait before each tick of the flush timer. Smaller values will make events delivered in a more real-time manner and also use more battery. `30` by default. + */ +@property (nonatomic, assign) NSTimeInterval flushInterval; /** * Whether the analytics client should automatically make a track call for application lifecycle events, such as "Application Installed", "Application Updated" and "Application Opened". diff --git a/Analytics/Classes/SEGAnalyticsConfiguration.m b/Analytics/Classes/SEGAnalyticsConfiguration.m index 77e38038e..f40525ae0 100644 --- a/Analytics/Classes/SEGAnalyticsConfiguration.m +++ b/Analytics/Classes/SEGAnalyticsConfiguration.m @@ -55,6 +55,7 @@ - (instancetype)init self.enableAdvertisingTracking = YES; self.shouldUseBluetooth = NO; self.flushAt = 20; + self.flushInterval = 30; _factories = [NSMutableArray array]; Class applicationClass = NSClassFromString(@"UIApplication"); if (applicationClass) { diff --git a/AnalyticsTests/AnalyticsTests.swift b/AnalyticsTests/AnalyticsTests.swift index 1baed04a9..5a0fc1aef 100644 --- a/AnalyticsTests/AnalyticsTests.swift +++ b/AnalyticsTests/AnalyticsTests.swift @@ -41,6 +41,7 @@ class AnalyticsTests: QuickSpec { it("initialized correctly") { expect(analytics.configuration.flushAt) == 20 + expect(analytics.configuration.flushInterval) == 30 expect(analytics.configuration.writeKey) == "QUI5ydwIGeFFTa1IvCBUhxL9PyW5B0jE" expect(analytics.configuration.shouldUseLocationServices) == false expect(analytics.configuration.enableAdvertisingTracking) == true @@ -120,6 +121,29 @@ class AnalyticsTests: QuickSpec { let task = UIApplication.shared.beginBackgroundTask(expirationHandler: nil) UIApplication.shared.endBackgroundTask(task) } + + it("flushes using flushTimer") { + let integration = analytics.test_integrationsManager()?.test_segmentIntegration() + + analytics.track("test") + + expect(integration?.test_flushTimer()).toEventuallyNot(beNil()) + expect(integration?.test_batchRequest()).to(beNil()) + + integration?.test_flushTimer()?.fire() + + expect(integration?.test_batchRequest()).toEventuallyNot(beNil()) + } + + it("respects flushInterval") { + let timer = analytics + .test_integrationsManager()? + .test_segmentIntegration()? + .test_flushTimer() + + expect(timer).toNot(beNil()) + expect(timer?.timeInterval) == config.flushInterval + } } } diff --git a/AnalyticsTests/Utils/TestUtils.swift b/AnalyticsTests/Utils/TestUtils.swift index 38ee94cb9..edd267542 100644 --- a/AnalyticsTests/Utils/TestUtils.swift +++ b/AnalyticsTests/Utils/TestUtils.swift @@ -58,6 +58,12 @@ extension SEGSegmentIntegration { func test_traits() -> [String: AnyObject]? { return self.value(forKey: "traits") as? [String: AnyObject] } + func test_flushTimer() -> Timer? { + return self.value(forKey: "flushTimer") as? Timer + } + func test_batchRequest() -> URLSessionUploadTask? { + return self.value(forKey: "batchRequest") as? URLSessionUploadTask + } } From 5b7be56e955d137add270f88fd510946b4e81910 Mon Sep 17 00:00:00 2001 From: Fathy Boundjadj Date: Mon, 16 Jul 2018 10:09:18 +0200 Subject: [PATCH 2/2] Improve property documentation --- Analytics/Classes/SEGAnalyticsConfiguration.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Analytics/Classes/SEGAnalyticsConfiguration.h b/Analytics/Classes/SEGAnalyticsConfiguration.h index da8fac9f6..4e319aad1 100644 --- a/Analytics/Classes/SEGAnalyticsConfiguration.h +++ b/Analytics/Classes/SEGAnalyticsConfiguration.h @@ -62,7 +62,10 @@ typedef NSMutableURLRequest *_Nonnull (^SEGRequestFactory)(NSURL *_Nonnull); @property (nonatomic, assign) NSUInteger flushAt; /** - * The amount of time to wait before each tick of the flush timer. Smaller values will make events delivered in a more real-time manner and also use more battery. `30` by default. + * The amount of time to wait before each tick of the flush timer. + * Smaller values will make events delivered in a more real-time manner and also use more battery. + * A value smaller than 10 seconds will seriously degrade overall performance. + * 30 seconds by default. */ @property (nonatomic, assign) NSTimeInterval flushInterval;