-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathexposure.js
103 lines (92 loc) · 2.45 KB
/
exposure.js
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
import 'intersection-observer'
import { track } from './sendData'
// 节流时间调整,默认100ms
IntersectionObserver.prototype['THROTTLE_TIMEOUT'] = 300
export default class Exposure {
constructor(maxNum = 20) {
this.cacheDataArr = []
this.maxNum = maxNum
this._timer = 0
this._observer = null
this.init()
}
/**
* 初始化
*/
init() {
const self = this
// 边界处理
this.trackFromLocalStorage()
this.beforeLeaveWebview()
// 实例化监听
this._observer = new IntersectionObserver(function(entries, observer) {
entries.forEach((entry) => {
// 出现在视窗中
if (entry.isIntersecting) {
// 清除当前定时器
clearInterval(this._timer)
// 获取参数
const tp = entry.target.attributes['track-params'].value
// 收集参数统一上报,减少网络请求
self.cacheDataArr.push(tp)
// 曝光之后取消观察
self._observer.unobserve(entry.target)
if (self.cacheDataArr.length >= self.maxNum) {
self.track()
} else {
self.storeIntoLocalStorage(self.cacheDataArr)
if (self.cacheDataArr.length > 0) {
// 没2秒上报一次
self._timer = setInterval(function() {
self.track()
}, 2000)
}
}
}
})
},
{
root: null,
rootMargin: '0px',
threshold: 0.5
})
}
/**
* 给元素添加监听
* @param {Element} entry
*/
add(entry) {
this._observer && this._observer.observe(entry.el)
}
/**
* 埋点上报
*/
track() {
const trackData = this.cacheDataArr.splice(0, this.maxNum)
track(trackData)
// 更新localStoragee
this.storeIntoLocalStorage(this.cacheDataArr)
}
/**
* 存储到localstorage, 防止在设定上报时间内用户退出
* @param { Arrary } data
*/
storeIntoLocalStorage(data) {
window.localStorage.setItem('cacheTrackData', data)
}
/**
* 首次进入先获取localStorage中的数据,也就是用户上次退出未上报的数据
*/
trackFromLocalStorage() {
const cacheData = window.localStorage.getItem('cacheTrackData')
if (cacheData) {
track(cacheData)
}
}
/**
* 用户退出系统时调用方法,需要和客户端同学协商注册事件
*/
beforeLeaveWebview() {
// 客户端自定义事件监听上报
}
}