-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathreportPerf.ts
43 lines (41 loc) · 1.11 KB
/
reportPerf.ts
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
import { config } from './config';
import { getNavigatorInfo } from './getNavigatorInfo';
import { visibility } from './onVisibilityChange';
import { IVitalsScore, INavigationType } from './types';
import { pushTask } from './utils';
/**
* Sends the User timing measure to analyticsTracker
*/
export const reportPerf = (
measureName: string,
data: any,
rating: IVitalsScore,
attribution: object,
navigationType?: INavigationType,
): void => {
const reportTask = () => {
if (!config.analyticsTracker) {
return;
}
// Doesn't send timing when page is hidden
if (visibility.isHidden && !['CLS', 'INP'].includes(measureName)) {
return;
}
// Send metric to custom Analytics service
config.analyticsTracker({
attribution,
metricName: measureName,
data,
navigatorInformation: getNavigatorInfo(),
rating,
navigationType,
});
};
// Send CLS and INP metrics immediately,
// because this metrics are reported when page is hidden or closed
if (['CLS', 'INP'].includes(measureName)) {
reportTask();
} else {
pushTask(reportTask);
}
};