Browse Source
* CLDC-1618 Add cookie banner * CLDC-1618 Add analytics script inclusion tests * CLDC-1618 Include cookie banner JS * CLDC-1618 Appease JS Standardpull/1003/head^2
Sam
2 years ago
committed by
GitHub
6 changed files with 186 additions and 13 deletions
@ -0,0 +1,57 @@ |
|||||||
|
const cookieBannerEl = document.querySelector('.js-cookie-banner') |
||||||
|
|
||||||
|
if (cookieBannerEl) { |
||||||
|
const cookieFormEl = document.querySelector('.js-cookie-form') |
||||||
|
|
||||||
|
cookieFormEl.addEventListener('click', (e) => { |
||||||
|
if (e.target.tagName !== 'BUTTON') { |
||||||
|
return |
||||||
|
} |
||||||
|
|
||||||
|
const body = new window.FormData(cookieFormEl) |
||||||
|
body.append('cookies_form[accept_analytics_cookies]', e.target.value) |
||||||
|
|
||||||
|
fetch(cookieFormEl.action, { |
||||||
|
method: 'PUT', |
||||||
|
headers: { |
||||||
|
Accept: 'application/json' |
||||||
|
}, |
||||||
|
body |
||||||
|
}) |
||||||
|
.then((res) => { |
||||||
|
if (res.status >= 200 && res.status < 300) { |
||||||
|
return res |
||||||
|
} |
||||||
|
|
||||||
|
throw new Error(res) |
||||||
|
}) |
||||||
|
.then((res) => res.json()) |
||||||
|
.then(({ message }) => { |
||||||
|
const messageEl = cookieBannerEl.querySelector('.js-cookie-message') |
||||||
|
messageEl.textContent = message |
||||||
|
|
||||||
|
cookieBannerEl |
||||||
|
.querySelector('.js-cookie-banner__form') |
||||||
|
.setAttribute('hidden', '') |
||||||
|
cookieBannerEl |
||||||
|
.querySelector('.js-cookie-banner__success') |
||||||
|
.removeAttribute('hidden') |
||||||
|
}) |
||||||
|
|
||||||
|
const gaSrc = window.analyticsScript |
||||||
|
if (e.target.value === 'on' && gaSrc) { |
||||||
|
const scriptEl = document.createElement('script') |
||||||
|
scriptEl.src = gaSrc |
||||||
|
document.body.appendChild(scriptEl) |
||||||
|
} |
||||||
|
|
||||||
|
e.preventDefault() |
||||||
|
}) |
||||||
|
|
||||||
|
const hideBannerEl = document.querySelector('.js-hide-cookie-banner') |
||||||
|
hideBannerEl.addEventListener('click', (e) => { |
||||||
|
e.preventDefault() |
||||||
|
|
||||||
|
cookieBannerEl.setAttribute('hidden', '') |
||||||
|
}) |
||||||
|
} |
@ -0,0 +1,48 @@ |
|||||||
|
<div class="govuk-cookie-banner js-cookie-banner" role="region" aria-label="Cookies on CORE"> |
||||||
|
<div class="govuk-cookie-banner__message govuk-width-container js-cookie-banner__form"> |
||||||
|
<div class="govuk-grid-row"> |
||||||
|
<div class="govuk-grid-column-two-thirds"> |
||||||
|
<h2 class="govuk-cookie-banner__heading govuk-heading-m">Cookies on CORE</h2> |
||||||
|
|
||||||
|
<div class="govuk-cookie-banner__content"> |
||||||
|
<p class="govuk-body">We use some essential cookies to make this service work.</p> |
||||||
|
<p class="govuk-body">We’d like to use analytics cookies so we can understand how you use the service and make improvements.</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
|
||||||
|
<%= form_with url: cookies_path, class: "js-cookie-form", method: :put do |f| %> |
||||||
|
<div class="govuk-button-group"> |
||||||
|
<button type="submit" class="govuk-button" name="cookies_form[analytics_consent]" value="on"> |
||||||
|
Accept analytics cookies |
||||||
|
</button> |
||||||
|
<button type="submit" class="govuk-button" name="cookies_form[analytics_consent]" value="off"> |
||||||
|
Reject analytics cookies |
||||||
|
</button> |
||||||
|
<%= govuk_link_to "View cookies", cookies_path %> |
||||||
|
</div> |
||||||
|
<% end %> |
||||||
|
</div> |
||||||
|
|
||||||
|
<div class="govuk-cookie-banner__message govuk-width-container js-cookie-banner__success" role="alert" hidden> |
||||||
|
<div class="govuk-grid-row"> |
||||||
|
<div class="govuk-grid-column-two-thirds"> |
||||||
|
|
||||||
|
<div class="govuk-cookie-banner__content"> |
||||||
|
<p class="govuk-body"> |
||||||
|
<span class="js-cookie-message"> |
||||||
|
You’ve set your cookie preferences. |
||||||
|
</span> |
||||||
|
You can <%= govuk_link_to "change your cookie settings", cookies_path %> at any time. |
||||||
|
</p> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
|
||||||
|
<div class="govuk-button-group"> |
||||||
|
<button class="govuk-button js-hide-cookie-banner"> |
||||||
|
Hide this message |
||||||
|
</button> |
||||||
|
</div> |
||||||
|
</div> |
||||||
|
</div> |
@ -0,0 +1,57 @@ |
|||||||
|
require "rails_helper" |
||||||
|
|
||||||
|
RSpec.describe "layouts/application" do |
||||||
|
shared_examples "analytics cookie elements" do |banner:, scripts:| |
||||||
|
define_negated_matcher :not_match, :match |
||||||
|
|
||||||
|
it "#{banner ? 'includes' : 'omits'} the cookie banner" do |
||||||
|
banner_text = "We’d like to use analytics cookies so we can understand how you use the service and make improvements." |
||||||
|
if banner |
||||||
|
expect(rendered).to match(banner_text) |
||||||
|
else |
||||||
|
expect(rendered).not_to match(banner_text) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
it "#{scripts ? 'includes' : 'omits'} the analytics scripts" do |
||||||
|
gtm_script_tag = /<script.*googletagmanager/ |
||||||
|
gtm_iframe_tag = /<iframe.*googletagmanager/ |
||||||
|
if scripts |
||||||
|
expect(rendered).to match(gtm_script_tag).and match(gtm_iframe_tag) |
||||||
|
else |
||||||
|
expect(rendered).to not_match(gtm_script_tag).and not_match(gtm_iframe_tag) |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "with no cookie set" do |
||||||
|
before do |
||||||
|
cookies[:accept_analytics_cookies] = nil |
||||||
|
render |
||||||
|
end |
||||||
|
|
||||||
|
include_examples "analytics cookie elements", banner: true, scripts: false |
||||||
|
|
||||||
|
it "sets window.analyticsScript for the JS to refer to if the user accepts" do |
||||||
|
expect(rendered).to match(/window\.analyticsScript = "https:\/\/www\.googletagmanager\.com\/gtag\/js\?id=G-[\w\d]+"/) |
||||||
|
end |
||||||
|
end |
||||||
|
|
||||||
|
context "with analytics accepted" do |
||||||
|
before do |
||||||
|
cookies[:accept_analytics_cookies] = "on" |
||||||
|
render |
||||||
|
end |
||||||
|
|
||||||
|
include_examples "analytics cookie elements", banner: false, scripts: true |
||||||
|
end |
||||||
|
|
||||||
|
context "with analytics rejected" do |
||||||
|
before do |
||||||
|
cookies[:accept_analytics_cookies] = "off" |
||||||
|
render |
||||||
|
end |
||||||
|
|
||||||
|
include_examples "analytics cookie elements", banner: false, scripts: false |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue