Track.js 3.58 KB
/**
 * 使用方法 wx.$Track.setTrack({
 *  参数1:val,
 *  参数2:val,
 *  参数...
 * }, act_id)
 * act_id :{
 *  1:点击,
 *  2:页面离开,
 *  3:登陆
 *  4:退出登录
 *  5:地理位置授权
 *  6:页面预览
 *  7:业务行为
 * 
 * }
 */
import api from '../api/request'
import {trackData} from '../utils/data'
let TRACK_TIME = null

class TRACK {
  constructor() {
    this.initPageProxy()
    this.appHide()
  }
  /**监听页面 */
  pageLeaveProxy(thisPage) {
    let _this = this,
      onLoad = thisPage.onLoad,
      onTabItemTap = thisPage.onTabItemTap
    thisPage.onTabItemTap = function (options) {
      onTabItemTap && onTabItemTap.apply(this, arguments),
        _this.setTrack({}, 1)
    }
    thisPage.onLoad = function () {
      onLoad && onLoad.apply(this, arguments)
      // , _this.setTrack()
    }
  }
  /**全局注册事件 */
  initPageProxy() {
    let thisPage = Page
    let _this = this
    Page = function (el) {
      try {
        el || (el = {}), _this.pageLeaveProxy(el), thisPage.apply(this, arguments)
      } catch (el) {
        thisPage.apply(this, arguments)
      }
    }
  }
  appShow() {
    wx.onAppShow(() => {
      if (TRACK_TIME) clearInterval(TRACK_TIME)
      // this.sendTrack()
      wx.offAppShow()
      this.appHide()
    })
  }
  appHide() {
    wx.onAppHide(() => {
      if (TRACK_TIME) clearInterval(TRACK_TIME)
      wx.offAppHide()
      this.appShow()
    })
  }
  /**设置埋点数据 */
  setTrack(params, act_id = 1) {
    console.log('---------------- 获取小程序启动时的参数 ----------------', wx.getLaunchOptionsSync())
    let userInfo = wx.$storage.get('aicard_user_info')
    console.log(this.getPageTitle(), userInfo)
    // if (this.getPageTitle() == '广联数科智能名片') {
    //   console.log()
    //   this.sendTrack(trackData.HOME)
    // }
  }
  /**发送埋点数据 */
  sendTrack(p, info) {
    console.log(info)
    console.log(wx.$trackData);
    const systemInfo = wx.$storage.get('aicard_system_info')
    let params = {
      method: 'entmicro.api.business.card.cardRecord',
      cardId: wx.$trackData.cardId,
      merchant: wx.$trackData.merchant || wx.$trackData.merchantId || wx.$storage.get("scene").merchant,
      type: systemInfo.environment ? 2 : 1
    }
    let userInfo = { }
    if (wx.$util.getUserInfo()) {
      const {nickName, openId, phone} = wx.$util.getUserInfo() || {}
      userInfo = {
        nickName,
        unionId: wx.$storage.get('aicard_info').unionId,
        openId,
        phone,
      }
    } else {
      userInfo.openId = info && info.openId ? info.openId : wx.$storage.get('aicard_info').openId
      userInfo.unionId = info && info.unionId ? info.unionId : wx.$storage.get('aicard_info').unionId
    }
    
    params = Object.assign(params, userInfo, p)
    console.log(params)
    api.reportTrack(params).then(({
      code,
      data,
      message
    }) => {
      if (code == 0) {
        console.log('埋点设置成功')
      }
    })
  }
  /**获取页面对象实例 */
  getPageInstance = (size = 1) => {
    const pages = getCurrentPages()
    return pages[pages.length - size]
  }
  /**获取页面标题 */
  getPageTitle() {
    let {
      route,
      navigationBarTitleText
    } = this.getPageInstance()
    if ('\u672a\u53d6\u5230' === route || !route) return ''
    let title = ''
    if (__wxConfig) {
      let {
        window
      } = __wxConfig.page[`${route}.html`]
      title = window.navigationStyle == 'custom' ? navigationBarTitleText : window.navigationBarTitleText
    }
    if (!title) title = '广汇车生活'
    return title
  }
}

wx.$Track = new TRACK()