Skip to content

Commit b5bb3d8

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: disable record screenview event if it's no enabled (#14)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent acefcae commit b5bb3d8

File tree

4 files changed

+36
-5
lines changed

4 files changed

+36
-5
lines changed

README.md

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Your `appId` and `endpoint` are already set up in it, here's an explanation of e
6262

6363
**3.Initialize the SDK**
6464

65-
Once you have configured the parameters, you need to initialize it in AppDelegate's `didFinishLaunchingWithOptions` lifecycle method to use the SDK.
65+
Once you have configured the parameters, you need to initialize it in your app delegate's `application(_:didFinishLaunchingWithOptions:)` lifecycle method:
6666

6767
```swift
6868
import Clickstream
@@ -78,7 +78,23 @@ func application(_ application: UIApplication, didFinishLaunchingWithOptions lau
7878
}
7979
```
8080

81-
**4.Config the SDK**
81+
If your project is developed with SwiftUI, you need to create an application delegate and attach it to your `App` through `UIApplicationDelegateAdaptor`.
82+
83+
```swift
84+
@main
85+
struct YourApp: App {
86+
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
87+
var body: some Scene {
88+
WindowGroup {
89+
YourView()
90+
}
91+
}
92+
}
93+
```
94+
95+
You also need to disable swzzling by setting `configuration.isTrackScreenViewEvents = false`, see the next configuration steps.
96+
97+
**4.Configure the SDK**
8298

8399
```swift
84100
import Clickstream
@@ -90,7 +106,7 @@ do {
90106
configuration.endpoint = "https://example.com/collect"
91107
configuration.authCookie = "your authentication cookie"
92108
configuration.sessionTimeoutDuration = 1800000
93-
configuration.isTrackAppExceptionEvents = false
109+
configuration.isTrackScreenViewEvents = true
94110
configuration.isLogEvents = true
95111
configuration.isCompressEvents = true
96112
configuration.isLogEvents = true

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class AutoRecordEventClient {
9292
}
9393

9494
func onViewDidAppear(screenName: String, screenPath: String) {
95+
if !clickstream.configuration.isTrackScreenViewEvents {
96+
return
97+
}
9598
let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.SCREEN_VIEW)
9699
event.addAttribute(screenName, forKey: Event.ReservedAttribute.SCREEN_NAME)
97100
event.addAttribute(screenPath, forKey: Event.ReservedAttribute.SCREEN_ID)

Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extension UserDefaults: UserDefaultsBehaviour {
5858
endpoint: String,
5959
sendEventsInterval: Int,
6060
isTrackAppExceptionEvents: Bool = true,
61-
isTrackAppLifecycleEvents: Bool = true,
61+
isTrackScreenViewEvents: Bool = true,
6262
isCompressEvents: Bool = true,
6363
isLogEvents: Bool = false,
6464
sessionTimeoutDuration: Int64 = 1_800_000)
@@ -67,7 +67,7 @@ extension UserDefaults: UserDefaultsBehaviour {
6767
self.endpoint = endpoint
6868
self.sendEventsInterval = sendEventsInterval
6969
self.isTrackAppExceptionEvents = isTrackAppExceptionEvents
70-
self.isTrackScreenViewEvents = isTrackAppLifecycleEvents
70+
self.isTrackScreenViewEvents = isTrackScreenViewEvents
7171
self.isCompressEvents = isCompressEvents
7272
self.isLogEvents = isLogEvents
7373
self.sessionTimeoutDuration = sessionTimeoutDuration

Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,16 @@ class AutoRecordEventClientTest: XCTestCase {
115115

116116
XCTAssertTrue(eventRecorder.lastSavedEvent!.attributes[Event.ReservedAttribute.ENTRANCES] as! Int == 0)
117117
}
118+
119+
func testCloseRecordScreenView() {
120+
clickstream.configuration.isTrackScreenViewEvents = false
121+
autoRecordEventClient.updateEngageTimestamp()
122+
autoRecordEventClient.setIsEntrances()
123+
let viewController = MockViewControllerA()
124+
let window = UIWindow(frame: UIScreen.main.bounds)
125+
window.rootViewController = viewController
126+
window.makeKeyAndVisible()
127+
XCTAssertTrue(viewController.viewDidAppearCalled)
128+
XCTAssertTrue(eventRecorder.saveCount == 0)
129+
}
118130
}

0 commit comments

Comments
 (0)