reportTrackFunMixin.js
4.71 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
import {
Util
} from "@/common/util"
import {
reportTrack
} from "@api/resident"
export default {
data () {
return {
prePagePath: "",
track_timerFlag: false,
track_batchId: null,
track_page_view_flag: false,
track_timer_out: null
}
},
beforeRouteEnter (to, from, next) {
next((vm) => {
vm.prePagePath = from.path
})
},
computed: {
loginAccount () {
return this.$store.state.d2admin.user.info.loginAccount
}
},
methods: {
/**
*
* @param {动作类型} eventType
* @param {事件别名} eventCode
* @param {业务参数} businessParam
* @param {浏览批次id} batchId
*/
setReportTrackData (
eventType,
eventCode = "",
businessParam = "",
batchId = ""
) {
let recordList = localStorage.getItem("recordList")
const route = this.$route
const {
loginAccout,
uuid,
userType
} = this.loginAccount
let {
prePagePath
} = this
const item = {
loginAccount: loginAccout,
uuid,
userType,
systemSource: 1,
eventTime: Util.dateFormate(Date.now(), "YYYY-MM-DD HH:mm:ss"),
eventType,
pagePath: route.path
}
if (!prePagePath) prePagePath = this.getPrePagePath(this)
item.prePagePath = prePagePath || "/index"
if (batchId) item.batchId = batchId
if (eventCode) item.eventCode = eventCode
if (businessParam) item.businessParam = JSON.stringify(businessParam)
if (!recordList) recordList = [item]
else {
recordList = JSON.parse(recordList)
recordList.push(item)
}
localStorage.setItem("recordList", JSON.stringify(recordList))
if (!window.TRACK_TIMER) {
console.log('---------------- 重新打开定时埋点 ----------------')
this.sendTrack() //开启定时埋点
}
if (recordList.length > 9) this.sendTrackData()
},
getRandomString (length = 30) {
const characters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
let result = ""
for (let i = 0; i < length; i++) {
result += characters.charAt(
Math.floor(Math.random() * characters.length)
)
}
return result
},
onPageShow () {
if (this.track_page_view_flag) return
if (!this.track_batchId) {
this.track_batchId = `DJ_${this.getRandomString()}_${Date.now()}`
}
this.track_timerFlag = true
this.track_page_view_flag = true
if (this.track_timer_out) {
clearTimeout(this.track_timer_out)
this.track_timer_out = null
}
setTimeout(() => {
this.setBrowseTrackData()
}, 300)
},
onPageHide () {
this.track_timerFlag = false
this.track_batchId = null
this.track_page_view_flag = false
clearTimeout(this.track_timer_out)
this.track_timer_out = null
},
setBrowseTrackData () {
try {
// || (this.$route.path != "/qualityControlList" && this.$route.path != "/shopConfig")
if (!this.track_timerFlag && !this.track_event_code) { //无eventcode时不上报
return
} else {
this.track_timer_out = setTimeout(() => {
this.setBrowseTrackData()
}, 10e3)
}
let trackParams = {}
if(this.track_businessParam_key) trackParams = this[this.track_businessParam_key] || {}
this.setReportTrackData(
2,
this.track_event_code,
trackParams,
this.track_batchId
)
} catch (error) {
console.log(error)
}
},
getPrePagePath (_this) {
let current = _this
const visited = new Set()
while (current) {
if (visited.has(current)) {
// 防止无限循环
console.error("Detected circular reference in parent chain")
return ""
}
visited.add(current)
if (current.$parent && current.$parent.prePagePath) {
return current.$parent.prePagePath
}
current = current.$parent
}
return ""
},
sendTrackData () {
let recordList = localStorage.getItem('recordList')
if (recordList && recordList.length) {
reportTrack({
recordList: JSON.parse(recordList)
}).then(res => {
if (res) localStorage.removeItem('recordList')
})
} else {
clearInterval(window.TRACK_TIMER)
window.TRACK_TIMER = null
console.log('---------------- 暂停定时埋点 ----------------')
}
},
/**定时发送埋点数据 */
sendTrack () {
window.TRACK_TIMER = setInterval(() => {
this.sendTrackData()
}, 30e3)
}
}
}