|
|
import cookie from 'cookie'; |
|
|
import isCountryInGdprZone from './is-country-in-gdpr-zone'; |
|
|
import isRegionInCcpaZone from './is-region-in-ccpa-zone'; |
|
|
|
|
|
export const TRACKING_PREFS_COOKIE_V1 = 'sensitive_pixel_option'; |
|
|
export const TRACKING_PREFS_COOKIE_V2 = 'sensitive_pixel_options'; |
|
|
|
|
|
export type TrackingPrefs = { |
|
|
ok: boolean; |
|
|
buckets: { |
|
|
essential: boolean; |
|
|
analytics: boolean; |
|
|
advertising: boolean; |
|
|
}; |
|
|
}; |
|
|
|
|
|
const prefsDisallowAll: TrackingPrefs = { |
|
|
ok: false, |
|
|
buckets: { |
|
|
essential: true, |
|
|
analytics: false, |
|
|
advertising: false, |
|
|
}, |
|
|
}; |
|
|
|
|
|
const prefsAllowAnalyticsGdpr: TrackingPrefs = { |
|
|
ok: false, |
|
|
buckets: { |
|
|
essential: true, |
|
|
analytics: true, |
|
|
advertising: false, |
|
|
}, |
|
|
}; |
|
|
|
|
|
const prefsAllowAll: TrackingPrefs = { |
|
|
ok: true, |
|
|
buckets: { |
|
|
essential: true, |
|
|
analytics: true, |
|
|
advertising: true, |
|
|
}, |
|
|
}; |
|
|
|
|
|
export const parseTrackingPrefs = ( |
|
|
cookieV2?: string, |
|
|
cookieV1?: string, |
|
|
defaultPrefs = prefsDisallowAll |
|
|
): TrackingPrefs => { |
|
|
const { ok, buckets }: Partial< TrackingPrefs > = cookieV2 ? JSON.parse( cookieV2 ) : {}; |
|
|
|
|
|
if ( typeof ok === 'boolean' ) { |
|
|
return { |
|
|
ok, |
|
|
buckets: { |
|
|
...defaultPrefs.buckets, |
|
|
...buckets, |
|
|
}, |
|
|
}; |
|
|
} else if ( cookieV1 && [ 'yes', 'no' ].includes( cookieV1 ) ) { |
|
|
return { |
|
|
ok: cookieV1 === 'yes', |
|
|
buckets: prefsAllowAll.buckets, |
|
|
}; |
|
|
} |
|
|
|
|
|
return defaultPrefs; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default function getTrackingPrefs(): TrackingPrefs { |
|
|
if ( typeof document === 'undefined' ) { |
|
|
|
|
|
return prefsAllowAll; |
|
|
} |
|
|
|
|
|
const cookies = cookie.parse( document.cookie ); |
|
|
const isCountryGdpr = isCountryInGdprZone( cookies.country_code ); |
|
|
const isCountryCcpa = isRegionInCcpaZone( cookies.country_code, cookies.region ); |
|
|
|
|
|
if ( ! isCountryGdpr && ! isCountryCcpa ) { |
|
|
return prefsAllowAll; |
|
|
} |
|
|
|
|
|
|
|
|
const defaultPrefs = isCountryGdpr ? prefsAllowAnalyticsGdpr : prefsAllowAll; |
|
|
|
|
|
const { ok, buckets } = parseTrackingPrefs( |
|
|
cookies[ TRACKING_PREFS_COOKIE_V2 ], |
|
|
cookies[ TRACKING_PREFS_COOKIE_V1 ], |
|
|
defaultPrefs |
|
|
); |
|
|
|
|
|
if ( isCountryCcpa ) { |
|
|
|
|
|
return { |
|
|
ok, |
|
|
buckets: { |
|
|
...prefsAllowAll.buckets, |
|
|
advertising: buckets.advertising, |
|
|
}, |
|
|
}; |
|
|
} |
|
|
|
|
|
|
|
|
return { ok, buckets }; |
|
|
} |
|
|
|