Skip to content

Commit

Permalink
Merge branch 'main' into feature_item
Browse files Browse the repository at this point in the history
  • Loading branch information
zhu-xiaowei authored Nov 30, 2023
2 parents dc3c9d0 + 1708b75 commit ca379be
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 34 deletions.
42 changes: 9 additions & 33 deletions .github/workflows/title-lint.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,24 @@
name: Commit Title Lint
name: Pull Request Title Lint

on:
pull_request:
branches: [ "main" ]
branches: [ "*" ]

jobs:
title-lint:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- uses: amannn/action-semantic-pull-request@v3.2.6
- uses: amannn/action-semantic-pull-request@v5
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# Configure which types are allowed.
# Default: https://github.com/commitizen/conventional-commit-types
types: |
fix
types: |-
feat
fix
chore
docs
style
refactor
perf
test
build
ci
chore
revert
release
# Configure that a scope must always be provided.
tests
requireScope: false
# Configure additional validation for the subject based on a regex.
# This example ensures the subject doesn't start with an uppercase character.
subjectPattern: ^(?![A-Z]).+$
# If `subjectPattern` is configured, you can use this property to override
# the default error message that is shown when the pattern doesn't match.
# The variables `subject` and `title` can be used within the message.
subjectPatternError: |
The subject "{subject}" found in the pull request title "{title}"
didn't match the configured pattern. Please ensure that the subject
doesn't start with an uppercase character.
# For work-in-progress PRs you can typically use draft pull requests
# from Github. However, private repositories on the free plan don't have
# this option and therefore this action allows you to opt-in to using the
# special "[WIP]" prefix to indicate this state. This will avoid the
# validation of the PR title and the pull request checks remain pending.
# Note that a second check will be reported if this is enabled.
wip: true
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,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 @@ -94,10 +94,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()
}
}
12 changes: 12 additions & 0 deletions Sources/Clickstream/ClickstreamAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,18 @@ public enum ClickstreamAnalytics {
// 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()
}

/// ClickstreamAnalytics item attributes
public enum Item {
static let ITEM_ID = "id"
Expand Down
11 changes: 11 additions & 0 deletions Sources/Clickstream/ClickstreamObjc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import Foundation
try ClickstreamAnalytics.getClickstreamConfiguration()
}


private static func getItems(_ items: [NSDictionary]) -> [ClickstreamAttribute] {
var resultItems: [ClickstreamAttribute] = []
for item in items {
Expand All @@ -79,6 +80,16 @@ import Foundation
return resultItems
}

/// 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
2 changes: 1 addition & 1 deletion Sources/Clickstream/PackageInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
enum PackageInfo {
/// the clickstream analytics iOS sdk version
/// note: update and align the version with new tag version before committing new tag
static let version = "0.7.0"
static let version = "0.8.0"
}
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 @@ -255,6 +256,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 @@ -341,6 +371,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

0 comments on commit ca379be

Please sign in to comment.