Skip to content

Commit c8503bc

Browse files
committed
refactored ga key to env
1 parent 2b9695a commit c8503bc

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

.github/workflows/cd.yml

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ jobs:
3333
- name: Build
3434
working-directory: frontend/
3535
run: yarn build
36+
env:
37+
GOOGLE_ANALYTICS_TRACKING_ID: ${{ secrets.GOOGLE_ANALYTICS_TRACKING_ID }}
3638
- name: Package
3739
working-directory: frontend/
3840
shell: bash

frontend/src/reportWebVitals.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { Metric } from 'web-vitals'
22
import ReactGA from 'react-ga4'
33

4+
const getAnalyticsId = () => process.env.GOOGLE_ANALYTICS_TRACKING_ID
5+
const isDev = () => process.env.NODE_ENV && process.env.NODE_ENV === 'development'
6+
47
const getEventValueFromMetric = (metric: Metric) => {
58
if (metric.name === 'CLS') {
69
return Math.round(metric.value * 1000)
@@ -17,19 +20,23 @@ const reportHandler = (metric: Metric) => {
1720
nonInteraction: true
1821
}
1922

20-
if (process.env.NODE_ENV && process.env.NODE_ENV === 'development') {
23+
if (isDev()) {
2124
console.debug(`[web-vitals-dev] ${event.action}: ${event.value}`, metric) // eslint-disable-line no-console
22-
} else {
25+
}
26+
27+
if (getAnalyticsId()) {
2328
ReactGA.event(event)
2429
}
2530
}
2631

2732
const reportWebVitals = () => {
28-
ReactGA.initialize([
29-
{
30-
trackingId: 'G-XEN77GP035'
31-
}
32-
])
33+
if (getAnalyticsId()) {
34+
ReactGA.initialize([
35+
{
36+
trackingId: getAnalyticsId()!
37+
}
38+
])
39+
}
3340
import('web-vitals').then(({ getCLS, getFID, getFCP, getLCP, getTTFB }) => {
3441
getCLS(reportHandler)
3542
getFID(reportHandler)
@@ -39,4 +46,4 @@ const reportWebVitals = () => {
3946
})
4047
}
4148

42-
export default reportWebVitals
49+
export default reportWebVitals

0 commit comments

Comments
 (0)