From 0f95819b0b9b0ce7c3e823727c9d77253d694497 Mon Sep 17 00:00:00 2001 From: Surai Date: Tue, 20 May 2025 13:43:54 +0200 Subject: [PATCH 1/3] Do not initialize GA before load --- integrations/googleanalytics/src/script.raw.js | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/integrations/googleanalytics/src/script.raw.js b/integrations/googleanalytics/src/script.raw.js index 17696d558..b2aa98fe3 100644 --- a/integrations/googleanalytics/src/script.raw.js +++ b/integrations/googleanalytics/src/script.raw.js @@ -37,21 +37,23 @@ win[layer] = win[layer] || []; win.gtag = function () { + // eslint-disable-next-line prefer-rest-params win[layer].push(arguments); }; - win.gtag('js', new Date()); - win.gtag('config', id); - win.gtag('consent', 'default', { - ad_storage: disableCookies ? 'denied' : 'granted', - analytics_storage: disableCookies ? 'denied' : 'granted', - }); const f = doc.getElementsByTagName(script)[0], j = doc.createElement(script), dl = layer !== 'dataLayer' ? `&l=${layer}` : ''; j.async = true; j.src = `https://www.googletagmanager.com/gtag/js?id=${id}${dl}`; j.onload = function () { + win.gtag('js', new Date()); + + win.gtag('consent', 'default', { + ad_storage: disableCookies ? 'denied' : 'granted', + analytics_storage: disableCookies ? 'denied' : 'granted', + }); + win.gtag('config', id, { send_page_view: false, anonymize_ip: true, From aee5541060593a0eae52365ba921eef779c53678 Mon Sep 17 00:00:00 2001 From: Surai Date: Tue, 20 May 2025 13:45:17 +0200 Subject: [PATCH 2/3] Changeset --- .changeset/spicy-impalas-swim.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spicy-impalas-swim.md diff --git a/.changeset/spicy-impalas-swim.md b/.changeset/spicy-impalas-swim.md new file mode 100644 index 000000000..98bbf6e82 --- /dev/null +++ b/.changeset/spicy-impalas-swim.md @@ -0,0 +1,5 @@ +--- +'@gitbook/integration-googleanalytics': minor +--- + +Initialize GA after script load From 684d45b0b1a76624f27db0f765ef0ca3fa0d3a5a Mon Sep 17 00:00:00 2001 From: Surai Date: Tue, 20 May 2025 13:46:09 +0200 Subject: [PATCH 3/3] Remove useless eslint directive --- integrations/googleanalytics/src/script.raw.js | 1 - 1 file changed, 1 deletion(-) diff --git a/integrations/googleanalytics/src/script.raw.js b/integrations/googleanalytics/src/script.raw.js index b2aa98fe3..4c023b0a1 100644 --- a/integrations/googleanalytics/src/script.raw.js +++ b/integrations/googleanalytics/src/script.raw.js @@ -37,7 +37,6 @@ win[layer] = win[layer] || []; win.gtag = function () { - // eslint-disable-next-line prefer-rest-params win[layer].push(arguments); };