KMAnalytics.swift 3.9 KB

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