Skip to content

Commit 717f9b5

Browse files
authored
Remove unsupported ga-lite and update existing calls to GA4 (#6366)
* Cleanup old ga-lite tag and refactor GA4 * cleanup * cleanup2
1 parent a0cacd7 commit 717f9b5

File tree

7 files changed

+18
-27
lines changed

7 files changed

+18
-27
lines changed

.env.production

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
NEXT_PUBLIC_GA_TRACKING_ID = 'UA-41298772-4'
1+
NEXT_PUBLIC_GA_TRACKING_ID = 'G-B1E83PJ3RT'

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
"classnames": "^2.2.6",
3131
"date-fns": "^2.16.1",
3232
"debounce": "^1.2.1",
33-
"ga-lite": "^2.1.4",
3433
"github-slugger": "^1.3.0",
3534
"next": "^13.4.1",
3635
"next-remote-watch": "^1.0.0",

src/components/Layout/Feedback.tsx

+6-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import {useState} from 'react';
66
import {useRouter} from 'next/router';
7-
import {ga} from '../../utils/analytics';
87

98
export function Feedback({onSubmit = () => {}}: {onSubmit?: () => void}) {
109
const {asPath} = useRouter();
@@ -48,14 +47,12 @@ const thumbsDownIcon = (
4847
function sendGAEvent(isPositive: boolean) {
4948
// Fragile. Don't change unless you've tested the network payload
5049
// and verified that the right events actually show up in GA.
51-
ga(
52-
'send',
53-
'event',
54-
'button',
55-
'feedback',
56-
window.location.pathname,
57-
isPositive ? '1' : '0'
58-
);
50+
// @ts-ignore
51+
gtag('event', 'feedback', {
52+
event_category: 'button',
53+
event_label: window.location.pathname,
54+
value: isPositive ? 1 : 0,
55+
});
5956
}
6057

6158
function SendFeedback({onSubmit}: {onSubmit: () => void}) {

src/pages/_app.tsx

+9-8
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
import {useEffect} from 'react';
66
import {AppProps} from 'next/app';
77
import {useRouter} from 'next/router';
8-
import {ga} from '../utils/analytics';
98

109
import '@docsearch/css';
1110
import '../styles/algolia.css';
1211
import '../styles/index.css';
1312
import '../styles/sandpack.css';
1413

1514
if (typeof window !== 'undefined') {
16-
if (process.env.NODE_ENV === 'production') {
17-
ga('create', process.env.NEXT_PUBLIC_GA_TRACKING_ID, 'auto');
18-
ga('send', 'pageview');
19-
}
2015
const terminationEvent = 'onpagehide' in window ? 'pagehide' : 'unload';
2116
window.addEventListener(terminationEvent, function () {
22-
ga('send', 'timing', 'JS Dependencies', 'unload');
17+
// @ts-ignore
18+
gtag('event', 'timing', {
19+
event_label: 'JS Dependencies',
20+
event: 'unload',
21+
});
2322
});
2423
}
2524

@@ -44,8 +43,10 @@ export default function MyApp({Component, pageProps}: AppProps) {
4443
useEffect(() => {
4544
const handleRouteChange = (url: string) => {
4645
const cleanedUrl = url.split(/[\?\#]/)[0];
47-
ga('set', 'page', cleanedUrl);
48-
ga('send', 'pageview');
46+
// @ts-ignore
47+
gtag('event', 'pageview', {
48+
event_label: cleanedUrl,
49+
});
4950
};
5051
router.events.on('routeChangeComplete', handleRouteChange);
5152
return () => {

src/pages/_document.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ const MyDocument = () => {
1111
<Head />
1212
<script
1313
async
14-
src={`https://www.googletagmanager.com/gtag/js?id=${siteConfig.googleAnalyticsTagId}`}
14+
src={`https://www.googletagmanager.com/gtag/js?id=${process.env.NEXT_PUBLIC_GA_TRACKING_ID}`}
1515
/>
1616
<script
1717
dangerouslySetInnerHTML={{
18-
__html: `window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', '${siteConfig.googleAnalyticsTagId}');`,
18+
__html: `window.dataLayer = window.dataLayer || [];function gtag(){dataLayer.push(arguments);}gtag('js', new Date());gtag('config', '${process.env.NEXT_PUBLIC_GA_TRACKING_ID}');`,
1919
}}
2020
/>
2121
<body className="font-text font-medium antialiased text-lg bg-wash dark:bg-wash-dark text-secondary dark:text-secondary-dark leading-base">

src/siteConfig.js

-1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ exports.siteConfig = {
1717
apiKey: 'e8451218980a351815563de007648b00',
1818
indexName: 'beta-react',
1919
},
20-
googleAnalyticsTagId: 'G-B1E83PJ3RT',
2120
};

yarn.lock

-5
Original file line numberDiff line numberDiff line change
@@ -2765,11 +2765,6 @@ functional-red-black-tree@^1.0.1:
27652765
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
27662766
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=
27672767

2768-
ga-lite@^2.1.4:
2769-
version "2.1.4"
2770-
resolved "https://registry.yarnpkg.com/ga-lite/-/ga-lite-2.1.4.tgz#f0c1fd3234bc44d8b15db640a93b34dbce84d43a"
2771-
integrity sha512-OJqPbutD8SfgF8SebyLh0uHdpQH32d5TuviXao5yPjyO2sGW3Gm+ooDmOhvXITgPepwwKxzv7ZMVKk2/hg/+lg==
2772-
27732768
gensync@^1.0.0-beta.2:
27742769
version "1.0.0-beta.2"
27752770
resolved "https://registry.yarnpkg.com/gensync/-/gensync-1.0.0-beta.2.tgz#32a6ee76c3d7f52d46b2b1ae5d93fea8580a25e0"

0 commit comments

Comments
 (0)