Commit ead69ed1 by chenwq

优化埋点功能,新增事件记录接口并调整定时推送逻辑

1 parent 13e502ec
......@@ -150,6 +150,12 @@ Page({
if (code == "200") {
this.customerLoginCallBack(data);
wx.p.isLogin = true;
// 登录成功埋点
if (wx.$Track && wx.$Track.reportEvent) {
wx.$Track.reportEvent("denglu_dianji");
}
wx.p.router.goHome();
} else {
throw message;
......
......@@ -16,6 +16,12 @@ export function setUserInfo(data) {
console.log(data)
storage.set('userInfo', data)
}
export function getEventRecordList() {
return storage.get('eventRecordList')
}
export function setEventRecordList(data) {
storage.set('eventRecordList', data)
}
export function removeSystemStorage() {
// storage.rm('srcm_shopmall_user_info')
// storage.rm('scrm_shopmall_token')
......@@ -61,6 +67,8 @@ export default{
setToken,
getUserInfo,
setUserInfo,
getEventRecordList,
setEventRecordList,
removeSystemStorage,
setStorageSync,
getStorageSync,
......
......@@ -11,7 +11,8 @@ export const STORAGE_OBJECTkEY = {
token: 'token',
bindInfo:'bindInfo',
unauthorized:'unauthorized',
wx_user_info:'wx_user_info'
wx_user_info:'wx_user_info',
eventRecordList:'eventRecordList'
}
export default{
......
......@@ -10,12 +10,29 @@ import { getUserInfo } from "~/utils/cache/userInfo";
let TRACK_TIME = null,
ADDRESS_INFO = null;
const track_interval_time = 60 * 1e3;
const track_interval_time = 30 * 1e3;
class TRACK {
constructor() {
console.log("---------------- 埋点初始化 ----------------");
this.initPageProxy();
this.appShow();
this.appHide();
this.startInterval();
}
startInterval() {
if (TRACK_TIME) clearInterval(TRACK_TIME);
TRACK_TIME = setInterval(() => {
this.sendEventData();
}, track_interval_time);
}
stopInterval() {
if (TRACK_TIME) {
clearInterval(TRACK_TIME);
TRACK_TIME = null;
}
}
/**监听页面 */
pageLeaveProxy(thisPage) {
......@@ -77,86 +94,70 @@ class TRACK {
}
appShow() {
wx.onAppShow(() => {
// if (TRACK_TIME) clearInterval(TRACK_TIME)
// this.sendTrack()
// wx.offAppShow()
// this.appHide()
// console.log('---------------- 重新打开埋点 ----------------')
this.stopInterval();
this.startInterval();
console.log('---------------- 重新打开埋点定时器 ----------------');
});
}
appHide() {
wx.onAppHide(() => {
// if (TRACK_TIME) clearInterval(TRACK_TIME)
// wx.offAppHide()
// this.appShow()
// console.log('---------------- APP隐藏 埋点停止推送 ----------------')
this.sendEventData(); // 隐藏时顺便兜底发一次
this.stopInterval();
console.log('---------------- APP隐藏 埋点停止推送定时器 ----------------');
});
}
/**设置埋点数据
* eventType:动作类型枚举 0点击 1滚动 2浏览
* eventCode:事件别名枚举
* businessParam:业务参数
* batchId 浏览批次id,当浏览-进入事件发生时,必填
/**
* 上报埋点事件
* @param {String} eventCode 事件代码
* @param {Array} businessKeys 业务对象键值 [{businessType: "xxx", businessKey: "xxx"}]
*/
setTrackData(eventType, eventCode = "", businessParam = "", batchId, scene) {
// console.log(eventCode, 'eventCode');
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条
reportEvent(eventCode, businessKeys = []) {
if (!eventCode) return;
let eventTime = "";
if (Date.prototype.format) {
eventTime = new Date().format("yyyy-MM-dd hh:mm:ss");
}
const eventItem = {
eventCode,
eventTime,
businessKeys
};
let recordList = wx.p.storage.get("eventRecordList") || [];
recordList.push(eventItem);
wx.p.storage.set("eventRecordList", recordList);
// 超过指定条数,触发发送
if (recordList.length > 9) {
this.sendEventData();
}
}
/**定时发送埋点数据 */
sendData() {
let recordList = wx.p.storage.get("recordList");
if (recordList && recordList.length)
api
.post(
"clue.api.burial.point.record.batch.insert",
{
recordList,
},
[],
"/rop-scrm-clue/router",
"scrm",
false,
)
.then(({ code, data, message }) => {
if (code == 0 && data) {
wx.p.storage.set("recordList", []);
console.log(
"---------------- 埋点定时推送成功 ----------------",
new Date().format("yyyy-MM-dd hh:mm:ss"),
":",
recordList,
);
} else
console.log(
"---------------- 埋点定时推送失败 ----------------",
new Date().format("yyyy-MM-dd hh:mm:ss"),
":",
message,
);
});
/**
* 将缓存中的埋点数据批量发送给服务端
*/
sendEventData() {
let recordList = wx.p.storage.get("eventRecordList");
if (!recordList || !recordList.length) return;
// 接口:/vr-api/rest/eventTracking/batchSave
api.post({
method: '/rest/eventTracking/batchSave',
projectName: 'vr',
events: recordList
}, false).then(res => {
// 发送成功后清空队列
wx.p.storage.set("eventRecordList", []);
console.log("---------------- 埋点定时推送成功 ----------------", recordList);
}).catch(err => {
console.log("---------------- 埋点推定时送失败 ----------------", err);
});
}
/**获取页面对象实例 */
getPageInstance = (size = 1) => {
const pages = getCurrentPages();
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!