KMAnalytics.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 DEBUG
  64. #else
  65. #if VERSION_DMG
  66. AppCenter.start(withAppSecret: "416b8e45-69bd-4a16-8fec-b5206e913c4a", services: [Analytics.self, Crashes.self])
  67. #else
  68. AppCenter.start(withAppSecret: "f0d082d0-9581-458c-9069-7aaf0a2b0a8c", services: [Analytics.self, Crashes.self])
  69. #endif
  70. #endif
  71. FirebaseApp.configure()
  72. // Analytics.logEvent(AnalyticsEventSignUp, parameters: [
  73. // AnalyticsParameterMethod: "method"
  74. // ])
  75. #if DEBUG
  76. var newArguments = ProcessInfo.processInfo.arguments
  77. newArguments.append("-FIRDebugEnabled")
  78. ProcessInfo.processInfo.setValue(newArguments, forKey: "arguments")
  79. #endif
  80. }
  81. // 发送事件
  82. static func trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase, appTarget: AppTarget = [.free]) {
  83. Swift.debugPrint("trackEvent: \(eventName)")
  84. if (appTarget.contains(.free)) {
  85. #if VERSION_FREE
  86. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  87. #endif
  88. }
  89. if (appTarget.contains(.pro)) {
  90. #if VERSION_PRO
  91. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  92. #endif
  93. }
  94. if (appTarget.contains(.dmg)) {
  95. #if VERSION_DMG
  96. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  97. #endif
  98. }
  99. }
  100. // MARK: - Private Methods
  101. // 发送事件
  102. fileprivate static func _trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase) {
  103. if (platform == .firebase) {
  104. Analytics.logEvent(eventName, parameters: parameters)
  105. } else if (platform == .AppCenter) {
  106. if let data = parameters as? [String : String] {
  107. Analytics.trackEvent(eventName, withProperties: data)
  108. }
  109. }
  110. }
  111. }