Skip to content

Commit 30580cf

Browse files
authored
Merge pull request #436 from alphagov/cookie-banner
[PP-7355] Support Google Tag Manager
2 parents 36758d8 + 464274b commit 30580cf

File tree

9 files changed

+98
-5
lines changed

9 files changed

+98
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
### New features
4+
5+
- [Support using Google Tag Manager for Google Analytics 4](https://github.com/alphagov/tech-docs-gem/pull/436)
6+
17
## 5.1.1
28

39
### Fixes

example/config/tech-docs.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ footer_links:
1919
Accessibility: /hidden-page.html
2020
Hidden Page: /hidden-page.html
2121

22-
# Tracking ID (if using GA4 e.g G-XXXXXX)
22+
# Tracking IDs
23+
# Google Analytics 4 using gtag.js (e.g G-XXXXXX)
2324
ga4_tracking_id:
24-
25-
# Tracking ID from Google Universal Analytics (e.g. UA-XXXX-Y)
25+
# Google Analytics 4 using Google Tag Manager (e.g GTM-XXXXXXXX)
26+
ga4_gtm_tracking_id:
27+
# Google Universal Analytics (e.g. UA-XXXX-Y)
2628
ga_tracking_id:
2729

2830
# Enable multipage navigation in the sidebar
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
(function ($, Modules) {
2+
'use strict'
3+
4+
Modules.CookieBanner = function () {
5+
let $cookieBanner
6+
7+
this.start = function ($element) {
8+
$cookieBanner = $element
9+
10+
if ($cookieBanner && getCookie('cookies_preferences_set') !== 'true') {
11+
$('#cookie-banner__accept').on('click', acceptCookies)
12+
$('#cookie-banner__reject').on('click', rejectCookies)
13+
14+
$cookieBanner.removeAttr('hidden')
15+
}
16+
}
17+
18+
function acceptCookies () {
19+
document.cookie = 'cookies_preferences_set=true'
20+
document.cookie = 'cookies_policy={"usage":true}'
21+
22+
$cookieBanner.attr('hidden', 'hidden')
23+
}
24+
25+
function rejectCookies () {
26+
document.cookie = 'cookies_preferences_set=true'
27+
document.cookie = 'cookies_policy={"usage":false}'
28+
29+
$cookieBanner.attr('hidden', 'hidden')
30+
}
31+
32+
function getCookie (name) {
33+
return document.cookie
34+
.split('; ')
35+
.find(cookie => cookie.split('=')[0] === name)
36+
?.split('=')[1]
37+
}
38+
}
39+
})(jQuery, window.GOVUK.Modules)

lib/assets/javascripts/_start-modules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//= require _govuk/modules
22
//= require _modules/anchored-headings
3+
//= require _modules/cookie-banner
34
//= require _modules/in-page-navigation
45
//= require _modules/navigation
56
//= require _modules/page-expiry

lib/assets/stylesheets/_govuk_tech_docs.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ $govuk-new-link-styles: true;
1717
@import "govuk/core/index";
1818
@import "govuk/objects/index";
1919

20+
@import "govuk/components/cookie-banner/index";
2021
@import "govuk/components/footer/index";
2122
@import "govuk/components/header/index";
2223
@import "govuk/components/inset-text/index";

lib/source/layouts/_analytics_head.erb renamed to lib/source/layouts/_analytics_ga4_gtag.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
gtag('js', new Date());
88
gtag('config', '<%= config[:tech_docs][:ga4_tracking_id] %>');
99
</script>
10-
<% end %>
10+
<% end %>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<% if config[:tech_docs][:ga4_gtm_tracking_id].is_a?(String) && !config[:tech_docs][:ga4_gtm_tracking_id].empty? %>
2+
<!-- Google Tag Manager -->
3+
<script>
4+
const cookies_policy_cookie = document.cookie
5+
.split('; ')
6+
.find(cookie => cookie.split('=')[0] === 'cookies_policy')
7+
?.split('=')[1]
8+
9+
if (cookies_policy_cookie && JSON.parse(cookies_policy_cookie).usage) {
10+
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
11+
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
12+
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
13+
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
14+
})(window,document,'script','dataLayer','<%= config[:tech_docs][:ga4_gtm_tracking_id] %>');
15+
}
16+
</script>
17+
<!-- End Google Tag Manager -->
18+
<% end %>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<% if config[:tech_docs][:ga4_gtm_tracking_id].is_a?(String) && !config[:tech_docs][:ga4_gtm_tracking_id].empty? %>
2+
<div class="govuk-cookie-banner" data-nosnippet role="region" aria-label="Cookies on <%= config[:tech_docs][:service_name] %>" hidden="hidden" data-module="cookie-banner">
3+
<div class="govuk-cookie-banner__message govuk-width-container">
4+
<div class="govuk-grid-row">
5+
<div class="govuk-grid-column-two-thirds">
6+
<h2 class="govuk-cookie-banner__heading govuk-heading-m">
7+
Cookies on <%= config[:tech_docs][:service_name] %>
8+
</h2>
9+
<div class="govuk-cookie-banner__content">
10+
<p class="govuk-body">We’d like to use analytics cookies so we can understand how you use the service and make improvements.</p>
11+
</div>
12+
</div>
13+
</div>
14+
<div class="govuk-button-group">
15+
<button id="cookie-banner__accept" type="button" class="govuk-button" data-module="govuk-button">
16+
Accept analytics cookies
17+
</button>
18+
<button id="cookie-banner__reject" type="button" class="govuk-button" data-module="govuk-button">
19+
Reject analytics cookies
20+
</button>
21+
</div>
22+
</div>
23+
</div>
24+
<% end %>

lib/source/layouts/core.erb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<!doctype html>
22
<html lang="en" class="govuk-template <%= config[:tech_docs][:show_govuk_logo] ? 'govuk-template--rebranded ' : '' %>no-js">
33
<head>
4-
<%= partial 'layouts/analytics_head' %>
4+
<%= partial 'layouts/analytics_ga4_gtag' %>
5+
<%= partial 'layouts/analytics_ga4_gtm' %>
56

67
<meta content="IE=edge" http-equiv="X-UA-Compatible">
78
<meta charset="utf-8">
@@ -51,6 +52,7 @@
5152
<script>document.body.className += ' js-enabled' + ('noModule' in HTMLScriptElement.prototype ? ' govuk-frontend-supported' : '');</script>
5253

5354
<div class="app-pane">
55+
<%= partial 'layouts/cookie_banner' %>
5456
<div class="app-pane__header toc-open-disabled">
5557
<a href="#content" class="govuk-skip-link" data-module="govuk-skip-link">Skip to main content</a>
5658

0 commit comments

Comments
 (0)