From a127c07cb5b3e8bbe35fe51e80f2552cfb90c16d Mon Sep 17 00:00:00 2001 From: "Pablo H. Paladino" Date: Fri, 28 Jan 2022 12:15:11 -0300 Subject: [PATCH] change google optimize load strategy to respect GDPR --- packages/core/src/CanonProvider.jsx | 14 +++++------ packages/core/src/helpers/services.js | 35 ++++++++++----------------- packages/core/src/server.jsx | 4 +-- 3 files changed, 21 insertions(+), 32 deletions(-) diff --git a/packages/core/src/CanonProvider.jsx b/packages/core/src/CanonProvider.jsx index 01173a72c..286508c69 100644 --- a/packages/core/src/CanonProvider.jsx +++ b/packages/core/src/CanonProvider.jsx @@ -38,7 +38,7 @@ class CanonProvider extends Component { * * Minor modifications by Chris Price to only polyfill when required. */ - (function(SVGElement) { + (function (SVGElement) { if (!SVGElement || "innerHTML" in SVGElement.prototype) { return; @@ -107,7 +107,7 @@ class CanonProvider extends Component { const dXML = new DOMParser(); dXML.async = false; // Wrap the markup into a SVG node to ensure parsing works. - const sXML = `${ markupText }`; + const sXML = `${markupText}`; const svgDocElement = dXML.parseFromString(sXML, "text/xml").documentElement; // Now take each node, import it and append to this element. @@ -136,7 +136,7 @@ class CanonProvider extends Component { onClick(e) { - if (typeof window.ga === "function") { + if (typeof window.ga === "function" && typeof window.ga.getAll === "function") { const clickEl = e.target; @@ -227,16 +227,16 @@ class CanonProvider extends Component { meta={helmet.meta} link={helmet.link} /> - { loading ? :
{ children }
} + {loading ? :
{children}
} - { gdpr ?
+ {gdpr ?
{t("GDPR.desc", {services})} - { privacy ? {t("GDPR.more")} : null } + {privacy ? {t("GDPR.more")} : null} -
: null } +
: null} ; } } diff --git a/packages/core/src/helpers/services.js b/packages/core/src/helpers/services.js index fd16944d8..842b0c9a3 100644 --- a/packages/core/src/helpers/services.js +++ b/packages/core/src/helpers/services.js @@ -19,19 +19,20 @@ const serviceJavaScript = { ${id.split(",").map((key, i) => `ga('create', '${key}', 'auto', 'tracker${i + 1}');`).join("\n ")} ${id.split(",").map((key, i) => `ga('tracker${i + 1}.send', 'pageview');`).join("\n ")}`, HOTJAR: id => `(function(h,o,t,j,a,r){ - h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)}; - h._hjSettings={hjid:${id},hjsv:6}; - a=o.getElementsByTagName('head')[0]; - r=o.createElement('script');r.async=1; - r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv; - a.appendChild(r); - })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');` + h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)}; + h._hjSettings={hjid:${id},hjsv:6}; + a=o.getElementsByTagName('head')[0]; + r=o.createElement('script');r.async=1; + r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv; + a.appendChild(r); + })(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');`, + GOOGLE_OPTIMIZE: id => `(function(w,d, optimizeId){ + var script = d.createElement('script'); + script.src = "https://www.googleoptimize.com/optimize.js?id="+optimizeId; + d.head.prepend(script); + })(window, document, "${id}")` }; -const serviceHeadTag = { - GOOGLE_OPTIMIZE: id => `` -} - const serviceHTML = { GOOGLE_TAG_MANAGER: id => `