Skip to content

Commit cbf6e16

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: change the screen unique id generation method (#30)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 041f3c6 commit cbf6e16

File tree

3 files changed

+7
-14
lines changed

3 files changed

+7
-14
lines changed

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

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,9 @@ class AutoRecordEventClient {
3535
}
3636

3737
func onViewDidAppear(screenName: String, screenPath: String, screenHashValue: String) {
38-
let screenUniqueId = getScreenUniqueId(screenHashValue)
39-
if !isSameScreen(screenName, screenPath, screenUniqueId) {
38+
if !isSameScreen(screenName, screenPath, screenHashValue) {
4039
recordUserEngagement()
41-
recordScreenView(screenName, screenPath, screenUniqueId)
40+
recordScreenView(screenName, screenPath, screenHashValue)
4241
}
4342
}
4443

@@ -101,13 +100,6 @@ class AutoRecordEventClient {
101100
}
102101
}
103102

104-
func getScreenUniqueId(_ screenHashValue: String) -> String {
105-
let shortDeviceId = clickstream.systemInfo.deviceId.padding(toLength: Constants.maxDeviceIdLength,
106-
withPad: Constants.paddingChar,
107-
startingAt: 0)
108-
return "\(shortDeviceId)-\(screenHashValue)"
109-
}
110-
111103
func isSameScreen(_ screenName: String, _ screenPath: String, _ screenUniqueId: String) -> Bool {
112104
lastScreenName != nil
113105
&& lastScreenPath != nil
@@ -220,8 +212,6 @@ class AutoRecordEventClient {
220212
extension AutoRecordEventClient {
221213
enum Constants {
222214
static let minEngagementTime = 1_000
223-
static let maxDeviceIdLength = 8
224-
static let paddingChar = "_"
225215
}
226216
}
227217

Sources/Clickstream/Dependency/Clickstream/Event/ClickstreamEvent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ class ClickstreamEvent: AnalyticsPropertiesModel, Hashable {
105105
func getJsonStringFromObject(jsonObject: JsonObject) -> String {
106106
do {
107107
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: [.sortedKeys])
108-
return String(data: jsonData, encoding: .utf8)!
108+
return String(data: jsonData, encoding: .utf8) ?? ""
109109
} catch {
110110
log.error("Error serializing dictionary to JSON: \(error.localizedDescription)")
111111
}

Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,16 @@ class AutoRecordEventClientTest: XCTestCase {
8080
XCTAssertEqual(eventRecorder.lastSavedEvent?.eventType, Event.PresetEvent.SCREEN_VIEW)
8181
XCTAssertNotNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_ID])
8282
XCTAssertNotNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_NAME])
83-
XCTAssertNotNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_UNIQUEID])
8483
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP])
8584
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.PREVIOUS_SCREEN_ID])
8685
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.PREVIOUS_SCREEN_NAME])
8786
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.PREVIOUS_SCREEN_UNIQUEID])
8887
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.PREVIOUS_TIMESTAMP])
8988
XCTAssertTrue(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.ENTRANCES] as! Int == 1)
89+
90+
let screenUniqueId = eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_UNIQUEID] as! String
91+
XCTAssertNotNil(screenUniqueId)
92+
XCTAssertEqual(screenUniqueId, String(describing: viewController.hashValue))
9093
}
9194

9295
func testTwoScreenViewWithoutUserEngagement() {

0 commit comments

Comments
 (0)