diff --git a/src/supermarket/app/helpers/feature_flag_helper.rb b/src/supermarket/app/helpers/feature_flag_helper.rb index 3fe0b81aba..3f51c16164 100644 --- a/src/supermarket/app/helpers/feature_flag_helper.rb +++ b/src/supermarket/app/helpers/feature_flag_helper.rb @@ -2,4 +2,16 @@ module FeatureFlagHelper def air_gapped? ENV["AIR_GAPPED"] == "true" end + + 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 5fae2dc63f..96a4d50327 100644 --- a/src/supermarket/app/views/application/_analytics.html.erb +++ b/src/supermarket/app/views/application/_analytics.html.erb @@ -1,20 +1,91 @@ -<% if ENV['SEGMENT_WRITE_KEY'] %> +<% if gtm_enabled? %> + <%= javascript_tag nonce: content_security_policy_nonce do %> - !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t"); - analytics.page(); - }}(); + 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(); + } + }); + }; + } + }; + })(); + <% end %> +<% end %> + +<% if onetrust_enabled? %> + +<% end %> + +<% 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': + new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0], + 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 %> + +<% 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 %> window.dataLayer = window.dataLayer || []; function gtag(){window.dataLayer.push(arguments);} gtag('js', new Date()); - gtag('config', '<%= ENV['GOOGLE_ANALYTICS_ID'] %>'); <% end %> -<% end %> \ No newline at end of file +<% end %> + +<% if ENV['SEGMENT_WRITE_KEY'] %> + <%= javascript_tag nonce: content_security_policy_nonce do %> + !function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t"); + analytics.page(); + }}(); + <% end %> +<% end %> diff --git a/src/supermarket/app/views/layouts/application.html.erb b/src/supermarket/app/views/layouts/application.html.erb index 792c3d7e5f..f1635568f1 100644 --- a/src/supermarket/app/views/layouts/application.html.erb +++ b/src/supermarket/app/views/layouts/application.html.erb @@ -18,11 +18,15 @@ <%= 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 gtm_enabled? && ENV['GTM_ID'] %> + + + + <% end %> +