Skip to content

Commit 9de9142

Browse files
zhu-xiaoweixiaoweii
andauthored
feat: add Swift-Objective C interoperability (#3)
--------- Co-authored-by: xiaoweii <xiaoweii@amazom.com>
1 parent 5b95b74 commit 9de9142

17 files changed

+271
-96
lines changed

.swiftlint.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ closing_brace: error
2222
colon:
2323
severity: error
2424
comma: error
25-
empty_count: warning
2625
empty_enum_arguments: error
2726
function_body_length:
2827
warning: 100

Sources/Clickstream/AWSClickstreamPlugin+ClientBehavior.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Amplify
99
import Foundation
1010

11-
public extension AWSClickstreamPlugin {
11+
extension AWSClickstreamPlugin {
1212
func identifyUser(userId: String, userProfile: AnalyticsUserProfile?) {
1313
if userId == Event.User.USER_ID_EMPTY {
1414
userProfile?.properties?.forEach { key, value in

Sources/Clickstream/AWSClickstreamPlugin+Configure.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import Network
1212
import UIKit
1313
#endif
1414

15-
public extension AWSClickstreamPlugin {
15+
extension AWSClickstreamPlugin {
1616
/// called when sdk init.
1717
func configure(using configuration: Any?) throws {
1818
guard let config = configuration as? JSONValue else {
@@ -72,7 +72,7 @@ public extension AWSClickstreamPlugin {
7272
// MARK: Internal
7373

7474
/// Internal configure method to set the properties of the plugin
75-
internal func configure(
75+
func configure(
7676
autoFlushEventsTimer: DispatchSourceTimer?,
7777
networkMonitor: NetworkMonitor
7878
) {

Sources/Clickstream/AWSClickstreamPlugin+Reset.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// SPDX-License-Identifier: Apache-2.0
66
//
77

8-
public extension AWSClickstreamPlugin {
8+
extension AWSClickstreamPlugin {
99
func reset() {
1010
if clickstream != nil {
1111
clickstream = nil

Sources/Clickstream/AWSClickstreamPlugin.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
import Amplify
99
import Foundation
1010

11-
public final class AWSClickstreamPlugin: AnalyticsCategoryPlugin {
12-
public var clickstream: ClickstreamContext!
11+
final class AWSClickstreamPlugin: AnalyticsCategoryPlugin {
12+
var clickstream: ClickstreamContext!
1313

1414
/// Automatically flushes the events that have been recorded on an interval
1515
var autoFlushEventsTimer: DispatchSourceTimer?
@@ -24,10 +24,10 @@ public final class AWSClickstreamPlugin: AnalyticsCategoryPlugin {
2424
var analyticsClient: AnalyticsClientBehaviour!
2525

2626
/// The configuration file plugin key of clickstream plugin
27-
public var key: PluginKey {
27+
var key: PluginKey {
2828
"awsClickstreamPlugin"
2929
}
3030

3131
/// Instantiates an instance of the AWSClickstreamPlugin
32-
public init() {}
32+
init() {}
3333
}

Sources/Clickstream/ClickstreamAnalytics.swift

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,30 @@
77

88
import Amplify
99

10+
/// ClickstreamAnalytics api for swift
1011
public enum ClickstreamAnalytics {
1112
/// Init ClickstreamAnalytics
1213
public static func initSDK() throws {
1314
try Amplify.add(plugin: AWSClickstreamPlugin())
1415
try Amplify.configure()
1516
}
1617

17-
/// Use this method to record Event.
18-
/// - Parameter event: ClickstreamEvent to record
19-
public static func recordEvent(event: AnalyticsEvent) {
20-
Amplify.Analytics.record(event: event)
21-
}
22-
23-
/// Use this method to record Event.
18+
/// Use this method to record event
2419
/// - Parameter eventName: the event name
2520
public static func recordEvent(eventName: String) {
2621
Amplify.Analytics.record(eventWithName: eventName)
2722
}
2823

29-
/// Use this method to send events immediately.
24+
/// The method to record event with ClickstreamAttribute
25+
/// - Parameters:
26+
/// - eventName: the event name
27+
/// - attributes: the event attributes
28+
public static func recordEvent(eventName: String, attributes: ClickstreamAttribute) {
29+
let event = BaseClickstreamEvent(name: eventName, attribute: attributes)
30+
Amplify.Analytics.record(event: event)
31+
}
32+
33+
/// Use this method to send events immediately
3034
public static func flushEvents() {
3135
Amplify.Analytics.flushEvents()
3236
}
@@ -38,7 +42,7 @@ public enum ClickstreamAnalytics {
3842
}
3943

4044
/// Delete global attributes
41-
/// - Parameter attributes: the global attributes to delete
45+
/// - Parameter attributes: the global attributes names to delete
4246
public static func deleteGlobalAttributes(attributes: String...) {
4347
Amplify.Analytics.unregisterGlobalProperties(attributes)
4448
}
@@ -61,10 +65,12 @@ public enum ClickstreamAnalytics {
6165
}
6266
}
6367

64-
/// Get clickstream configuration, please config it after initialize.
65-
/// - Returns: ClickstreamContextConfiguration: current userId, nil for logout
66-
public static func getClickStreamConfiguration() throws -> ClickstreamContextConfiguration? {
68+
/// Get Clickstream configuration, please config it after initialize sdk
69+
/// - Returns: ClickstreamContextConfiguration to modify the configuration of clickstream sdk
70+
public static func getClickstreamConfiguration() throws -> ClickstreamContextConfiguration {
6771
let plugin = try Amplify.Analytics.getPlugin(for: "awsClickstreamPlugin")
68-
return (plugin as? AWSClickstreamPlugin)?.getEscapeHatch().configuration
72+
// swiftlint:disable force_cast
73+
return (plugin as! AWSClickstreamPlugin).getEscapeHatch().configuration
74+
// swiftlint:enable force_cast
6975
}
7076
}

Sources/Clickstream/ClickstreamAttribute.swift

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,29 @@
77

88
import Amplify
99

10+
/// the attribute for Clickstream which support String, Int, Int64, Double and Bool
1011
public typealias ClickstreamAttribute = AnalyticsProperties
11-
public typealias AttributeValue = AnalyticsPropertyValue
12+
13+
typealias AttributeValue = AnalyticsPropertyValue
1214
extension Int64: AnalyticsPropertyValue {}
1315

14-
public struct BaseClickstreamEvent: AnalyticsEvent {
15-
public var properties: AnalyticsProperties?
16+
struct BaseClickstreamEvent: AnalyticsEvent {
17+
var properties: AnalyticsProperties?
1618

1719
/// The name of the event
18-
public var name: String
20+
var name: String
1921

2022
/// Properties of the event
21-
public var attribute: ClickstreamAttribute?
23+
var attribute: ClickstreamAttribute?
2224

2325
/// Initializer
2426
/// - Parameters:
2527
/// - name: The name of the event
2628
/// - attribute: Attribute of the event
27-
public init(name: String,
28-
attribute: ClickstreamAttribute? = nil)
29+
init(name: String,
30+
attribute: ClickstreamAttribute? = nil)
2931
{
3032
self.name = name
3133
self.attribute = attribute
3234
}
3335
}
34-
35-
public struct ClickstreamUserAttribute {
36-
public var attribute: ClickstreamAttribute?
37-
public init(attribute: ClickstreamAttribute?) {
38-
self.attribute = attribute
39-
}
40-
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// Copyright Amazon.com Inc. or its affiliates.
3+
// All Rights Reserved.
4+
//
5+
// SPDX-License-Identifier: Apache-2.0
6+
//
7+
8+
import Foundation
9+
10+
/// ClickstreamAnalytics api for objective-c
11+
@objcMembers public class ClickstreamObjc: NSObject {
12+
/// Init the Clickstream sdk
13+
public static func initSDK() throws {
14+
try ClickstreamAnalytics.initSDK()
15+
}
16+
17+
/// Use this method to record event
18+
/// - Parameter eventName: the event name
19+
public static func recordEvent(_ eventName: String) {
20+
ClickstreamAnalytics.recordEvent(eventName: eventName)
21+
}
22+
23+
/// The method to record event with attributes
24+
/// - Parameters:
25+
/// - eventName: the event name
26+
/// - attributes: the event attributes which type is NSDictionary
27+
public static func recordEvent(_ eventName: String, _ attributes: NSDictionary) {
28+
ClickstreamAnalytics.recordEvent(eventName: eventName, attributes: getAttributes(attributes))
29+
}
30+
31+
/// Use this method to send events immediately
32+
public static func flushEvents() {
33+
ClickstreamAnalytics.flushEvents()
34+
}
35+
36+
/// Add global attributes
37+
/// - Parameter attributes: the global attributes to add
38+
public static func addGlobalAttributes(_ attributes: NSDictionary) {
39+
ClickstreamAnalytics.addGlobalAttributes(attributes: getAttributes(attributes))
40+
}
41+
42+
/// Delete global attributes
43+
/// - Parameter attributes: the global attributes names to delete
44+
public static func deleteGlobalAttributes(_ attributes: [String]) {
45+
for attribute in attributes {
46+
ClickstreamAnalytics.deleteGlobalAttributes(attributes: attribute)
47+
}
48+
}
49+
50+
/// Add user attributes
51+
/// - Parameter attributes: the user attributes to add
52+
public static func addUserAttributes(_ attributes: NSDictionary) {
53+
ClickstreamAnalytics.addUserAttributes(attributes: getAttributes(attributes))
54+
}
55+
56+
/// Set user id for login and logout
57+
/// - Parameter userId: current userId, nil for logout
58+
public static func setUserId(_ userId: String?) {
59+
ClickstreamAnalytics.setUserId(userId: userId)
60+
}
61+
62+
/// Get Clickstream configuration, please config it after initialize sdk
63+
/// - Returns: ClickstreamContextConfiguration to modify the configuration of clickstream sdk
64+
public static func getClickstreamConfiguration() throws -> ClickstreamContextConfiguration {
65+
try ClickstreamAnalytics.getClickstreamConfiguration()
66+
}
67+
68+
private static func getAttributes(_ attributes: NSDictionary) -> ClickstreamAttribute {
69+
var result: ClickstreamAttribute = [:]
70+
for case let (key as String, value) in attributes {
71+
if value is String {
72+
result[key] = value as? String
73+
} else if value is Bool {
74+
if let boolValue = value as? Bool {
75+
result[key] = boolValue ? true : false
76+
}
77+
} else if value is Int {
78+
result[key] = value as? Int
79+
} else if value is Double {
80+
result[key] = value as? Double
81+
}
82+
}
83+
return result
84+
}
85+
}

Sources/Clickstream/Configuration/AWSClickstreamPluginConfiguration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Amplify
99

10-
public struct AWSClickstreamConfiguration {
10+
struct AWSClickstreamConfiguration {
1111
static let appIdKey = "appId"
1212
static let endpointKey = "endpoint"
1313
static let sendEventsIntervalKey = "autoFlushEventsInterval"

Sources/Clickstream/Dependency/Clickstream/ClickstreamContext.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,25 @@ extension UserDefaults: UserDefaultsBehaviour {
3333

3434
// MARK: - ClickstreamContext
3535

36-
/// the configuration object contains the necessary and optional param which required to use clickstream
37-
public class ClickstreamContextConfiguration {
38-
// The clickstream appId
36+
/// The configuration object for clickstream, modify the params after sdk initialize
37+
@objcMembers public class ClickstreamContextConfiguration: NSObject {
38+
/// The clickstream appId
3939
public var appId: String
4040
/// The clickstream endpoint
4141
public var endpoint: String
4242
/// Time interval after which the events are automatically submitted to server
4343
private let sendEventsInterval: Int
44-
/// Whether to track app lifecycle events automatically
45-
public var isTrackAppExceptionEvents: Bool
4644
/// Whether to track app exception events automatically
45+
public var isTrackAppExceptionEvents: Bool
46+
/// Whether to track app scren view events automatically
4747
public var isTrackScreenViewEvents: Bool
48-
/// Whether to compress events
48+
/// Whether to compress events when send to server
4949
public var isCompressEvents: Bool
50-
/// Whether to log events json in terminal when debug
50+
/// Whether to log events json in console when debug
5151
public var isLogEvents: Bool
52+
/// The auth cookie for request
5253
public var authCookie: String?
54+
/// The session timeout calculated the duration from last app in background, defalut is 1800000ms
5355
public var sessionTimeoutDuration: Int64
5456

5557
init(appId: String,
@@ -76,7 +78,7 @@ struct ClickstreamContextStorage {
7678
let userDefaults: UserDefaultsBehaviour
7779
}
7880

79-
public class ClickstreamContext {
81+
class ClickstreamContext {
8082
var sessionClient: SessionClientBehaviour!
8183
var analyticsClient: AnalyticsClientBehaviour!
8284
var networkMonitor: NetworkMonitor!

0 commit comments

Comments
 (0)