sensors.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. import { ENVIRONMENTS } from '@/common/js/BaseDictionary';
  2. import sensors from '@/lib/Sensorsdata-UniPlugin-JS';
  3. import Vue from 'vue';
  4. import uni from '@/utils/uniHooks';
  5. import store from '@/store/index'
  6. import { getPlatform } from '@/utils/index';
  7. /**
  8. * 上传埋点数据至神策(第三方埋点平台)
  9. * param eventName string 埋点事件名
  10. * param properites object 埋点数据 (包含action, current event duration)
  11. * param eventEnable boolean 是否允许上传埋点数据,默认为true
  12. * param owner string 埋点负责人,默认为undefined
  13. * */
  14. const normalTrack = (eventName, properites, trackingEnable = true) => {
  15. if (trackingEnable)
  16. sensors.track(eventName, properites);
  17. }
  18. // 设置全埋点事件及埋点公共字段(仅在小程序中实现,H5另定)
  19. const trackingInit = () => {
  20. const platform = getPlatform();
  21. if ( platform !== 'miniprogram') {
  22. return
  23. }
  24. // console.log(23, window.toWXSendMsg);
  25. // console.log(23, store.state.appId);
  26. // 微信小程序
  27. if (getPlatform() === 'miniprogram') {
  28. // window.toWXSendMsg({
  29. // type: 'scanQRCode',
  30. // });
  31. // window.subscribe('scanQRCodeOver', (options) => {
  32. // console.log('微信扫码结束之后的返回参数', options);
  33. // runScanFn(options);
  34. // });
  35. }
  36. if (store.state.appId === '') {
  37. // window.toWXSendMsg =
  38. }
  39. return
  40. const project = 'kerryplus';
  41. const defaultLbs = uni.getStorageSync("DEFAULT_LBS");
  42. const lbsId = defaultLbs.lbs?.id;
  43. const lbsName = defaultLbs.lbs?.name;
  44. const brandId = defaultLbs.lbs?.brand.id;
  45. const brandName = defaultLbs.lbs?.brand.name;
  46. const openId = uni.getStorageSync("OPENID");
  47. const profileId = uni.getStorageSync("PROFILE_ID");
  48. //设置全埋点事件和其他参数
  49. sensors.setPara({
  50. name: 'sensors',
  51. server_url: `${environment.trackingBaseUrl}?project=${project}&remark=${environment.trackingRemarkPre}_${lbsId}`,
  52. //全埋点控制开关
  53. autoTrack: {
  54. // $MPClick小程序元素点击,当 Page 中定义的事件处理函数被触发时采集。
  55. // 目前只支持 tap、longtap、longpress 三类事件
  56. mpClick: true,
  57. // $MPLaunch小程序初始化完成时触发或者小程序进入后台一定时间后被微信杀死进程后再次启动小程序时触发
  58. appLaunch: true,
  59. // $MPShow小程序显示,小程序启动时触发或者从后台切换到前台时触发
  60. appShow: true,
  61. // $MPHide小程序从前台进入后台
  62. appHide: true,
  63. // $MPViewScreen小程序页面浏览,打开一个小程序页面时触发
  64. pageShow: true,
  65. // $MPShare小程序分享,设置 Page.onShareAppMessage 这个函数后,点击小程序右上角三个点,
  66. // 然后点击”发送给朋友“触发上报
  67. pageShare: true,
  68. // $MPPageLeave 预置事件中会采集 event_duration 预置属性来记录页面浏览时长,单位为秒
  69. pageLeave: true,
  70. // $MPAddFavorites小程序收藏,在微信中,仅 Android 手机有”收藏“功能,所以只有 Android 手机支持此事件
  71. // page.onAddToFavorites
  72. mpFavorite: true, //默认为true
  73. },
  74. //自定义渠道追踪参数,如source_channel:['custom_param']
  75. source_channel: ['tpName'], //这里增加一个tpName。用来给tpName走UTM逻辑
  76. //是否允许控制台打印查看埋点数据(建议打开)
  77. show_log: false,
  78. //是否允许修改onShareAppMessage里return的path,用来增加(登录ID,分享层级,当前的path),
  79. //在app onShow中自动获取这些参数来查看具体分享来源、层级等
  80. allow_amend_share_path: true,
  81. //是否允许批量发送
  82. batch_send: false,
  83. app_flush_interval: 15000, //设置两次数据发送的最小时间间隔 @Platform Android&iOS
  84. app_flush_bulkSize: 100, //设置本地缓存日志的最大条目数,最小 50 条 @Platform Android&iOS
  85. app_flush_network_policy: 30, //设置 flush 时网络发送策略,默认 3G、4G、WI-FI 环境下都会尝试 flush @Platform Android&iOS
  86. app_session_interval_time: 30000, //Session 时长,若 App 在后台超过设定事件,则认为当前 Session 结束,发送 $AppEnd 事件,单位毫秒 @Platform Andorid
  87. app_data_collect: true
  88. });
  89. //对于所有事件都需要添加的属性,可在初始化 SDK 前,调用 registerApp() 将属性注册为公共属性
  90. sensors.register({
  91. saglobal_product_name: project + '_c',
  92. saglobal_digital_brand_id: brandId,
  93. saglobal_digital_brand_name: brandName,
  94. saglobal_lbs_id: lbsId,
  95. saglobal_lbs_name: lbsName,
  96. saglobal_open_id: openId,
  97. saglobal_profile_id: profileId
  98. });
  99. sensors.registerApp({
  100. saglobal_product_name: project,
  101. //小程序的brand
  102. saglobal_digital_brand_id: uni.getStorageSync('groupId') || '',
  103. //小程序的LBS ID
  104. saglobal_lbs_id: uni.getStorageSync('mallid') || '',
  105. // 小程序的LBS Name
  106. saglobal_lbs_name: _this.globalData.marketName || '',
  107. })
  108. //埋点初始化
  109. sensors.init();
  110. }
  111. class TrackFactory {
  112. /**
  113. * 埋点数据初始化
  114. * param eventName string 埋点事件名
  115. * param eventData object 埋点数据
  116. * param isUpload boolean 是否允许上传埋点数据,默认为true
  117. * param owner string 埋点负责人,默认为undefined
  118. * */
  119. constructor(eventName, eventData, isUpload = true, owner = 'undefined') {
  120. this.eventData = {};
  121. this.isUpload = true;
  122. this.eventName = '';
  123. this.timeStart = new Date().getTime();
  124. this.apiTimeStart = new Date().getTime();
  125. this.instanceRecorder = {};
  126. eventData = eventData || {};
  127. eventData['owner'] = owner;
  128. this.isUpload = isUpload;
  129. this.eventName = eventName;
  130. this.eventData = eventData;
  131. // 记录页面展示action
  132. this.track(1);
  133. }
  134. /**
  135. * 获取埋点事件实例
  136. * param eventName string 埋点事件名
  137. * param eventData object 埋点数据
  138. * param isForce boolean 是否强制实例化
  139. * param isUpload boolean 是否允许上传埋点数据,默认为true
  140. * param owner string 埋点负责人,默认为undefined
  141. *
  142. * return object
  143. * */
  144. static getInstance(eventName, eventData, isForce, isUpload, owner) {
  145. // 判断埋点事件名是否被注册,利用instanceRecorder记录被注册埋点事件实例
  146. // 如果已经实例化过,就直接返回实例,无需再new
  147. // 如果要强制实例化,可以把forceInit设为true
  148. if (!this.instanceRecorder.hasOwnProperty(eventName) || isForce) {
  149. eventData = eventData || {};
  150. this.instanceRecorder[eventName] = new TrackFactory(eventName, eventData, isUpload, owner);
  151. }
  152. return this.instanceRecorder[eventName];
  153. }
  154. /**
  155. * 销毁埋点事件实例
  156. * param eventName string 埋点事件名
  157. * */
  158. static destory(eventName) {
  159. delete this.instanceRecorder[eventName];
  160. }
  161. /**
  162. * 记录埋点的action
  163. * param action number action code
  164. * param onceData object 针对指定action的埋点数据,具体见埋点需求
  165. * */
  166. track(action, onceData = {}) {
  167. if (action == 100 || action == 600) {
  168. this.apiTimeStart = new Date().getTime();
  169. this.eventData['user_mainstory_timecost'] = new Date().getTime() - this.timeStart;
  170. }
  171. const trackData = { ...this.eventData, ...onceData };
  172. trackData['action'] = action;
  173. trackData['current_event_duration'] = new Date().getTime() - this.timeStart;
  174. if (action == 200 || action == 400 || action == 700 || action == 800)
  175. trackData['api_timecost'] = new Date().getTime() - this.apiTimeStart;
  176. if (this.isUpload) {
  177. normalTrack(this.eventName, trackData);
  178. }
  179. }
  180. }
  181. /**
  182. * 创建并返回一个 IntersectionObserver 对象实例。
  183. * 该实例会观测 option.selector 节点。
  184. * 当节点在 viewport 的观测区域停留 option.delayInSecond 秒,
  185. * 就认为该节点已曝光,会触发 visibilityCb。
  186. *
  187. * 注意:
  188. * 1. 节点多次曝光会触发多次 visibilityCb
  189. * 2. 待观测的 option.selector 节点必须有唯一的 id
  190. * 3. 调用该方法时,待观测的节点必须在已经在页面上渲染完成。一般在 onReady / mounted 方法中调用。
  191. * 4. 在 onUnload / beforeDestroy 需要执行 observer.disconnect() 来释放资源。
  192. * 5. observer 不能作为 vue 实例的响应式数据。
  193. *
  194. * https://uniapp.dcloud.net.cn/api/ui/intersection-observer.html#createintersectionobserver
  195. *
  196. * @param elContainer 自定义组件实例,一般传入 this
  197. * @param visibilityCb 回调函数
  198. * @param option
  199. * @returns IntersectionObserver 对象实例。
  200. */
  201. const getVisibilityObserver = (
  202. elContainer,
  203. visibilityCb,
  204. option) => {
  205. const opt = Object.assign({}, {
  206. selector: '.watch-me',
  207. threshold: 0.5,
  208. delayInSecond: 1,
  209. margin: {
  210. bottom: 0,
  211. }
  212. }, option ? option : {});
  213. let timer = 0;
  214. const inMap = {};
  215. const outMap = {};
  216. const delay = opt.delayInSecond * 1000;
  217. const observer = uni.createIntersectionObserver(elContainer, { observeAll: true, thresholds: [opt.threshold], initialRatio: opt.threshold });
  218. observer.relativeToViewport(opt.margin).observe(opt.selector, (res) => {
  219. // eslint-disable-next-line no-console
  220. // console.log(res);
  221. // res 身上除了 UniApp.ObserveResult 中列明的属性外
  222. // 还有两个属性:
  223. // - id:string,节点的 id
  224. // - dataset:object,存放节点上面的 data-* 数据
  225. // 所以我们可以用 id 来做 inMap 和 outMap 的 key。
  226. const key = res.id;
  227. // 进入 viewpoint
  228. if (res.intersectionRatio > opt.threshold) {
  229. inMap[key] = res;
  230. if (outMap[key]) {
  231. delete outMap[key];
  232. }
  233. } else { // 离开 viewpoint
  234. outMap[key] = res;
  235. if (inMap[key]) {
  236. // 计算停留时间
  237. if (outMap[key].time - inMap[key].time >= delay) {
  238. visibilityCb(key, outMap[key]);
  239. delete outMap[key];
  240. }
  241. delete inMap[key];
  242. }
  243. }
  244. // 处理那些一直停留在 viewpoint 内的节点
  245. if (timer) {
  246. clearTimeout(timer);
  247. }
  248. timer = setTimeout(() => {
  249. Object.keys(inMap).forEach(id => {
  250. visibilityCb(id, inMap[id]);
  251. delete inMap[id];
  252. })
  253. }, delay);
  254. });
  255. return observer;
  256. }
  257. // export {
  258. // trackingInit,
  259. // TrackFactory,
  260. // getVisibilityObserver
  261. // }
  262. export default () => {
  263. // console.log('ENVIRONMENTS', ENVIRONMENTS[window.env]);
  264. // console.log('ENVIRONMENTS', sensors);
  265. // 埋点参数设置及公共字段初始化
  266. trackingInit();
  267. Vue.prototype.$trackFactory = TrackFactory;
  268. }