analytics.js 3.39 KB
import {LOG_EVENT_HOST} from '../libs/config';
import {UPLOAD_LOG} from '../libs/request';
import {getYHStorageSync} from '../utils/util';

const YB_ENTER_FOREGROUND = 'YB_ENTER_FOREGROUND';
const YB_ENTER_BACKGROUND = 'YB_ENTER_BACKGROUND';
const YB_LAUNCH_APP = 'YB_LAUNCH_APP';
const YB_EXIT_APP = 'YB_EXIT_APP';
const YB_PAGE_OPEN_L = 'YB_PAGE_OPEN_L';
const YB_MAIN_TAB_C = 'YB_MAIN_TAB_C';

const YB_MY_LOGIN = 'YB_MY_LOGIN';
const YB_REGISTER_SUCCESS = 'YB_REGISTER_SUCCESS';
const YB_AWAKE_MP = 'YB_AWAKE_MP';
const YB_GRP_BUY_DT_TOG_C = 'YB_GRP_BUY_DT_TOG_C';
const YB_MAIN_POP_UP_C = 'YB_MAIN_POP_UP_C';

let logEvent = function (eventId, data, appData) {
  let app = appData ? appData : getApp();
  let ln;//语言
  let os;//系统类型
  let dm;//设备型号
  let re;//屏幕大小
  let osv;//系统版本
  let ak = 'yoholuck_mp';//
  let net = '0';
  let ts = new Date().getTime() + '';
  //用户id
  let uid = app && app.globalData && app.globalData.userInfo && app.globalData.userInfo.uid ? app.globalData.userInfo.uid : '';
  //设备id
  let udid = app && app.globalData && app.globalData.udid ? app.globalData.udid : '';
  //会话id
  let sid = app && app.globalData && app.globalData.sid ? app.globalData.sid : '';
  let cid = app && app.globalData && app.globalData.cid ? app.globalData.cid : '';

  let open_id = app && app.globalData && app.globalData.openID ? app.globalData.openID : getYHStorageSync('openID', 'analytics');
  let union_id = app && app.globalData && app.globalData.WXUnion_ID ? app.globalData.WXUnion_ID : getYHStorageSync('unionID', 'analytics');
  let ch = app && app.globalData && app.globalData.ch ? app.globalData.ch : '';

  wx.getSystemInfo({
    success: function (res) {
      //返回设备信息
      dm = res.model;
      ln = res.language;
      os = res.platform;
      osv = res.system;
      re = res.screenWidth + '*' + res.screenHeight;
    },
    complete: function () {
      wx.getNetworkType({
        success: function (res) {
          var networkType = res.networkType
          if (networkType === 'wifi') {
            net = '1';
          } else if (networkType === '2g') {
            net = '2';
          } else if (networkType === '3g') {
            net = '3';
          } else if (networkType === '4g') {
            net = '4';
          } else {
            net = '0';
          }
        },
        complete: function () {
          let union_type = app.getUnion_type();

          let statusParam = {'net': net, 'ln': ln};
          let deviceParam = {
            'res': re,
            'osv': osv,
            'os': os,
            'ak': ak,
            'dm': dm,
            'udid': udid,
            'ch': union_type !== '' ? union_type + "" : ch + ""
          };
          let userParam = data;
          userParam.C_ID = cid;
          userParam.UNION_ID = union_id;
          let eventParam = [{'param': userParam, 'ts': ts, 'op': eventId, 'uid': uid, 'sid': sid}]
          let parameters = {'status': statusParam, 'device': deviceParam, 'events': eventParam}

          UPLOAD_LOG(LOG_EVENT_HOST, parameters)
            .then(function (data) {
            })
            .catch(function (error) {
            });
        }
      })
    }
  })
};

export {
  logEvent,
  YB_ENTER_FOREGROUND,
  YB_ENTER_BACKGROUND,
  YB_LAUNCH_APP,
  YB_EXIT_APP,
  YB_PAGE_OPEN_L,
  YB_MAIN_TAB_C,
  YB_MY_LOGIN,
  YB_REGISTER_SUCCESS,
  YB_AWAKE_MP,
  YB_GRP_BUY_DT_TOG_C,
  YB_MAIN_POP_UP_C
};