-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathevents.ts
More file actions
449 lines (389 loc) · 13.2 KB
/
events.ts
File metadata and controls
449 lines (389 loc) · 13.2 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import Cookies from '@/frame/components/lib/cookies'
import { ANALYTICS_ENABLED } from '@/frame/lib/constants'
import { parseUserAgent } from './user-agent'
import { Router } from 'next/router'
import { isLoggedIn } from '@/frame/components/hooks/useHasAccount'
import { getExperimentVariationForContext } from './experiments/experiment'
import { EventType, EventPropsByType } from '../types'
import { isHeadless } from './is-headless'
const COOKIE_NAME = '_docs-events'
const startVisitTime = Date.now()
const BATCH_INTERVAL = 5000 // 5 seconds
let initialized = false
let cookieValue: string | undefined
let pageEventId: string | undefined
let sentExit = false
let pauseScrolling = false
let scrollPosition = 0
let scrollDirection = 1
let scrollFlipCount = 0
let maxScrollY = 0
let previousPath: string | undefined
let hoveredUrls = new Set()
let eventQueue: any[] = []
function scheduleNextFlush() {
setTimeout(() => {
flushQueue()
scheduleNextFlush()
}, BATCH_INTERVAL)
}
scheduleNextFlush()
function resetPageParams() {
sentExit = false
pauseScrolling = false
scrollPosition = 0
scrollDirection = 1
scrollFlipCount = 0
maxScrollY = 0
// Don't reset previousPath
hoveredUrls = new Set()
}
// Temporary polyfill for crypto.randomUUID()
// Necessary for localhost development (doesn't have https://)
export function uuidv4(): string {
try {
return crypto.randomUUID()
} catch {
// https://stackoverflow.com/a/2117523
return (<any>[1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, (c: number) =>
(c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)))).toString(16),
)
}
}
export function getUserEventsId() {
if (cookieValue) return cookieValue
cookieValue = Cookies.get(COOKIE_NAME)
if (cookieValue) return cookieValue
cookieValue = uuidv4()
Cookies.set(COOKIE_NAME, cookieValue)
return cookieValue
}
function getMetaContent(name: string) {
const metaTag = document.querySelector(`meta[name="${name}"]`) as HTMLMetaElement
return metaTag?.content
}
export function sendEvent<T extends EventType>({
type,
version = '1.0.0',
eventGroupKey,
eventGroupId,
...props
}: {
type: T
version?: string
eventGroupKey?: string
eventGroupId?: string
} & EventPropsByType[T]) {
const body = {
type,
context: {
// Primitives
event_id: uuidv4(),
user: getUserEventsId(),
version,
created: new Date().toISOString(),
page_event_id: pageEventId,
// Content information
referrer: getReferrer(document.referrer),
title: document.title,
href: location.href, // full URL
hostname: location.hostname, // origin without protocol or port
path: location.pathname, // path without search or host
search: location.search, // also known as query string
hash: location.hash, // also known as anchor
path_language: getMetaContent('path-language'),
path_version: getMetaContent('path-version'),
path_product: getMetaContent('path-product'),
path_article: getMetaContent('path-article'),
page_document_type: getMetaContent('page-document-type'),
page_type: getMetaContent('page-type'),
content_type: getMetaContent('page-content-type'),
status: Number(getMetaContent('status') || 0),
is_logged_in: isLoggedIn(),
// Device information
// os, os_version, browser, browser_version:
...parseUserAgent(),
is_headless: isHeadless(),
viewport_width: document.documentElement.clientWidth,
viewport_height: document.documentElement.clientHeight,
screen_width: window.screen.width,
screen_height: window.screen.height,
pixel_ratio: window.devicePixelRatio || 1,
user_agent: navigator.userAgent,
// Location information
timezone: new Date().getTimezoneOffset() / -60,
user_language: navigator.language,
// Preference information
application_preference: Cookies.get('toolPreferred'),
color_mode_preference: getColorModePreference(),
os_preference: Cookies.get('osPreferred'),
code_display_preference: Cookies.get('annotate-mode'),
experiment_variation:
getExperimentVariationForContext(
getMetaContent('path-language'),
getMetaContent('path-version'),
) || '',
// Event grouping
event_group_key: eventGroupKey,
event_group_id: eventGroupId,
},
...props,
}
queueEvent(body)
if (type === EventType.exit) {
flushQueue()
}
return body
}
function flushQueue() {
if (!eventQueue.length) return
const endpoint = '/api/events'
const eventsBody = JSON.stringify(eventQueue)
eventQueue = []
try {
navigator.sendBeacon(endpoint, new Blob([eventsBody], { type: 'application/json' }))
} catch (err) {
console.warn(`sendBeacon to '${endpoint}' failed.`, err)
}
}
function queueEvent(eventBody: unknown) {
eventQueue.push(eventBody)
}
// Sometimes using the back button means the internal referrer path is not there,
// So this fills it in with a JavaScript variable
function getReferrer(documentReferrer: string) {
if (!previousPath) return documentReferrer
try {
// new URL() throws an error if not a valid URL
const referrerUrl = new URL(documentReferrer)
if (!referrerUrl.pathname || referrerUrl.pathname === '/') {
return location.origin + previousPath
}
} catch {}
return documentReferrer
}
function getColorModePreference() {
// color mode is set as attributes on <html>, we'll use that information
// along with media query checking rather than parsing the cookie value
// set by github.com
let color_mode_preference = document.querySelector('html')?.dataset.colorMode
if (color_mode_preference === 'auto') {
if (window.matchMedia('(prefers-color-scheme: light)').matches) {
color_mode_preference += ':light'
} else if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
color_mode_preference += ':dark'
}
}
return color_mode_preference
}
function getPerformance() {
const paint = performance
?.getEntriesByType('paint')
?.find(({ name }) => name === 'first-contentful-paint')
const nav = performance?.getEntriesByType('navigation')?.[0] as
| PerformanceNavigationTiming
| undefined
return {
firstContentfulPaint: paint ? paint.startTime / 1000 : undefined,
domInteractive: nav ? nav.domInteractive / 1000 : undefined,
domComplete: nav ? nav.domComplete / 1000 : undefined,
render: nav ? (nav.responseEnd - nav.requestStart) / 1000 : undefined,
}
}
function trackScroll() {
// Throttle the calculations to no more than five per second
if (pauseScrolling) return
pauseScrolling = true
setTimeout(() => {
pauseScrolling = false
}, 200)
// Calculate where we are on the page
const scrollPixels = window.scrollY + window.innerHeight
const newScrollPosition = scrollPixels / document.documentElement.scrollHeight
// Count scroll flips
const newScrollDirection = Math.sign(newScrollPosition - scrollPosition)
if (newScrollDirection !== scrollDirection) scrollFlipCount++
// Update maximum scroll position reached
if (newScrollPosition > maxScrollY) maxScrollY = newScrollPosition
// Update before the next event
scrollDirection = newScrollDirection
scrollPosition = newScrollPosition
}
function sendPage() {
const pageEvent = sendEvent({ type: EventType.page })
pageEventId = pageEvent?.context?.event_id
}
function sendExit() {
if (sentExit) return
sentExit = true
const { render, firstContentfulPaint, domInteractive, domComplete } = getPerformance()
const clampedScrollLength = Math.min(maxScrollY, 1)
return sendEvent({
type: EventType.exit,
exit_render_duration: render,
exit_first_paint: firstContentfulPaint,
exit_dom_interactive: domInteractive,
exit_dom_complete: domComplete,
exit_visit_duration: (Date.now() - startVisitTime) / 1000,
exit_scroll_length: clampedScrollLength,
exit_scroll_flip: scrollFlipCount,
})
}
function initPageAndExitEvent() {
sendPage() // Initial page hit
// Regular page exits
window.addEventListener('scroll', trackScroll)
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'hidden') {
sendExit()
} else {
flushQueue()
}
})
// Client-side routing
Router.events.on('routeChangeStart', async (url) => {
// Don't trigger page events on query string or hash changes
previousPath = location.pathname // pathname set to "prior" url, arg "upcoming" url
const newPath = url?.toString().split('?')[0].split('#')[0]
const shouldSendEvents = newPath !== previousPath
if (shouldSendEvents) {
sendExit()
await waitForPageReady()
resetPageParams()
sendPage()
}
})
}
// We want to wait for the DOM to mutate the <meta> tags
// as well as finish routeChangeComplete (location.pathname)
// before sending the page event in order to get accurate data
async function waitForPageReady() {
const route = new Promise((resolve) => {
const handler = () => {
Router.events.off('routeChangeComplete', handler)
setTimeout(() => resolve(true))
}
Router.events.on('routeChangeComplete', handler)
})
const mutate = new Promise((resolve) => {
const observer = new MutationObserver((mutations) => {
const metaMutated = mutations.find(
(mutation) =>
mutation.target?.nodeName === 'META' ||
Array.from(mutation.addedNodes).find((node) => node.nodeName === 'META') ||
Array.from(mutation.removedNodes).find((node) => node.nodeName === 'META'),
)
if (metaMutated) {
observer.disconnect()
setTimeout(() => resolve(true))
}
})
observer.observe(document.getElementsByTagName('head')[0], {
subtree: true,
childList: true,
attributes: true,
})
})
return Promise.all([route, mutate])
}
function initClipboardEvent() {
for (const verb of ['copy', 'cut', 'paste']) {
document.documentElement.addEventListener(verb, () => {
sendEvent({ type: EventType.clipboard, clipboard_operation: verb })
})
}
}
function initCopyButtonEvent() {
document.documentElement.addEventListener('click', (evt) => {
const target = evt.target as HTMLElement
const button = target.closest('.js-btn-copy') as HTMLButtonElement
if (!button) return
sendEvent({
type: EventType.clipboard,
clipboard_operation: 'copy',
clipboard_target: '.js-btn-copy',
})
})
}
function initLinkEvent() {
document.documentElement.addEventListener('click', (evt) => {
const target = evt.target as HTMLElement
const link = target.closest('a[href]') as HTMLAnchorElement
if (!link) return
const sameSite = link.origin === location.origin
const container = target.closest(`[data-container]`) as HTMLElement | null
// We can attach `data-group-key` and `data-group-id` to any anchor element to include them in the event
const eventGroupKey = link?.dataset?.groupKey || undefined
const eventGroupId = link?.dataset?.groupId || undefined
sendEvent({
type: EventType.link,
link_url: link.href,
link_samesite: sameSite,
link_samepage: sameSite && link.pathname === location.pathname,
link_container: container?.dataset.container,
eventGroupKey,
eventGroupId,
})
})
// Add tracking for scroll to top button
document.documentElement.addEventListener('click', (evt) => {
const target = evt.target as HTMLElement
if (!target.closest('.ghd-scroll-to-top')) return
const url = window.location.href.split('#')[0] // Remove hash
sendEvent({
type: EventType.link,
link_url: `${url}#scroll-to-top`,
link_samesite: true,
link_samepage: true,
link_container: 'static',
})
})
}
function initHoverEvent() {
let timer: number | null = null
document.documentElement.addEventListener('mouseover', (evt) => {
const target = evt.target as HTMLElement
const link = target.closest('a[href]') as HTMLAnchorElement | null
if (!link) return
// For hover events, we only want to record them for links inside the
// content area.
const mainContent = document.querySelector('#main-content') as HTMLElement | null
if (!mainContent || !mainContent.contains(link)) return
if (hoveredUrls.has(link.href)) return // Otherwise this is a flood of events
if (timer) {
window.clearTimeout(timer)
}
timer = window.setTimeout(() => {
const sameSite = link.origin === location.origin
hoveredUrls.add(link.href)
sendEvent({
type: EventType.hover,
hover_url: link.href,
hover_samesite: sameSite,
})
}, 500)
})
// Doesn't matter which link you hovered on that triggered a timer,
// you're clearly not hovering over it anymore.
document.documentElement.addEventListener('mouseout', () => {
if (timer) {
window.clearTimeout(timer)
}
})
}
function initPrintEvent() {
window.addEventListener('beforeprint', () => {
sendEvent({ type: EventType.print })
})
}
export function initializeEvents() {
if (!ANALYTICS_ENABLED) return
if (initialized) return
initialized = true
initPageAndExitEvent() // must come first
initLinkEvent()
initHoverEvent()
initClipboardEvent()
initCopyButtonEvent()
initPrintEvent()
}