123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- //
- // KMAnalytics.swift
- // PDF Master
- //
- // Created by tangchao on 2023/7/12.
- //
- import Cocoa
- import AppCenter
- import AppCenterAnalytics
- import AppCenterCrashes
- import FirebaseCore
- import FirebaseAnalytics
- // 数据埋点 平台
- @objc enum KMAnalyticsPlatform: Int {
- case AppCenter
- case firebase
- }
- // 数据埋点 Category 参数
- extension KMAnalytics.Parameter.Category {
- public static let tbr = "Tbr"
- public static let subTbr_annotation = "SubTbr_Annotation"
- public static let subTbr_editPDF = "SubTbr_EditPDF"
- public static let subTbr_PageEdit = "SubTbr_PageEdit"
- public static let subTbr_Converter = "SubTbr_Converter"
- public static let subTbr_Tools = "SubTbr_Tools"
- public static let home = "Home"
- public static let leftSideBar = "LeftSideBar"
- public static let puw = "PUW"
- }
- // 数据埋点 Label 参数
- extension KMAnalytics.Parameter.Label {
- public static let tbr_Btn = "Tbr_Btn"
-
- public static let subTbr_Btn = "SubTbr_Btn"
- public static let ai_Btn = "AI_Btn"
- public static let create_Btn = "Create_Btn"
- public static let leftSideBar_Btn = "LeftSideBar_Btn"
- public static let sub_PUW = "Sub_PUW"
- }
- // 数据埋点 参数
- extension KMAnalytics.Parameter {
- public static let categoryKey = "Category"
- public static let labelKey = "Label"
-
- public struct Category {
-
- }
-
- public struct Label {
-
- }
- }
- // 数据埋点 工具类
- @objc class KMAnalytics: NSObject {
- public struct AppTarget: OptionSet, Codable {
- let rawValue: Int
-
- public static var free = AppTarget(rawValue: 1 << 0)
- public static var pro = AppTarget(rawValue: 1 << 1)
- public static var dmg = AppTarget(rawValue: 1 << 2)
- public static var all: AppTarget = [.free, .pro, .dmg]
- }
-
- public struct Parameter {
- }
-
- public static let Category = KMAnalytics.Parameter.Category.self
- public static let Label = KMAnalytics.Parameter.Label.self
-
- // 配置
- static func configure() {
- #if VERSION_DMG
- AppCenter.start(withAppSecret: "416b8e45-69bd-4a16-8fec-b5206e913c4a", services: [Analytics.self, Crashes.self])
- #else
- AppCenter.start(withAppSecret: "f0d082d0-9581-458c-9069-7aaf0a2b0a8c", services: [Analytics.self, Crashes.self])
- #endif
-
- FirebaseApp.configure()
- // Analytics.logEvent(AnalyticsEventSignUp, parameters: [
- // AnalyticsParameterMethod: "method"
- // ])
-
- #if DEBUG
- var newArguments = ProcessInfo.processInfo.arguments
- newArguments.append("-FIRDebugEnabled")
- ProcessInfo.processInfo.setValue(newArguments, forKey: "arguments")
- #endif
- }
- // 发送事件
- static func trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase, appTarget: AppTarget = [.free]) {
- Swift.debugPrint("trackEvent: \(eventName)")
-
- if (appTarget.contains(.free)) {
- #if VERSION_FREE
- self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
- #endif
- }
- if (appTarget.contains(.pro)) {
- #if VERSION_PRO
- self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
- #endif
- }
- if (appTarget.contains(.dmg)) {
- #if VERSION_DMG
- self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
- #endif
- }
- }
-
- // MARK: - Private Methods
- // 发送事件
- fileprivate static func _trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase) {
- if (platform == .firebase) {
- Analytics.logEvent(eventName, parameters: parameters)
- } else if (platform == .AppCenter) {
- if let data = parameters as? [String : String] {
- Analytics.trackEvent(eventName, withProperties: data)
- }
- }
- }
- }
|