Skip to content

Commit daa98c8

Browse files
zhu-xiaoweixiaoweii
and
xiaoweii
authored
feat: support add global attributes when using Objc api (#64)
Co-authored-by: xiaoweii <[email protected]>
1 parent 6242310 commit daa98c8

11 files changed

+31
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ ClickstreamAnalytics.recordEvent(ClickstreamAnalytics.EventName.SCREEN_VIEW, [
226226
import Clickstream
227227

228228
let globalAttribute: ClickstreamAttribute = [
229-
"channel": "apple",
229+
ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL: "apple",
230230
"class": 6,
231231
"level": 5.1,
232232
"isOpenNotification": true,

Sources/Clickstream/ClickstreamObjc.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ import Foundation
103103
ClickstreamAnalytics.enable()
104104
}
105105

106-
private static func getAttributes(_ attributes: NSDictionary) -> ClickstreamAttribute {
106+
static func getAttributes(_ attributes: NSDictionary) -> ClickstreamAttribute {
107107
var result: ClickstreamAttribute = [:]
108108
for case let (key as String, value) in attributes {
109109
if value is String {

Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ extension UserDefaults: UserDefaultsBehaviour {
124124
self.globalAttributes = globalAttributes
125125
return self
126126
}
127+
128+
public func withInitialGlobalAttributesObjc(_ globalAttributes: NSDictionary) -> ClickstreamConfiguration {
129+
self.globalAttributes = ClickstreamObjc.getAttributes(globalAttributes)
130+
return self
131+
}
127132
}
128133

129134
struct ClickstreamContextStorage {

Tests/ClickstreamTests/Clickstream/AnalyticsClientTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class AnalyticsClientTest: XCTestCase {
1919
override func setUp() async throws {
2020
UserDefaults.standard.removePersistentDomain(forName: Bundle.main.bundleIdentifier!)
2121
let contextConfiguration = ClickstreamConfiguration.getDefaultConfiguration()
22-
.withAppId(testAppId)
22+
.withAppId(testAppId + UUID().uuidString)
2323
.withEndpoint(testEndpoint)
2424
.withSendEventInterval(10_000)
2525
.withTrackAppExceptionEvents(false)

Tests/ClickstreamTests/Clickstream/AutoRecordEventClientTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class AutoRecordEventClientTest: XCTestCase {
2424
activityTracker = MockActivityTracker()
2525

2626
let contextConfiguration = ClickstreamConfiguration.getDefaultConfiguration()
27-
.withAppId(testAppId)
27+
.withAppId(testAppId + UUID().uuidString)
2828
.withEndpoint(testEndpoint)
2929
.withSendEventInterval(10_000)
3030
.withTrackAppExceptionEvents(true)

Tests/ClickstreamTests/Clickstream/EventRecorderTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class EventRecorderTest: XCTestCase {
4040
return HttpResponse.ok(.text("request success"))
4141
}
4242
try! server.start()
43-
let appId = testAppId + String(describing: Date().millisecondsSince1970)
43+
let appId = testAppId + UUID().uuidString
4444

4545
let contextConfiguration = ClickstreamConfiguration.getDefaultConfiguration()
4646
.withAppId(appId)

Tests/ClickstreamTests/Clickstream/SessionClientTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class SessionClientTests: XCTestCase {
2424
mockNetworkMonitor = MockNetworkMonitor()
2525

2626
let contextConfiguration = ClickstreamConfiguration.getDefaultConfiguration()
27-
.withAppId(testAppId)
27+
.withAppId(testAppId + UUID().uuidString)
2828
.withEndpoint(testEndpoint)
2929
.withSendEventInterval(10_000)
3030
.withTrackAppExceptionEvents(false)

Tests/ClickstreamTests/ClickstreamPluginTestBase.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ClickstreamPluginTestBase: XCTestCase {
2121
analyticsPlugin = AWSClickstreamPlugin()
2222

2323
let contextConfiguration = ClickstreamConfiguration.getDefaultConfiguration()
24-
.withAppId(testAppId)
24+
.withAppId(testAppId + UUID().uuidString)
2525
.withEndpoint(testEndpoint)
2626
.withSendEventInterval(10_000)
2727
.withTrackAppExceptionEvents(false)
@@ -34,7 +34,7 @@ class ClickstreamPluginTestBase: XCTestCase {
3434
let eventRecorder = try EventRecorder(clickstream: clickstream)
3535
let analyticsClient = try AnalyticsClient(clickstream: clickstream,
3636
eventRecorder: eventRecorder,
37-
sessionClient: sessionClient )
37+
sessionClient: sessionClient)
3838
analyticsPlugin.analyticsClient = analyticsClient
3939
clickstream.analyticsClient = analyticsClient
4040
clickstream.networkMonitor = mockNetworkMonitor

Tests/ClickstreamTests/DataBase/ClickstreamDBUtilTest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class ClickstreamDBUtiltest: XCTestCase {
1717

1818
override func setUp() {
1919
do {
20-
let appId = testAppId + String(describing: Date().millisecondsSince1970)
20+
let appId = testAppId + UUID().uuidString
2121
dbAdapter = try BaseDBAdapter(prefixPath: EventRecorder.Constants.dbPathPrefix,
2222
databaseName: appId)
2323
dbUtil = ClickstreamDBUtil(dbAdapter: dbAdapter)

Tests/ClickstreamTests/IntegrationTest.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class IntegrationTest: XCTestCase {
3030
}
3131
try! server.start()
3232
await Amplify.reset()
33-
let appId = "testAppId" + String(describing: Date().millisecondsSince1970)
33+
let appId = "testAppId" + UUID().uuidString
3434
let configure = ClickstreamConfiguration.getDefaultConfiguration()
3535
.withAppId(appId)
3636
.withEndpoint("http://localhost:8080/collect")
@@ -117,7 +117,7 @@ class IntegrationTest: XCTestCase {
117117
ClickstreamAnalytics.recordEvent("testEvent")
118118
Thread.sleep(forTimeInterval: 0.1)
119119
ClickstreamAnalytics.flushEvents()
120-
Thread.sleep(forTimeInterval: 0.2)
120+
Thread.sleep(forTimeInterval: 0.5)
121121
let eventCount = try eventRecorder.dbUtil.getEventCount()
122122
XCTAssertEqual(0, eventCount)
123123
}

Tests/ClickstreamTests/SDKInitialTest.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,28 @@ class SDKInitialTest: XCTestCase {
4444
.withTrackUserEngagementEvents(false)
4545
.withAuthCookie("testAuthCookie")
4646
.withInitialGlobalAttributes([
47-
"channel": "AppStore",
47+
ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL: "AppStore",
4848
"level": 5.1,
4949
"class": 5,
5050
"isOpenNotification": true
5151
])
5252
try ClickstreamAnalytics.initSDK(configure)
5353
}
5454

55+
func testInitSDKWithGlobalAttributeForObjc() throws {
56+
let globalAttribute: NSDictionary = [
57+
ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL: "SMS",
58+
"Successful": true,
59+
"Score": 90.1,
60+
"level": 5
61+
]
62+
let configure = ClickstreamConfiguration()
63+
.withAppId("testAppId")
64+
.withEndpoint("https://example.com/collect")
65+
.withInitialGlobalAttributesObjc(globalAttribute)
66+
try ClickstreamAnalytics.initSDK(configure)
67+
}
68+
5569
func testInitSDKOverrideAllAmplifyConfiguration() throws {
5670
let configure = ClickstreamConfiguration()
5771
.withAppId("testAppId1")

0 commit comments

Comments
 (0)