Skip to content

Commit

Permalink
feat: support lazy initialization of the SDK (#58)
Browse files Browse the repository at this point in the history
Co-authored-by: xiaoweii <[email protected]>
  • Loading branch information
zhu-xiaowei and xiaoweii authored Mar 22, 2024
1 parent a850795 commit 32f44a8
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/integration_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ jobs:
token: ${{ secrets.PROJECT_TOKEN }}
- name: Modify SDK for integration test
run: |
sed -i '' -e "s#isLogEvents: Bool = false#isLogEvents: Bool = true#g" Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift
sed -i '' -e "s#private(set) var bundleSequenceId: Int#private(set) var bundleSequenceId: Int\n var allEventJson: String = \"\"#g" Sources/Clickstream/Dependency/Clickstream/Analytics/EventRecorder.swift
sed -i '' -e "s#toPrettierJsonString())\")#toPrettierJsonString())\")\n allEventJson.append(\"Saved event \\\(event.eventType):\\\(eventObject.toJsonString())\\\n\")\n UIPasteboard.general.string = allEventJson#g" Sources/Clickstream/Dependency/Clickstream/Analytics/EventRecorder.swift
sed -i '' -e "s#batchEvent.eventCount) events\")#batchEvent.eventCount) events\")\n allEventJson.append(\"Send \\\(batchEvent.eventCount) events\\\n\")\n UIPasteboard.general.string = allEventJson#g" Sources/Clickstream/Dependency/Clickstream/Analytics/EventRecorder.swift
Expand Down
1 change: 1 addition & 0 deletions Sources/Clickstream/AWSClickstreamPlugin+Configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ extension AWSClickstreamPlugin {
networkMonitor: networkMonitor
)
log.debug("initialize Clickstream SDK successful")
sessionClient.handleAppStart()
}

// MARK: Internal
Expand Down
9 changes: 9 additions & 0 deletions Sources/Clickstream/ClickstreamObjc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ import Foundation
/// - Parameters:
/// - eventName: the event name
/// - attributes: the event attributes which type is NSDictionary
public static func recordEvent(_ eventName: String, _ attributes: NSDictionary) {
ClickstreamAnalytics.recordEvent(eventName, getAttributes(attributes))
}

/// The method to record event with attributes and items
/// - Parameters:
/// - eventName: the event name
/// - attributes: the event attributes which type is NSDictionary
/// - items: the event items which type is NSDictionary
public static func recordEvent(_ eventName: String, _ attributes: NSDictionary, _ items: [NSDictionary] = []) {
ClickstreamAnalytics.recordEvent(eventName, getAttributes(attributes), getItems(items))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class AutoRecordEventClient {
private var isEntrances = false
private var isFirstOpen: Bool
private var isFirstTime = true
private var isAppEndCalled = false
private var lastEngageTime: Int64 = 0
private(set) var lastScreenName: String?
private var lastScreenPath: String?
Expand Down Expand Up @@ -190,15 +191,18 @@ class AutoRecordEventClient {
}

func handleAppStart() {
let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.APP_START)
event.addAttribute(isFirstTime, forKey: Event.ReservedAttribute.IS_FIRST_TIME)
recordEvent(event)
if isFirstTime || isAppEndCalled {
let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.APP_START)
event.addAttribute(isFirstTime, forKey: Event.ReservedAttribute.IS_FIRST_TIME)
recordEvent(event)
}
isFirstTime = false
}

func recordAppEnd() {
let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.APP_END)
recordEvent(event)
isAppEndCalled = true
}

func recordSessionStartEvent() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class SessionClient: SessionClientBehaviour {
}
}

func handleAppStart() {
autoRecordClient.handleFirstOpen()
autoRecordClient.handleAppStart()
handleSesionStart()
}

func storeSession() {
session.pause()
UserDefaultsUtil.saveSession(storage: clickstream.storage, session: session)
Expand Down
17 changes: 17 additions & 0 deletions Tests/ClickstreamTests/Clickstream/SessionClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,23 @@ class SessionClientTests: XCTestCase {
XCTAssertEqual(Event.PresetEvent.SESSION_START, events[2].eventType)
}

func testRunningInForegroundAfterHandleAppStart() {
sessionClient.handleAppStart()
activityTracker.callback?(.runningInForeground)
let session = sessionClient.getCurrentSession()
XCTAssertTrue(session.isNewSession)
XCTAssertTrue(session.sessionIndex == 1)
XCTAssertNotNil(session.sessionId)
XCTAssertNotNil(session.startTime)

Thread.sleep(forTimeInterval: 0.1)
let events = eventRecorder.savedEvents
XCTAssertEqual(3, events.count)
XCTAssertEqual(Event.PresetEvent.FIRST_OPEN, events[0].eventType)
XCTAssertEqual(Event.PresetEvent.APP_START, events[1].eventType)
XCTAssertEqual(Event.PresetEvent.SESSION_START, events[2].eventType)
}

func testGoBackground() {
activityTracker.callback?(.runningInForeground)
Thread.sleep(forTimeInterval: 0.1)
Expand Down
7 changes: 5 additions & 2 deletions Tests/ClickstreamTests/IntegrationTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class IntegrationTest: XCTestCase {
} catch {
XCTFail("Error setting up Amplify: \(error)")
}
let eventCount = try eventRecorder.dbUtil.getEventCount()
XCTAssertEqual(3, eventCount)
try eventRecorder.dbUtil.deleteAllEvents()
}

override func tearDown() async throws {
Expand Down Expand Up @@ -108,7 +111,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(0, attributes[Event.ReservedAttribute.ENTRANCES] as! Int)
XCTAssertEqual(1, attributes[Event.ReservedAttribute.ENTRANCES] as! Int)
}

func testFlushEvents() throws {
Expand Down Expand Up @@ -351,7 +354,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(0, attributes[Event.ReservedAttribute.ENTRANCES] as! Int)
XCTAssertEqual(1, attributes[Event.ReservedAttribute.ENTRANCES] as! Int)
}

func testGlobalAttributeForObjc() throws {
Expand Down

0 comments on commit 32f44a8

Please sign in to comment.