Commit ead69ed1 by chenwq

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

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