Track.js
3.58 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
/**
* 使用方法 wx.$Track.setTrack({
* 参数1:val,
* 参数2:val,
* 参数...
* }, act_id)
* act_id :{
* 1:点击,
* 2:页面离开,
* 3:登陆
* 4:退出登录
* 5:地理位置授权
* 6:页面预览
* 7:业务行为
*
* }
*/
import api from '../api/request'
import {trackData} from '../utils/data'
let TRACK_TIME = null
class TRACK {
constructor() {
this.initPageProxy()
this.appHide()
}
/**监听页面 */
pageLeaveProxy(thisPage) {
let _this = this,
onLoad = thisPage.onLoad,
onTabItemTap = thisPage.onTabItemTap
thisPage.onTabItemTap = function (options) {
onTabItemTap && onTabItemTap.apply(this, arguments),
_this.setTrack({}, 1)
}
thisPage.onLoad = function () {
onLoad && onLoad.apply(this, arguments)
// , _this.setTrack()
}
}
/**全局注册事件 */
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()
})
}
appHide() {
wx.onAppHide(() => {
if (TRACK_TIME) clearInterval(TRACK_TIME)
wx.offAppHide()
this.appShow()
})
}
/**设置埋点数据 */
setTrack(params, act_id = 1) {
console.log('---------------- 获取小程序启动时的参数 ----------------', wx.getLaunchOptionsSync())
let userInfo = wx.$storage.get('aicard_user_info')
console.log(this.getPageTitle(), userInfo)
// if (this.getPageTitle() == '广联数科智能名片') {
// console.log()
// this.sendTrack(trackData.HOME)
// }
}
/**发送埋点数据 */
sendTrack(p, info) {
console.log(info)
console.log(wx.$trackData);
const systemInfo = wx.$storage.get('aicard_system_info')
let params = {
method: 'entmicro.api.business.card.cardRecord',
cardId: wx.$trackData.cardId,
merchant: wx.$trackData.merchant || wx.$trackData.merchantId || wx.$storage.get("scene").merchant,
type: systemInfo.environment ? 2 : 1
}
let userInfo = { }
if (wx.$util.getUserInfo()) {
const {nickName, openId, phone} = wx.$util.getUserInfo() || {}
userInfo = {
nickName,
unionId: wx.$storage.get('aicard_info').unionId,
openId,
phone,
}
} else {
userInfo.openId = info && info.openId ? info.openId : wx.$storage.get('aicard_info').openId
userInfo.unionId = info && info.unionId ? info.unionId : wx.$storage.get('aicard_info').unionId
}
params = Object.assign(params, userInfo, p)
console.log(params)
api.reportTrack(params).then(({
code,
data,
message
}) => {
if (code == 0) {
console.log('埋点设置成功')
}
})
}
/**获取页面对象实例 */
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
}
}
wx.$Track = new TRACK()