reportTrackPageMixin.js
1.72 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
import { reportTrack } from "@api/resident"
import reportTrackFunMixin from './reportTrackFunMixin'
function getRandomString (length = 10) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
let result = '';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
export default {
mixins: [reportTrackFunMixin],
data () {
return {
timerFlag: false,
track_set_timer: null
}
},
beforeDestroy () {
this.onPageHide()
window.removeEventListener('click', this.getTrackData)
this.timerFlag = false
clearTimeout(this.track_set_timer)
this.track_set_timer = null
},
mounted () {
window.addEventListener('click', this.getTrackData)
this.timerFlag = true
this.intervalRepoet()
},
methods: {
getTrackData (e) {
setTimeout(() => {
if (e.id == 'app') return
let target = e.target || e
if (target.dataset && Object.keys(target.dataset).length && target.dataset.eventCode) this.setReportTrackData(0, target.dataset.eventCode, JSON.parse(target.dataset.track))
else this.getTrackData(target.parentElement)
}, 100)
},
intervalRepoet () {
if (!this.timerFlag) return
this.track_set_timer = setTimeout(() => {
this.sendData()
if (this.timerFlag) this.intervalRepoet()
}, 30 * 1e3)
},
getRandomString: getRandomString,
sendData () {
let recordList = localStorage.getItem('recordList')
if (recordList) {
reportTrack({
recordList: JSON.parse(recordList)
}).then(res => {
if (res) localStorage.removeItem('recordList')
})
}
}
}
}