Skip to content

Commit 3dfe309

Browse files
zhu-xiaoweixiaoweii
andauthored
fix: inconsistent issue when setting attributes asynchronously (#20)
Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent b7c25ab commit 3dfe309

File tree

12 files changed

+184
-197
lines changed

12 files changed

+184
-197
lines changed

.github/workflows/test.yml

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,23 @@ on:
88

99
jobs:
1010
test:
11-
runs-on: macos-12
11+
runs-on: macos-13-xl
1212
steps:
1313
- uses: actions/checkout@v3
1414
- name: Run test
15-
run: xcodebuild test -scheme aws-solution-clickstream-swift -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=latest' -resultBundlePath .build/test/TestResults.xcresult | xcpretty --simple --color --report junit
15+
run: xcodebuild test -scheme aws-solution-clickstream-swift -sdk iphonesimulator -derivedDataPath .build/ -destination 'platform=iOS Simulator,name=iPhone 14 Pro,OS=latest' -enableCodeCoverage YES | xcpretty --simple --color --report junit && exit ${PIPESTATUS[0]}
16+
- name: Convert to json format
17+
run: |
18+
cd .build/Build/ProfileData
19+
cd $(ls -d */|head -n 1)
20+
directory=${PWD##*/}
21+
pathCoverage=.build/Build/ProfileData/${directory}/Coverage.profdata
22+
cd ../../../../
23+
xcrun llvm-cov export -format="lcov" -instr-profile $pathCoverage .build/Build/Products/Debug-iphonesimulator/Clickstream.o > .build/info.lcov
1624
- name: Upload Test Report
1725
uses: codecov/codecov-action@v3
1826
with:
1927
name: report
20-
files: .build/test/TestResults.xcresult
28+
files: .build/info.lcov
2129
swift: true
2230

Sources/Clickstream/AWSClickstreamPlugin+ClientBehavior.swift

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,21 @@ import Foundation
1010

1111
extension AWSClickstreamPlugin {
1212
func identifyUser(userId: String, userProfile: AnalyticsUserProfile?) {
13-
Task {
14-
if userId == Event.User.USER_ID_EMPTY {
15-
if let attributes = userProfile?.properties {
16-
for attribute in attributes {
17-
await analyticsClient.addUserAttribute(attribute.value, forKey: attribute.key)
18-
}
13+
if userId == Event.User.USER_ID_EMPTY {
14+
if let attributes = userProfile?.properties {
15+
for attribute in attributes {
16+
analyticsClient.addUserAttribute(attribute.value, forKey: attribute.key)
1917
}
18+
}
19+
} else {
20+
if userId == Event.User.USER_ID_NIL {
21+
analyticsClient.updateUserId(nil)
2022
} else {
21-
if userId == Event.User.USER_ID_NIL {
22-
await analyticsClient.updateUserId(nil)
23-
} else {
24-
await analyticsClient.updateUserId(userId)
25-
}
23+
analyticsClient.updateUserId(userId)
2624
}
27-
await analyticsClient.updateUserAttributes()
25+
}
26+
analyticsClient.updateUserAttributes()
27+
Task {
2828
record(eventWithName: Event.PresetEvent.PROFILE_SET)
2929
}
3030
}
@@ -59,17 +59,13 @@ extension AWSClickstreamPlugin {
5959

6060
func registerGlobalProperties(_ properties: AnalyticsProperties) {
6161
properties.forEach { key, value in
62-
Task {
63-
await analyticsClient.addGlobalAttribute(value, forKey: key)
64-
}
62+
analyticsClient.addGlobalAttribute(value, forKey: key)
6563
}
6664
}
6765

6866
func unregisterGlobalProperties(_ keys: Set<String>?) {
6967
keys?.forEach { key in
70-
Task {
71-
await analyticsClient.removeGlobalAttribute(forKey: key)
72-
}
68+
analyticsClient.removeGlobalAttribute(forKey: key)
7369
}
7470
}
7571

@@ -83,9 +79,7 @@ extension AWSClickstreamPlugin {
8379
log.error("Device is offline, skipping submitting events to Clickstream server")
8480
return
8581
}
86-
Task {
87-
try await analyticsClient.submitEvents()
88-
}
82+
analyticsClient.submitEvents()
8983
}
9084

9185
func getEscapeHatch() -> ClickstreamContext {

Sources/Clickstream/Dependency/Clickstream/Analytics/AnalyticsClient.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,22 @@
77

88
import Foundation
99

10-
protocol AnalyticsClientBehaviour: Actor {
10+
protocol AnalyticsClientBehaviour {
1111
func addGlobalAttribute(_ attribute: AttributeValue, forKey key: String)
1212
func addUserAttribute(_ attribute: AttributeValue, forKey key: String)
1313
func removeGlobalAttribute(forKey key: String)
1414
func removeUserAttribute(forKey key: String)
1515
func updateUserId(_ id: String?)
1616
func updateUserAttributes()
1717

18-
nonisolated func createEvent(withEventType eventType: String) -> ClickstreamEvent
18+
func createEvent(withEventType eventType: String) -> ClickstreamEvent
1919
func record(_ event: ClickstreamEvent) async throws
20-
func submitEvents() throws
20+
func submitEvents()
2121
}
2222

2323
typealias SessionProvider = () -> Session?
2424

25-
actor AnalyticsClient: AnalyticsClientBehaviour {
25+
class AnalyticsClient: AnalyticsClientBehaviour {
2626
private(set) var eventRecorder: AnalyticsEventRecording
2727
private let sessionProvider: SessionProvider
2828
private(set) lazy var globalAttributes: [String: AttributeValue] = [:]
@@ -101,7 +101,7 @@ actor AnalyticsClient: AnalyticsClientBehaviour {
101101

102102
// MARK: - Event recording
103103

104-
nonisolated func createEvent(withEventType eventType: String) -> ClickstreamEvent {
104+
func createEvent(withEventType eventType: String) -> ClickstreamEvent {
105105
let (isValid, errorType) = Event.isValidEventType(eventType: eventType)
106106
precondition(isValid, errorType)
107107

@@ -124,7 +124,7 @@ actor AnalyticsClient: AnalyticsClientBehaviour {
124124
try eventRecorder.save(event)
125125
}
126126

127-
func submitEvents() throws {
128-
try eventRecorder.submitEvents()
127+
func submitEvents() {
128+
eventRecorder.submitEvents()
129129
}
130130
}

Sources/Clickstream/Dependency/Clickstream/Analytics/EventRecorder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protocol AnalyticsEventRecording {
1616

1717
/// Submit locally stored events
1818
/// - Returns: A collection of events submitted to Clickstream
19-
func submitEvents() throws
19+
func submitEvents()
2020
}
2121

2222
/// An AnalyticsEventRecording implementation that stores and submits clickstream events

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,11 @@ class AutoRecordEventClient {
155155

156156
func recordEvent(_ event: ClickstreamEvent) {
157157
Task {
158-
try await clickstream.analyticsClient.record(event)
158+
do {
159+
try await clickstream.analyticsClient.record(event)
160+
} catch {
161+
log.error("Record event error:\(error)")
162+
}
159163
}
160164
}
161165
}

Sources/Clickstream/PackageInfo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@
88
enum PackageInfo {
99
/// the clickstream analytics iOS sdk version
1010
/// note: update and align the version with new tag version before committing new tag
11-
static let version = "0.5.0"
11+
static let version = "0.5.1"
1212
}

0 commit comments

Comments
 (0)