Browse Source

CLDC-1620 Add Google Tag Manager scripts to layout (#964)

* CLDC-1620 Add Google Tag Manager scripts to layout

* Update app/views/layouts/application.html.erb

Co-authored-by: James Rose <james@jbpr.net>

* CLDC-1620 Extract GTM IDs to helper

Co-authored-by: James Rose <james@jbpr.net>
pull/972/head
Sam 2 years ago committed by GitHub
parent
commit
ede79c451e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      app/helpers/analytics_helper.rb
  2. 35
      app/views/layouts/application.html.erb

13
app/helpers/analytics_helper.rb

@ -0,0 +1,13 @@
# frozen_string_literal: true
module AnalyticsHelper
def get_gtm_container
# Additional environments tbc
"GTM-M6GS7FF"
end
def get_gtm_id
# Additional environments tbc
"G-1RH26G5KVP"
end
end

35
app/views/layouts/application.html.erb

@ -21,6 +21,34 @@
</script> </script>
<%= javascript_include_tag "vendor/polyfill-output-value.js" %> <%= javascript_include_tag "vendor/polyfill-output-value.js" %>
<%= javascript_include_tag "vendor/outerHTML.js" %> <%= javascript_include_tag "vendor/outerHTML.js" %>
<% gtm_container = get_gtm_container %>
<% gtm_id = get_gtm_id %>
<!-- Google Tag Manager (doesn't store personal info until permission given) -->
<script>(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','<%= gtm_container %>');</script>
<!-- End Google Tag Manager -->
<% if cookies[:accept_analytics_cookies] == "on" %>
<script async src="https://www.googletagmanager.com/gtag/js?id=<%= gtm_id %>"></script>
<% else %>
<script>
// For adding the script once the user has given permission
window.analyticsScript = "https://www.googletagmanager.com/gtag/js?id=<%= gtm_id %>";
</script>
<% end %>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', '<%= gtm_id %>');
</script>
<%= javascript_include_tag "application", defer: true %> <%= javascript_include_tag "application", defer: true %>
<% if content_for?(:head) %> <% if content_for?(:head) %>
@ -44,6 +72,13 @@
document.body.className = ((document.body.className) ? document.body.className + " js-enabled" : "js-enabled"); document.body.className = ((document.body.className) ? document.body.className + " js-enabled" : "js-enabled");
</script> </script>
<!-- Google Tag Manager (noscript) -->
<% if cookies[:accept_analytics_cookies] %>
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=<% gtm_container %>" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<% end %>
<%= govuk_skip_link %> <%= govuk_skip_link %>
<%= govuk_header( <%= govuk_header(

Loading…
Cancel
Save