|
| 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 | +} |
0 commit comments