KMAnalytics.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // KMAnalytics.swift
  3. // PDF Reader Pro
  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. #if VERSION_FREE
  65. #if VERSION_DMG
  66. let appSecret = "fb9d37aa-e3f2-4969-bd06-f65ce529a565"
  67. #else
  68. let appSecret = "8c08296d-ca5c-44da-b68a-b4382f119b1f"
  69. #endif
  70. #else
  71. let appSecret = "54212f10-3ac9-42d9-96c0-5387f4b78d30"
  72. #endif
  73. AppCenter.start(withAppSecret: appSecret, services: [Analytics.self, Crashes.self])
  74. #else
  75. // #if VERSION_DMG
  76. // AppCenter.start(withAppSecret: "416b8e45-69bd-4a16-8fec-b5206e913c4a", services: [Analytics.self, Crashes.self])
  77. // #else
  78. // AppCenter.start(withAppSecret: "f0d082d0-9581-458c-9069-7aaf0a2b0a8c", services: [Analytics.self, Crashes.self])
  79. // #endif
  80. #if VERSION_FREE
  81. #if VERSION_DMG
  82. let appSecret = "fb9d37aa-e3f2-4969-bd06-f65ce529a565"
  83. #else
  84. let appSecret = "8c08296d-ca5c-44da-b68a-b4382f119b1f"
  85. #endif
  86. #else
  87. let appSecret = "54212f10-3ac9-42d9-96c0-5387f4b78d30"
  88. #endif
  89. AppCenter.start(withAppSecret: appSecret, services: [Analytics.self, Crashes.self])
  90. #endif
  91. // let version = AppCenter.sdkVersion
  92. // KMPrint("AppCenter version is \(version)")
  93. // FirebaseApp.configure()
  94. // Analytics.logEvent(AnalyticsEventSignUp, parameters: [
  95. // AnalyticsParameterMethod: "method"
  96. // ])
  97. #if DEBUG
  98. // var newArguments = ProcessInfo.processInfo.arguments
  99. // newArguments.append("-FIRDebugEnabled")
  100. // ProcessInfo.processInfo.setValue(newArguments, forKey: "arguments")
  101. #endif
  102. }
  103. // 发送事件
  104. static func trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase, appTarget: AppTarget = [.free]) {
  105. // Swift.debugPrint("trackEvent: \(eventName)")
  106. if (appTarget.contains(.free)) {
  107. #if VERSION_FREE
  108. #if VERSION_DMG
  109. #else
  110. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  111. #endif
  112. #endif
  113. }
  114. if (appTarget.contains(.pro)) {
  115. #if VERSION_FREE
  116. #else
  117. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  118. #endif
  119. }
  120. if (appTarget.contains(.dmg)) {
  121. #if VERSION_DMG
  122. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  123. #endif
  124. }
  125. }
  126. // MARK: - Private Methods
  127. // 发送事件
  128. fileprivate static func _trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase) {
  129. if (platform == .firebase) {
  130. Analytics.logEvent(eventName, parameters: parameters)
  131. } else if (platform == .AppCenter) {
  132. if let data = parameters as? [String : String] {
  133. Analytics.trackEvent(eventName, withProperties: data)
  134. }
  135. }
  136. }
  137. }