Skip to content

Commit 11e595f

Browse files
committed
switch ga to use beacon
1 parent c8503bc commit 11e595f

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

frontend/src/reportWebVitals.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Metric } from 'web-vitals'
22
import ReactGA from 'react-ga4'
3+
import { UaEventOptions } from 'react-ga4/types/ga4'
34

4-
const getAnalyticsId = () => process.env.GOOGLE_ANALYTICS_TRACKING_ID
5+
const analyticsId = process.env.GOOGLE_ANALYTICS_TRACKING_ID
56
const isDev = () => process.env.NODE_ENV && process.env.NODE_ENV === 'development'
67

78
const getEventValueFromMetric = (metric: Metric) => {
@@ -12,28 +13,29 @@ const getEventValueFromMetric = (metric: Metric) => {
1213
}
1314

1415
const reportHandler = (metric: Metric) => {
15-
const event = {
16+
const event: UaEventOptions = {
1617
category: 'Web Vitals',
1718
action: metric.name,
1819
value: getEventValueFromMetric(metric),
1920
label: metric.id,
20-
nonInteraction: true
21+
nonInteraction: true,
22+
transport: 'beacon'
2123
}
2224

2325
if (isDev()) {
2426
console.debug(`[web-vitals-dev] ${event.action}: ${event.value}`, metric) // eslint-disable-line no-console
2527
}
2628

27-
if (getAnalyticsId()) {
29+
if (analyticsId) {
2830
ReactGA.event(event)
2931
}
3032
}
3133

3234
const reportWebVitals = () => {
33-
if (getAnalyticsId()) {
35+
if (analyticsId) {
3436
ReactGA.initialize([
3537
{
36-
trackingId: getAnalyticsId()!
38+
trackingId: analyticsId
3739
}
3840
])
3941
}

0 commit comments

Comments
 (0)