From e3d45e0d2f5fd26133ca70c22b93f8efe716b370 Mon Sep 17 00:00:00 2001 From: Linh Nguyen Date: Mon, 5 Feb 2024 11:12:08 -0500 Subject: [PATCH] Start using GA4 (#2111) --- src/telemetry/ga.js | 16 ++++++++++------ src/telemetry/index.js | 10 +++++----- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/telemetry/ga.js b/src/telemetry/ga.js index 51829416b..f4b7daf62 100644 --- a/src/telemetry/ga.js +++ b/src/telemetry/ga.js @@ -1,8 +1,12 @@ export const googleAnalytics = (gaID) => { - (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){ - (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o), - m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) - })(window,document,'script','https://www.google-analytics.com/analytics.js','ga'); + const script = document.createElement('script'); + script.src = `https://www.googletagmanager.com/gtag/js?id=${gaID}`; + script.async = true; + document.head.appendChild(script); - ga('create', gaID, 'auto'); -}; + window.dataLayer = window.dataLayer || []; + function gtag(){dataLayer.push(arguments);} + gtag('js', new Date()); + + gtag('config', gaID); +}; \ No newline at end of file diff --git a/src/telemetry/index.js b/src/telemetry/index.js index 514e8e409..c857b0a79 100644 --- a/src/telemetry/index.js +++ b/src/telemetry/index.js @@ -58,11 +58,11 @@ export function initializeTelemetry() { * @param {string} path The current page path. */ export function submitPageViewTelemetry(path) { - // Send telemetry to Google Analytics. - if (window.ga) { - // `ga` will not be set if not using google analytics - ga("set", "page", path); - ga("send", "pageview"); + // Send page view event to GA4. + if (window.gtag) { + window.gtag("event", "page_view", { + page_path: path, + }); } // Use the standard events.