Skip to content

Commit 32f44a8

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: support lazy initialization of the SDK (#58)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent a850795 commit 32f44a8

File tree

7 files changed

+46
-5
lines changed

7 files changed

+46
-5
lines changed

.github/workflows/integration_test.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ jobs:
2525
token: ${{ secrets.PROJECT_TOKEN }}
2626
- name: Modify SDK for integration test
2727
run: |
28+
sed -i '' -e "s#isLogEvents: Bool = false#isLogEvents: Bool = true#g" Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift
2829
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
2930
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
3031
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

Sources/Clickstream/AWSClickstreamPlugin+Configure.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ extension AWSClickstreamPlugin {
6262
networkMonitor: networkMonitor
6363
)
6464
log.debug("initialize Clickstream SDK successful")
65+
sessionClient.handleAppStart()
6566
}
6667

6768
// MARK: Internal

Sources/Clickstream/ClickstreamObjc.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ import Foundation
3030
/// - Parameters:
3131
/// - eventName: the event name
3232
/// - attributes: the event attributes which type is NSDictionary
33+
public static func recordEvent(_ eventName: String, _ attributes: NSDictionary) {
34+
ClickstreamAnalytics.recordEvent(eventName, getAttributes(attributes))
35+
}
36+
37+
/// The method to record event with attributes and items
38+
/// - Parameters:
39+
/// - eventName: the event name
40+
/// - attributes: the event attributes which type is NSDictionary
41+
/// - items: the event items which type is NSDictionary
3342
public static func recordEvent(_ eventName: String, _ attributes: NSDictionary, _ items: [NSDictionary] = []) {
3443
ClickstreamAnalytics.recordEvent(eventName, getAttributes(attributes), getItems(items))
3544
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class AutoRecordEventClient {
1515
private var isEntrances = false
1616
private var isFirstOpen: Bool
1717
private var isFirstTime = true
18+
private var isAppEndCalled = false
1819
private var lastEngageTime: Int64 = 0
1920
private(set) var lastScreenName: String?
2021
private var lastScreenPath: String?
@@ -190,15 +191,18 @@ class AutoRecordEventClient {
190191
}
191192

192193
func handleAppStart() {
193-
let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.APP_START)
194-
event.addAttribute(isFirstTime, forKey: Event.ReservedAttribute.IS_FIRST_TIME)
195-
recordEvent(event)
194+
if isFirstTime || isAppEndCalled {
195+
let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.APP_START)
196+
event.addAttribute(isFirstTime, forKey: Event.ReservedAttribute.IS_FIRST_TIME)
197+
recordEvent(event)
198+
}
196199
isFirstTime = false
197200
}
198201

199202
func recordAppEnd() {
200203
let event = clickstream.analyticsClient.createEvent(withEventType: Event.PresetEvent.APP_END)
201204
recordEvent(event)
205+
isAppEndCalled = true
202206
}
203207

204208
func recordSessionStartEvent() {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ class SessionClient: SessionClientBehaviour {
3838
}
3939
}
4040

41+
func handleAppStart() {
42+
autoRecordClient.handleFirstOpen()
43+
autoRecordClient.handleAppStart()
44+
handleSesionStart()
45+
}
46+
4147
func storeSession() {
4248
session.pause()
4349
UserDefaultsUtil.saveSession(storage: clickstream.storage, session: session)

Tests/ClickstreamTests/Clickstream/SessionClientTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ class SessionClientTests: XCTestCase {
7474
XCTAssertEqual(Event.PresetEvent.SESSION_START, events[2].eventType)
7575
}
7676

77+
func testRunningInForegroundAfterHandleAppStart() {
78+
sessionClient.handleAppStart()
79+
activityTracker.callback?(.runningInForeground)
80+
let session = sessionClient.getCurrentSession()
81+
XCTAssertTrue(session.isNewSession)
82+
XCTAssertTrue(session.sessionIndex == 1)
83+
XCTAssertNotNil(session.sessionId)
84+
XCTAssertNotNil(session.startTime)
85+
86+
Thread.sleep(forTimeInterval: 0.1)
87+
let events = eventRecorder.savedEvents
88+
XCTAssertEqual(3, events.count)
89+
XCTAssertEqual(Event.PresetEvent.FIRST_OPEN, events[0].eventType)
90+
XCTAssertEqual(Event.PresetEvent.APP_START, events[1].eventType)
91+
XCTAssertEqual(Event.PresetEvent.SESSION_START, events[2].eventType)
92+
}
93+
7794
func testGoBackground() {
7895
activityTracker.callback?(.runningInForeground)
7996
Thread.sleep(forTimeInterval: 0.1)

Tests/ClickstreamTests/IntegrationTest.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class IntegrationTest: XCTestCase {
5151
} catch {
5252
XCTFail("Error setting up Amplify: \(error)")
5353
}
54+
let eventCount = try eventRecorder.dbUtil.getEventCount()
55+
XCTAssertEqual(3, eventCount)
56+
try eventRecorder.dbUtil.deleteAllEvents()
5457
}
5558

5659
override func tearDown() async throws {
@@ -108,7 +111,7 @@ class IntegrationTest: XCTestCase {
108111
let attributes = event["attributes"] as! [String: Any]
109112
XCTAssertEqual("HomeView", attributes[ClickstreamAnalytics.Attr.SCREEN_NAME] as! String)
110113
XCTAssertEqual("23ac31df", attributes[ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID] as! String)
111-
XCTAssertEqual(0, attributes[Event.ReservedAttribute.ENTRANCES] as! Int)
114+
XCTAssertEqual(1, attributes[Event.ReservedAttribute.ENTRANCES] as! Int)
112115
}
113116

114117
func testFlushEvents() throws {
@@ -351,7 +354,7 @@ class IntegrationTest: XCTestCase {
351354
let attributes = event["attributes"] as! [String: Any]
352355
XCTAssertEqual("HomeView", attributes[ClickstreamAnalytics.Attr.SCREEN_NAME] as! String)
353356
XCTAssertEqual("23ac31df", attributes[ClickstreamAnalytics.Attr.SCREEN_UNIQUE_ID] as! String)
354-
XCTAssertEqual(0, attributes[Event.ReservedAttribute.ENTRANCES] as! Int)
357+
XCTAssertEqual(1, attributes[Event.ReservedAttribute.ENTRANCES] as! Int)
355358
}
356359

357360
func testGlobalAttributeForObjc() throws {

0 commit comments

Comments
 (0)