Skip to content

Commit

Permalink
feat: add screen view event after session start (#53)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoweii <[email protected]>
  • Loading branch information
zhu-xiaowei and xiaoweii authored Mar 1, 2024
1 parent 3f3239b commit 63e1df1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
4 changes: 0 additions & 4 deletions IntegrationTest/devicefarm/logcat_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,9 @@ def test_first_screen_view(self, path):
if screen_view_event['event_json'].get('attributes')['_entrances'] == 0:
screen_view_event = sorted_screen_view_events[1]
assert screen_view_event['event_json'].get('attributes')['_entrances'] == 1
assert '_screen_id' in screen_view_event['event_json'].get('attributes')
assert '_screen_name' in screen_view_event['event_json'].get('attributes')
assert '_screen_unique_id' in screen_view_event['event_json'].get('attributes')

assert '_session_id' in screen_view_event['event_json'].get('attributes')
assert '_session_start_timestamp' in screen_view_event['event_json'].get('attributes')
assert '_session_duration' in screen_view_event['event_json'].get('attributes')
assert '_session_number' in screen_view_event['event_json'].get('attributes')
Expand All @@ -85,11 +83,9 @@ def test_last_screen_view(self, path):
(event for event in reversed(self.recorded_events) if '_screen_view' in event.get('event_name', '')),
None)
assert screen_view_event['event_json'].get('attributes')['_entrances'] == 0
assert '_screen_id' in screen_view_event['event_json'].get('attributes')
assert '_screen_name' in screen_view_event['event_json'].get('attributes')
assert '_screen_unique_id' in screen_view_event['event_json'].get('attributes')

assert '_previous_screen_id' in screen_view_event['event_json'].get('attributes')
assert '_previous_screen_name' in screen_view_event['event_json'].get('attributes')
assert '_previous_screen_unique_id' in screen_view_event['event_json'].get('attributes')
assert '_previous_timestamp' in screen_view_event['event_json'].get('attributes')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ class AutoRecordEventClient {
}
}

func recordScreenViewAfterSessionStart() {
if lastScreenName != nil {
let currentScreenName = lastScreenName!
let currentScreenUniqueId = lastScreenUniqueId ?? ""
let currentScreenId = lastScreenPath ?? ""
lastScreenName = nil
lastScreenUniqueId = nil
lastScreenPath = nil
onViewDidAppear(screenName: currentScreenName, screenPath: currentScreenId,
screenHashValue: currentScreenUniqueId)
}
}

func recordScreenViewEvent(_ event: ClickstreamEvent, _ screenName: String,
_ screenPath: String?, _ screenUniqueId: String?)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SessionClient: SessionClientBehaviour {
if session!.isNewSession {
autoRecordClient.recordSessionStartEvent()
autoRecordClient.setIsEntrances()
autoRecordClient.recordScreenViewAfterSessionStart()
}
}

Expand Down
30 changes: 30 additions & 0 deletions Tests/ClickstreamTests/Clickstream/SessionClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,36 @@ class SessionClientTests: XCTestCase {
XCTAssertFalse(appStartEvent.attributes[Event.ReservedAttribute.IS_FIRST_TIME] as! Bool)
}

func testReopenAppAfterSessionTimeoutWillRecordScreenView() {
clickstream.configuration.sessionTimeoutDuration = 0
activityTracker.callback?(.runningInForeground)
let viewController = MockViewControllerA()
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = viewController
window.makeKeyAndVisible()
activityTracker.callback?(.runningInBackground)
activityTracker.callback?(.runningInForeground)
Thread.sleep(forTimeInterval: 0.1)
let events = eventRecorder.savedEvents
XCTAssertEqual(8, events.count)
XCTAssertEqual(Event.PresetEvent.FIRST_OPEN, events[0].eventType)
XCTAssertEqual(Event.PresetEvent.APP_START, events[1].eventType)
XCTAssertEqual(Event.PresetEvent.SESSION_START, events[2].eventType)
XCTAssertEqual(Event.PresetEvent.SCREEN_VIEW, events[3].eventType)
XCTAssertEqual(Event.PresetEvent.APP_END, events[4].eventType)
XCTAssertEqual(Event.PresetEvent.APP_START, events[5].eventType)
XCTAssertEqual(Event.PresetEvent.SESSION_START, events[6].eventType)

XCTAssertEqual(Event.PresetEvent.SCREEN_VIEW, events[7].eventType)
XCTAssertNotNil(events[7].attributes[Event.ReservedAttribute.SCREEN_NAME])
XCTAssertNotNil(events[7].attributes[Event.ReservedAttribute.SCREEN_ID])
XCTAssertNotNil(events[7].attributes[Event.ReservedAttribute.SCREEN_UNIQUEID])
XCTAssertNil(events[7].attributes[Event.ReservedAttribute.PREVIOUS_SCREEN_NAME])
XCTAssertNil(events[7].attributes[Event.ReservedAttribute.PREVIOUS_SCREEN_ID])
XCTAssertNil(events[7].attributes[Event.ReservedAttribute.PREVIOUS_SCREEN_UNIQUEID])
XCTAssertEqual(1, events[7].attributes[Event.ReservedAttribute.ENTRANCES] as! Int)
}

func testLastScreenStartTimeStampUpdatedAfterReturnToForeground() {
activityTracker.callback?(.runningInForeground)
let viewController = MockViewControllerA()
Expand Down

0 comments on commit 63e1df1

Please sign in to comment.