-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.js
189 lines (165 loc) · 3.88 KB
/
hello.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
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
/*! withcabin.com 0.5.9 */
;(async function (window, document, host) {
// Use a custom domain or not
host = host[0] === '{' ? 'ping.withcabin.com' : host
const nav = window.navigator
// Kill requests from bots and spiders
if (nav.userAgent.search(/(bot|spider|crawl)/gi) > -1) {
return
}
const disable = 'disableCabin',
ael = 'addEventListener',
rel = 'removeEventListener',
cache = '/cache?',
ps = 'pushState',
sb = 'sendBeacon',
tm = 'timing',
ls = 'localStorage',
ci = 'Cabin is',
blocked = ['unblocked', 'blocked'],
dce = 'data-cabin-event',
log = console.log
const block = viaPage => {
let b = parseFloat(window[ls].getItem(blocked[1]))
b &&
viaPage &&
log(
ci +
' blocked on ' +
loc.hostname +
' - Use cabin.blockMe(0) to unblock'
)
return b
}
const send = (url, viaPage) => {
if (block(viaPage)) {
return new Promise(resolve => resolve())
}
const xhr = new XMLHttpRequest()
return new Promise((resolve, reject) => {
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
resolve(parseFloat(xhr.response))
}
}
xhr.open('GET', url)
xhr.send()
})
}
const loc = window.location
const perf = window.performance
const screen = window.screen
const url = 'https://' + host // Should always be https://, only http:// for local testing
const now = () => Date.now()
const add = () => (duration += now() - snapshot)
const params = data =>
Object.keys(data)
.map(key => `${key}=${encodeURIComponent(data[key])}`)
.join('&')
const beacon = (url, data) => {
if (block()) {
return
}
if (nav[sb]) {
nav[sb](url, JSON.stringify(data))
} else {
return send(`${url}?${params(data)}`)
}
}
let start, snapshot, duration, data
const pageview = async () => {
if (window[disable]) {
delete window[disable]
return
}
delete window[disable]
start = now()
snapshot = start
duration = 0
// Get the load time
let time =
perf && perf[tm]
? perf[tm].domContentLoadedEventEnd - perf[tm].navigationStart
: 0
// set data package
data = {
r: document.referrer,
w: screen.width,
s: 0, // temporary placeholder
t: time > 0 ? time : 0,
p: loc.href,
}
let h = loc.hostname
let p = loc.pathname
await Promise.all([
send(url + cache + h).then(u => {
data.u = u
}),
send(url + cache + h + p).then(up => {
data.up = up
}),
])
send(url + '/hello?' + params(data), true)
}
document[ael]('visibilitychange', () => {
document.hidden ? add() : (snapshot = now())
})
const sendDuration = async () => {
if (window[disable]) {
return
}
!document.hidden ? add() : null
await beacon(url + '/duration', { d: duration, n: start, p: loc.href })
}
// log the pageview duration
window[ael]('beforeunload', sendDuration)
let _pushState = function (type) {
let original = history[type]
return function () {
let r = original.apply(this, arguments),
e
if (typeof Event == 'function') {
e = new Event(type)
} else {
e = doc.createEvent('Event')
e.initEvent(type, true, true)
}
e.arguments = arguments
window.dispatchEvent(e)
return r
}
}
window.history[ps] = _pushState(ps)
window[ael](ps, () => {
sendDuration()
pageview()
})
let listener = e => cabin.event(e.target.getAttribute(dce))
// add global object for capturing events
window.cabin = {
async event(value, callback) {
add()
const data = {
e: value,
p: loc.href,
d: duration,
n: start,
}
await beacon(url + '/event', data)
callback && callback()
},
initEvents() {
document.querySelectorAll('[' + dce + ']').forEach(item => {
item[rel]('click', listener)
item[ael]('click', listener)
})
},
blockMe(v) {
v = v ? 1 : 0
window[ls].setItem(blocked[1], v)
log(ci + ' now ' + blocked[v] + ' on ' + loc.hostname)
},
}
pageview()
cabin.initEvents()
})(window, document, '{{.Host}}')