From 055dde692a8589c3a9f6f9d6807534b8d78845ad Mon Sep 17 00:00:00 2001 From: Steven Hall Date: Fri, 15 Dec 2023 10:37:35 +0000 Subject: [PATCH] Fix an issue where GA, Heap, and Hotjar integrations could interfere with the global scope of the page. --- .vscode/settings.json | 2 +- .../googleanalytics/src/script.raw.js | 53 ++++++++-------- integrations/heap/src/script.raw.js | 62 +++++++++++-------- integrations/hotjar/src/script.raw.js | 3 +- package-lock.json | 2 +- 5 files changed, 65 insertions(+), 57 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index cfc017047..26ed77580 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -21,7 +21,7 @@ "eslint.format.enable": true, // Fix errors on save using ESLint "editor.codeActionsOnSave": { - "source.fixAll.eslint": true + "source.fixAll.eslint": "explicit" }, // Format using Prettier to format on save "editor.defaultFormatter": "esbenp.prettier-vscode", diff --git a/integrations/googleanalytics/src/script.raw.js b/integrations/googleanalytics/src/script.raw.js index eaeaf33f3..8b78f14a6 100644 --- a/integrations/googleanalytics/src/script.raw.js +++ b/integrations/googleanalytics/src/script.raw.js @@ -1,35 +1,34 @@ -const trackingID = ''; +(function (win, doc, script, layer) { + const id = ''; + const GRANTED_COOKIE = '__gitbook_cookie_granted'; -const GANTED_COOKIE = '__gitbook_cookie_granted'; + function triggerView(win) { + win.gtag('event', 'page_view', { + page_path: win.location.pathname, + page_location: win.location.href, + page_title: win.document.title, + send_to: 'tracking_views', + }); + } -function getCookie(cname) { - const name = `${cname}=`; - const decodedCookie = decodeURIComponent(document.cookie); - const ca = decodedCookie.split(';'); - for (let i = 0; i < ca.length; i++) { - let c = ca[i]; - while (c.charAt(0) === ' ') { - c = c.substring(1); - } - if (c.indexOf(name) === 0) { - return c.substring(name.length, c.length); + function getCookie(cname) { + const name = `${cname}=`; + const decodedCookie = decodeURIComponent(document.cookie); + const ca = decodedCookie.split(';'); + for (let i = 0; i < ca.length; i++) { + let c = ca[i]; + while (c.charAt(0) === ' ') { + c = c.substring(1); + } + if (c.indexOf(name) === 0) { + return c.substring(name.length, c.length); + } } + return ''; } - return ''; -} - -function triggerView(win) { - win.gtag('event', 'page_view', { - page_path: win.location.pathname, - page_location: win.location.href, - page_title: win.document.title, - send_to: 'tracking_views', - }); -} -(function (win, doc, script, layer, id) { let disableCookies = false; - const cookie = getCookie(GANTED_COOKIE); + const cookie = getCookie(GRANTED_COOKIE); if (cookie === 'yes') { disableCookies = false; } else if (cookie === 'no') { @@ -69,4 +68,4 @@ function triggerView(win) { }); }; f.parentNode.insertBefore(j, f); -})(window, document, 'script', 'dataLayer', trackingID); +})(window, document, 'script', 'dataLayer'); diff --git a/integrations/heap/src/script.raw.js b/integrations/heap/src/script.raw.js index 1d6f0a760..55e2fa2d5 100644 --- a/integrations/heap/src/script.raw.js +++ b/integrations/heap/src/script.raw.js @@ -1,26 +1,36 @@ -const trackingID = ''; - -(function (win, doc, trackingID) { - win.heap=win.heap||[] - heap.load=function(e,t) { - win.heap.appid = e, win.heap.config=t=t||{}; - var r=doc.createElement("script"); - r.type="text/javascript", - r.async=!0, - r.src="https://cdn.heapanalytics.com/js/heap-"+e+".js"; - var a=doc.getElementsByTagName("script")[0]; - a.parentNode.insertBefore(r,a); - for(var n=function(e){ - return function () { - heap.push([e].concat(Array.prototype.slice.call(arguments,0))) - } - }, p=["addEventProperties","addUserProperties","clearEventProperties","identify","resetIdentity","removeEventProperty","setEventProperties","track","unsetEventProperty"], - o=0; o < p.length; o++) - heap[p[o]] = n(p[o]) - }; - heap.load(trackingID); -})(window, document, trackingID); - - - - \ No newline at end of file +(function (win, doc) { + const trackingID = ''; + win.heap = win.heap || []; + heap.load = function (e, t) { + (win.heap.appid = e), (win.heap.config = t = t || {}); + var r = doc.createElement('script'); + (r.type = 'text/javascript'), + (r.async = !0), + (r.src = 'https://cdn.heapanalytics.com/js/heap-' + e + '.js'); + var a = doc.getElementsByTagName('script')[0]; + a.parentNode.insertBefore(r, a); + for ( + var n = function (e) { + return function () { + heap.push([e].concat(Array.prototype.slice.call(arguments, 0))); + }; + }, + p = [ + 'addEventProperties', + 'addUserProperties', + 'clearEventProperties', + 'identify', + 'resetIdentity', + 'removeEventProperty', + 'setEventProperties', + 'track', + 'unsetEventProperty', + ], + o = 0; + o < p.length; + o++ + ) + heap[p[o]] = n(p[o]); + }; + heap.load(trackingID); +})(window, document); diff --git a/integrations/hotjar/src/script.raw.js b/integrations/hotjar/src/script.raw.js index 702663766..608a68bdb 100644 --- a/integrations/hotjar/src/script.raw.js +++ b/integrations/hotjar/src/script.raw.js @@ -1,6 +1,5 @@ -const trackingID = ''; - (function (h, o, t, j, a, r) { + const trackingID = ''; h.hj = h.hj || function () { diff --git a/package-lock.json b/package-lock.json index cee2d75e3..ab9af8d2c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11974,7 +11974,7 @@ }, "packages/api": { "name": "@gitbook/api", - "version": "0.19.0", + "version": "0.20.1", "dependencies": { "swagger-typescript-api": "^13.0.3" },