track.js
7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/**
*/
import api from "~/utils/request/request.js"
// 实例化对象存储
import {
Storage
} from "~/utils/cache/storage";
const $Storage = new Storage()
let TRACK_TIME = null
const track_interval_time = 30 * 1e3,
filterFun = ['data', 'onLoad', 'onShow', 'onReady', 'onHide', 'onUnload', 'onPullDownRefresh', 'onReachBottom', 'onShareAppMessage', 'onPageScroll', 'onResize', 'onTabItemTap']
class TRACK {
constructor() {
console.log('---------------- 埋点初始化 ----------------')
this.initPageProxy()
this.sendTrack()
this.appHide()
}
/**监听页面 */
pageLeaveProxy(thisPage) {
let _this = this,
onHide = thisPage.onHide,
onUnload = thisPage.onUnload,
onLoad = thisPage.onLoad,
onShow = thisPage.onShow,
onTabItemTap = thisPage.onTabItemTap,
keys = [],
fun = JSON.parse(JSON.stringify(Object.keys(thisPage)))
const filtered = filterFun.filter(e => !keys.includes(e))
keys.push(...fun.filter(e => !filtered.includes(e)))
fun = fun.filter(e => filtered.includes(e))
keys.map(key => {
if (typeof thisPage[key] === 'function') {
const FUN = thisPage[key]
thisPage[key] = function (event) {
if (event && event.type == 'tap') {
let {
eventCode,
track
} = event.currentTarget.dataset
if (eventCode) _this.setTrackData(0, eventCode, track)
}
return FUN.apply(this, arguments)
}
}
})
.
thisPage.onTabItemTap = function (options) {
onTabItemTap && onTabItemTap.apply(this, arguments)
// , _this.setTrackData({}, 1)
}
thisPage.onLoad = function () {
onLoad && onLoad.apply(this, arguments)
//, _this.setTrackData({
// enterTime: new Date().format('yyyy-MM-dd hh:mm:ss')
// })
}
thisPage.onShow = function () {
onShow && onShow.apply(this, arguments)
// , (_this.getPageInstance().page_show_time = Date.now())
}
thisPage.onHide = function () {
let {
page_show_time
} = _this.getPageInstance()
onHide && onHide.apply(this, arguments)
// ,_this.setTrackData(
// {
// duration: getDuration(page_show_time)
// },
// 2
// )
}
thisPage.onUnload = function () {
let {
page_show_time
} = _this.getPageInstance()
onUnload && onUnload.apply(this, arguments)
// ,_this.setTrackData(
// {
// duration: getDuration(page_show_time)
// },
// 2
// )
}
const getDuration = page_show_time => (Date.now() - page_show_time) / 1e3
}
/**全局注册事件 */
initPageProxy() {
let thisPage = Page
let _this = this
Page = function (el) {
try {
el || (el = {}), _this.pageLeaveProxy(el), thisPage.apply(this, arguments)
} catch (el) {
thisPage.apply(this, arguments)
}
}
}
appShow() {
wx.onAppShow(() => {
if (TRACK_TIME) clearInterval(TRACK_TIME)
this.sendTrack()
wx.offAppShow()
this.appHide()
console.log('---------------- 重新打开埋点 ----------------')
})
}
appHide() {
wx.onAppHide(() => {
if (TRACK_TIME) clearInterval(TRACK_TIME)
wx.offAppHide()
this.appShow()
console.log('---------------- APP隐藏 埋点停止推送 ----------------')
})
}
/**设置埋点数据
* eventType:动作类型枚举 0点击 1滚动 2浏览
* eventCode:事件别名枚举
* businessParam:业务参数
* batchId 浏览批次id,当浏览-进入事件发生时,必填
*/
setTrackData(eventType, eventCode = '', businessParam = '', batchId) {
let recordList = $Storage.get('recordList'),
{
route
} = this.getPageInstance(),
prePagePath = this.getPageInstance(2),
{
accountName,
uuid,
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
}
// 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)
$Storage.set('recordList', recordList)
if (recordList.length > 9) this.sendData() //10条
}
getGuestNo() {
let guest_no = $Storage.get('guest_no')
if (!guest_no) {
guest_no = this.setRandomId()
$Storage.set('guest_no', guest_no)
}
return guest_no
}
/**定时发送埋点数据 */
sendTrack() {
TRACK_TIME = setInterval(() => {
this.sendData()
}, track_interval_time)
}
/**调取API */
sendData() {
$Storage.set('recordList', []) // 去掉埋点调用
// let recordList = $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) {
// $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)
// })
}
/**获取页面对象实例 */
getPageInstance = (size = 1) => {
const pages = getCurrentPages()
return pages[pages.length - size]
}
/**获取页面标题 */
getPageTitle() {
let {
route,
navigationBarTitleText
} = this.getPageInstance()
if ('\u672a\u53d6\u5230' === route || !route) return ''
let title = ''
if (__wxConfig) {
let {
window
} = __wxConfig.page[`${route}.html`]
title = window.navigationStyle == 'custom' ? navigationBarTitleText : window.navigationBarTitleText
}
if (!title) title = '生意助手员工版'
return title
}
/**获取设备信息 */
getSystemInfo() {
let {
SDKVersion,
brand,
model,
system,
version
} = $Storage.get('system_info')
return {
SDK_version: SDKVersion,
brand,
model,
system,
version
}
}
/**获取门店信息 */
getMerchantInfo() {
let {
merchantId
} = $Storage.get('merchant_info')
return {
merchant_id: merchantId
}
}
/**设置随机ID */
setRandomId(prefix = 'DJ', 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 `${prefix}_${name}_${Date.now()}`
}
}
export default TRACK