Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support enable and disable sdk #39

Merged
merged 1 commit into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,20 @@ import Clickstream
ClickstreamAnalytics.flushEvents()
```

#### Disable SDK

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.

```swift
import Clickstream

// disable SDK
ClickstreamAnalytics.disable()

// enable SDK
ClickstreamAnalytics.enable()
```

## How to build&test locally

### Config your code format
Expand Down
6 changes: 6 additions & 0 deletions Sources/Clickstream/AWSClickstreamPlugin+ClientBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,16 @@ extension AWSClickstreamPlugin {
}

func enable() {
if isEnabled { return }
self.autoFlushEventsTimer?.resume()
clickstream.isEnable = true
isEnabled = true
}

func disable() {
if !isEnabled { return }
isEnabled = false
clickstream.isEnable = false
self.autoFlushEventsTimer?.suspend()
}
}
13 changes: 13 additions & 0 deletions Sources/Clickstream/ClickstreamAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,17 @@ public enum ClickstreamAnalytics {
return (plugin as! AWSClickstreamPlugin).getEscapeHatch().configuration
// swiftlint:enable force_cast
}

/// Disable the SDK
/// - Parameter userId: current userId, nil for logout
public static func disable() {
Amplify.Analytics.disable()
}


/// Enable the SDK
/// - Parameter userId: current userId, nil for logout
public static func enable() {
Amplify.Analytics.enable()
}
}
10 changes: 10 additions & 0 deletions Sources/Clickstream/ClickstreamObjc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ import Foundation
try ClickstreamAnalytics.getClickstreamConfiguration()
}

/// Disable the SDK
public static func disable() {
ClickstreamAnalytics.disable()
}

/// Enable the SDK
public static func enable() {
ClickstreamAnalytics.enable()
}

private static func getAttributes(_ attributes: NSDictionary) -> ClickstreamAttribute {
var result: ClickstreamAttribute = [:]
for case let (key as String, value) in attributes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class AutoRecordEventClient {
}

func onViewDidAppear(screenName: String, screenPath: String, screenHashValue: String) {
if !clickstream.isEnable { return }
if !isSameScreen(screenName, screenPath, screenHashValue) {
if lastScreenName != nil {
recordUserEngagement()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class ClickstreamContext {
var configuration: ClickstreamContextConfiguration
var userUniqueId: String
let storage: ClickstreamContextStorage
var isEnable: Bool

init(with configuration: ClickstreamContextConfiguration,
userDefaults: UserDefaultsBehaviour = UserDefaults.standard) throws
Expand All @@ -97,5 +98,6 @@ class ClickstreamContext {
self.userUniqueId = UserDefaultsUtil.getCurrentUserUniqueId(storage: storage)
self.systemInfo = SystemInfo(storage: storage)
self.configuration = configuration
self.isEnable = true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class SessionClient: SessionClientBehaviour {
}

private func respond(to newState: ApplicationState) {
if !clickstream.isEnable { return }
switch newState {
case .runningInForeground:
handleAppEnterForeground()
Expand Down
11 changes: 11 additions & 0 deletions Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,15 @@ class AutoRecordEventClientTest: XCTestCase {
window.makeKeyAndVisible()
XCTAssertNotEqual(Event.PresetEvent.USER_ENGAGEMENT, eventRecorder.savedEvents[1].eventType)
}

func testDisableSDKWillNotRecordScreenViewEvents() {
clickstream.isEnable = false
activityTracker.callback?(.runningInForeground)
autoRecordEventClient.setIsEntrances()
let viewController = MockViewControllerA()
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = viewController
window.makeKeyAndVisible()
XCTAssertTrue(eventRecorder.saveCount == 0)
}
}
8 changes: 8 additions & 0 deletions Tests/ClickstreamTests/Clickstream/SessionClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,12 @@ class SessionClientTests: XCTestCase {
activityTracker.callback?(.runningInForeground)
XCTAssertTrue(Date().millisecondsSince1970 - sessionClient.autoRecordClient.lastScreenStartTimestamp < 200)
}

func testDisableSDKWillNotRecordSessionEvents() {
clickstream.isEnable = false
activityTracker.callback?(.runningInForeground)
Thread.sleep(forTimeInterval: 0.1)
let events = eventRecorder.savedEvents
XCTAssertEqual(0, events.count)
}
}
1 change: 1 addition & 0 deletions Tests/ClickstreamTests/ClickstreamPluginBehaviorTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class ClickstreamPluginBehaviorTest: ClickstreamPluginTestBase {
try await super.setUp()
analyticsClient = MockAnalyticsClient()
analyticsPlugin.analyticsClient = analyticsClient
analyticsPlugin.clickstream = clickstream
}

func testIdentifyUser() {
Expand Down
40 changes: 40 additions & 0 deletions Tests/ClickstreamTests/IntegrationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class IntegrationTest: XCTestCase {
}

override func tearDown() async throws {
ClickstreamAnalytics.enable()
await Amplify.reset()
analyticsPlugin.reset()
server.stop()
Expand Down Expand Up @@ -236,6 +237,35 @@ class IntegrationTest: XCTestCase {
XCTAssertEqual(2, eventCount)
}

func testDisableSDKWillNotRecordCustomEvents() throws {
ClickstreamAnalytics.disable()
ClickstreamAnalytics.recordEvent("testEvent")
Thread.sleep(forTimeInterval: 0.1)
let eventCount = try eventRecorder.dbUtil.getEventCount()
XCTAssertEqual(1, eventCount)
}

func testDisableSDKWillNotRecordExceptionEvents() throws {
ClickstreamAnalytics.disable()
let exception = NSException(name: NSExceptionName("TestException"), reason: "Testing", userInfo: nil)
AutoRecordEventClient.handleException(exception)
Thread.sleep(forTimeInterval: 0.5)
let eventArray = try eventRecorder.getBatchEvent().eventsJson.jsonArray()
XCTAssertEqual(1, eventArray.count)
}

func testDisableAndEnableSDKTwice() throws {
ClickstreamAnalytics.disable()
ClickstreamAnalytics.disable()
ClickstreamAnalytics.recordEvent("testEvent")
ClickstreamAnalytics.enable()
ClickstreamAnalytics.enable()
ClickstreamAnalytics.recordEvent("testEvent")
Thread.sleep(forTimeInterval: 0.1)
let eventCount = try eventRecorder.dbUtil.getEventCount()
XCTAssertEqual(2, eventCount)
}

// MARK: - Objc test

func testRecordEventForObjc() throws {
Expand Down Expand Up @@ -301,6 +331,16 @@ class IntegrationTest: XCTestCase {
XCTAssertEqual(2, eventCount)
}

func testDisableAndEnableSDKForObjc() throws {
ClickstreamObjc.disable()
ClickstreamObjc.recordEvent("testEvent")
ClickstreamObjc.enable()
ClickstreamObjc.recordEvent("testEvent")
Thread.sleep(forTimeInterval: 0.1)
let eventCount = try eventRecorder.dbUtil.getEventCount()
XCTAssertEqual(2, eventCount)
}

func testAppException() throws {
let exception = NSException(name: NSExceptionName("TestException"), reason: "Testing", userInfo: nil)
AutoRecordEventClient.handleException(exception)
Expand Down