index.js 2.2 KB
Newer Older
1
import { get_platform_name, get_page_vm, is_debug } from './utils/pageInfo.js'
2 3 4 5 6 7 8
import Stat from './core/stat.js'
const stat = Stat.getInstance()

// 用于判断是隐藏页面还是卸载页面
let isHide = false

const lifecycle = {
9 10 11
  onLaunch(options) {
    // 进入应用上报数据
    stat.launch(options, this)
12 13
    // 上报push推送id
    stat.pushEvent(options)
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  },
  onLoad(options) {
    stat.load(options, this)
    // 重写分享,获取分享上报事件
    if (this.$scope && this.$scope.onShareAppMessage) {
      let oldShareAppMessage = this.$scope.onShareAppMessage
      this.$scope.onShareAppMessage = function (options) {
        stat.interceptShare(false)
        return oldShareAppMessage.call(this, options)
      }
    }
  },
  onShow() {
    isHide = false
    stat.show(this)
  },
  onHide() {
    isHide = true
    stat.hide(this)
  },
  onUnload() {
    if (isHide) {
      isHide = false
      return
    }
    stat.hide(this)
  },
  onError(e) {
    stat.error(e)
  },
44 45
}

46 47 48 49 50 51 52 53 54
// 加载统计代码
function load_stat() {
  // #ifdef VUE3
  uni.onCreateVueApp((app) => {
    app.mixin(lifecycle)
    uni.report = function (type, options) {
      stat.sendEvent(type, options)
    }
  })
55

56 57 58 59 60 61 62 63 64
  if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') {
    uni.onAppHide(() => {
      stat.appHide(get_page_vm())
    })
    uni.onAppShow(() => {
      stat.appShow(get_page_vm())
    })
  }
  // #endif
65

66 67 68 69 70 71 72 73 74
  // #ifndef VUE3
  // eslint-disable-next-line no-restricted-globals
  const Vue = require('vue')
  ;(Vue.default || Vue).mixin(lifecycle)
  uni.report = function (type, options) {
    stat.sendEvent(type, options)
  }
  // #endif
}
75

76 77 78 79
function main() {
  if (is_debug) {
    if (__STAT_VERSION__ === '1') {
      // #ifndef APP-NVUE
M
mehaotian 已提交
80
      console.log('=== uni统计开启,version:1.0 ===')
81 82 83 84
      // #endif
    }
    if (__STAT_VERSION__ === '2') {
      // #ifndef APP-NVUE
M
mehaotian 已提交
85
      console.log('=== uni统计开启,version:2.0 ===')
86 87 88 89 90 91 92 93 94 95
      // #endif
    }
    load_stat()
  } else {
    if (process.env.NODE_ENV === 'development') {
      uni.report = function (type, options) {}
    } else {
      load_stat()
    }
  }
96 97
}

98
main()