Skip to content

Commit 620e846

Browse files
committed
push web vitals to google analytics
1 parent 205a9cd commit 620e846

File tree

3 files changed

+32
-1
lines changed

3 files changed

+32
-1
lines changed

frontend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@types/react-dom": "^18.0.0",
1010
"react": "^18.2.0",
1111
"react-dom": "^18.2.0",
12+
"react-ga4": "^2.1.0",
1213
"react-hotkeys-hook": "^4.5.0",
1314
"react-router-dom": "^6.22.1",
1415
"react-select": "^5.8.0",

frontend/src/index.tsx

+26-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import './index.css'
44
import App from './App'
55
import reportWebVitals from './reportWebVitals'
66
import { BrowserRouter } from 'react-router-dom'
7+
import { Metric } from 'web-vitals'
8+
import ReactGA from 'react-ga4'
79

810
const root = ReactDOM.createRoot(
911
document.getElementById('root') as HTMLElement
@@ -16,7 +18,30 @@ root.render(
1618
</React.StrictMode>
1719
)
1820

21+
ReactGA.initialize([
22+
{
23+
trackingId: 'G-XEN77GP035'
24+
}
25+
])
26+
27+
const getEventValueFromMetric = (metric: Metric) => {
28+
if (metric.name === 'CLS') {
29+
return Math.round(metric.value * 1000)
30+
}
31+
return Math.round(metric.value)
32+
}
33+
34+
const reportHandler = (metric: Metric) => {
35+
ReactGA.event({
36+
category: 'Web Vitals',
37+
action: metric.name,
38+
value: getEventValueFromMetric(metric),
39+
label: metric.id,
40+
nonInteraction: true
41+
})
42+
}
43+
1944
// If you want to start measuring performance in your app, pass a function
2045
// to log results (for example: reportWebVitals(console.log))
2146
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
22-
reportWebVitals()
47+
reportWebVitals(reportHandler)

frontend/yarn.lock

+5
Original file line numberDiff line numberDiff line change
@@ -8689,6 +8689,11 @@ react-error-overlay@^6.0.11:
86898689
resolved "https://registry.yarnpkg.com/react-error-overlay/-/react-error-overlay-6.0.11.tgz#92835de5841c5cf08ba00ddd2d677b6d17ff9adb"
86908690
integrity sha512-/6UZ2qgEyH2aqzYZgQPxEnz33NJ2gNsnHA2o5+o4wW9bLM/JYQitNP9xPhsXwC08hMMovfGe/8retsdDsczPRg==
86918691

8692+
react-ga4@^2.1.0:
8693+
version "2.1.0"
8694+
resolved "https://registry.yarnpkg.com/react-ga4/-/react-ga4-2.1.0.tgz#56601f59d95c08466ebd6edfbf8dede55c4678f9"
8695+
integrity sha512-ZKS7PGNFqqMd3PJ6+C2Jtz/o1iU9ggiy8Y8nUeksgVuvNISbmrQtJiZNvC/TjDsqD0QlU5Wkgs7i+w9+OjHhhQ==
8696+
86928697
react-hotkeys-hook@^4.5.0:
86938698
version "4.5.0"
86948699
resolved "https://registry.yarnpkg.com/react-hotkeys-hook/-/react-hotkeys-hook-4.5.0.tgz#807b389b15256daf6a813a1ec09e6698064fe97f"

0 commit comments

Comments
 (0)