-
Notifications
You must be signed in to change notification settings - Fork 232
Expand file tree
/
Copy pathindex.ts
More file actions
36 lines (33 loc) · 1.54 KB
/
Copy pathindex.ts
File metadata and controls
36 lines (33 loc) · 1.54 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
import { BrowserPlugin } from '@snowplow/browser-tracker-core';
import { constructNavigationTimingContext, getPerformanceNavigationTimingContext } from './contexts';
import { SelfDescribingJson } from '@snowplow/tracker-core';
let navigationTimingEntry: SelfDescribingJson<Record<string, unknown>>[];
/**
* Adds Performance Navigation Timing context to events
*
* @remarks
* May not be fully populated when initial Page View fires
* Often a good idea to take the latest performance timing context
* for a given page view id when analyzing in the warehouse
*/
export function PerformanceNavigationTimingPlugin(): BrowserPlugin {
return {
activateBrowserPlugin: () => {
/* Setup a PerformanceObserver to allow caching of a completed navigation timing entry. We don't need to query the API after that. */
if (typeof PerformanceObserver !== 'undefined') {
const perfObserver = new PerformanceObserver((observedEntries) => {
const [observedNavigationTiming] = observedEntries.getEntries() as PerformanceNavigationTiming[];
/* We mark the entry as final by the measurement of loadEventEnd which is the final in the processing model */
if (observedNavigationTiming.loadEventEnd > 0) {
navigationTimingEntry = constructNavigationTimingContext(observedNavigationTiming);
}
});
perfObserver.observe({
type: 'navigation',
buffered: true,
});
}
},
contexts: () => navigationTimingEntry || getPerformanceNavigationTimingContext(),
};
}