analytics.js 4.39 KB
'use strict'
import {LOG_EVENT_HOST, API_HOST, SERVICE_HOST } from '../libs/config';
import { UPLOAD_LOG } from '../libs/request';

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_CATEGORY_FL = 'YB_CATEGORY_FLR_C';//品类页楼层
const YB_MAIN_EVENT = 'YB_MAIN_EVENT';       //首页-楼层事件点击
const YB_MY_LOGIN = 'YB_MY_LOGIN';         //当用户使用该登录方式登录成功时
const YB_GDS_DT_BILL = 'YB_GDS_DT_BILL';      //点击商品详情页底部的立即购买按钮,选择完颜色/尺寸后上报
const YB_SC_ORD = 'YB_SC_ORD';           //生成订单
const YB_SC_PAY_RES = 'YB_SC_PAY_RES';       //购物车-付款结果
const YB_GDS_ADD_TO_CART_C = 'YB_GDS_ADD_TO_CART_C';       //商品详情页-加入购物车
const YB_REGISTER_SUCCESS = 'YB_REGISTER_SUCCESS';       //注册成功
const YB_SHARE_RESULT_L = 'YB_SHARE_RESULT_L';       //分享
const YB_AWAKE_MP = 'YB_AWAKE_MP' //渠道落地页

let logEvent = function(eventId, data,appData) {
  let app = appData ? appData : getApp();
  let ln;//语言
  let os;//系统类型
  let dm;//设备型号
  let re;//屏幕大小
  let osv;//系统版本
  let ak = 'yohobuy_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 : wx.getStorageSync('openID');
  let union_id = app && app.globalData && app.globalData.WXUnion_ID ? app.globalData.WXUnion_ID : wx.getStorageSync('unionID');
  let ch = app && app.globalData && app.globalData.ch ? app.globalData.ch : '';
  let union_type = app && app.globalData && app.globalData.union_type ? app.globalData.union_type : '';

  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) {
          // 返回网络类型, 有效值:
          // wifi/2g/3g/4g/unknown(Android下不常见的网络类型)/none(无网络)
          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 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 }
          // console.log(parameters)

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

}

module.exports = {
  logEvent, 
  YB_ENTER_FOREGROUND, 
  YB_ENTER_BACKGROUND, 
  YB_LAUNCH_APP,
  YB_EXIT_APP,
  YB_PAGE_OPEN_L,
  YB_MAIN_TAB_C,
  YB_MAIN_EVENT,
  YB_MY_LOGIN,
  YB_GDS_DT_BILL,
  YB_SC_ORD,
  YB_SC_PAY_RES,
  YB_GDS_ADD_TO_CART_C,
  YB_CATEGORY_FL,
  YB_REGISTER_SUCCESS,
  YB_SHARE_RESULT_L,
  YB_AWAKE_MP,
}