From d66446b1312e2002840d0ec5ebaa37d79d512779 Mon Sep 17 00:00:00 2001 From: Andrew Hosgood Date: Tue, 30 Jul 2024 15:17:09 +0100 Subject: [PATCH] Change default cookie age to session --- CHANGELOG.md | 1 + src/nationalarchives/analytics.mjs | 6 +++--- .../components/cookie-banner/cookie-banner.mjs | 2 +- src/nationalarchives/components/footer/footer.mjs | 2 +- src/nationalarchives/lib/cookies.mjs | 6 +++--- 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d3ada10..a45b1656 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Allow search field to be smaller than the browser default - Reduced space between paragraphs in hero captions +- Default cookie age changed from `31536000` (1 year) to `null` (session cookie) ### Deprecated ### Removed diff --git a/src/nationalarchives/analytics.mjs b/src/nationalarchives/analytics.mjs index d0525819..7e72f9cb 100644 --- a/src/nationalarchives/analytics.mjs +++ b/src/nationalarchives/analytics.mjs @@ -264,7 +264,7 @@ class GA4 extends EventTracker { window.dataLayer = window.dataLayer || []; if (!this.cookies.isPolicyAccepted("usage")) { window[this.ga4Disable] = true; - this.cookies.set(this.ga4Disable, "true"); + this.cookies.set(this.ga4Disable, "true", { maxAge: 31536000 }); } this.start(initAll); } @@ -302,7 +302,7 @@ class GA4 extends EventTracker { enableTracking() { if (!this.trackingEnabled) { window[this.ga4Disable] = false; - this.cookies.set(this.ga4Disable, "false"); + this.cookies.set(this.ga4Disable, "false", { maxAge: 31536000 }); if (!this.trackingCodeAdded && this.addTrackingCode) { if (!this.gTagId) { throw Error("ID was not specified"); @@ -334,7 +334,7 @@ class GA4 extends EventTracker { disableTracking() { if (this.trackingEnabled) { window[this.ga4Disable] = true; - this.cookies.set(this.ga4Disable, "true"); + this.cookies.set(this.ga4Disable, "true", { maxAge: 31536000 }); Object.keys(this.cookies.all).forEach((key) => { if (key.startsWith("_ga")) { this.cookies.delete(key); diff --git a/src/nationalarchives/components/cookie-banner/cookie-banner.mjs b/src/nationalarchives/components/cookie-banner/cookie-banner.mjs index 36c2596c..d86a4a6a 100644 --- a/src/nationalarchives/components/cookie-banner/cookie-banner.mjs +++ b/src/nationalarchives/components/cookie-banner/cookie-banner.mjs @@ -82,7 +82,7 @@ export class CookieBanner { } complete() { - this.cookies.set(this.cookiePreferencesSet, true); + this.cookies.set(this.cookiePreferencesSet, true, { maxAge: 31536000 }); this.$closeButtons.forEach(($closeButton) => { $closeButton.addEventListener("click", () => this.close()); }); diff --git a/src/nationalarchives/components/footer/footer.mjs b/src/nationalarchives/components/footer/footer.mjs index 7c8588bd..3df02ba1 100644 --- a/src/nationalarchives/components/footer/footer.mjs +++ b/src/nationalarchives/components/footer/footer.mjs @@ -70,7 +70,7 @@ export class Footer { } else { return; } - this.cookies.set("theme", theme); + this.cookies.set("theme", theme, { maxAge: 31536000 }); } selectThemeSelectorButton($selectedButton) { diff --git a/src/nationalarchives/lib/cookies.mjs b/src/nationalarchives/lib/cookies.mjs index 76faa3b9..a7d16b17 100644 --- a/src/nationalarchives/lib/cookies.mjs +++ b/src/nationalarchives/lib/cookies.mjs @@ -175,7 +175,7 @@ export default class Cookies { * @param {String} key - The cookie name. * @param {String|Number|Boolean} value - The cookie value. * @param {Object} options - * @param {Number} [options.maxAge=31536000] - The maximum age of the cookie in seconds. + * @param {Number|null} [options.maxAge=null] - The maximum age of the cookie in seconds. * @param {String} [options.path=/] - The path to register the cookie for. * @param {String} [options.sameSite=Lax] - The sameSite attribute. * @param {String} [options.domain=this.domain] - The domain to register the cookie with. @@ -184,7 +184,7 @@ export default class Cookies { */ set(key, value, options = {}) { const { - maxAge = 60 * 60 * 24 * 365, + maxAge = null, sameSite = "Lax", domain = this.domain, path = this.path, @@ -195,7 +195,7 @@ export default class Cookies { } const cookie = `${encodeURIComponent(key)}=${encodeURIComponent(value)};${ domain ? ` domain=${domain}; ` : "" - } samesite=${sameSite}; path=${path}; max-age=${maxAge}${ + } samesite=${sameSite}; path=${path};${maxAge !== null ? ` max-age=${maxAge}` : ""}${ secure ? "; secure" : "" }`; document.cookie = cookie;