Skip to content

Commit 42b7d44

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: support enable and disable sdk (#39)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 3be024e commit 42b7d44

11 files changed

+107
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ import Clickstream
207207
ClickstreamAnalytics.flushEvents()
208208
```
209209

210+
#### Disable SDK
211+
212+
You can disable the SDK in the scenario you need. After disabling the SDK, the SDK will not handle the logging and sending of any events. Of course you can enable the SDK when you need to continue logging events.
213+
214+
```swift
215+
import Clickstream
216+
217+
// disable SDK
218+
ClickstreamAnalytics.disable()
219+
220+
// enable SDK
221+
ClickstreamAnalytics.enable()
222+
```
223+
210224
## How to build&test locally
211225

212226
### Config your code format

Sources/Clickstream/AWSClickstreamPlugin+ClientBehavior.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,16 @@ extension AWSClickstreamPlugin {
9191
}
9292

9393
func enable() {
94+
if isEnabled { return }
95+
self.autoFlushEventsTimer?.resume()
96+
clickstream.isEnable = true
9497
isEnabled = true
9598
}
9699

97100
func disable() {
101+
if !isEnabled { return }
98102
isEnabled = false
103+
clickstream.isEnable = false
104+
self.autoFlushEventsTimer?.suspend()
99105
}
100106
}

Sources/Clickstream/ClickstreamAnalytics.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,17 @@ public enum ClickstreamAnalytics {
7373
return (plugin as! AWSClickstreamPlugin).getEscapeHatch().configuration
7474
// swiftlint:enable force_cast
7575
}
76+
77+
/// Disable the SDK
78+
/// - Parameter userId: current userId, nil for logout
79+
public static func disable() {
80+
Amplify.Analytics.disable()
81+
}
82+
83+
84+
/// Enable the SDK
85+
/// - Parameter userId: current userId, nil for logout
86+
public static func enable() {
87+
Amplify.Analytics.enable()
88+
}
7689
}

Sources/Clickstream/ClickstreamObjc.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ import Foundation
7171
try ClickstreamAnalytics.getClickstreamConfiguration()
7272
}
7373

74+
/// Disable the SDK
75+
public static func disable() {
76+
ClickstreamAnalytics.disable()
77+
}
78+
79+
/// Enable the SDK
80+
public static func enable() {
81+
ClickstreamAnalytics.enable()
82+
}
83+
7484
private static func getAttributes(_ attributes: NSDictionary) -> ClickstreamAttribute {
7585
var result: ClickstreamAttribute = [:]
7686
for case let (key as String, value) in attributes {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class AutoRecordEventClient {
3535
}
3636

3737
func onViewDidAppear(screenName: String, screenPath: String, screenHashValue: String) {
38+
if !clickstream.isEnable { return }
3839
if !isSameScreen(screenName, screenPath, screenHashValue) {
3940
if lastScreenName != nil {
4041
recordUserEngagement()

Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ class ClickstreamContext {
8989
var configuration: ClickstreamContextConfiguration
9090
var userUniqueId: String
9191
let storage: ClickstreamContextStorage
92+
var isEnable: Bool
9293

9394
init(with configuration: ClickstreamContextConfiguration,
9495
userDefaults: UserDefaultsBehaviour = UserDefaults.standard) throws
@@ -97,5 +98,6 @@ class ClickstreamContext {
9798
self.userUniqueId = UserDefaultsUtil.getCurrentUserUniqueId(storage: storage)
9899
self.systemInfo = SystemInfo(storage: storage)
99100
self.configuration = configuration
101+
self.isEnable = true
100102
}
101103
}

Sources/Clickstream/Dependency/Clickstream/Session/SessionClient.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class SessionClient: SessionClientBehaviour {
7373
}
7474

7575
private func respond(to newState: ApplicationState) {
76+
if !clickstream.isEnable { return }
7677
switch newState {
7778
case .runningInForeground:
7879
handleAppEnterForeground()

Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,15 @@ class AutoRecordEventClientTest: XCTestCase {
197197
window.makeKeyAndVisible()
198198
XCTAssertNotEqual(Event.PresetEvent.USER_ENGAGEMENT, eventRecorder.savedEvents[1].eventType)
199199
}
200+
201+
func testDisableSDKWillNotRecordScreenViewEvents() {
202+
clickstream.isEnable = false
203+
activityTracker.callback?(.runningInForeground)
204+
autoRecordEventClient.setIsEntrances()
205+
let viewController = MockViewControllerA()
206+
let window = UIWindow(frame: UIScreen.main.bounds)
207+
window.rootViewController = viewController
208+
window.makeKeyAndVisible()
209+
XCTAssertTrue(eventRecorder.saveCount == 0)
210+
}
200211
}

Tests/ClickstreamTests/Clickstream/SessionClientTests.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,4 +197,12 @@ class SessionClientTests: XCTestCase {
197197
activityTracker.callback?(.runningInForeground)
198198
XCTAssertTrue(Date().millisecondsSince1970 - sessionClient.autoRecordClient.lastScreenStartTimestamp < 200)
199199
}
200+
201+
func testDisableSDKWillNotRecordSessionEvents() {
202+
clickstream.isEnable = false
203+
activityTracker.callback?(.runningInForeground)
204+
Thread.sleep(forTimeInterval: 0.1)
205+
let events = eventRecorder.savedEvents
206+
XCTAssertEqual(0, events.count)
207+
}
200208
}

Tests/ClickstreamTests/ClickstreamPluginBehaviorTest.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class ClickstreamPluginBehaviorTest: ClickstreamPluginTestBase {
2222
try await super.setUp()
2323
analyticsClient = MockAnalyticsClient()
2424
analyticsPlugin.analyticsClient = analyticsClient
25+
analyticsPlugin.clickstream = clickstream
2526
}
2627

2728
func testIdentifyUser() {

0 commit comments

Comments
 (0)