Skip to content

Commit b3570db

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: optimize engagement time and previous timestamp attribute in first screen view (#36)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 459f25b commit b3570db

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ class AutoRecordEventClient {
3636

3737
func onViewDidAppear(screenName: String, screenPath: String, screenHashValue: String) {
3838
if !isSameScreen(screenName, screenPath, screenHashValue) {
39-
recordUserEngagement()
39+
if lastScreenName != nil {
40+
recordUserEngagement()
41+
}
4042
recordScreenView(screenName, screenPath, screenHashValue)
4143
}
4244
}
@@ -86,11 +88,7 @@ class AutoRecordEventClient {
8688
}
8789

8890
func getPreviousScreenViewTimestamp() -> Int64 {
89-
if lastScreenStartTimestamp > 0 {
90-
return lastScreenStartTimestamp
91-
} else {
92-
return UserDefaultsUtil.getPreviousScreenViewTimestamp(storage: clickstream.storage)
93-
}
91+
UserDefaultsUtil.getPreviousScreenViewTimestamp(storage: clickstream.storage)
9492
}
9593

9694
func isSameScreen(_ screenName: String, _ screenPath: String, _ screenUniqueId: String) -> Bool {

Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,40 @@ class AutoRecordEventClientTest: XCTestCase {
1313
private var clickstream: ClickstreamContext!
1414
private var eventRecorder: MockEventRecorder!
1515
private var autoRecordEventClient: AutoRecordEventClient!
16+
private var activityTracker: MockActivityTracker!
17+
private var sessionClient: SessionClient!
1618
let testAppId = "testAppId"
1719
let testEndpoint = "https://example.com/collect"
1820

1921
override func setUp() async throws {
2022
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
2123
let mockNetworkMonitor = MockNetworkMonitor()
24+
activityTracker = MockActivityTracker()
2225
let contextConfiguration = ClickstreamContextConfiguration(appId: testAppId,
2326
endpoint: testEndpoint,
2427
sendEventsInterval: 10_000,
2528
isTrackAppExceptionEvents: true,
2629
isCompressEvents: false)
2730
clickstream = try ClickstreamContext(with: contextConfiguration)
2831
clickstream.networkMonitor = mockNetworkMonitor
32+
sessionClient = SessionClient(activityTracker: activityTracker, clickstream: clickstream)
33+
clickstream.sessionClient = sessionClient
2934
eventRecorder = MockEventRecorder()
3035
let analyticsClient = try AnalyticsClient(
3136
clickstream: clickstream,
3237
eventRecorder: eventRecorder,
3338
sessionProvider: { nil }
3439
)
3540
clickstream.analyticsClient = analyticsClient
36-
autoRecordEventClient = AutoRecordEventClient(clickstream: clickstream)
37-
analyticsClient.autoRecordClient = autoRecordEventClient
41+
42+
autoRecordEventClient = sessionClient.autoRecordClient
3843
}
3944

4045
override func tearDown() {
4146
eventRecorder = nil
47+
activityTracker = nil
48+
sessionClient = nil
49+
activityTracker?.resetCounters()
4250
}
4351

4452
func testAppVersionUpdate() {
@@ -70,14 +78,15 @@ class AutoRecordEventClientTest: XCTestCase {
7078
}
7179

7280
func testOneScreenView() {
81+
activityTracker.callback?(.runningInForeground)
7382
autoRecordEventClient.setIsEntrances()
7483
let viewController = MockViewControllerA()
7584
let window = UIWindow(frame: UIScreen.main.bounds)
7685
window.rootViewController = viewController
7786
window.makeKeyAndVisible()
7887
XCTAssertTrue(viewController.viewDidAppearCalled)
7988

80-
XCTAssertTrue(eventRecorder.saveCount == 1)
89+
XCTAssertTrue(eventRecorder.saveCount == 4)
8190
XCTAssertEqual(eventRecorder.lastSavedEvent?.eventType, Event.PresetEvent.SCREEN_VIEW)
8291
XCTAssertNotNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_ID])
8392
XCTAssertNotNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_NAME])
@@ -137,7 +146,7 @@ class AutoRecordEventClientTest: XCTestCase {
137146
let window = UIWindow(frame: UIScreen.main.bounds)
138147
window.rootViewController = viewControllerA
139148
window.makeKeyAndVisible()
140-
149+
141150
autoRecordEventClient.updateLastScreenStartTimestamp(Date().millisecondsSince1970 - 1_100)
142151
Thread.sleep(forTimeInterval: 0.02)
143152
window.rootViewController = viewControllerB

0 commit comments

Comments
 (0)