KMAnalytics.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. // 数据埋点 平台
  12. @objc enum KMAnalyticsPlatform: Int {
  13. case AppCenter
  14. case firebase
  15. }
  16. // 数据埋点 Category 参数
  17. extension KMAnalytics.Parameter.Category {
  18. public static let tbr = "Tbr"
  19. public static let subTbr_annotation = "SubTbr_Annotation"
  20. public static let subTbr_editPDF = "SubTbr_EditPDF"
  21. public static let subTbr_PageEdit = "SubTbr_PageEdit"
  22. public static let subTbr_Converter = "SubTbr_Converter"
  23. public static let subTbr_Tools = "SubTbr_Tools"
  24. public static let home = "Home"
  25. public static let leftSideBar = "LeftSideBar"
  26. public static let puw = "PUW"
  27. }
  28. // 数据埋点 Label 参数
  29. extension KMAnalytics.Parameter.Label {
  30. public static let tbr_Btn = "Tbr_Btn"
  31. public static let subTbr_Btn = "SubTbr_Btn"
  32. public static let ai_Btn = "AI_Btn"
  33. public static let create_Btn = "Create_Btn"
  34. public static let leftSideBar_Btn = "LeftSideBar_Btn"
  35. public static let sub_PUW = "Sub_PUW"
  36. }
  37. // 数据埋点 参数
  38. extension KMAnalytics.Parameter {
  39. public static let categoryKey = "Category"
  40. public static let labelKey = "Label"
  41. public struct Category {
  42. }
  43. public struct Label {
  44. }
  45. }
  46. // 数据埋点 工具类
  47. @objc class KMAnalytics: NSObject {
  48. public struct AppTarget: OptionSet, Codable {
  49. let rawValue: Int
  50. public static var free = AppTarget(rawValue: 1 << 0)
  51. public static var pro = AppTarget(rawValue: 1 << 1)
  52. public static var dmg = AppTarget(rawValue: 1 << 2)
  53. public static var all: AppTarget = [.free, .pro, .dmg]
  54. }
  55. public struct Parameter {
  56. }
  57. public static let Category = KMAnalytics.Parameter.Category.self
  58. public static let Label = KMAnalytics.Parameter.Label.self
  59. // 配置
  60. static func configure() {
  61. #if DEBUG
  62. #if VERSION_FREE
  63. #if VERSION_DMG
  64. let appSecret = "fb9d37aa-e3f2-4969-bd06-f65ce529a565"
  65. #else
  66. let appSecret = "8c08296d-ca5c-44da-b68a-b4382f119b1f"
  67. #endif
  68. #else
  69. let appSecret = "54212f10-3ac9-42d9-96c0-5387f4b78d30"
  70. #endif
  71. AppCenter.start(withAppSecret: appSecret, services: [Analytics.self, Crashes.self])
  72. #else
  73. #if VERSION_FREE
  74. #if VERSION_DMG
  75. let appSecret = "fb9d37aa-e3f2-4969-bd06-f65ce529a565"
  76. #else
  77. let appSecret = "8c08296d-ca5c-44da-b68a-b4382f119b1f"
  78. #endif
  79. #else
  80. let appSecret = "54212f10-3ac9-42d9-96c0-5387f4b78d30"
  81. #endif
  82. AppCenter.start(withAppSecret: appSecret, services: [Analytics.self, Crashes.self])
  83. #endif
  84. }
  85. // 发送事件
  86. static func trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase, appTarget: AppTarget = [.free]) {
  87. // Swift.debugPrint("trackEvent: \(eventName)")
  88. if (appTarget.contains(.free)) {
  89. #if VERSION_FREE
  90. #if VERSION_DMG
  91. #else
  92. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  93. #endif
  94. #endif
  95. }
  96. if (appTarget.contains(.pro)) {
  97. #if VERSION_FREE
  98. #else
  99. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  100. #endif
  101. }
  102. if (appTarget.contains(.dmg)) {
  103. #if VERSION_DMG
  104. self._trackEvent(eventName: eventName, parameters: parameters, platform: platform)
  105. #endif
  106. }
  107. }
  108. // MARK: - Private Methods
  109. // 发送事件
  110. fileprivate static func _trackEvent(eventName: String, parameters: [String : Any]? = nil, platform: KMAnalyticsPlatform = .firebase) {
  111. if (platform == .firebase) {
  112. } else if (platform == .AppCenter) {
  113. if let data = parameters as? [String : String] {
  114. Analytics.trackEvent(eventName, withProperties: data)
  115. }
  116. }
  117. }
  118. }