From 0c157282f3b6198d0515f5017c0a61cdc0cc7622 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Mestres?= Date: Wed, 25 Oct 2023 15:08:14 +0200 Subject: [PATCH] chore: fake gtag function in dev & test env if forceEnable is not true (#118) --- app/initializers/gtag.ts | 11 +++++++++++ config/environment.js | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/initializers/gtag.ts b/app/initializers/gtag.ts index c73c6e5..4992786 100644 --- a/app/initializers/gtag.ts +++ b/app/initializers/gtag.ts @@ -3,8 +3,11 @@ const { gTag, environment } = config; export function initialize() { if (!gTag?.forceEnable && ['development', 'test'].includes(environment)) { + // Just define gtag function to prevent dev/test to fail + fakeGtag(); return; } else if (!gTag?.tag) { + console.error('no GTAG defined, skipping Gtag script load'); return; } @@ -31,6 +34,14 @@ function initGtag() { document.body.appendChild(script); } +function fakeGtag() { + const script = document.createElement('script'); + script.innerHTML = `function gtag(){}`; + + // Append the script tag to the document. + document.body.appendChild(script); +} + export default { initialize, }; diff --git a/config/environment.js b/config/environment.js index 16469e8..09cabf4 100644 --- a/config/environment.js +++ b/config/environment.js @@ -16,7 +16,7 @@ module.exports = function (environment) { gTag: { tag: 'G-47QBQ5GB3Y', - forceEnable: true, + // forceEnable: true, // For testing }, 'ember-toggle': {