KMAnalytics.swift 4.1 KB

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