Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Add configurable cookie expiry #152

Merged
merged 1 commit into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`<br>
Default: 365

The number of dates until the `tracking-preferences` cookie should expire.

##### bannerContent

Type: `PropTypes.node`
Expand Down Expand Up @@ -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`<br>
Default: 365

The number of dates until the `tracking-preferences` cookie should expire.

#### Render Props

##### destinations
Expand Down
18 changes: 15 additions & 3 deletions src/consent-manager-builder/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ interface Props {

cookieDomain?: string

/**
* Number of days until the preferences cookie should expire
*/
cookieExpires?: number

/**
* An initial selection of Preferences
*/
Expand Down Expand Up @@ -179,6 +184,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
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
Expand Down Expand Up @@ -210,7 +216,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
const mapped = mapCustomPreferences(destinations, preferences)
destinationPreferences = mapped.destinationPreferences
customPreferences = mapped.customPreferences
savePreferences({ destinationPreferences, customPreferences, cookieDomain })
savePreferences({ destinationPreferences, customPreferences, cookieDomain, cookieExpires })
}
} else {
preferences = destinationPreferences || initialPreferences
Expand Down Expand Up @@ -263,7 +269,13 @@ export default class ConsentManagerBuilder extends Component<Props, State> {
}

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
Expand Down Expand Up @@ -297,7 +309,7 @@ export default class ConsentManagerBuilder extends Component<Props, State> {

// 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,
Expand Down
11 changes: 6 additions & 5 deletions src/consent-manager-builder/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +28,7 @@ export function loadPreferences(): Preferences {
}
}

type SavePreferences = Preferences & { cookieDomain?: string }
type SavePreferences = Preferences & { cookieDomain?: string; cookieExpires?: number }

const emitter = new EventEmitter()

Expand All @@ -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) {
Expand All @@ -58,14 +58,15 @@ export function savePreferences({
}

const domain = cookieDomain || topDomain(window.location.href)
const expires = cookieExpires || COOKIE_DEFAULT_EXPIRES
const value = {
version: 1,
destinations: destinationPreferences,
custom: customPreferences
}

cookies.set(COOKIE_KEY, value, {
expires: COOKIE_EXPIRES,
expires,
domain
})

Expand Down
3 changes: 3 additions & 0 deletions src/consent-manager/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
implyConsentOnInteraction: false,
onError: undefined,
cookieDomain: undefined,
cookieExpires: undefined,
customCategories: undefined,
bannerTextColor: '#fff',
bannerSubContent: 'You can change your preferences at any time.',
Expand All @@ -35,6 +36,7 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
shouldRequireConsent,
implyConsentOnInteraction,
cookieDomain,
cookieExpires,
bannerContent,
bannerSubContent,
bannerTextColor,
Expand All @@ -56,6 +58,7 @@ export default class ConsentManager extends PureComponent<ConsentManagerProps, {
otherWriteKeys={otherWriteKeys}
shouldRequireConsent={shouldRequireConsent}
cookieDomain={cookieDomain}
cookieExpires={cookieExpires}
initialPreferences={this.getInitialPreferences()}
mapCustomPreferences={this.handleMapCustomPreferences}
customCategories={customCategories}
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export interface ConsentManagerProps {
shouldRequireConsent?: () => Promise<boolean> | boolean
implyConsentOnInteraction?: boolean
cookieDomain?: string
cookieExpires?: number
bannerContent: React.ReactNode
bannerSubContent?: string
bannerTextColor?: string
Expand Down