From cd5045ff2e20b2bd595eb375f4b56205668fe36e Mon Sep 17 00:00:00 2001 From: Kathryn Middleton Date: Fri, 20 Oct 2023 19:40:33 +0000 Subject: [PATCH 1/3] Cleanup old ga-lite tag and refactor GA4 --- .env.production | 2 +- package.json | 1 - src/components/Layout/Feedback.tsx | 15 +++++------- src/pages/_app.tsx | 37 ++++++++++++++++++++---------- src/pages/_document.tsx | 9 -------- src/siteConfig.js | 1 - yarn.lock | 5 ---- 7 files changed, 32 insertions(+), 38 deletions(-) diff --git a/.env.production b/.env.production index d25eb7dd4b6..e403f96b683 100644 --- a/.env.production +++ b/.env.production @@ -1 +1 @@ -NEXT_PUBLIC_GA_TRACKING_ID = 'UA-41298772-4' \ No newline at end of file +NEXT_PUBLIC_GA_TRACKING_ID = 'G-B1E83PJ3RT' \ No newline at end of file diff --git a/package.json b/package.json index 472ef79c912..b5e07d70a84 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,6 @@ "classnames": "^2.2.6", "date-fns": "^2.16.1", "debounce": "^1.2.1", - "ga-lite": "^2.1.4", "github-slugger": "^1.3.0", "next": "^13.4.1", "next-remote-watch": "^1.0.0", diff --git a/src/components/Layout/Feedback.tsx b/src/components/Layout/Feedback.tsx index 2bf9afe5755..86fc91350a9 100644 --- a/src/components/Layout/Feedback.tsx +++ b/src/components/Layout/Feedback.tsx @@ -4,7 +4,6 @@ import {useState} from 'react'; import {useRouter} from 'next/router'; -import {ga} from '../../utils/analytics'; export function Feedback({onSubmit = () => {}}: {onSubmit?: () => void}) { const {asPath} = useRouter(); @@ -48,14 +47,12 @@ const thumbsDownIcon = ( function sendGAEvent(isPositive: boolean) { // Fragile. Don't change unless you've tested the network payload // and verified that the right events actually show up in GA. - ga( - 'send', - 'event', - 'button', - 'feedback', - window.location.pathname, - isPositive ? '1' : '0' - ); + // @ts-ignore + gtag('event', 'feedback', { + event_category: 'button', + event_label: window.location.pathname, + value: isPositive ? 1 : 0, + }); } function SendFeedback({onSubmit}: {onSubmit: () => void}) { diff --git a/src/pages/_app.tsx b/src/pages/_app.tsx index 7173759688b..6bc04fe1113 100644 --- a/src/pages/_app.tsx +++ b/src/pages/_app.tsx @@ -5,7 +5,7 @@ import {useEffect} from 'react'; import {AppProps} from 'next/app'; import {useRouter} from 'next/router'; -import {ga} from '../utils/analytics'; +import Script from 'next/script'; import '@docsearch/css'; import '../styles/algolia.css'; @@ -13,14 +13,15 @@ import '../styles/index.css'; import '../styles/sandpack.css'; if (typeof window !== 'undefined') { - if (process.env.NODE_ENV === 'production') { - ga('create', process.env.NEXT_PUBLIC_GA_TRACKING_ID, 'auto'); - ga('send', 'pageview'); - } - const terminationEvent = 'onpagehide' in window ? 'pagehide' : 'unload'; - window.addEventListener(terminationEvent, function () { - ga('send', 'timing', 'JS Dependencies', 'unload'); - }); + // @ts-ignore + window.dataLayer = window.dataLayer || []; + // @ts-ignore + window.gtag = function () { + // @ts-ignore + window.dataLayer.push(arguments); + }; + // @ts-ignore + gtag('js', new Date()); } export default function MyApp({Component, pageProps}: AppProps) { @@ -44,8 +45,10 @@ export default function MyApp({Component, pageProps}: AppProps) { useEffect(() => { const handleRouteChange = (url: string) => { const cleanedUrl = url.split(/[\?\#]/)[0]; - ga('set', 'page', cleanedUrl); - ga('send', 'pageview'); + // @ts-ignore + gtag('config', process.env.NEXT_PUBLIC_GA_TRACKING_ID, { + page_path: cleanedUrl, + }); }; router.events.on('routeChangeComplete', handleRouteChange); return () => { @@ -53,5 +56,15 @@ export default function MyApp({Component, pageProps}: AppProps) { }; }, [router.events]); - return ; + return ( + <> + + {process.env.NODE_ENV === 'production' && ( +