Skip to content

Commit b4aed84

Browse files
committed
migrated from react-ga4 to next/third-parties for analytics
1 parent def8637 commit b4aed84

File tree

7 files changed

+40
-35
lines changed

7 files changed

+40
-35
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"@mui/material-nextjs": "7.3.6",
1212
"@mui/x-date-pickers": "8.23.0",
1313
"@mui/x-tree-view": "8.23.0",
14+
"@next/third-parties": "^16.1.6",
1415
"@reduxjs/toolkit": "^1.9.6",
1516
"@sentry/nextjs": "^10.42.0",
1617
"@turf/center": "^6.5.0",
@@ -39,7 +40,6 @@
3940
"react": "^19.2.3",
4041
"react-dom": "^19.2.3",
4142
"react-draggable": "^4.5.0",
42-
"react-ga4": "^2.1.0",
4343
"react-google-recaptcha": "^3.1.0",
4444
"react-hook-form": "^7.52.1",
4545
"react-leaflet": "^4.2.1",

src/app/[locale]/layout.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SpeedInsights } from '@vercel/speed-insights/next';
22
import { Analytics } from '@vercel/analytics/next';
3+
import { GoogleAnalytics } from '@next/third-parties/google';
34
import ThemeRegistry from '../registry';
45
import { Providers } from '../providers';
56
import { type ReactElement } from 'react';
@@ -12,6 +13,7 @@ import Footer from '../components/Footer';
1213
import Header from '../components/Header';
1314
import { Container } from '@mui/material';
1415
import { type Locale, routing } from '../../i18n/routing';
16+
import { getEnvConfig } from '../utils/config';
1517

1618
export const metadata = {
1719
title:
@@ -125,6 +127,11 @@ export default async function LocaleLayout({
125127
</NextIntlClientProvider>
126128
</ThemeRegistry>
127129
</body>
130+
{getEnvConfig('NEXT_PUBLIC_GOOGLE_ANALYTICS_ID') !== '' && (
131+
<GoogleAnalytics
132+
gaId={getEnvConfig('NEXT_PUBLIC_GOOGLE_ANALYTICS_ID')}
133+
/>
134+
)}
128135
</html>
129136
);
130137
}

src/app/components/CoveredAreaMap.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import ModeOfTravelIcon from '@mui/icons-material/ModeOfTravel';
3636
import { GtfsVisualizationMap } from './GtfsVisualizationMap';
3737
import ZoomOutMapIcon from '@mui/icons-material/ZoomOutMap';
3838
import { useRemoteConfig } from '../context/RemoteConfigProvider';
39-
import ReactGA from 'react-ga4';
39+
import { sendGAEvent } from '@next/third-parties/google';
4040
import { selectGtfsDatasetRoutesLoadingStatus } from '../store/supporting-files-selectors';
4141
import {
4242
getLatestGbfsVersion,
@@ -190,10 +190,9 @@ const CoveredAreaMap: React.FC<CoveredAreaMapProps> = ({
190190
};
191191

192192
const handleOpenDetailedMapClick = (): void => {
193-
ReactGA.event({
194-
category: 'engagement',
195-
action: 'gtfs_visualization_open_detailed_map',
196-
label: 'Open Detailed Map',
193+
sendGAEvent('event', 'gtfs_visualization_open_detailed_map', {
194+
event_category: 'engagement',
195+
event_label: 'Open Detailed Map',
197196
});
198197
};
199198

src/app/screens/Feed/components/ClientDownloadButton.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Button } from '@mui/material';
44
import DownloadIcon from '@mui/icons-material/Download';
5+
import { sendGAEvent } from '@next/third-parties/google';
56
import { useTranslations } from 'next-intl';
67

78
export default function ClientDownloadButton({
@@ -11,13 +12,10 @@ export default function ClientDownloadButton({
1112
}): React.ReactElement {
1213
const t = useTranslations('feeds');
1314

14-
const handleDownloadLatestClick = async (): Promise<void> => {
15-
// Lazy load react-ga4 to avoid loading it unnecessarily
16-
const ReactGA = (await import('react-ga4')).default;
17-
ReactGA.event({
18-
category: 'engagement',
19-
action: 'download_latest_dataset',
20-
label: 'Download Latest Dataset',
15+
const handleDownloadLatestClick = (): void => {
16+
sendGAEvent('event', 'download_latest_dataset', {
17+
event_category: 'engagement',
18+
event_label: 'Download Latest Dataset',
2119
});
2220
};
2321

@@ -30,9 +28,7 @@ export default function ClientDownloadButton({
3028
rel='noreferrer nofollow'
3129
id='download-latest-button'
3230
endIcon={<DownloadIcon />}
33-
onClick={() => {
34-
void handleDownloadLatestClick();
35-
}}
31+
onClick={handleDownloadLatestClick}
3632
>
3733
{t('downloadLatest')}
3834
</Button>

src/app/screens/Feed/components/ClientQualityReportButton.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { Button } from '@mui/material';
44
import OpenInNewIcon from '@mui/icons-material/OpenInNew';
5+
import { sendGAEvent } from '@next/third-parties/google';
56
import { useTranslations } from 'next-intl';
67

78
export default function ClientQualityReportButton({
@@ -11,12 +12,10 @@ export default function ClientQualityReportButton({
1112
}): React.ReactElement {
1213
const t = useTranslations('feeds');
1314

14-
const handleOpenFullQualityReportClick = async (): Promise<void> => {
15-
const ReactGA = (await import('react-ga4')).default;
16-
ReactGA.event({
17-
category: 'engagement',
18-
action: 'open_full_quality_report',
19-
label: 'Open Full Quality Report',
15+
const handleOpenFullQualityReportClick = (): void => {
16+
sendGAEvent('event', 'open_full_quality_report', {
17+
event_category: 'engagement',
18+
event_label: 'Open Full Quality Report',
2019
});
2120
};
2221

@@ -28,9 +27,7 @@ export default function ClientQualityReportButton({
2827
target='_blank'
2928
rel='noreferrer nofollow'
3029
endIcon={<OpenInNewIcon />}
31-
onClick={() => {
32-
void handleOpenFullQualityReportClick();
33-
}}
30+
onClick={handleOpenFullQualityReportClick}
3431
>
3532
{t('openFullQualityReport')}
3633
</Button>

src/app/screens/Feed/components/FeedSummary.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ import OpenInNewIcon from '@mui/icons-material/OpenInNew';
4343
import GavelIcon from '@mui/icons-material/Gavel';
4444
import { getFeedStatusData } from '../../../utils/feedStatusConsts';
4545
import Link from 'next/link';
46-
import ReactGA from 'react-ga4';
46+
import { sendGAEvent } from '@next/third-parties/google';
4747
import { getRouteTypeTranslatedName } from '../../../constants/RouteTypes';
4848
import {
4949
featureChipsStyle,
@@ -95,10 +95,9 @@ export default function FeedSummary({
9595
}, [feed]);
9696

9797
const handleOpenDetailedMapClick = (): void => {
98-
ReactGA.event({
99-
category: 'engagement',
100-
action: 'gtfs_visualization_open_detailed_map',
101-
label: 'Open Detailed Map',
98+
sendGAEvent('event', 'gtfs_visualization_open_detailed_map', {
99+
event_category: 'engagement',
100+
event_label: 'Open Detailed Map',
102101
});
103102
};
104103

yarn.lock

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,6 +2340,13 @@
23402340
resolved "https://registry.yarnpkg.com/@next/swc-win32-x64-msvc/-/swc-win32-x64-msvc-16.1.1.tgz#96e9e335e2577481dab6fc7a2af48f3e4a28fbdb"
23412341
integrity sha512-Ncwbw2WJ57Al5OX0k4chM68DKhEPlrXBaSXDCi2kPi5f4d8b3ejr3RRJGfKBLrn2YJL5ezNS7w2TZLHSti8CMw==
23422342

2343+
"@next/third-parties@^16.1.6":
2344+
version "16.1.6"
2345+
resolved "https://registry.yarnpkg.com/@next/third-parties/-/third-parties-16.1.6.tgz#366980adab28f808ce6142af6b474066e2ad49da"
2346+
integrity sha512-/cLY1egaH529ylSMSK+C8dA3nWDLL4hOFR4fca9OLWWxjcNwzsbuq2pPb/tmdWL9Zj3K1nTjd1pWQoSlaDQ0VA==
2347+
dependencies:
2348+
third-party-capital "1.0.20"
2349+
23432350
"@nodelib/fs.scandir@2.1.5":
23442351
version "2.1.5"
23452352
resolved "https://registry.yarnpkg.com/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz#7619c2eb21b25483f6d167548b4cfd5a7488c3d5"
@@ -11404,11 +11411,6 @@ react-fast-compare@^2.0.1:
1140411411
resolved "https://registry.yarnpkg.com/react-fast-compare/-/react-fast-compare-2.0.4.tgz#e84b4d455b0fec113e0402c329352715196f81f9"
1140511412
integrity sha512-suNP+J1VU1MWFKcyt7RtjiSWUjvidmQSlqu+eHslq+342xCbGTYmC0mEhPCOHxlW0CywylOC1u2DFAT+bv4dBw==
1140611413

11407-
react-ga4@^2.1.0:
11408-
version "2.1.0"
11409-
resolved "https://registry.yarnpkg.com/react-ga4/-/react-ga4-2.1.0.tgz#56601f59d95c08466ebd6edfbf8dede55c4678f9"
11410-
integrity sha512-ZKS7PGNFqqMd3PJ6+C2Jtz/o1iU9ggiy8Y8nUeksgVuvNISbmrQtJiZNvC/TjDsqD0QlU5Wkgs7i+w9+OjHhhQ==
11411-
1141211414
react-google-recaptcha@^3.1.0:
1141311415
version "3.1.0"
1141411416
resolved "https://registry.yarnpkg.com/react-google-recaptcha/-/react-google-recaptcha-3.1.0.tgz#44aaab834495d922b9d93d7d7a7fb2326315b4ab"
@@ -12789,6 +12791,11 @@ thenify-all@^1.0.0:
1278912791
dependencies:
1279012792
any-promise "^1.0.0"
1279112793

12794+
third-party-capital@1.0.20:
12795+
version "1.0.20"
12796+
resolved "https://registry.yarnpkg.com/third-party-capital/-/third-party-capital-1.0.20.tgz#e218a929a35bf4d2245da9addb8ab978d2f41685"
12797+
integrity sha512-oB7yIimd8SuGptespDAZnNkzIz+NWaJCu2RMsbs4Wmp9zSDUM8Nhi3s2OOcqYuv3mN4hitXc8DVx+LyUmbUDiA==
12798+
1279212799
throttleit@^1.0.0:
1279312800
version "1.0.1"
1279412801
resolved "https://registry.yarnpkg.com/throttleit/-/throttleit-1.0.1.tgz#304ec51631c3b770c65c6c6f76938b384000f4d5"

0 commit comments

Comments
 (0)