Skip to content

Commit

Permalink
feat: add Swift-Objective C interoperability (#3)
Browse files Browse the repository at this point in the history
---------

Co-authored-by: xiaoweii <[email protected]>
  • Loading branch information
zhu-xiaowei and xiaoweii authored May 19, 2023
1 parent 5b95b74 commit 9de9142
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 96 deletions.
1 change: 0 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ closing_brace: error
colon:
severity: error
comma: error
empty_count: warning
empty_enum_arguments: error
function_body_length:
warning: 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Amplify
import Foundation

public extension AWSClickstreamPlugin {
extension AWSClickstreamPlugin {
func identifyUser(userId: String, userProfile: AnalyticsUserProfile?) {
if userId == Event.User.USER_ID_EMPTY {
userProfile?.properties?.forEach { key, value in
Expand Down
4 changes: 2 additions & 2 deletions Sources/Clickstream/AWSClickstreamPlugin+Configure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Network
import UIKit
#endif

public extension AWSClickstreamPlugin {
extension AWSClickstreamPlugin {
/// called when sdk init.
func configure(using configuration: Any?) throws {
guard let config = configuration as? JSONValue else {
Expand Down Expand Up @@ -72,7 +72,7 @@ public extension AWSClickstreamPlugin {
// MARK: Internal

/// Internal configure method to set the properties of the plugin
internal func configure(
func configure(
autoFlushEventsTimer: DispatchSourceTimer?,
networkMonitor: NetworkMonitor
) {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Clickstream/AWSClickstreamPlugin+Reset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// SPDX-License-Identifier: Apache-2.0
//

public extension AWSClickstreamPlugin {
extension AWSClickstreamPlugin {
func reset() {
if clickstream != nil {
clickstream = nil
Expand Down
8 changes: 4 additions & 4 deletions Sources/Clickstream/AWSClickstreamPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import Amplify
import Foundation

public final class AWSClickstreamPlugin: AnalyticsCategoryPlugin {
public var clickstream: ClickstreamContext!
final class AWSClickstreamPlugin: AnalyticsCategoryPlugin {
var clickstream: ClickstreamContext!

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

/// The configuration file plugin key of clickstream plugin
public var key: PluginKey {
var key: PluginKey {
"awsClickstreamPlugin"
}

/// Instantiates an instance of the AWSClickstreamPlugin
public init() {}
init() {}
}
32 changes: 19 additions & 13 deletions Sources/Clickstream/ClickstreamAnalytics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@

import Amplify

/// ClickstreamAnalytics api for swift
public enum ClickstreamAnalytics {
/// Init ClickstreamAnalytics
public static func initSDK() throws {
try Amplify.add(plugin: AWSClickstreamPlugin())
try Amplify.configure()
}

/// Use this method to record Event.
/// - Parameter event: ClickstreamEvent to record
public static func recordEvent(event: AnalyticsEvent) {
Amplify.Analytics.record(event: event)
}

/// Use this method to record Event.
/// Use this method to record event
/// - Parameter eventName: the event name
public static func recordEvent(eventName: String) {
Amplify.Analytics.record(eventWithName: eventName)
}

/// Use this method to send events immediately.
/// The method to record event with ClickstreamAttribute
/// - Parameters:
/// - eventName: the event name
/// - attributes: the event attributes
public static func recordEvent(eventName: String, attributes: ClickstreamAttribute) {
let event = BaseClickstreamEvent(name: eventName, attribute: attributes)
Amplify.Analytics.record(event: event)
}

/// Use this method to send events immediately
public static func flushEvents() {
Amplify.Analytics.flushEvents()
}
Expand All @@ -38,7 +42,7 @@ public enum ClickstreamAnalytics {
}

/// Delete global attributes
/// - Parameter attributes: the global attributes to delete
/// - Parameter attributes: the global attributes names to delete
public static func deleteGlobalAttributes(attributes: String...) {
Amplify.Analytics.unregisterGlobalProperties(attributes)
}
Expand All @@ -61,10 +65,12 @@ public enum ClickstreamAnalytics {
}
}

/// Get clickstream configuration, please config it after initialize.
/// - Returns: ClickstreamContextConfiguration: current userId, nil for logout
public static func getClickStreamConfiguration() throws -> ClickstreamContextConfiguration? {
/// Get Clickstream configuration, please config it after initialize sdk
/// - Returns: ClickstreamContextConfiguration to modify the configuration of clickstream sdk
public static func getClickstreamConfiguration() throws -> ClickstreamContextConfiguration {
let plugin = try Amplify.Analytics.getPlugin(for: "awsClickstreamPlugin")
return (plugin as? AWSClickstreamPlugin)?.getEscapeHatch().configuration
// swiftlint:disable force_cast
return (plugin as! AWSClickstreamPlugin).getEscapeHatch().configuration
// swiftlint:enable force_cast
}
}
23 changes: 9 additions & 14 deletions Sources/Clickstream/ClickstreamAttribute.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,29 @@

import Amplify

/// the attribute for Clickstream which support String, Int, Int64, Double and Bool
public typealias ClickstreamAttribute = AnalyticsProperties
public typealias AttributeValue = AnalyticsPropertyValue

typealias AttributeValue = AnalyticsPropertyValue
extension Int64: AnalyticsPropertyValue {}

public struct BaseClickstreamEvent: AnalyticsEvent {
public var properties: AnalyticsProperties?
struct BaseClickstreamEvent: AnalyticsEvent {
var properties: AnalyticsProperties?

/// The name of the event
public var name: String
var name: String

/// Properties of the event
public var attribute: ClickstreamAttribute?
var attribute: ClickstreamAttribute?

/// Initializer
/// - Parameters:
/// - name: The name of the event
/// - attribute: Attribute of the event
public init(name: String,
attribute: ClickstreamAttribute? = nil)
init(name: String,
attribute: ClickstreamAttribute? = nil)
{
self.name = name
self.attribute = attribute
}
}

public struct ClickstreamUserAttribute {
public var attribute: ClickstreamAttribute?
public init(attribute: ClickstreamAttribute?) {
self.attribute = attribute
}
}
85 changes: 85 additions & 0 deletions Sources/Clickstream/ClickstreamObjc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// Copyright Amazon.com Inc. or its affiliates.
// All Rights Reserved.
//
// SPDX-License-Identifier: Apache-2.0
//

import Foundation

/// ClickstreamAnalytics api for objective-c
@objcMembers public class ClickstreamObjc: NSObject {
/// Init the Clickstream sdk
public static func initSDK() throws {
try ClickstreamAnalytics.initSDK()
}

/// Use this method to record event
/// - Parameter eventName: the event name
public static func recordEvent(_ eventName: String) {
ClickstreamAnalytics.recordEvent(eventName: eventName)
}

/// The method to record event with attributes
/// - Parameters:
/// - eventName: the event name
/// - attributes: the event attributes which type is NSDictionary
public static func recordEvent(_ eventName: String, _ attributes: NSDictionary) {
ClickstreamAnalytics.recordEvent(eventName: eventName, attributes: getAttributes(attributes))
}

/// Use this method to send events immediately
public static func flushEvents() {
ClickstreamAnalytics.flushEvents()
}

/// Add global attributes
/// - Parameter attributes: the global attributes to add
public static func addGlobalAttributes(_ attributes: NSDictionary) {
ClickstreamAnalytics.addGlobalAttributes(attributes: getAttributes(attributes))
}

/// Delete global attributes
/// - Parameter attributes: the global attributes names to delete
public static func deleteGlobalAttributes(_ attributes: [String]) {
for attribute in attributes {
ClickstreamAnalytics.deleteGlobalAttributes(attributes: attribute)
}
}

/// Add user attributes
/// - Parameter attributes: the user attributes to add
public static func addUserAttributes(_ attributes: NSDictionary) {
ClickstreamAnalytics.addUserAttributes(attributes: getAttributes(attributes))
}

/// Set user id for login and logout
/// - Parameter userId: current userId, nil for logout
public static func setUserId(_ userId: String?) {
ClickstreamAnalytics.setUserId(userId: userId)
}

/// Get Clickstream configuration, please config it after initialize sdk
/// - Returns: ClickstreamContextConfiguration to modify the configuration of clickstream sdk
public static func getClickstreamConfiguration() throws -> ClickstreamContextConfiguration {
try ClickstreamAnalytics.getClickstreamConfiguration()
}

private static func getAttributes(_ attributes: NSDictionary) -> ClickstreamAttribute {
var result: ClickstreamAttribute = [:]
for case let (key as String, value) in attributes {
if value is String {
result[key] = value as? String
} else if value is Bool {
if let boolValue = value as? Bool {
result[key] = boolValue ? true : false
}
} else if value is Int {
result[key] = value as? Int
} else if value is Double {
result[key] = value as? Double
}
}
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Amplify

public struct AWSClickstreamConfiguration {
struct AWSClickstreamConfiguration {
static let appIdKey = "appId"
static let endpointKey = "endpoint"
static let sendEventsIntervalKey = "autoFlushEventsInterval"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,25 @@ extension UserDefaults: UserDefaultsBehaviour {

// MARK: - ClickstreamContext

/// the configuration object contains the necessary and optional param which required to use clickstream
public class ClickstreamContextConfiguration {
// The clickstream appId
/// The configuration object for clickstream, modify the params after sdk initialize
@objcMembers public class ClickstreamContextConfiguration: NSObject {
/// The clickstream appId
public var appId: String
/// The clickstream endpoint
public var endpoint: String
/// Time interval after which the events are automatically submitted to server
private let sendEventsInterval: Int
/// Whether to track app lifecycle events automatically
public var isTrackAppExceptionEvents: Bool
/// Whether to track app exception events automatically
public var isTrackAppExceptionEvents: Bool
/// Whether to track app scren view events automatically
public var isTrackScreenViewEvents: Bool
/// Whether to compress events
/// Whether to compress events when send to server
public var isCompressEvents: Bool
/// Whether to log events json in terminal when debug
/// Whether to log events json in console when debug
public var isLogEvents: Bool
/// The auth cookie for request
public var authCookie: String?
/// The session timeout calculated the duration from last app in background, defalut is 1800000ms
public var sessionTimeoutDuration: Int64

init(appId: String,
Expand All @@ -76,7 +78,7 @@ struct ClickstreamContextStorage {
let userDefaults: UserDefaultsBehaviour
}

public class ClickstreamContext {
class ClickstreamContext {
var sessionClient: SessionClientBehaviour!
var analyticsClient: AnalyticsClientBehaviour!
var networkMonitor: NetworkMonitor!
Expand Down
Loading

0 comments on commit 9de9142

Please sign in to comment.