Skip to content

Commit

Permalink
Add a flushInterval configuration option
Browse files Browse the repository at this point in the history
Ref: LIB-443
  • Loading branch information
fathyb committed Jul 13, 2018
1 parent 7a8f7d3 commit fcc084b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Analytics/Classes/Internal/SEGSegmentIntegration.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

/*
Expand Down
4 changes: 4 additions & 0 deletions Analytics/Classes/SEGAnalyticsConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down
1 change: 1 addition & 0 deletions Analytics/Classes/SEGAnalyticsConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions AnalyticsTests/AnalyticsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}

}
6 changes: 6 additions & 0 deletions AnalyticsTests/Utils/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}


Expand Down

0 comments on commit fcc084b

Please sign in to comment.