From b6d09c3228795ead23f92c14f067d3222304f598 Mon Sep 17 00:00:00 2001 From: hannako Date: Mon, 21 Feb 2022 16:51:00 +0000 Subject: [PATCH 1/3] Add climate_controll So that we can manipulate the state of env vars in tests. --- Gemfile | 1 + Gemfile.lock | 2 ++ 2 files changed, 3 insertions(+) diff --git a/Gemfile b/Gemfile index 116f6347d..c33ab07c4 100644 --- a/Gemfile +++ b/Gemfile @@ -30,6 +30,7 @@ group :development, :test do end group :test do + gem "climate_control" gem "govuk-content-schema-test-helpers" gem "jasmine-core" gem "minitest" diff --git a/Gemfile.lock b/Gemfile.lock index e3bba7048..1e620ef67 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -82,6 +82,7 @@ GEM regexp_parser (>= 1.5, < 3.0) xpath (~> 3.2) childprocess (3.0.0) + climate_control (1.0.1) coderay (1.1.3) concurrent-ruby (1.1.9) crack (0.4.4) @@ -349,6 +350,7 @@ DEPENDENCIES actionpack-page_caching better_errors binding_of_caller + climate_control gds-api-adapters govuk-content-schema-test-helpers govuk_app_config From fd4ecffce5a367a130d7c1bd6b6b5d70d5bc7595 Mon Sep 17 00:00:00 2001 From: hannako Date: Mon, 21 Feb 2022 16:52:05 +0000 Subject: [PATCH 2/3] Conditionally render the GTM component In the presence of GTM env vars. --- app/views/root/_gem_base.html.erb | 8 ++++++++ .../google_tag_manager_component_test.rb | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/integration/google_tag_manager_component_test.rb diff --git a/app/views/root/_gem_base.html.erb b/app/views/root/_gem_base.html.erb index 10803c686..1828f78e5 100644 --- a/app/views/root/_gem_base.html.erb +++ b/app/views/root/_gem_base.html.erb @@ -32,6 +32,14 @@ global_bar << global_banner if global_banner %> +<% 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 %> + <%= 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 new file mode 100644 index 000000000..b0be68687 --- /dev/null +++ b/test/integration/google_tag_manager_component_test.rb @@ -0,0 +1,19 @@ +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 From 73ff6c4059f0f3b98ff72583ed06bb5c38aa8dc6 Mon Sep 17 00:00:00 2001 From: Jess Jones Date: Tue, 22 Feb 2022 16:27:50 +0000 Subject: [PATCH 3/3] Pass the GTM script up to the layout component So that way we can carry out the GTM experiment with minimal impact on the existing component (as yield in is already available). Co-authored-by: Alex-J --- app/views/root/_gem_base.html.erb | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/app/views/root/_gem_base.html.erb b/app/views/root/_gem_base.html.erb index 1828f78e5..863439f0a 100644 --- a/app/views/root/_gem_base.html.erb +++ b/app/views/root/_gem_base.html.erb @@ -32,13 +32,15 @@ global_bar << global_banner if global_banner %> -<% 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 %> +<% 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,