BFBaseEventTrack.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // BFBaseEventTrack.swift
  3. // PQSpeed
  4. //
  5. // Created by SanW on 2020/11/3.
  6. // Copyright © 2020 BytesFlow. All rights reserved.
  7. //
  8. import BFCommonKit
  9. import BFNetRequestKit
  10. import UIKit
  11. // MARK: - 埋点数据上报
  12. /// 埋点数据上报
  13. public class BFBaseEventTrack: NSObject {
  14. /// 基础数据上报 - frontend库上报
  15. /// - Parameters:
  16. /// - logType: 数据库类型
  17. /// - businessType: businessType
  18. /// - objectType: objectType
  19. /// - eventData: eventData
  20. /// - pageSource: pageSource页面场景
  21. /// - extParams: extParams 扩展字段,为json对象
  22. /// - remindmsg: remindmsg 打印提示信息
  23. /// - Returns: <#description#>
  24. public class func bf_frontendReportUpload(url: String, logType: Int = 30, businessType: String?, objectType: String?, pageSource: String?, params: [String: Any]? = nil, eventData: [String: Any]? = nil, extParams: [String: Any]? = nil, commonParams: [String: Any]? = nil) {
  25. DispatchQueue.global().async {
  26. // LogType
  27. var tempParams: [String: Any] = params ?? [:]
  28. tempParams["LogType"] = logType
  29. // pageSource
  30. if pageSource != nil {
  31. tempParams["pageSource"] = pageSource ?? ""
  32. }
  33. // eventData
  34. var tempEventData: [String: Any] = eventData ?? [:]
  35. if objectType != nil {
  36. tempEventData["objectType"] = objectType ?? ""
  37. }
  38. if businessType != nil {
  39. tempEventData["businessType"] = businessType ?? ""
  40. }
  41. if tempEventData.keys.count > 0 {
  42. tempParams["eventData"] = bf_dictionaryToJsonString(tempEventData)
  43. }
  44. // extParams
  45. if extParams != nil, (extParams?.keys.count ?? 0) > 0 {
  46. tempParams["extParams"] = bf_dictionaryToJsonString(extParams!)
  47. }
  48. BFNetRequestAdaptor.postRequestData(url: url, parames: tempParams, commonParams: commonParams) { _, _, _, _ in
  49. }
  50. }
  51. }
  52. /// 消息数据上报
  53. /// - Parameters:
  54. /// - messageIds: 消息Id,多个用逗号分隔
  55. /// - clickId: 子入口点击ID,标识一次子入口点击动作。子入口内消息列表中的消息点击行为都带有此字段,分享空间消息除外
  56. /// - messageType: 消息类型
  57. /// - messageSubType: 消息子类型
  58. /// - actionType: 动作类型(backendCreate:后端构建;backendReturn:后端返回;frontendPull:前端拉取;view:曝光;click:点击)
  59. /// - objectType: <#objectType description#>
  60. /// - pageSource: <#pageSource description#>
  61. /// - readStatus: 已读状态:1:页面上显示未读 2:页面上显示已读
  62. /// - eventData: 扩展数据,json格式,日志系统里会展开存储
  63. /// - extParams: 扩展字段 json格式
  64. /// - remindmsg: 打印提示信息
  65. /// - Returns: <#description#>
  66. public class func bf_messageReportUpload(url: String, messageIds: String?, clickId: String?, messageType: Int?, messageSubType: Int?, actionType: String?, readStatus: Int = 1, objectType: String?, pageSource: String?, eventData: [String: Any]? = nil, extParams: [String: Any]? = nil, commonParams: [String: Any]? = nil) {
  67. DispatchQueue.global().async {
  68. var tempParams: [String: Any] = extParams ?? [:]
  69. if messageIds != nil {
  70. tempParams["messageIds"] = messageIds
  71. }
  72. if messageType != nil {
  73. tempParams["messageType"] = messageType ?? ""
  74. }
  75. if messageSubType != nil {
  76. tempParams["messageSubType"] = messageSubType ?? ""
  77. }
  78. if actionType != nil {
  79. tempParams["actionType"] = actionType ?? ""
  80. }
  81. tempParams["readStatus"] = readStatus + 1
  82. if pageSource != nil {
  83. tempParams["pageSource"] = pageSource ?? ""
  84. }
  85. // eventData
  86. var tempEventData: [String: Any] = eventData ?? [:]
  87. if objectType != nil {
  88. tempEventData["objectType"] = objectType ?? ""
  89. }
  90. if tempEventData.keys.count > 0 {
  91. tempParams["eventData"] = bf_dictionaryToJsonString(tempEventData)
  92. }
  93. // extParams
  94. var tempExtParams: [String: Any] = extParams ?? [:]
  95. if clickId != nil {
  96. tempExtParams["clickId"] = clickId
  97. }
  98. if tempExtParams.keys.count > 0 {
  99. tempParams["extParams"] = bf_dictionaryToJsonString(tempExtParams)
  100. }
  101. BFNetRequestAdaptor.postRequestData(url: url, parames: tempParams, commonParams: commonParams) { _, _, _, _ in
  102. }
  103. }
  104. }
  105. /// 视频相关数据上报
  106. /// - Parameters:
  107. /// - reportLogType: 日志类型
  108. /// - videoId: 视频Id
  109. /// - headVideoId: 当前的相关推荐视频是属于哪个视频的相关推荐,值为那个头部视频的videoId
  110. /// - videoIds: reportLogType_view时传
  111. /// - pageSource: 页面
  112. /// - playId: 播放ID 对于每一次播放操作,生成唯一playid,标示唯一一次播放操作,视频播放中暂停,再继续播放时,不算一次新的播放,不需要生成新的playid。重播视频算一次新的播放,即需要生成新的playid。
  113. /// - recommendId: 推荐链路ID 列表返回
  114. /// - recommendLogVO: 推荐日志对象 列表返回
  115. /// - abInfoData: AB信息 列表返回
  116. /// - measureType:
  117. /// - measureId:
  118. /// - businessType: 操作类型
  119. /// - targetUid: 视频用户ID
  120. /// - Returns: <#description#>
  121. public class func bf_videoReportUpload(url: String, videoId: String?, headVideoId: String?, playId: String?, recommendId: String?, recommendLogVO: String?, flowPool: String?, abInfoData: String?, measureType: Int?, measureId: Int?, targetUid: Int = 0, businessType: String?, objectType: String?, pageSource: String?, params: [String: Any]? = nil, eventData: [String: Any]? = nil, extParams: [String: Any]? = nil, commonParams: [String: Any]? = nil) {
  122. DispatchQueue.global().async {
  123. var tempParams: [String: Any] = params ?? [:]
  124. if !tempParams.keys.contains("videoId") {
  125. tempParams["videoId"] = Int64(videoId ?? "") ?? 0
  126. }
  127. if !tempParams.keys.contains("pageSource") {
  128. tempParams["pageSource"] = pageSource ?? ""
  129. }
  130. if !tempParams.keys.contains("playId") {
  131. tempParams["playId"] = playId ?? ""
  132. }
  133. if !tempParams.keys.contains("targetUid") {
  134. tempParams["targetUid"] = targetUid
  135. }
  136. if measureType != nil {
  137. tempParams["measureType"] = measureType
  138. }
  139. if measureId != nil {
  140. tempParams["measureId"] = measureId
  141. }
  142. if recommendId != nil, !(recommendId?.isEmpty ?? true) {
  143. tempParams["recommendId"] = recommendId
  144. }
  145. if recommendLogVO != nil, !(recommendLogVO?.isEmpty ?? true) {
  146. tempParams["recommendLogVO"] = recommendLogVO
  147. }
  148. if flowPool != nil, (flowPool?.count ?? 0) > 0 {
  149. tempParams["flowPool"] = flowPool
  150. }
  151. if abInfoData != nil, !(abInfoData?.isEmpty ?? true) {
  152. tempParams["abInfoData"] = abInfoData
  153. }
  154. if pageSource?.contains("speedApp-category") ?? false {
  155. tempParams["pageCategoryId"] = 55
  156. }
  157. // eventData
  158. var tempEventData: [String: Any] = eventData ?? [:]
  159. if objectType != nil {
  160. tempEventData["objectType"] = objectType ?? ""
  161. }
  162. if businessType != nil {
  163. tempEventData["businessType"] = businessType ?? ""
  164. tempParams["businessType"] = businessType ?? ""
  165. }
  166. tempEventData["pageSource"] = pageSource ?? ""
  167. // extParams
  168. var tempExtParams: [String: Any] = extParams ?? [:]
  169. if headVideoId != nil, (headVideoId?.count ?? 0) > 0 {
  170. tempExtParams["headVideoId"] = Int64(headVideoId ?? "0") ?? 0
  171. }
  172. if videoId != nil, (videoId?.count ?? 0) > 0 {
  173. tempExtParams["videoId"] = Int64(videoId ?? "") ?? 0
  174. }
  175. if tempEventData.keys.count > 0 {
  176. tempParams["eventData"] = bf_dictionaryToJsonString(tempEventData)
  177. }
  178. if tempExtParams.keys.count > 0 {
  179. tempParams["extParams"] = bf_dictionaryToJsonString(tempExtParams)
  180. }
  181. BFNetRequestAdaptor.postRequestData(url: url, parames: tempParams, commonParams: commonParams) { _, _, _, _ in
  182. }
  183. }
  184. }
  185. // 分享数据上报
  186. // - Parameters:
  187. // - isShareVideo: 是否是分享视频
  188. // - screenType: 分享场景 1-分享视频/用户 2-分享视频到朋友圈 3-分享视频到好友
  189. // - videoId: 视频Id
  190. // - pageSource: 页面枚举
  191. // - recommendId: <#recommendId description#>
  192. // - recommendLogVO: <#recommendLogVO description#>
  193. // - abInfoData: <#abInfoData description#>
  194. // - measureType: <#measureType description#>
  195. // - measureId: <#measureId description#>
  196. // - businessType: <#businessType description#>
  197. // - targetUid: <#targetUid description#>
  198. // - shareId: <#shareId description#>
  199. public class func bf_shareReportUpload(url: String, isShareVideo: Bool = true, screenType _: Int = 1, videoId: String?, recommendId: String?, recommendLogVO: String?, flowPool: String?, abInfoData: String?, measureType: Int?, measureId: Int?, targetUid: Int?, shareId: String = "", playId: String?, wxOpenId: String?, rootPageTimestamp: Int64, businessType: String?, pageSource: String?, extParams: [String: Any]? = nil, commonParams: [String: Any]? = nil) {
  200. DispatchQueue.global().async {
  201. var params: [String: Any] = ["type": isShareVideo ? "1" : "2", "videoId": videoId ?? "", "pageSource": pageSource ?? "", "playId": playId ?? "", "targetUid": targetUid ?? 0, "shareDepth": "0", "rootPageTimestamp": rootPageTimestamp]
  202. if extParams != nil {
  203. params["extParams"] = bf_dictionaryToJsonString(extParams!)
  204. }
  205. params["eventData"] = bf_dictionaryToJsonString(["rootPageTimestamp": rootPageTimestamp])
  206. if measureType != nil {
  207. params["measureType"] = measureType
  208. }
  209. if measureId != nil {
  210. params["measureId"] = measureId
  211. }
  212. params["shareId"] = shareId
  213. params["rootLaunchShareId"] = shareId
  214. params["parentShareId"] = shareId
  215. params["rootShareId"] = shareId
  216. if wxOpenId != nil {
  217. params["shareUi"] = wxOpenId ?? ""
  218. }
  219. if pageSource?.contains("speedApp-category") ?? false {
  220. params["pageCategoryId"] = 55
  221. }
  222. if isShareVideo {
  223. params["shareObjectId"] = videoId
  224. } else {
  225. params["shareObjectId"] = targetUid
  226. }
  227. if businessType != nil {
  228. params["businessType"] = businessType ?? ""
  229. }
  230. if recommendId != nil, !(recommendId?.isEmpty ?? true) {
  231. params["recommendId"] = recommendId
  232. }
  233. if recommendLogVO != nil, !(recommendLogVO?.isEmpty ?? true) {
  234. params["recommendLogVO"] = recommendLogVO
  235. }
  236. if flowPool != nil, (flowPool?.count ?? 0) > 0 {
  237. params["flowPool"] = flowPool
  238. }
  239. if abInfoData != nil, !(abInfoData?.isEmpty ?? true) {
  240. params["abInfoData"] = abInfoData
  241. }
  242. BFNetRequestAdaptor.postRequestData(url: url, parames: params, commonParams: commonParams) { _, _, _, _ in
  243. }
  244. }
  245. }
  246. /// 推送点击数据上报
  247. /// - Parameters:
  248. /// - pushId: 推送Id
  249. /// - pushTargetType: 1-推送单个视频 2-整体关注有更新 3-关注单个up主有更新(暂废弃) 4-订阅某人有更新
  250. /// - pushBrand: 推送平台 APPLE_TYPE
  251. /// - pushTargetId: pushTargetType == 1 视频ID,pushTargetType == 4 用户ID
  252. /// - bizParam 扩展参数
  253. /// - Returns: <#description#>
  254. public class func bf_pushActionReportUpload(url: String, pushId: String, pushTargetType: Int, pushBrand: String?, pushTargetId: String?, bizParam: [String: Any]? = nil, commonParams: [String: Any]? = nil) {
  255. DispatchQueue.global().async {
  256. var params: [String: Any] = bizParam ?? Dictionary<String, Any>.init()
  257. if pushTargetId != nil {
  258. params["pushTargetId"] = pushTargetId
  259. }
  260. if pushId.count > 0 {
  261. params["pushId"] = pushId
  262. }
  263. params["pushTargetType"] = pushTargetType
  264. params["pushBrand"] = pushBrand
  265. params["pushReportType"] = "click"
  266. if params.keys.contains("aps") {
  267. params.removeValue(forKey: "aps")
  268. }
  269. BFNetRequestAdaptor.postRequestData(url: url, parames: params, commonParams: commonParams) { _, _, _, _ in
  270. }
  271. }
  272. }
  273. /// deviceToken数据上报
  274. /// - Parameter registerId: 设备id
  275. /// - Parameter deviceToken: <#deviceToken description#>
  276. /// - Returns: <#description#>
  277. public class func bf_deviceTokenReportUpload(url: String, registerId: String, deviceToken: String, brand: String, commonParams: [String: Any]? = nil, completeHander: @escaping (_ isSuccess: Bool) -> Void) {
  278. DispatchQueue.global().async {
  279. BFNetRequestAdaptor.postRequestData(url: url, parames: ["registerId": registerId, "deviceToken": deviceToken, "brand": brand], commonParams: commonParams) { _, _, error, _ in
  280. completeHander(error == nil ? true : false)
  281. }
  282. }
  283. }
  284. /// 搜索数据上报
  285. /// - Parameters:
  286. /// - keyWord: 搜索词
  287. /// - searchType: 1 热搜词搜索 2 历史记录 3 普通搜索
  288. /// - searchNumber: 数量
  289. /// - reportType: 1 into 2 click 3 show
  290. /// - Returns: <#description#>
  291. public class func bf_searchReportUpload(url: String, keyWord: String, searchType: Int, searchNumber: Int = 10, reportType: Int = 2, commonParams: [String: Any]? = nil) {
  292. DispatchQueue.global().async {
  293. BFNetRequestAdaptor.postRequestData(url: url, parames: ["keyWord": keyWord, "searchType": searchType, "searchNumber": searchNumber, "reportType": reportType], commonParams: commonParams) { _, _, _, _ in
  294. }
  295. }
  296. }
  297. /// 冷热启动数据上报
  298. /// - Parameters:
  299. /// - url: 上报地址
  300. /// - isFirstLaunch: 是否是第一次安装 1-是 0-否
  301. /// - firstParams: 第一次启动未上报数据
  302. /// - isHotLaunch: 是否是热启动
  303. /// - launchType:启动类型
  304. /// - commandLaunchParams: 剪切板数据
  305. /// - commandReportParams: 剪切板上报数据
  306. /// - params: <#params description#>
  307. /// - eventData: <#eventData description#>
  308. /// - extParams: <#extParams description#>
  309. /// - commonParams: <#commonParams description#>
  310. public class func appLaunchReportUpload(url: String, isFirstLaunch: Int = 1, firstParams: [String: Any]? = nil, isHotLaunch: Bool = false, launchType: String = "userActiveOpen", commandLaunchParams: [String: Any]? = nil, commandReportParams: [String: Any]? = nil, params: [String: Any]? = nil, eventData _: [String: Any]? = nil, extParams: [String: Any]? = nil, commonParams: [String: Any]?) {
  311. DispatchQueue.global().async {
  312. var params: [String: Any] = params ?? [:]
  313. params["LogType"] = "60"
  314. // 参数
  315. var extParams: [String: Any] = Dictionary<String, Any>.init()
  316. extParams["downloadChannel"] = "AppStore"
  317. if commandLaunchParams != nil {
  318. extParams["launchParams"] = commandLaunchParams!
  319. }
  320. if commandReportParams != nil {
  321. for (key, value) in commandReportParams!.reversed() {
  322. extParams[key] = value
  323. }
  324. }
  325. if !isHotLaunch {
  326. extParams["coldLaunchType"] = launchType
  327. } else {
  328. extParams["hotLaunchType"] = launchType
  329. }
  330. // 是否第一次安装
  331. extParams["isFirstLaunch"] = isFirstLaunch
  332. params["extParams"] = bf_dictionaryToJsonString(extParams)
  333. // 第一次未启动数据
  334. if isFirstLaunch == 1, firstParams != nil, (firstParams?.keys.count ?? 0) > 0 {
  335. for (key, value) in firstParams!.reversed() {
  336. params[key] = value
  337. }
  338. }
  339. BFNetRequestAdaptor.postRequestData(url: url, parames: params, commonParams: commonParams) { response, _, error, _ in
  340. BFLog(message: "冷热启动上报:\(String(describing: error)),\(response ?? [:]),params = \(params)")
  341. }
  342. }
  343. }
  344. }