Skip to content

Commit

Permalink
Merge pull request #2711 from alphagov/gtm_component
Browse files Browse the repository at this point in the history
Conditionally add the GTM component
  • Loading branch information
hannako authored Feb 25, 2022
2 parents 40b2ae1 + 73ff6c4 commit 63bcdc3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions app/views/root/_gem_base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
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,
Expand Down
19 changes: 19 additions & 0 deletions test/integration/google_tag_manager_component_test.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 63bcdc3

Please sign in to comment.