Skip to content

Commit

Permalink
fix: change the screen unique id generation method (#30)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
  • Loading branch information
zhu-xiaowei and xiaoweii authored Sep 9, 2023
1 parent 041f3c6 commit cbf6e16
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,9 @@ class AutoRecordEventClient {
}

func onViewDidAppear(screenName: String, screenPath: String, screenHashValue: String) {
let screenUniqueId = getScreenUniqueId(screenHashValue)
if !isSameScreen(screenName, screenPath, screenUniqueId) {
if !isSameScreen(screenName, screenPath, screenHashValue) {
recordUserEngagement()
recordScreenView(screenName, screenPath, screenUniqueId)
recordScreenView(screenName, screenPath, screenHashValue)
}
}

Expand Down Expand Up @@ -101,13 +100,6 @@ class AutoRecordEventClient {
}
}

func getScreenUniqueId(_ screenHashValue: String) -> String {
let shortDeviceId = clickstream.systemInfo.deviceId.padding(toLength: Constants.maxDeviceIdLength,
withPad: Constants.paddingChar,
startingAt: 0)
return "\(shortDeviceId)-\(screenHashValue)"
}

func isSameScreen(_ screenName: String, _ screenPath: String, _ screenUniqueId: String) -> Bool {
lastScreenName != nil
&& lastScreenPath != nil
Expand Down Expand Up @@ -220,8 +212,6 @@ class AutoRecordEventClient {
extension AutoRecordEventClient {
enum Constants {
static let minEngagementTime = 1_000
static let maxDeviceIdLength = 8
static let paddingChar = "_"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ClickstreamEvent: AnalyticsPropertiesModel, Hashable {
func getJsonStringFromObject(jsonObject: JsonObject) -> String {
do {
let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: [.sortedKeys])
return String(data: jsonData, encoding: .utf8)!
return String(data: jsonData, encoding: .utf8) ?? ""
} catch {
log.error("Error serializing dictionary to JSON: \(error.localizedDescription)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,16 @@ class AutoRecordEventClientTest: XCTestCase {
XCTAssertEqual(eventRecorder.lastSavedEvent?.eventType, Event.PresetEvent.SCREEN_VIEW)
XCTAssertNotNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_ID])
XCTAssertNotNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_NAME])
XCTAssertNotNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_UNIQUEID])
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.ENGAGEMENT_TIMESTAMP])
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.PREVIOUS_SCREEN_ID])
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.PREVIOUS_SCREEN_NAME])
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.PREVIOUS_SCREEN_UNIQUEID])
XCTAssertNil(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.PREVIOUS_TIMESTAMP])
XCTAssertTrue(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.ENTRANCES] as! Int == 1)

let screenUniqueId = eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.SCREEN_UNIQUEID] as! String
XCTAssertNotNil(screenUniqueId)
XCTAssertEqual(screenUniqueId, String(describing: viewController.hashValue))
}

func testTwoScreenViewWithoutUserEngagement() {
Expand Down

0 comments on commit cbf6e16

Please sign in to comment.