KMAnalytics+KMExtensions.swift 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // KMAnalytics+KMExtensions.swift
  3. // PDF Reader Pro
  4. //
  5. // Created by User-Tangchao on 2024/9/6.
  6. //
  7. import Foundation
  8. // 数据埋点 Category 参数
  9. extension KMAnalytics.Parameter.Category {
  10. public static let purchase_Tools = "Purchase_Tools"
  11. public static let purchase_PageEdit = "Purchase_PageEdit"
  12. public static let purchase_Editor = "Purchase_Editor"
  13. public static let purchase_Converter = "Purchase_Converter"
  14. public static let purchase_Form = "Purchase_Form"
  15. public static let purchase_Sign = "Purchase_Sign"
  16. public static let purchase_EditPDF = "Purchase_EditPDF"
  17. public static let purchase_Redact = "Purchase_Redact"
  18. public static let purchase_OCR = "Purchase_OCR"
  19. public static let purchase_PageDisplay = "Purchase_PageDisplay"
  20. public static let purchase_Others = "Purchase_Others"
  21. }
  22. @objc extension NSViewController {
  23. private static var _eventNameKey = "KMEventNameKey"
  24. var kEventName: String? {
  25. get {
  26. return objc_getAssociatedObject(self, &Self._eventNameKey) as? String
  27. }
  28. set {
  29. objc_setAssociatedObject(self, &Self._eventNameKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
  30. }
  31. }
  32. private static var _eventParamsKey = "KMEventParamsKey"
  33. var kEventParams: [String : Any]? {
  34. get {
  35. return objc_getAssociatedObject(self, &Self._eventParamsKey) as? [String : Any]
  36. }
  37. set {
  38. objc_setAssociatedObject(self, &Self._eventParamsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  39. }
  40. }
  41. private static var _eventTagKey = "KMEventTagKey"
  42. var kEventTag: Int {
  43. get {
  44. return objc_getAssociatedObject(self, &Self._eventTagKey) as? Int ?? 0
  45. }
  46. set {
  47. objc_setAssociatedObject(self, &Self._eventTagKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
  48. }
  49. }
  50. @objc func trackEvent_buyNow_tool(type: CAnnotationType, index: Int) {
  51. KMAnalytics._trackEvent_buyNow_tool(type: type, index: index, appTarget: .all)
  52. }
  53. // MARK: - Track Events Basic
  54. @objc func trackEvent(eventName eveN: String, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  55. KMAnalytics.trackEvent(eventName: eveN, parameters: params, platform: platform, appTarget: .all)
  56. }
  57. @objc func trackEvent_dmg(eventName eveN: String, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  58. KMAnalytics.trackEvent(eventName: eveN, parameters: params, platform: platform, appTarget: .dmg)
  59. }
  60. @objc func trackEvent_free(eventName eveN: String, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  61. KMAnalytics.trackEvent(eventName: eveN, parameters: params, platform: platform, appTarget: .free)
  62. }
  63. @objc func trackEvent_pro(eventName eveN: String, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  64. KMAnalytics.trackEvent(eventName: eveN, parameters: params, platform: platform, appTarget: .pro)
  65. }
  66. @objc func trackEvent_appstore(eventName eveN: String, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  67. KMAnalytics.trackEvent(eventName: eveN, parameters: params, platform: platform, appTarget: [.free, .pro])
  68. }
  69. func clearEventData() {
  70. self.kEventName = nil
  71. self.kEventTag = 0
  72. self.kEventParams = nil
  73. }
  74. }
  75. @objc extension NSWindowController {
  76. private static var _eventNameKey = "KMEventNameKey"
  77. var kEventName: String? {
  78. get {
  79. return objc_getAssociatedObject(self, &Self._eventNameKey) as? String
  80. }
  81. set {
  82. objc_setAssociatedObject(self, &Self._eventNameKey, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
  83. }
  84. }
  85. private static var _eventParamsKey = "KMEventParamsKey"
  86. var kEventParams: [String : Any]? {
  87. get {
  88. return objc_getAssociatedObject(self, &Self._eventParamsKey) as? [String : Any]
  89. }
  90. set {
  91. objc_setAssociatedObject(self, &Self._eventParamsKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
  92. }
  93. }
  94. private static var _eventTagKey = "KMEventTagKey"
  95. var kEventTag: Int {
  96. get {
  97. return objc_getAssociatedObject(self, &Self._eventTagKey) as? Int ?? 0
  98. }
  99. set {
  100. objc_setAssociatedObject(self, &Self._eventTagKey, newValue, .OBJC_ASSOCIATION_ASSIGN)
  101. }
  102. }
  103. // MARK: - Track Events Basic
  104. @objc func trackEvent(eventName eveN: String?, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  105. if let data = eveN {
  106. KMAnalytics.trackEvent(eventName: data, parameters: params, platform: platform, appTarget: .all)
  107. }
  108. }
  109. @objc func trackEvent_dmg(eventName eveN: String?, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  110. if let data = eveN {
  111. KMAnalytics.trackEvent(eventName: data, parameters: params, platform: platform, appTarget: .dmg)
  112. }
  113. }
  114. @objc func trackEvent_free(eventName eveN: String?, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  115. if let data = eveN {
  116. KMAnalytics.trackEvent(eventName: data, parameters: params, platform: platform, appTarget: .free)
  117. }
  118. }
  119. @objc func trackEvent_pro(eventName eveN: String?, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  120. if let data = eveN {
  121. KMAnalytics.trackEvent(eventName: data, parameters: params, platform: platform, appTarget: .pro)
  122. }
  123. }
  124. @objc func trackEvent_appstore(eventName eveN: String?, params: [String : Any]?, platform: KMAnalyticsPlatform) {
  125. if let data = eveN {
  126. KMAnalytics.trackEvent(eventName: data, parameters: params, platform: platform, appTarget: [.free, .pro])
  127. }
  128. }
  129. func clearEventData() {
  130. self.kEventName = nil
  131. self.kEventTag = 0
  132. self.kEventParams = nil
  133. }
  134. }
  135. // MARK: - KMAnalytics Private Methods
  136. extension KMAnalytics {
  137. fileprivate static func _trackEvent_buyNow_tool(type: CAnnotationType, index: Int, platform: KMAnalyticsPlatform = .firebase, appTarget: AppTarget = [.free]) {
  138. if let eventName = KMBuyNowEventNameFromCAnnotationType(type, index: index) {
  139. let params: [String : Any]? = [KMAnalytics.Parameter.categoryKey : "Purchase_Tools", KMAnalytics.Parameter.labelKey : "SubTbr_Tools"]
  140. KMAnalytics.trackEvent(eventName: eventName, parameters: params, platform: platform, appTarget: appTarget)
  141. }
  142. }
  143. }
  144. // MARK: - Pulick Methods
  145. public func KMBuyNowEventNameFromCAnnotationType(_ type: CAnnotationType, index: Int) -> String? {
  146. if type == .link {
  147. if index == 2 {
  148. return "Reading_WebsiteLink_BuyNow"
  149. } else if index == 3 {
  150. return "Reading_EmailLink_BuyNow"
  151. }
  152. } else if type == .stamp {
  153. if index == 2 {
  154. return "Reading_DynamicStamp_BuyNow"
  155. } else if index == 3 {
  156. return "Reading_CustomStamp_BuyNow"
  157. }
  158. } else if type == .signature {
  159. return "Reading_Signature_BuyNow"
  160. }
  161. return nil
  162. }