From 2340cd975eb8af10b80b227be8374c10b8bcc60f Mon Sep 17 00:00:00 2001 From: xiaoweii Date: Thu, 11 Apr 2024 17:41:48 +0800 Subject: [PATCH] feat: support add global attribute when using Objc api --- README.md | 2 +- Sources/Clickstream/ClickstreamObjc.swift | 2 +- .../Clickstream/ClickstreamContext.swift | 5 +++++ Tests/ClickstreamTests/SDKInitialTest.swift | 16 +++++++++++++++- 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 47965d6..b62ff7f 100644 --- a/README.md +++ b/README.md @@ -226,7 +226,7 @@ ClickstreamAnalytics.recordEvent(ClickstreamAnalytics.EventName.SCREEN_VIEW, [ import Clickstream let globalAttribute: ClickstreamAttribute = [ - "channel": "apple", + ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL: "apple", "class": 6, "level": 5.1, "isOpenNotification": true, diff --git a/Sources/Clickstream/ClickstreamObjc.swift b/Sources/Clickstream/ClickstreamObjc.swift index f2cfd62..7268fbf 100644 --- a/Sources/Clickstream/ClickstreamObjc.swift +++ b/Sources/Clickstream/ClickstreamObjc.swift @@ -103,7 +103,7 @@ import Foundation ClickstreamAnalytics.enable() } - private static func getAttributes(_ attributes: NSDictionary) -> ClickstreamAttribute { + static func getAttributes(_ attributes: NSDictionary) -> ClickstreamAttribute { var result: ClickstreamAttribute = [:] for case let (key as String, value) in attributes { if value is String { diff --git a/Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift b/Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift index 094d5f5..3d34cef 100644 --- a/Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift +++ b/Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift @@ -124,6 +124,11 @@ extension UserDefaults: UserDefaultsBehaviour { self.globalAttributes = globalAttributes return self } + + public func withInitialGlobalAttributesObjc(_ globalAttributes: NSDictionary) -> ClickstreamConfiguration { + self.globalAttributes = ClickstreamObjc.getAttributes(globalAttributes) + return self + } } struct ClickstreamContextStorage { diff --git a/Tests/ClickstreamTests/SDKInitialTest.swift b/Tests/ClickstreamTests/SDKInitialTest.swift index b8694d8..e9390d1 100644 --- a/Tests/ClickstreamTests/SDKInitialTest.swift +++ b/Tests/ClickstreamTests/SDKInitialTest.swift @@ -44,7 +44,7 @@ class SDKInitialTest: XCTestCase { .withTrackUserEngagementEvents(false) .withAuthCookie("testAuthCookie") .withInitialGlobalAttributes([ - "channel": "AppStore", + ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL: "AppStore", "level": 5.1, "class": 5, "isOpenNotification": true @@ -52,6 +52,20 @@ class SDKInitialTest: XCTestCase { try ClickstreamAnalytics.initSDK(configure) } + func testInitSDKWithGlobalAttributeForObjc() throws { + let globalAttribute: NSDictionary = [ + ClickstreamAnalytics.Attr.APP_INSTALL_CHANNEL: "SMS", + "Successful": true, + "Score": 90.1, + "level": 5 + ] + let configure = ClickstreamConfiguration() + .withAppId("testAppId") + .withEndpoint("https://example.com/collect") + .withInitialGlobalAttributesObjc(globalAttribute) + try ClickstreamAnalytics.initSDK(configure) + } + func testInitSDKOverrideAllAmplifyConfiguration() throws { let configure = ClickstreamConfiguration() .withAppId("testAppId1")