Skip to content

Commit f1cc830

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: update screen start timestamp when app enter foreground (#28)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 5f925a7 commit f1cc830

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

Sources/Clickstream/Dependency/Clickstream/AutoRecord/AutoRecordEventClient.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ class AutoRecordEventClient {
8989
}
9090
}
9191

92+
func updateLastScreenStartTimestamp() {
93+
lastScreenStartTimestamp = Date().millisecondsSince1970
94+
}
95+
9296
func getPreviousScreenViewTimestamp() -> Int64 {
9397
if lastScreenStartTimestamp > 0 {
9498
return lastScreenStartTimestamp

Sources/Clickstream/Dependency/Clickstream/Session/SessionClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class SessionClient: SessionClientBehaviour {
5858
private func handleAppEnterForeground() {
5959
log.debug("Application entered the foreground.")
6060
autoRecordClient.handleAppStart()
61+
autoRecordClient.updateLastScreenStartTimestamp()
6162
let isNewSession = initialSession()
6263
if isNewSession {
6364
autoRecordClient.setIsEntrances()

Tests/ClickstreamTests/Clickstream/SessionClientTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,4 +187,21 @@ class SessionClientTests: XCTestCase {
187187
XCTAssertNotNil(appStartEvent.attributes[Event.ReservedAttribute.SCREEN_ID])
188188
XCTAssertFalse(appStartEvent.attributes[Event.ReservedAttribute.IS_FIRST_TIME] as! Bool)
189189
}
190+
191+
func testLastScreenStartTimeStampUpdatedAfterReturnToForeground() {
192+
activityTracker.callback?(.runningInForeground)
193+
let viewController = MockViewControllerA()
194+
let window = UIWindow(frame: UIScreen.main.bounds)
195+
window.rootViewController = viewController
196+
window.makeKeyAndVisible()
197+
Thread.sleep(forTimeInterval: 0.2)
198+
activityTracker.callback?(.runningInForeground)
199+
Thread.sleep(forTimeInterval: 1)
200+
activityTracker.callback?(.runningInBackground)
201+
let events = eventRecorder.savedEvents
202+
XCTAssertEqual(7, events.count)
203+
let userEngagementEvent = events[6]
204+
XCTAssertEqual(Event.PresetEvent.USER_ENGAGEMENT, userEngagementEvent.eventType)
205+
XCTAssertTrue((userEngagementEvent.attributes[Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP] as! Int64) < 1_200)
206+
}
190207
}

0 commit comments

Comments
 (0)