From cd0884f9bdc53e535368c741c63479411a47cf0a Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Mon, 10 Jun 2024 15:34:13 +0200 Subject: [PATCH] [docs] Migrate to GA4 (#42590) --- docs/pages/_app.js | 12 +++++++++--- docs/pages/_document.js | 16 +++++++++++++--- docs/src/modules/components/Ad.js | 7 ++----- docs/src/modules/components/GoogleAnalytics.js | 18 ++++++++++++------ .../components/material-icons/SearchIcons.js | 4 +--- 5 files changed, 37 insertions(+), 20 deletions(-) diff --git a/docs/pages/_app.js b/docs/pages/_app.js index ffd1cdaeffe36c..7dee41be60ef81 100644 --- a/docs/pages/_app.js +++ b/docs/pages/_app.js @@ -142,11 +142,15 @@ function Analytics() { const codeVariant = usePersistCodeVariant(); React.useEffect(() => { - window.ga('set', 'dimension1', codeVariant); + window.gtag('set', 'user_properties', { + codeVariant, + }); }, [codeVariant]); React.useEffect(() => { - window.ga('set', 'dimension2', options.userLanguage); + window.gtag('set', 'user_properties', { + userLanguage: options.userLanguage, + }); }, [options.userLanguage]); React.useEffect(() => { @@ -160,7 +164,9 @@ function Analytics() { * Adjusted to track 3 or more different ratios */ function trackDevicePixelRation() { - window.ga('set', 'dimension3', Math.round(window.devicePixelRatio * 10) / 10); + window.gtag('set', 'user_properties', { + devicePixelRatio: Math.round(window.devicePixelRatio * 10) / 10, + }); matchMedia = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`); // Need to setup again. diff --git a/docs/pages/_document.js b/docs/pages/_document.js index 2e7d79c01e036b..37e7955825fb1b 100644 --- a/docs/pages/_document.js +++ b/docs/pages/_document.js @@ -26,7 +26,8 @@ if (process.env.NODE_ENV === 'production') { cleanCSS = new CleanCSS(); } -const GOOGLE_ID = process.env.NODE_ENV === 'production' ? 'UA-106598593-2' : 'UA-106598593-3'; +const GOOGLE_ANALYTICS_ID_V4 = + process.env.NODE_ENV === 'production' ? 'G-5NXDQLC2ZK' : 'G-XJ83JQEK7J'; export default class MyDocument extends Document { render() { @@ -85,11 +86,20 @@ export default class MyDocument extends Document { // eslint-disable-next-line react/no-danger dangerouslySetInnerHTML={{ __html: ` - window.ga=window.ga||function(){(ga.q=ga.q||[]).push(arguments)};ga.l=+new Date; - window.ga('create','${GOOGLE_ID}','auto'); +window.dataLayer = window.dataLayer || []; +function gtag(){dataLayer.push(arguments);} +window.gtag = gtag; +gtag('js', new Date()); +gtag('config', '${GOOGLE_ANALYTICS_ID_V4}', { + send_page_view: false, +}); `, }} /> +