From 2b8af230b517003b3e2eacbdc40e0371468730fe Mon Sep 17 00:00:00 2001 From: ayush-billore Date: Tue, 14 Nov 2023 10:55:06 +0530 Subject: [PATCH] add feature flag changes Signed-off-by: ayush-billore --- .../app/helpers/feature_flag_helper.rb | 12 +- .../app/views/application/_analytics.html.erb | 110 +++++++++--------- .../app/views/layouts/application.html.erb | 6 +- 3 files changed, 70 insertions(+), 58 deletions(-) diff --git a/src/supermarket/app/helpers/feature_flag_helper.rb b/src/supermarket/app/helpers/feature_flag_helper.rb index 3fe0b81aba..313f20371a 100644 --- a/src/supermarket/app/helpers/feature_flag_helper.rb +++ b/src/supermarket/app/helpers/feature_flag_helper.rb @@ -1,5 +1,13 @@ module FeatureFlagHelper - def air_gapped? - ENV["AIR_GAPPED"] == "true" + def gtag_enabled? + ENV["ENABLE_GTAG"] == "true" + end + + def gtm_enabled? + ENV["ENABLE_GTM"] == "true" + end + + def onetrust_enabled? + ENV["ENABLE_ONETRUST"] == "true" end end diff --git a/src/supermarket/app/views/application/_analytics.html.erb b/src/supermarket/app/views/application/_analytics.html.erb index 7cc115abf6..5583c77bd3 100644 --- a/src/supermarket/app/views/application/_analytics.html.erb +++ b/src/supermarket/app/views/application/_analytics.html.erb @@ -1,33 +1,37 @@ - -<%= javascript_tag nonce: content_security_policy_nonce do %> - var oneTrustHelper = (function () { - function evalGTMScript() { - var gtmScript = document.getElementById("GTMScript"); - gtmScript.type = "text/javascript"; - gtmScript.classList.remove("optanon-category-1"); - eval(gtmScript.innerHTML); - }; +<% if gtm_enabled? %> + + <%= javascript_tag nonce: content_security_policy_nonce do %> + var oneTrustHelper = (function () { + function evalGTMScript() { + var gtmScript = document.getElementById("GTMScript"); + gtmScript.type = "text/javascript"; + gtmScript.classList.remove("optanon-category-1"); + eval(gtmScript.innerHTML); + }; - return { - gtmFallback: function () { - console.warn('OneTrust not loaded.'); - if (document.readyState !== 'loading') { - evalGTMScript(); - } else { - document.addEventListener('readystatechange', function () { - if (document.readyState === 'interactive') { - evalGTMScript(); - } - }); - }; - } - }; - })(); + return { + gtmFallback: function () { + console.warn('OneTrust not loaded.'); + if (document.readyState !== 'loading') { + evalGTMScript(); + } else { + document.addEventListener('readystatechange', function () { + if (document.readyState === 'interactive') { + evalGTMScript(); + } + }); + }; + } + }; + })(); + <% end %> <% end %> - +<% if onetrust_enabled? %> + +<% end %> -<% if ENV['GTM_ID'] %> +<% if gtm_enabled? && ENV['GTM_ID'] %> <%= javascript_tag type: "text/plain", id: "GTMScript", class: "optanon-category-1" do %> (function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start': @@ -35,36 +39,38 @@ j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src= 'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f); })(window,document,'script','dataLayer','<%= ENV['GTM_ID'] %>'); - <% end %> + <% end %> <% end %> - -<%= javascript_tag nonce: content_security_policy_nonce do %> - var oneTrustHelper = (function () { - function evalGTagScript() { - var gtScript = document.getElementById("GTagScript"); - gtScript.type = "text/javascript"; - gtScript.classList.remove("optanon-category-2"); - eval(gtScript.innerHTML); - }; - return { - gtagFallback: function () { - console.warn('OneTrust not loaded.'); - if (document.readyState !== 'loading') { - evalGTagScript(); - } else { - document.addEventListener('readystatechange', function () { - if (document.readyState === 'interactive') { - evalGTagScript(); - } - }); - }; - } - }; - })(); +<% if gtag_enabled? %> + + <%= javascript_tag nonce: content_security_policy_nonce do %> + var oneTrustHelper = (function () { + function evalGTagScript() { + var gtScript = document.getElementById("GTagScript"); + gtScript.type = "text/javascript"; + gtScript.classList.remove("optanon-category-2"); + eval(gtScript.innerHTML); + }; + return { + gtagFallback: function () { + console.warn('OneTrust not loaded.'); + if (document.readyState !== 'loading') { + evalGTagScript(); + } else { + document.addEventListener('readystatechange', function () { + if (document.readyState === 'interactive') { + evalGTagScript(); + } + }); + }; + } + }; + })(); + <% end %> <% end %> -<% if ENV['GOOGLE_ANALYTICS_ID'] %> +<% if gtag_enabled? && ENV['GOOGLE_ANALYTICS_ID'] %> <%= javascript_tag nonce: content_security_policy_nonce do %> diff --git a/src/supermarket/app/views/layouts/application.html.erb b/src/supermarket/app/views/layouts/application.html.erb index 73c2e91c8b..024f7f1886 100644 --- a/src/supermarket/app/views/layouts/application.html.erb +++ b/src/supermarket/app/views/layouts/application.html.erb @@ -18,12 +18,10 @@ <%= stylesheet_link_tag "//fonts.googleapis.com/css?family=Montserrat:400,700" %> <% end %> <%= csrf_meta_tags %> - <% unless air_gapped? %> - <%= render 'application/analytics' %> - <% end %> + <%= render 'application/analytics' %> - <% if ENV['GTM_ID'] && air_gapped? %> + <% if gtm_enabled? && ENV['GTM_ID'] %>