From b3c356725bb642504d6f50261347a6480243dea0 Mon Sep 17 00:00:00 2001 From: Tom Aisthorpe Date: Tue, 11 May 2021 16:36:35 +0100 Subject: [PATCH] Add configurable cookie expiry --- README.md | 14 ++++++++++++++ src/consent-manager-builder/index.tsx | 18 +++++++++++++++--- src/consent-manager-builder/preferences.ts | 11 ++++++----- src/consent-manager/index.tsx | 3 +++ src/types.ts | 1 + 5 files changed, 39 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7b0feac1..aee33f08 100644 --- a/README.md +++ b/README.md @@ -275,6 +275,13 @@ Default: the [top most domain][top-domain] and all sub domains The domain the `tracking-preferences` cookie should be scoped to. +##### cookieExpires + +Type: `number`
+Default: 365 + +The number of dates until the `tracking-preferences` cookie should expire. + ##### bannerContent Type: `PropTypes.node` @@ -468,6 +475,13 @@ Default: the [top most domain][top-domain] and all sub domains The domain the `tracking-preferences` cookie should be scoped to. +#### cookieExpires + +Type: `number`
+Default: 365 + +The number of dates until the `tracking-preferences` cookie should expire. + #### Render Props ##### destinations diff --git a/src/consent-manager-builder/index.tsx b/src/consent-manager-builder/index.tsx index dbce3b92..43022b86 100644 --- a/src/consent-manager-builder/index.tsx +++ b/src/consent-manager-builder/index.tsx @@ -35,6 +35,11 @@ interface Props { cookieDomain?: string + /** + * Number of days until the preferences cookie should expire + */ + cookieExpires?: number + /** * An initial selection of Preferences */ @@ -179,6 +184,7 @@ export default class ConsentManagerBuilder extends Component { mapCustomPreferences, defaultDestinationBehavior, cookieDomain, + cookieExpires, cdnHost = ConsentManagerBuilder.defaultProps.cdnHost } = this.props // TODO: add option to run mapCustomPreferences on load so that the destination preferences automatically get updated @@ -210,7 +216,7 @@ export default class ConsentManagerBuilder extends Component { const mapped = mapCustomPreferences(destinations, preferences) destinationPreferences = mapped.destinationPreferences customPreferences = mapped.customPreferences - savePreferences({ destinationPreferences, customPreferences, cookieDomain }) + savePreferences({ destinationPreferences, customPreferences, cookieDomain, cookieExpires }) } } else { preferences = destinationPreferences || initialPreferences @@ -263,7 +269,13 @@ export default class ConsentManagerBuilder extends Component { } handleSaveConsent = (newPreferences: CategoryPreferences | undefined, shouldReload: boolean) => { - const { writeKey, cookieDomain, mapCustomPreferences, defaultDestinationBehavior } = this.props + const { + writeKey, + cookieDomain, + cookieExpires, + mapCustomPreferences, + defaultDestinationBehavior + } = this.props this.setState(prevState => { const { destinations, preferences: existingPreferences, isConsentRequired } = prevState @@ -297,7 +309,7 @@ export default class ConsentManagerBuilder extends Component { // If preferences haven't changed, don't reload the page as it's a disruptive experience for end-users if (prevState.havePreferencesChanged || newDestinations.length > 0) { - savePreferences({ destinationPreferences, customPreferences, cookieDomain }) + savePreferences({ destinationPreferences, customPreferences, cookieDomain, cookieExpires }) conditionallyLoadAnalytics({ writeKey, destinations, diff --git a/src/consent-manager-builder/preferences.ts b/src/consent-manager-builder/preferences.ts index c443198b..74bdbe9d 100644 --- a/src/consent-manager-builder/preferences.ts +++ b/src/consent-manager-builder/preferences.ts @@ -5,8 +5,7 @@ import { WindowWithAJS, Preferences, CategoryPreferences } from '../types' import { EventEmitter } from 'events' const COOKIE_KEY = 'tracking-preferences' -// TODO: Make cookie expiration configurable -const COOKIE_EXPIRES = 365 +const COOKIE_DEFAULT_EXPIRES = 365 export interface PreferencesManager { loadPreferences(): Preferences @@ -29,7 +28,7 @@ export function loadPreferences(): Preferences { } } -type SavePreferences = Preferences & { cookieDomain?: string } +type SavePreferences = Preferences & { cookieDomain?: string; cookieExpires?: number } const emitter = new EventEmitter() @@ -47,7 +46,8 @@ export function onPreferencesSaved(listener: (prefs: Preferences) => void) { export function savePreferences({ destinationPreferences, customPreferences, - cookieDomain + cookieDomain, + cookieExpires }: SavePreferences) { const wd = window as WindowWithAJS if (wd.analytics) { @@ -58,6 +58,7 @@ export function savePreferences({ } const domain = cookieDomain || topDomain(window.location.href) + const expires = cookieExpires || COOKIE_DEFAULT_EXPIRES const value = { version: 1, destinations: destinationPreferences, @@ -65,7 +66,7 @@ export function savePreferences({ } cookies.set(COOKIE_KEY, value, { - expires: COOKIE_EXPIRES, + expires, domain }) diff --git a/src/consent-manager/index.tsx b/src/consent-manager/index.tsx index 8bb9a699..8f87822c 100644 --- a/src/consent-manager/index.tsx +++ b/src/consent-manager/index.tsx @@ -19,6 +19,7 @@ export default class ConsentManager extends PureComponent Promise | boolean implyConsentOnInteraction?: boolean cookieDomain?: string + cookieExpires?: number bannerContent: React.ReactNode bannerSubContent?: string bannerTextColor?: string