Commit 13e502ec by chenwq

优化埋点模块,调整代码结构并清理无用注释

1 parent e17d6ddb
...@@ -6,7 +6,10 @@ import { getUpdateManager } from "./lib/index"; ...@@ -6,7 +6,10 @@ import { getUpdateManager } from "./lib/index";
import utils from "./utils/index"; import utils from "./utils/index";
import { areaList } from "@vant/area-data"; //预加载省市区 import { areaList } from "@vant/area-data"; //预加载省市区
// 埋点 有需要再引入 // 埋点 有需要再引入
// import './utils/track/Track'
/**全埋点 */
import './utils/track/Track'
/**分享 */ /**分享 */
import "~/utils/share"; import "~/utils/share";
App({ App({
......
/** /**
*/ */
import mapApi from '../mapapi/mapapi' // import mapApi from '../mapapi/mapapi'
import api from '~/api/request/http' import api from "~/api/request/http";
import { getUserInfo } from '~/utils/cache/userInfo' import { getUserInfo } from "~/utils/cache/userInfo";
// 实例化对象存储 // 实例化对象存储
import { Storage } from '~/utils/cache/storage' // import { Storage } from "~/utils/cache/storage";
const $Storage = new Storage() // const $Storage = wx.p.storage
let TRACK_TIME = null, let TRACK_TIME = null,
ADDRESS_INFO = null ADDRESS_INFO = null;
const track_interval_time = 60 * 1e3 const track_interval_time = 60 * 1e3;
class TRACK { class TRACK {
constructor() { constructor() {
console.log('---------------- 埋点初始化 ----------------') console.log("---------------- 埋点初始化 ----------------");
this.initPageProxy() this.initPageProxy();
// this.sendTrack()
//this.getAddressInfo().then(res => (ADDRESS_INFO = res))
// this.appHide()
} }
/**监听页面 */ /**监听页面 */
pageLeaveProxy(thisPage) { pageLeaveProxy(thisPage) {
...@@ -27,54 +24,56 @@ class TRACK { ...@@ -27,54 +24,56 @@ class TRACK {
onUnload = thisPage.onUnload, onUnload = thisPage.onUnload,
onLoad = thisPage.onLoad, onLoad = thisPage.onLoad,
onShow = thisPage.onShow, onShow = thisPage.onShow,
onTabItemTap = thisPage.onTabItemTap onTabItemTap = thisPage.onTabItemTap;
thisPage.onTabItemTap = function (options) { thisPage.onTabItemTap = function (options) {
onTabItemTap && onTabItemTap.apply(this, arguments) onTabItemTap && onTabItemTap.apply(this, arguments);
// , _this.setTrack({}, 1) // , _this.setTrack({}, 1)
} };
thisPage.onLoad = function () { thisPage.onLoad = function () {
onLoad && onLoad.apply(this, arguments) onLoad && onLoad.apply(this, arguments);
//, _this.setTrack({ //, _this.setTrack({
// enterTime: new Date().format('yyyy-MM-dd hh:mm:ss') // enterTime: new Date().format('yyyy-MM-dd hh:mm:ss')
// }) // })
} };
thisPage.onShow = function () { thisPage.onShow = function () {
onShow && onShow.apply(this, arguments) onShow && onShow.apply(this, arguments);
// , (_this.getPageInstance().page_show_time = Date.now()) // , (_this.getPageInstance().page_show_time = Date.now())
} };
thisPage.onHide = function () { thisPage.onHide = function () {
let { page_show_time } = _this.getPageInstance() let { page_show_time } = _this.getPageInstance();
onHide && onHide.apply(this, arguments) onHide && onHide.apply(this, arguments);
// ,_this.setTrack( // ,_this.setTrack(
// { // {
// duration: getDuration(page_show_time) // duration: getDuration(page_show_time)
// }, // },
// 2 // 2
// ) // )
} };
thisPage.onUnload = function () { thisPage.onUnload = function () {
let { page_show_time } = _this.getPageInstance() let { page_show_time } = _this.getPageInstance();
onUnload && onUnload.apply(this, arguments) onUnload && onUnload.apply(this, arguments);
// ,_this.setTrack( // ,_this.setTrack(
// { // {
// duration: getDuration(page_show_time) // duration: getDuration(page_show_time)
// }, // },
// 2 // 2
// ) // )
} };
const getDuration = page_show_time => (Date.now() - page_show_time) / 1e3 const getDuration = (page_show_time) => (Date.now() - page_show_time) / 1e3;
} }
/**全局注册事件 */ /**全局注册事件 */
initPageProxy() { initPageProxy() {
let thisPage = Page let thisPage = Page;
let _this = this let _this = this;
Page = function (el) { Page = function (el) {
try { try {
el || (el = {}), _this.pageLeaveProxy(el), thisPage.apply(this, arguments) (el || (el = {}),
_this.pageLeaveProxy(el),
thisPage.apply(this, arguments));
} catch (el) { } catch (el) {
thisPage.apply(this, arguments) thisPage.apply(this, arguments);
} }
} };
} }
appShow() { appShow() {
wx.onAppShow(() => { wx.onAppShow(() => {
...@@ -83,7 +82,7 @@ class TRACK { ...@@ -83,7 +82,7 @@ class TRACK {
// wx.offAppShow() // wx.offAppShow()
// this.appHide() // this.appHide()
// console.log('---------------- 重新打开埋点 ----------------') // console.log('---------------- 重新打开埋点 ----------------')
}) });
} }
appHide() { appHide() {
wx.onAppHide(() => { wx.onAppHide(() => {
...@@ -91,101 +90,105 @@ class TRACK { ...@@ -91,101 +90,105 @@ class TRACK {
// wx.offAppHide() // wx.offAppHide()
// this.appShow() // this.appShow()
// console.log('---------------- APP隐藏 埋点停止推送 ----------------') // console.log('---------------- APP隐藏 埋点停止推送 ----------------')
}) });
} }
/**设置埋点数据 */ /**设置埋点数据
setTrack() {} * eventType:动作类型枚举 0点击 1滚动 2浏览
getGuestNo() { * eventCode:事件别名枚举
let guest_no = $Storage.get('guest_no') * businessParam:业务参数
if (!guest_no) { * batchId 浏览批次id,当浏览-进入事件发生时,必填
guest_no = this.setGuestNo() */
$Storage.set('guest_no', guest_no) setTrackData(eventType, eventCode = "", businessParam = "", batchId, scene) {
} // console.log(eventCode, 'eventCode');
return guest_no let recordList = wx.p.storage.get("recordList"),
{ route } = this.getPageInstance(),
prePagePath = this.getPageInstance(2),
{ userType } = wx.$util.getUserInfo(),
item = {
loginAccount: accountName,
uuid,
userType,
systemSource: 0,
eventTime: new Date().format("yyyy-MM-dd hh:mm:ss"),
eventType,
prePagePath: prePagePath ? prePagePath.route : "/",
pagePath: route,
scene,
};
// console.log(new Date().format('yyyy-MM-dd hh:mm:ss'),'当前页:',route,'=========')
if (batchId) item.batchId = batchId;
if (eventCode) item.eventCode = eventCode;
if (businessParam) item.businessParam = JSON.stringify(businessParam);
if (!recordList) recordList = [item];
else recordList.push(item);
wx.p.storage.set("recordList", recordList);
if (recordList.length > 9) this.sendData(); //10条
} }
/**定时发送埋点数据 */ /**定时发送埋点数据 */
sendTrack() { sendData() {
TRACK_TIME = setInterval(() => { let recordList = wx.p.storage.get("recordList");
let prepare_data = $Storage.get('prepare_data') if (recordList && recordList.length)
let data = { api
method: 'entmicro.api.customercollect.reportCustomerList', .post(
jsonList: prepare_data "clue.api.burial.point.record.batch.insert",
} {
if (prepare_data && prepare_data.length) recordList,
api.post(data, true).then(({ code, data, message }) => { },
[],
"/rop-scrm-clue/router",
"scrm",
false,
)
.then(({ code, data, message }) => {
if (code == 0 && data) { if (code == 0 && data) {
$Storage.set('prepare_data', []) wx.p.storage.set("recordList", []);
console.log('---------------- 埋点定时推送成功 ----------------', new Date().format('yyyy-MM-dd hh:mm:ss'), ':', prepare_data) console.log(
} else console.log('---------------- 埋点定时推送失败 ----------------', new Date().format('yyyy-MM-dd hh:mm:ss'), ':', message) "---------------- 埋点定时推送成功 ----------------",
}) new Date().format("yyyy-MM-dd hh:mm:ss"),
}, track_interval_time) ":",
recordList,
);
} else
console.log(
"---------------- 埋点定时推送失败 ----------------",
new Date().format("yyyy-MM-dd hh:mm:ss"),
":",
message,
);
});
} }
/**获取页面对象实例 */ /**获取页面对象实例 */
getPageInstance = (size = 1) => { getPageInstance = (size = 1) => {
const pages = getCurrentPages() const pages = getCurrentPages();
return pages[pages.length - size] return pages[pages.length - size];
} };
/**获取页面标题 */ /**获取页面标题 */
getPageTitle() { getPageTitle() {
let { route, navigationBarTitleText } = this.getPageInstance() let { route, navigationBarTitleText } = this.getPageInstance();
if ('\u672a\u53d6\u5230' === route || !route) return '' if ("\u672a\u53d6\u5230" === route || !route) return "";
let title = '' let title = "";
if (__wxConfig) { if (__wxConfig) {
let { window } = __wxConfig.page[`${route}.html`] let { window } = __wxConfig.page[`${route}.html`];
title = window.navigationStyle == 'custom' ? navigationBarTitleText : window.navigationBarTitleText title =
window.navigationStyle == "custom"
? navigationBarTitleText
: window.navigationBarTitleText;
} }
if (!title) title = '生意助手' if (!title) title = "生意助手";
return title return title;
} }
/**获取设备信息 */ /**获取设备信息 */
getSystemInfo() { getSystemInfo() {
let { SDKVersion, brand, model, system, version } = $Storage.get('system_info') let { SDKVersion, brand, model, system, version } =
wx.p.storage.get("system_info");
return { return {
SDK_version: SDKVersion, SDK_version: SDKVersion,
brand, brand,
model, model,
system, system,
version version,
} };
}
/**获取门店信息 */
getMerchantInfo() {
let { merchantId } = $Storage.get('merchant_info')
return {
merchant_id: merchantId
}
}
/**获取用户位置信息 */
getAddressInfo() {
let addressInfo = $Storage.get('address_info')
return new Promise(resolve => {
if (addressInfo && Date.now() - addressInfo.createTime < 2 * 60 * 1e3) resolve(addressInfo)
else
wx.getLocation({
type: 'gcj02'
}).then(({ latitude, longitude }) => {
if (!addressInfo) this.setTrack({}, 5)
console.log('---------------- 更新地理位置 ----------------')
mapApi.detailedAdrress([latitude, longitude]).then(detail => {
let info = {
...detail,
createTime: Date.now()
}
resolve(info)
$Storage.set('address_info', info)
})
})
})
}
/**获取游客ID */
setGuestNo(length = 30) {
let name = '',
n = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
for (let i = 0; i < Number(length); i++) {
name += n[Math.round(Math.random() * (n.length - 1))]
}
return `GH_${name}_${Date.now()}`
} }
} }
wx.$Track = new TRACK() wx.$Track = new TRACK();
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!