KMAnalytics.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // KMAnalytics.swift
  3. // PDF Master
  4. //
  5. // Created by tangchao on 2023/7/12.
  6. //
  7. import Cocoa
  8. import AppCenter
  9. import AppCenterAnalytics
  10. import AppCenterCrashes
  11. import FirebaseCore
  12. import FirebaseAnalytics
  13. // 数据埋点 平台
  14. @objc enum KMAnalyticsPlatform: Int {
  15. case AppCenter
  16. case firebase
  17. }
  18. // 数据埋点 Category 参数
  19. extension KMAnalytics.Parameter.Category {
  20. public static let tbr = "Tbr"
  21. public static let subTbr_annotation = "SubTbr_Annotation"
  22. public static let subTbr_editPDF = "SubTbr_EditPDF"
  23. public static let subTbr_PageEdit = "SubTbr_PageEdit"
  24. public static let subTbr_Converter = "SubTbr_Converter"
  25. public static let subTbr_Tools = "SubTbr_Tools"
  26. public static let home = "Home"
  27. public static let leftSideBar = "LeftSideBar"
  28. public static let puw = "PUW"
  29. }
  30. // 数据埋点 Label 参数
  31. extension KMAnalytics.Parameter.Label {
  32. public static let tbr_Btn = "Tbr_Btn"
  33. public static let subTbr_Btn = "SubTbr_Btn"
  34. public static let ai_Btn = "AI_Btn"
  35. public static let create_Btn = "Create_Btn"
  36. public static let leftSideBar_Btn = "LeftSideBar_Btn"
  37. public static let sub_PUW = "Sub_PUW"
  38. }
  39. // 数据埋点 参数
  40. extension KMAnalytics.Parameter {
  41. public static let categoryKey = "Category"
  42. public static let labelKey = "Label"
  43. public struct Category {
  44. }
  45. public struct Label {
  46. }
  47. }
  48. // 数据埋点 工具类
  49. @objc class KMAnalytics: NSObject {
  50. public struct AppTarget: OptionSet, Codable {
  51. let rawValue: Int
  52. public static var free = AppTarget(rawValue: 1 << 0)
  53. public static var pro = AppTarget(rawValue: 1 << 1)
  54. public static var dmg = AppTarget(rawValue: 1 << 2)
  55. public static var all: AppTarget = [.free, .pro, .dmg]
  56. }
  57. public struct Parameter {
  58. }
  59. public static let Category = KMAnalytics.Parameter.Category.self
  60. public static let Label = KMAnalytics.Parameter.Label.self
  61. // 配置
  62. static func configure() {
  63. #if VERSION_DMG
  64. AppCenter.start(withAppSecret: "416b8e45-69bd-4a16-8fec-b5206e913c4a", services: [Analytics.self, Crashes.self])
  65. #else
  66. AppCenter.start(withAppSecret: "f0d082d0-9581-458c-9069-7aaf0a2b0a8c", services: [Analytics.self, Crashes.self])
  67. #endif
  68. FirebaseApp.configure()
  69. // Analytics.logEvent(AnalyticsEventSignUp, parameters: [
  70. // AnalyticsParameterMethod: "method"
  71. // ])
  72. #if DEBUG
  73. var newArguments = ProcessInfo.processInfo.arguments
  74. newArguments.append("-FIRDebugEnabled")
  75. ProcessInfo.processInfo.setValue(newArguments, forKey: "arguments")
  76. #endif
  77. }
  78. // 发送事件
  79. static func trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase, appTarget: AppTarget = [.free]) {
  80. Swift.debugPrint("trackEvent: \(eventName)")
  81. if (appTarget.contains(.free)) {
  82. #if VERSION_FREE
  83. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  84. #endif
  85. }
  86. if (appTarget.contains(.pro)) {
  87. #if VERSION_PRO
  88. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  89. #endif
  90. }
  91. if (appTarget.contains(.dmg)) {
  92. #if VERSION_DMG
  93. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  94. #endif
  95. }
  96. }
  97. // MARK: - Private Methods
  98. // 发送事件
  99. fileprivate static func _trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase) {
  100. if (platform == .firebase) {
  101. Analytics.logEvent(eventName, parameters: parameters)
  102. } else if (platform == .AppCenter) {
  103. if let data = parameters as? [String : String] {
  104. Analytics.trackEvent(eventName, withProperties: data)
  105. }
  106. }
  107. }
  108. }