KMAnalytics.swift 4.5 KB

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