diff --git a/app/assets/javascripts/analytics.js.erb b/app/assets/javascripts/analytics.js.erb index cd36ec289..8ebbebb46 100644 --- a/app/assets/javascripts/analytics.js.erb +++ b/app/assets/javascripts/analytics.js.erb @@ -144,3 +144,15 @@ window.GOVUK.analyticsVars.linkedDomains = linkedDomains if (typeof window.GOVUK.analyticsInit !== 'undefined') { window.GOVUK.analyticsInit() } + +<% if ENV["GOOGLE_TAG_MANAGER_ID"] %> + if (typeof window.GOVUK.analyticsGA4.init !== 'undefined') { + window.GOVUK.analyticsGA4 = window.GOVUK.analyticsGA4 || {} + window.GOVUK.analyticsGA4.vars = window.GOVUK.analyticsGA4.vars || {} + window.GOVUK.analyticsGA4.vars.id = '<%= ENV["GOOGLE_TAG_MANAGER_ID"] %>' + window.GOVUK.analyticsGA4.vars.auth = '<%= ENV["GOOGLE_TAG_MANAGER_AUTH"] %>' + window.GOVUK.analyticsGA4.vars.preview = '<%= ENV["GOOGLE_TAG_MANAGER_PREVIEW"] %>' + + window.GOVUK.analyticsGA4.init() + } +<% end %> diff --git a/app/views/root/_error_page.html.erb b/app/views/root/_error_page.html.erb index 4740d32fc..dc3d67725 100644 --- a/app/views/root/_error_page.html.erb +++ b/app/views/root/_error_page.html.erb @@ -11,16 +11,6 @@ global_bar = render "components/global_bar" %> -<% content_for :head do %> - <% if ENV["GOOGLE_TAG_MANAGER_ID"] %> - <%= render "govuk_publishing_components/components/google_tag_manager_script", { - gtm_id: ENV["GOOGLE_TAG_MANAGER_ID"], - gtm_auth: ENV["GOOGLE_TAG_MANAGER_AUTH"], - gtm_preview: ENV["GOOGLE_TAG_MANAGER_PREVIEW"] - } %> - <% end %> -<% end %> - <%= render "govuk_publishing_components/components/layout_for_public", { emergency_banner: emergency_banner, full_width: false, diff --git a/app/views/root/_gem_base.html.erb b/app/views/root/_gem_base.html.erb index 16559cfb6..b4308c4b4 100644 --- a/app/views/root/_gem_base.html.erb +++ b/app/views/root/_gem_base.html.erb @@ -30,16 +30,6 @@ global_bar << global_banner if global_banner %> -<% content_for :head do %> - <% if ENV["GOOGLE_TAG_MANAGER_ID"] %> - <%= render "govuk_publishing_components/components/google_tag_manager_script", { - gtm_id: ENV["GOOGLE_TAG_MANAGER_ID"], - gtm_auth: ENV["GOOGLE_TAG_MANAGER_AUTH"], - gtm_preview: ENV["GOOGLE_TAG_MANAGER_PREVIEW"] - } %> - <% end %> -<% end %> - <%= render "govuk_publishing_components/components/layout_for_public", { account_nav_location: account_nav_location, draft_watermark: draft_environment, diff --git a/test/integration/google_tag_manager_component_test.rb b/test/integration/google_tag_manager_component_test.rb deleted file mode 100644 index b0be68687..000000000 --- a/test/integration/google_tag_manager_component_test.rb +++ /dev/null @@ -1,19 +0,0 @@ -require "integration_test_helper" - -class GoogleTagManagerComponentTest < ActionDispatch::IntegrationTest - context "GTM environment variables are present" do - should "render the GTM component on the gem_base template" do - ClimateControl.modify GOOGLE_TAG_MANAGER_ID: "a-nice-id" do - visit "/templates/gem_layout.html.erb" - assert_match "https://www.googletagmanager.com/gtm.js", page.body - end - end - end - - context "GTM environment variables are absent" do - should "not render the GTM component on the gem_base template" do - visit "/templates/gem_layout.html.erb" - assert_no_match "https://www.googletagmanager.com/gtm.js", page.body - end - end -end