From e621ef982050460f416ca267042ce6ed42297ab6 Mon Sep 17 00:00:00 2001 From: Patrick Cartlidge Date: Wed, 13 Mar 2024 16:15:24 +0000 Subject: [PATCH] Add GA4 analytics Use the `load-analytics' script of govuk_publishing_components to add GA4 analytics to content-tagger. Includes extra configuration to set the correct domains for GA4 to initialise and code to automatically accept all cookies to make tracking work. --- Gemfile | 1 + Gemfile.lock | 15 +++++++++++++++ app/assets/javascripts/application.js | 6 ++++++ app/assets/javascripts/domain-config.js | 24 ++++++++++++++++++++++++ app/views/layouts/application.html.erb | 4 ++++ config/application.rb | 2 ++ 6 files changed, 52 insertions(+) create mode 100644 app/assets/javascripts/domain-config.js diff --git a/Gemfile b/Gemfile index 905c64456..3dfdb60ef 100644 --- a/Gemfile +++ b/Gemfile @@ -21,6 +21,7 @@ gem "uglifier" gem "gds-api-adapters" gem "gds-sso" gem "govuk_admin_template" +gem "govuk_publishing_components" gem "govuk_sidekiq" gem "plek" diff --git a/Gemfile.lock b/Gemfile.lock index dfd76d24c..937f9d2be 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -173,6 +173,18 @@ GEM sentry-rails (~> 5.3) sentry-ruby (~> 5.3) statsd-ruby (~> 1.5) + govuk_personalisation (0.16.0) + plek (>= 1.9.0) + rails (>= 6, < 8) + govuk_publishing_components (37.10.0) + govuk_app_config + govuk_personalisation (>= 0.7.0) + kramdown + plek + rails (>= 6) + rouge + sprockets (>= 3) + sprockets-rails govuk_schemas (4.7.0) json-schema (>= 2.8, < 4.2) govuk_sidekiq (7.1.5) @@ -222,6 +234,8 @@ GEM activerecord kaminari-core (= 1.2.2) kaminari-core (1.2.2) + kramdown (2.4.0) + rexml language_server-protocol (3.17.0.3) link_header (0.0.8) logstasher (2.1.5) @@ -720,6 +734,7 @@ DEPENDENCIES gds-sso govuk_admin_template govuk_app_config + govuk_publishing_components govuk_schemas govuk_sidekiq govuk_test diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index fe625b172..5151d6a38 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -7,6 +7,12 @@ //= require_self //= require jquery.waypoints.min.js +//= require govuk_publishing_components/dependencies +//= require govuk_publishing_components/analytics + $(document).ready(function () { $('.select2:not(.tagging_project):not(.bulk_tagger)').select2({ allowClear: true }) }) + +window.GOVUK.approveAllCookieTypes() +window.GOVUK.cookie('cookies_preferences_set', 'true', { days: 365 }) diff --git a/app/assets/javascripts/domain-config.js b/app/assets/javascripts/domain-config.js new file mode 100644 index 000000000..7c3686bc0 --- /dev/null +++ b/app/assets/javascripts/domain-config.js @@ -0,0 +1,24 @@ +window.GOVUK = window.GOVUK || {} +window.GOVUK.vars = window.GOVUK.vars || {} +window.GOVUK.vars.extraDomains = [ + { + name: 'production', + domains: [ + 'content-tagger.publishing.service.gov.uk' + ], + initialiseGA4: true, + id: 'GTM-W573BJPG', + gaProperty: 'UA-26179049-6' + }, + { + name: 'integration', + domains: [ + 'content-tagger.integration.publishing.service.gov.uk' + ], + initialiseGA4: true, + id: 'GTM-W573BJPG', + auth: '2jaYNm4QqCxqsb10EcYo5w', + preview: 'env-18', + gaProperty: 'UA-26179049-6' + } +] diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4d4c44104..4c35915e2 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,4 +1,8 @@ <% content_for :head do %> + + <%= javascript_include_tag "domain-config" %> + <%= javascript_include_tag "govuk_publishing_components/load-analytics" %> + <%= stylesheet_link_tag "application", media: "all" %> <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> diff --git a/config/application.rb b/config/application.rb index 42b71e66d..4afef268d 100644 --- a/config/application.rb +++ b/config/application.rb @@ -19,6 +19,8 @@ class Application < Rails::Application # Configure denylisted tag types by publishing app config.denylisted_tag_types = config_for(:denylisted_tag_types) + config.assets.css_compressor = nil + config.active_record.belongs_to_required_by_default = false # Set asset path to be application specific so that we can put all GOV.UK