mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-05 14:11:41 -04:00
Clear old cookies to be based on consent
We have been clearing all the Google Analytics cookies on each page request. It is now possible for a user to consent to having Google Analytics cookies so this should have been checking for that before deleting them. This makes that change, with tests for those scenarios.
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
(function (window) {
|
||||
"use strict";
|
||||
|
||||
function hasConsentFor (cookieCategory) {
|
||||
const consentCookie = window.GOVUK.getConsentCookie();
|
||||
function hasConsentFor (cookieCategory, consentCookie) {
|
||||
if (consentCookie === undefined) { consentCookie = window.GOVUK.getConsentCookie(); }
|
||||
|
||||
if (consentCookie === null) { return false; }
|
||||
|
||||
|
||||
@@ -4,14 +4,21 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
|
||||
(function (Modules) {
|
||||
function CookieBanner () { }
|
||||
|
||||
CookieBanner.clearOldCookies = function () {
|
||||
// clear any cookies set by the previous version
|
||||
var oldCookies = ['seen_cookie_message', '_ga', '_gid'];
|
||||
CookieBanner.clearOldCookies = function (consent) {
|
||||
var gaCookies = ['_ga', '_gid'];
|
||||
|
||||
for (var i = 0; i < oldCookies.length; i++) {
|
||||
if (window.GOVUK.cookie(oldCookies[i])) {
|
||||
var cookieString = oldCookies[i] + '=;expires=' + new Date() + ';domain=' + window.location.hostname.replace(/^www\./, '.') + ';path=/';
|
||||
document.cookie = cookieString;
|
||||
// clear old cookie set by our previous JS, set on the www domain
|
||||
if (window.GOVUK.cookie('seen_cookie_message')) {
|
||||
document.cookie = 'seen_cookie_message=;expires=' + new Date() + ';domain=' + window.location.hostname + ';path=/';
|
||||
}
|
||||
|
||||
if (consent === null) {
|
||||
for (var i = 0; i < gaCookies.length; i++) {
|
||||
if (window.GOVUK.cookie(gaCookies[i])) {
|
||||
// GA cookies are set on the base domain so need the www stripping
|
||||
var cookieString = gaCookies[i] + '=;expires=' + new Date() + ';domain=' + window.location.hostname.replace(/^www\./, '.') + ';path=/';
|
||||
document.cookie = cookieString;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
window.GOVUK.Frontend.initAll();
|
||||
|
||||
window.GOVUK.Modules.CookieBanner.clearOldCookies();
|
||||
var consentData = window.GOVUK.getConsentCookie();
|
||||
window.GOVUK.Modules.CookieBanner.clearOldCookies(consentData);
|
||||
|
||||
if (window.GOVUK.hasConsentFor('analytics')) {
|
||||
if (window.GOVUK.hasConsentFor('analytics', consentData)) {
|
||||
window.GOVUK.initAnalytics();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user