From ee3abff92a39ffd75d205ae55dde6f2ed675a897 Mon Sep 17 00:00:00 2001 From: Xiaowei Zhu <33129495+zhu-xiaowei@users.noreply.github.com> Date: Tue, 5 Mar 2024 12:02:41 +0800 Subject: [PATCH] fix: sessionId in first open and session start events (#54) Co-authored-by: xiaoweii --- .../AWSClickstreamPlugin+Configure.swift | 13 ++--- .../Analytics/AnalyticsClient.swift | 30 +++++------ .../AutoRecord/AutoRecordEventClient.swift | 19 ++++--- .../Clickstream/Session/Session.swift | 20 ++++--- .../Clickstream/Session/SessionClient.swift | 37 +++++++------ .../Clickstream/AnalyticsClientTest.swift | 9 ++-- .../AutoRecordEventClientTest.swift | 3 +- .../Clickstream/EventRecorderTest.swift | 12 ++--- .../Clickstream/SessionClientTests.swift | 52 ++++++++++++------- .../ClickstreamPluginTestBase.swift | 8 +-- Tests/ClickstreamTests/IntegrationTest.swift | 26 +++++----- 11 files changed, 117 insertions(+), 112 deletions(-) diff --git a/Sources/Clickstream/AWSClickstreamPlugin+Configure.swift b/Sources/Clickstream/AWSClickstreamPlugin+Configure.swift index af80867..9d315c6 100644 --- a/Sources/Clickstream/AWSClickstreamPlugin+Configure.swift +++ b/Sources/Clickstream/AWSClickstreamPlugin+Configure.swift @@ -37,20 +37,15 @@ extension AWSClickstreamPlugin { let sessionClient = SessionClient(clickstream: clickstream) clickstream.sessionClient = sessionClient - let sessionProvider: () -> Session? = { [weak sessionClient] in - guard let sessionClient else { - fatalError("SessionClient was deallocated") - } - return sessionClient.getCurrentSession() - } let eventRecorder = try EventRecorder(clickstream: clickstream) analyticsClient = try AnalyticsClient(clickstream: clickstream, eventRecorder: eventRecorder, - sessionProvider: sessionProvider) + sessionClient: sessionClient) clickstream.analyticsClient = analyticsClient + let networkMonitor = NWPathMonitor() clickstream.networkMonitor = networkMonitor - sessionClient.initialSession() + sessionClient.startActivityTracking() var autoFlushEventsTimer: DispatchSourceTimer? if configuration.sendEventsInterval != 0 { let timeInterval = TimeInterval(Double(configuration.sendEventsInterval) / 1_000) @@ -66,7 +61,7 @@ extension AWSClickstreamPlugin { autoFlushEventsTimer: autoFlushEventsTimer, networkMonitor: networkMonitor ) - log.debug("init the sdk success") + log.debug("initialize Clickstream SDK successful") } // MARK: Internal diff --git a/Sources/Clickstream/Dependency/Clickstream/Analytics/AnalyticsClient.swift b/Sources/Clickstream/Dependency/Clickstream/Analytics/AnalyticsClient.swift index 3c511e0..e20d932 100644 --- a/Sources/Clickstream/Dependency/Clickstream/Analytics/AnalyticsClient.swift +++ b/Sources/Clickstream/Dependency/Clickstream/Analytics/AnalyticsClient.swift @@ -21,29 +21,27 @@ protocol AnalyticsClientBehaviour { func submitEvents(isBackgroundMode: Bool) } -typealias SessionProvider = () -> Session? - class AnalyticsClient: AnalyticsClientBehaviour { private(set) var eventRecorder: AnalyticsEventRecording - private let sessionProvider: SessionProvider + private let sessionClient: SessionClient private(set) lazy var globalAttributes: [String: AttributeValue] = [:] private(set) var allUserAttributes: [String: Any] = [:] private(set) var simpleUserAttributes: [String: Any] = [:] private let clickstream: ClickstreamContext private(set) var userId: String? - var autoRecordClient: AutoRecordEventClient? + var autoRecordClient: AutoRecordEventClient init(clickstream: ClickstreamContext, eventRecorder: AnalyticsEventRecording, - sessionProvider: @escaping SessionProvider) throws + sessionClient: SessionClient) throws { self.clickstream = clickstream self.eventRecorder = eventRecorder - self.sessionProvider = sessionProvider + self.sessionClient = sessionClient self.userId = UserDefaultsUtil.getCurrentUserId(storage: clickstream.storage) self.allUserAttributes = UserDefaultsUtil.getUserAttributes(storage: clickstream.storage) + self.autoRecordClient = sessionClient.autoRecordClient self.simpleUserAttributes = getSimpleUserAttributes() - self.autoRecordClient = (clickstream.sessionClient as? SessionClient)?.autoRecordClient } func addGlobalAttribute(_ attribute: AttributeValue, forKey key: String) { @@ -116,7 +114,7 @@ class AnalyticsClient: AnalyticsClientBehaviour { let event = ClickstreamEvent(eventType: eventType, appId: clickstream.configuration.appId, uniqueId: clickstream.userUniqueId, - session: sessionProvider(), + session: sessionClient.getCurrentSession(), systemInfo: clickstream.systemInfo, netWorkType: clickstream.networkMonitor.netWorkType) return event @@ -135,15 +133,13 @@ class AnalyticsClient: AnalyticsClientBehaviour { for (key, attribute) in globalAttributes { event.addGlobalAttribute(attribute, forKey: key) } - if let autoRecordClient { - if autoRecordClient.lastScreenName != nil { - event.addGlobalAttribute(autoRecordClient.lastScreenName!, - forKey: Event.ReservedAttribute.SCREEN_NAME) - } - if autoRecordClient.lastScreenUniqueId != nil { - event.addGlobalAttribute(autoRecordClient.lastScreenUniqueId!, - forKey: Event.ReservedAttribute.SCREEN_UNIQUEID) - } + if autoRecordClient.lastScreenName != nil { + event.addGlobalAttribute(autoRecordClient.lastScreenName!, + forKey: Event.ReservedAttribute.SCREEN_NAME) + } + if autoRecordClient.lastScreenUniqueId != nil { + event.addGlobalAttribute(autoRecordClient.lastScreenUniqueId!, + forKey: Event.ReservedAttribute.SCREEN_UNIQUEID) } if event.eventType == Event.PresetEvent.PROFILE_SET { event.setUserAttribute(allUserAttributes) diff --git a/Sources/Clickstream/Dependency/Clickstream/AutoRecord/AutoRecordEventClient.swift b/Sources/Clickstream/Dependency/Clickstream/AutoRecord/AutoRecordEventClient.swift index 345fabe..d652c3b 100644 --- a/Sources/Clickstream/Dependency/Clickstream/AutoRecord/AutoRecordEventClient.swift +++ b/Sources/Clickstream/Dependency/Clickstream/AutoRecord/AutoRecordEventClient.swift @@ -48,8 +48,10 @@ class AutoRecordEventClient { if !clickstream.configuration.isTrackScreenViewEvents { return } - let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.SCREEN_VIEW) - recordScreenViewEvent(event, screenName, screenPath, screenUniqueId) + if clickstream.analyticsClient != nil { + let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.SCREEN_VIEW) + recordScreenViewEvent(event, screenName, screenPath, screenUniqueId) + } } func recordViewScreenManually(_ event: ClickstreamEvent) { @@ -176,17 +178,18 @@ class AutoRecordEventClient { } } - func handleAppStart() { - if isFirstTime { - checkAppVersionUpdate(clickstream: clickstream) - checkOSVersionUpdate(clickstream: clickstream) - } + func handleFirstOpen() { + checkAppVersionUpdate(clickstream: clickstream) + checkOSVersionUpdate(clickstream: clickstream) if isFirstOpen { let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.FIRST_OPEN) recordEvent(event) UserDefaultsUtil.saveIsFirstOpen(storage: clickstream.storage, isFirstOpen: "false") isFirstOpen = false } + } + + func handleAppStart() { let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.APP_START) event.addAttribute(isFirstTime, forKey: Event.ReservedAttribute.IS_FIRST_TIME) recordEvent(event) @@ -229,7 +232,7 @@ class AutoRecordEventClient { func recordEvent(_ event: ClickstreamEvent) { do { - try clickstream.analyticsClient.record(event) + try clickstream.analyticsClient?.record(event) } catch { log.error("Failed to record event with error:\(error)") } diff --git a/Sources/Clickstream/Dependency/Clickstream/Session/Session.swift b/Sources/Clickstream/Dependency/Clickstream/Session/Session.swift index 1b78729..9397f87 100644 --- a/Sources/Clickstream/Dependency/Clickstream/Session/Session.swift +++ b/Sources/Clickstream/Dependency/Clickstream/Session/Session.swift @@ -12,6 +12,7 @@ class Session: Codable { let startTime: Int64 let sessionIndex: Int private(set) var pauseTime: Int64? + var isRecorded = false init(uniqueId: String, sessionIndex: Int) { self.sessionId = Self.generateSessionId(uniqueId: uniqueId) @@ -27,19 +28,22 @@ class Session: Codable { self.sessionIndex = sessionIndex } - static func getCurrentSession(clickstream: ClickstreamContext) -> Session { - let storedSession = UserDefaultsUtil.getSession(storage: clickstream.storage) - var sessionIndex = 1 - if storedSession != nil { - if Date().millisecondsSince1970 - storedSession!.pauseTime! + static func getCurrentSession(clickstream: ClickstreamContext, previousSession: Session? = nil) -> Session { + var session = previousSession + if session == nil { + session = UserDefaultsUtil.getSession(storage: clickstream.storage) + } + if session != nil { + if session!.isNewSession || Date().millisecondsSince1970 - session!.pauseTime! < clickstream.configuration.sessionTimeoutDuration { - return storedSession! + return session! } else { - sessionIndex = storedSession!.sessionIndex + 1 + return Session(uniqueId: clickstream.userUniqueId, sessionIndex: session!.sessionIndex + 1) } + } else { + return Session(uniqueId: clickstream.userUniqueId, sessionIndex: 1) } - return Session(uniqueId: clickstream.userUniqueId, sessionIndex: sessionIndex) } var isNewSession: Bool { diff --git a/Sources/Clickstream/Dependency/Clickstream/Session/SessionClient.swift b/Sources/Clickstream/Dependency/Clickstream/Session/SessionClient.swift index 896e32e..29e3166 100644 --- a/Sources/Clickstream/Dependency/Clickstream/Session/SessionClient.swift +++ b/Sources/Clickstream/Dependency/Clickstream/Session/SessionClient.swift @@ -10,13 +10,12 @@ import Foundation protocol SessionClientBehaviour: AnyObject { func startActivityTracking() - func initialSession() func storeSession() func onManualScreenView(_ event: ClickstreamEvent) } class SessionClient: SessionClientBehaviour { - private var session: Session? + private var session: Session private let clickstream: ClickstreamContext private let activityTracker: ActivityTrackerBehaviour private let sessionClientQueue = DispatchQueue(label: Constants.queue, @@ -25,9 +24,9 @@ class SessionClient: SessionClientBehaviour { init(activityTracker: ActivityTrackerBehaviour? = nil, clickstream: ClickstreamContext) { self.clickstream = clickstream - self.activityTracker = activityTracker ?? ActivityTracker() + self.session = Session.getCurrentSession(clickstream: clickstream) self.autoRecordClient = AutoRecordEventClient(clickstream: clickstream) - startActivityTracking() + self.activityTracker = activityTracker ?? ActivityTracker() } func startActivityTracking() { @@ -39,31 +38,31 @@ class SessionClient: SessionClientBehaviour { } } - func initialSession() { - session = Session.getCurrentSession(clickstream: clickstream) - if session!.isNewSession { - autoRecordClient.recordSessionStartEvent() - autoRecordClient.setIsEntrances() - autoRecordClient.recordScreenViewAfterSessionStart() - } - } - func storeSession() { - if session != nil { - session?.pause() - UserDefaultsUtil.saveSession(storage: clickstream.storage, session: session!) - } + session.pause() + UserDefaultsUtil.saveSession(storage: clickstream.storage, session: session) } - func getCurrentSession() -> Session? { + func getCurrentSession() -> Session { session } private func handleAppEnterForeground() { log.debug("Application entered the foreground.") + autoRecordClient.handleFirstOpen() + session = Session.getCurrentSession(clickstream: clickstream, previousSession: session) autoRecordClient.handleAppStart() autoRecordClient.updateLastScreenStartTimestamp(Date().millisecondsSince1970) - initialSession() + handleSesionStart() + } + + private func handleSesionStart() { + if session.isNewSession, !session.isRecorded { + autoRecordClient.recordSessionStartEvent() + autoRecordClient.setIsEntrances() + autoRecordClient.recordScreenViewAfterSessionStart() + session.isRecorded = true + } } private func handleAppEnterBackground() { diff --git a/Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift b/Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift index afe31d8..f09ec5a 100644 --- a/Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift +++ b/Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift @@ -24,15 +24,16 @@ class AnalyticsClientTest: XCTestCase { isTrackAppExceptionEvents: false, isCompressEvents: false) clickstream = try ClickstreamContext(with: contextConfiguration) + let sessionClient = SessionClient(clickstream: clickstream) + clickstream.sessionClient = sessionClient + clickstream.networkMonitor = MockNetworkMonitor() eventRecorder = MockEventRecorder() - session = Session(uniqueId: "uniqueId", sessionIndex: 1) + session = sessionClient.getCurrentSession() analyticsClient = try AnalyticsClient( clickstream: clickstream, eventRecorder: eventRecorder, - sessionProvider: { - self.session - } + sessionClient: sessionClient ) } diff --git a/Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift b/Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift index a24bf0a..47490c3 100644 --- a/Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift +++ b/Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift @@ -35,7 +35,7 @@ class AutoRecordEventClientTest: XCTestCase { let analyticsClient = try AnalyticsClient( clickstream: clickstream, eventRecorder: eventRecorder, - sessionProvider: { nil } + sessionClient: sessionClient ) clickstream.analyticsClient = analyticsClient @@ -78,6 +78,7 @@ class AutoRecordEventClientTest: XCTestCase { } func testOneScreenView() { + sessionClient.startActivityTracking() activityTracker.callback?(.runningInForeground) autoRecordEventClient.setIsEntrances() let viewController = MockViewControllerA() diff --git a/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift b/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift index 45dd280..7840c2f 100644 --- a/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift +++ b/Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift @@ -22,6 +22,7 @@ class EventRecorderTest: XCTestCase { var clickstream: ClickstreamContext! var server: HttpServer! var activityTracker: MockActivityTracker! + var sessionClient: SessionClient! override func setUp() async throws { do { @@ -56,18 +57,12 @@ class EventRecorderTest: XCTestCase { dbUtil = eventRecorder.dbUtil activityTracker = MockActivityTracker() - let sessionClient = SessionClient(activityTracker: activityTracker, clickstream: clickstream) + sessionClient = SessionClient(activityTracker: activityTracker, clickstream: clickstream) clickstream.sessionClient = sessionClient - let sessionProvider: () -> Session? = { [weak sessionClient] in - guard let sessionClient else { - fatalError("SessionClient was deallocated") - } - return sessionClient.getCurrentSession() - } let analyticsClient = try AnalyticsClient( clickstream: clickstream, eventRecorder: eventRecorder, - sessionProvider: sessionProvider + sessionClient: sessionClient ) clickstream.analyticsClient = analyticsClient clickstream.networkMonitor = MockNetworkMonitor() @@ -408,6 +403,7 @@ class EventRecorderTest: XCTestCase { } func testBackgroundModeAutoSendRequest() throws { + sessionClient.startActivityTracking() activityTracker.callback?(.runningInForeground) try eventRecorder.save(clickstreamEvent) activityTracker.callback?(.runningInBackground) diff --git a/Tests/ClickstreamTests/Clickstream/SessionClientTests.swift b/Tests/ClickstreamTests/Clickstream/SessionClientTests.swift index caa4b15..281f3be 100644 --- a/Tests/ClickstreamTests/Clickstream/SessionClientTests.swift +++ b/Tests/ClickstreamTests/Clickstream/SessionClientTests.swift @@ -34,19 +34,13 @@ class SessionClientTests: XCTestCase { eventRecorder = MockEventRecorder() - let sessionProvider: () -> Session? = { [weak sessionClient] in - guard let sessionClient else { - fatalError("SessionClient was deallocated") - } - return sessionClient.getCurrentSession() - } - analyticsClient = try AnalyticsClient( clickstream: clickstream, eventRecorder: eventRecorder, - sessionProvider: sessionProvider + sessionClient: sessionClient ) clickstream.analyticsClient = analyticsClient + sessionClient.startActivityTracking() } override func tearDown() { @@ -65,9 +59,8 @@ class SessionClientTests: XCTestCase { } func testRunningInForeground() { - XCTAssertTrue(sessionClient.getCurrentSession() == nil) activityTracker.callback?(.runningInForeground) - let session = sessionClient.getCurrentSession()! + let session = sessionClient.getCurrentSession() XCTAssertTrue(session.isNewSession) XCTAssertTrue(session.sessionIndex == 1) XCTAssertNotNil(session.sessionId) @@ -82,11 +75,10 @@ class SessionClientTests: XCTestCase { } func testGoBackground() { - XCTAssertTrue(sessionClient.getCurrentSession() == nil) activityTracker.callback?(.runningInForeground) Thread.sleep(forTimeInterval: 0.1) activityTracker.callback?(.runningInBackground) - let session = sessionClient.getCurrentSession()! + let session = sessionClient.getCurrentSession() XCTAssertTrue(session.pauseTime != nil) let storedSession = UserDefaultsUtil.getSession(storage: clickstream.storage) XCTAssertTrue(storedSession != nil) @@ -111,7 +103,7 @@ class SessionClientTests: XCTestCase { sessionClient.autoRecordClient.updateLastScreenStartTimestamp(Date().millisecondsSince1970 - 1_100) activityTracker.callback?(.runningInBackground) - let session = sessionClient.getCurrentSession()! + let session = sessionClient.getCurrentSession() XCTAssertTrue(session.pauseTime != nil) let storedSession = UserDefaultsUtil.getSession(storage: clickstream.storage) XCTAssertTrue(storedSession != nil) @@ -130,11 +122,11 @@ class SessionClientTests: XCTestCase { func testReturnToForeground() { activityTracker.callback?(.runningInForeground) - let session1 = sessionClient.getCurrentSession()! + let session1 = sessionClient.getCurrentSession() XCTAssertTrue(session1.isNewSession) activityTracker.callback?(.runningInBackground) activityTracker.callback?(.runningInForeground) - let session2 = sessionClient.getCurrentSession()! + let session2 = sessionClient.getCurrentSession() XCTAssertTrue(session1.sessionId == session2.sessionId) XCTAssertFalse(session2.isNewSession) } @@ -142,11 +134,11 @@ class SessionClientTests: XCTestCase { func testReturnToForegroundWithSessionTimeout() { clickstream.configuration.sessionTimeoutDuration = 0 activityTracker.callback?(.runningInForeground) - let session1 = sessionClient.getCurrentSession()! + let session1 = sessionClient.getCurrentSession() XCTAssertTrue(session1.isNewSession) activityTracker.callback?(.runningInBackground) activityTracker.callback?(.runningInForeground) - let session2 = sessionClient.getCurrentSession()! + let session2 = sessionClient.getCurrentSession() XCTAssertTrue(session1.sessionIndex != session2.sessionIndex) XCTAssertTrue(session2.isNewSession) XCTAssertTrue(session2.sessionIndex == 2) @@ -194,6 +186,7 @@ class SessionClientTests: XCTestCase { window.rootViewController = viewController window.makeKeyAndVisible() activityTracker.callback?(.runningInBackground) + Thread.sleep(forTimeInterval: 0.01) activityTracker.callback?(.runningInForeground) Thread.sleep(forTimeInterval: 0.1) let events = eventRecorder.savedEvents @@ -205,8 +198,8 @@ class SessionClientTests: XCTestCase { 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]) @@ -216,6 +209,28 @@ class SessionClientTests: XCTestCase { XCTAssertEqual(1, events[7].attributes[Event.ReservedAttribute.ENTRANCES] as! Int) } + func testEventsHaveTheSameSessionId() { + 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) + Thread.sleep(forTimeInterval: 0.1) + let events = eventRecorder.savedEvents + var sessionIdSet = Set() + 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) + for event in events { + sessionIdSet.insert(event.session?.sessionId ?? "") + } + XCTAssertEqual(1, sessionIdSet.count) + } + func testLastScreenStartTimeStampUpdatedAfterReturnToForeground() { activityTracker.callback?(.runningInForeground) let viewController = MockViewControllerA() @@ -232,6 +247,7 @@ class SessionClientTests: XCTestCase { clickstream.isEnable = false activityTracker.callback?(.runningInForeground) Thread.sleep(forTimeInterval: 0.1) + activityTracker.callback?(.runningInBackground) let events = eventRecorder.savedEvents XCTAssertEqual(0, events.count) } diff --git a/Tests/ClickstreamTests/ClickstreamPluginTestBase.swift b/Tests/ClickstreamTests/ClickstreamPluginTestBase.swift index f321e30..842169d 100644 --- a/Tests/ClickstreamTests/ClickstreamPluginTestBase.swift +++ b/Tests/ClickstreamTests/ClickstreamPluginTestBase.swift @@ -28,17 +28,11 @@ class ClickstreamPluginTestBase: XCTestCase { let sessionClient = SessionClient(clickstream: clickstream) clickstream.sessionClient = sessionClient - let sessionProvider: () -> Session? = { [weak sessionClient] in - guard let sessionClient else { - fatalError("SessionClient was deallocated") - } - return sessionClient.getCurrentSession() - } let eventRecorder = try EventRecorder(clickstream: clickstream) let analyticsClient = try AnalyticsClient(clickstream: clickstream, eventRecorder: eventRecorder, - sessionProvider: sessionProvider) + sessionClient: sessionClient ) analyticsPlugin.analyticsClient = analyticsClient clickstream.analyticsClient = analyticsClient clickstream.networkMonitor = mockNetworkMonitor diff --git a/Tests/ClickstreamTests/IntegrationTest.swift b/Tests/ClickstreamTests/IntegrationTest.swift index 8d47431..c491de3 100644 --- a/Tests/ClickstreamTests/IntegrationTest.swift +++ b/Tests/ClickstreamTests/IntegrationTest.swift @@ -69,7 +69,7 @@ class IntegrationTest: XCTestCase { ]) Thread.sleep(forTimeInterval: 0.2) let eventCount = try eventRecorder.dbUtil.getEventCount() - XCTAssertEqual(2, eventCount) + XCTAssertEqual(1, eventCount) } func testRecordEventWithItem() throws { @@ -95,7 +95,7 @@ class IntegrationTest: XCTestCase { ClickstreamAnalytics.recordEvent("testEvent") Thread.sleep(forTimeInterval: 0.2) let eventCount = try eventRecorder.dbUtil.getEventCount() - XCTAssertEqual(2, eventCount) + XCTAssertEqual(1, eventCount) } func testRecordCustomScreenViewEvent() throws { @@ -108,7 +108,7 @@ class IntegrationTest: XCTestCase { let attributes = event["attributes"] as! [String: Any] XCTAssertEqual("HomeView", attributes[ClickstreamAnalytics.Attr.SCREEN_NAME] as! String) XCTAssertEqual("23ac31df", attributes[ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID] as! String) - XCTAssertEqual(1, attributes[Event.ReservedAttribute.ENTRANCES] as! Int) + XCTAssertEqual(0, attributes[Event.ReservedAttribute.ENTRANCES] as! Int) } func testFlushEvents() throws { @@ -258,7 +258,7 @@ class IntegrationTest: XCTestCase { ClickstreamAnalytics.recordEvent("testEvent") Thread.sleep(forTimeInterval: 0.2) let eventCount = try eventRecorder.dbUtil.getEventCount() - XCTAssertEqual(2, eventCount) + XCTAssertEqual(1, eventCount) } func testModifyConfiguration() throws { @@ -271,7 +271,7 @@ class IntegrationTest: XCTestCase { ]) Thread.sleep(forTimeInterval: 0.2) let eventCount = try eventRecorder.dbUtil.getEventCount() - XCTAssertEqual(2, eventCount) + XCTAssertEqual(1, eventCount) } func testDisableSDKWillNotRecordCustomEvents() throws { @@ -279,7 +279,7 @@ class IntegrationTest: XCTestCase { ClickstreamAnalytics.recordEvent("testEvent") Thread.sleep(forTimeInterval: 0.1) let eventCount = try eventRecorder.dbUtil.getEventCount() - XCTAssertEqual(1, eventCount) + XCTAssertEqual(0, eventCount) } func testDisableSDKWillNotRecordExceptionEvents() throws { @@ -288,7 +288,7 @@ class IntegrationTest: XCTestCase { AutoRecordEventClient.handleException(exception) Thread.sleep(forTimeInterval: 0.5) let eventArray = try eventRecorder.getBatchEvent().eventsJson.jsonArray() - XCTAssertEqual(1, eventArray.count) + XCTAssertEqual(0, eventArray.count) } func testDisableAndEnableSDKTwice() throws { @@ -300,7 +300,7 @@ class IntegrationTest: XCTestCase { ClickstreamAnalytics.recordEvent("testEvent") Thread.sleep(forTimeInterval: 0.1) let eventCount = try eventRecorder.dbUtil.getEventCount() - XCTAssertEqual(2, eventCount) + XCTAssertEqual(1, eventCount) } // MARK: - Objc test @@ -316,7 +316,7 @@ class IntegrationTest: XCTestCase { ClickstreamObjc.recordEvent("testEvent", attribute) Thread.sleep(forTimeInterval: 0.2) let eventCount = try eventRecorder.dbUtil.getEventCount() - XCTAssertEqual(3, eventCount) + XCTAssertEqual(2, eventCount) } func testRecordEventWithItemForObjc() throws { @@ -351,7 +351,7 @@ class IntegrationTest: XCTestCase { let attributes = event["attributes"] as! [String: Any] XCTAssertEqual("HomeView", attributes[ClickstreamAnalytics.Attr.SCREEN_NAME] as! String) XCTAssertEqual("23ac31df", attributes[ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID] as! String) - XCTAssertEqual(1, attributes[Event.ReservedAttribute.ENTRANCES] as! Int) + XCTAssertEqual(0, attributes[Event.ReservedAttribute.ENTRANCES] as! Int) } func testGlobalAttributeForObjc() throws { @@ -400,7 +400,7 @@ class IntegrationTest: XCTestCase { ClickstreamAnalytics.recordEvent("testEvent") Thread.sleep(forTimeInterval: 0.2) let eventCount = try eventRecorder.dbUtil.getEventCount() - XCTAssertEqual(2, eventCount) + XCTAssertEqual(1, eventCount) } func testDisableAndEnableSDKForObjc() throws { @@ -410,14 +410,14 @@ class IntegrationTest: XCTestCase { ClickstreamObjc.recordEvent("testEvent") Thread.sleep(forTimeInterval: 0.1) let eventCount = try eventRecorder.dbUtil.getEventCount() - XCTAssertEqual(2, eventCount) + XCTAssertEqual(1, eventCount) } func testAppException() throws { let exception = NSException(name: NSExceptionName("TestException"), reason: "Testing", userInfo: nil) AutoRecordEventClient.handleException(exception) Thread.sleep(forTimeInterval: 0.5) - let event = try eventRecorder.getBatchEvent().eventsJson.jsonArray()[1] + let event = try eventRecorder.getBatchEvent().eventsJson.jsonArray()[0] XCTAssertTrue(event["event_type"] as! String == Event.PresetEvent.APP_EXCEPTION) let attributes = event["attributes"] as! [String: Any] XCTAssertTrue(attributes[Event.ReservedAttribute.EXCEPTION_NAME] as! String == exception.name.rawValue)