@coinbase/cookie-manager
Advanced tools
Comparing version 1.1.2 to 1.1.3
# Changelog | ||
## 1.1.3 (05/03/2024) | ||
- Added logic to honor GCP in non-EU localities | ||
- Fixed failing spec | ||
## 1.1.2 (02/26/2024) | ||
@@ -4,0 +9,0 @@ |
@@ -35,2 +35,4 @@ "use strict"; | ||
const types_1 = require("./types"); | ||
const applyGpcToAdPref_1 = require("./utils/applyGpcToAdPref"); | ||
const applyGpcToCookiePref_1 = require("./utils/applyGpcToCookiePref"); | ||
const getAllCookies_1 = __importStar(require("./utils/getAllCookies")); | ||
@@ -48,3 +50,4 @@ const getDefaultTrackingPreference_1 = __importDefault(require("./utils/getDefaultTrackingPreference")); | ||
const POLL_INTERVAL = 500; | ||
const [cookieValues, setCookieValues] = (0, react_1.useState)(() => (0, getAllCookies_1.default)()); | ||
const [cookieValues, setCookieValues] = (0, react_1.useState)(() => (0, getAllCookies_1.default)(region)); | ||
let priorCookieValue; | ||
let trackingPreference; | ||
@@ -66,7 +69,9 @@ let adTrackingPreference; | ||
const checkCookies = () => { | ||
const currentCookie = (0, getAllCookies_1.default)(); | ||
if (!(0, getAllCookies_1.areRecordsEqual)(cookieValues, currentCookie)) { | ||
const currentCookie = (0, getAllCookies_1.default)(region); | ||
if (priorCookieValue == undefined || !(0, getAllCookies_1.areRecordsEqual)(priorCookieValue, currentCookie)) { | ||
priorCookieValue = currentCookie; | ||
setCookieValues(currentCookie); | ||
// Grab out prefences (they wil have GPC applied if present) | ||
trackingPreference = getTrackingPreference(currentCookie, region, config); | ||
adTrackingPreference = getAdTrackingPreference(currentCookie); | ||
adTrackingPreference = getAdTrackingPreference(currentCookie, region); | ||
(0, setGTMVariables_1.default)(trackingPreference, adTrackingPreference); | ||
@@ -173,8 +178,15 @@ const cookiesToRemove = []; | ||
: cookieCache[constants_1.DEFAULT_CONSENT_PREFERENCES_COOKIE]; | ||
return trackingPreference || (0, getDefaultTrackingPreference_1.default)(region, config); | ||
// Example preference | ||
// { region: Region.EU, consent: ['necessary'] } | ||
const preference = trackingPreference || (0, getDefaultTrackingPreference_1.default)(region, config); | ||
// Apply GPC when present | ||
return (0, applyGpcToCookiePref_1.applyGpcToCookiePref)(preference); | ||
}; | ||
const adTrackingDefault = { value: 'true' }; | ||
const getAdTrackingPreference = (cookieCache) => { | ||
// Do we want to change the ADVERTISING_SHARING_ALLOWED value to clear prior values? | ||
const getAdTrackingPreference = (cookieCache, region) => { | ||
const adTrackingPreference = cookieCache[constants_1.ADVERTISING_SHARING_ALLOWED]; | ||
return adTrackingPreference || adTrackingDefault; | ||
const adTrackingDefault = region === types_1.Region.EU ? { value: 'false' } : { value: 'true' }; | ||
// Example: adPreference { value: 'false' } | ||
const adPreference = adTrackingPreference || adTrackingDefault; | ||
return (0, applyGpcToAdPref_1.applyGpcToAdPref)(region, adPreference); | ||
}; | ||
@@ -181,0 +193,0 @@ const useCookie = (cookieName) => { |
@@ -1,3 +0,5 @@ | ||
export declare const deserializeCookies: (cookies: Record<string, string>) => Record<string, any>; | ||
export default function getAllCookies(initialCookies?: Record<string, string>): Record<string, any>; | ||
export { Region } from '../types'; | ||
import { Region } from '../types'; | ||
export declare const deserializeCookies: (region: Region, cookies: Record<string, string>) => Record<string, any>; | ||
export default function getAllCookies(region: Region, initialCookies?: Record<string, string>): Record<string, any>; | ||
export declare function areRecordsEqual(record1: Record<string, any>, record2: Record<string, any>): boolean; |
@@ -6,5 +6,11 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.areRecordsEqual = exports.deserializeCookies = void 0; | ||
exports.areRecordsEqual = exports.deserializeCookies = exports.Region = void 0; | ||
const js_cookie_1 = __importDefault(require("js-cookie")); | ||
const deserializeCookies = (cookies) => { | ||
var types_1 = require("../types"); | ||
Object.defineProperty(exports, "Region", { enumerable: true, get: function () { return types_1.Region; } }); | ||
const constants_1 = require("../constants"); | ||
const types_2 = require("../types"); | ||
const applyGpcToAdPref_1 = require("./applyGpcToAdPref"); | ||
const applyGpcToCookiePref_1 = require("./applyGpcToCookiePref"); | ||
const deserializeCookies = (region, cookies) => { | ||
const parsedCookies = {}; | ||
@@ -18,2 +24,3 @@ Object.keys(cookies).forEach((c) => { | ||
} | ||
parsedCookies[c] = filterCookieValue(region, c, parsedCookies[c]); | ||
}); | ||
@@ -23,9 +30,21 @@ return parsedCookies; | ||
exports.deserializeCookies = deserializeCookies; | ||
function getAllCookies(initialCookies) { | ||
function getAllCookies(region, initialCookies) { | ||
if (typeof window === 'undefined' && initialCookies) { | ||
return (0, exports.deserializeCookies)(initialCookies); | ||
return (0, exports.deserializeCookies)(region, initialCookies); | ||
} | ||
return (0, exports.deserializeCookies)(js_cookie_1.default.get() || {}); | ||
return (0, exports.deserializeCookies)(region, js_cookie_1.default.get() || {}); | ||
} | ||
exports.default = getAllCookies; | ||
// Apply in in memory filters to the cookie values. Currently we are just apply | ||
// Global Privacy Control (GPC) logic to ensure we are honoring GPC | ||
function filterCookieValue(region, cookieName, cookieValue) { | ||
if (cookieName == constants_1.ADVERTISING_SHARING_ALLOWED) { | ||
cookieValue = (0, applyGpcToAdPref_1.applyGpcToAdPref)(region, cookieValue); | ||
} | ||
else if ((region == types_2.Region.DEFAULT && cookieName == constants_1.DEFAULT_CONSENT_PREFERENCES_COOKIE) || | ||
(region == types_2.Region.EU && cookieName == constants_1.EU_CONSENT_PREFERENCES_COOKIE)) { | ||
cookieValue = (0, applyGpcToCookiePref_1.applyGpcToCookiePref)(cookieValue); | ||
} | ||
return cookieValue; | ||
} | ||
function areRecordsEqual(record1, record2) { | ||
@@ -32,0 +51,0 @@ // Check if the number of keys is the same |
{ | ||
"name": "@coinbase/cookie-manager", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Coinbase Cookie Manager", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
import Cookies from 'js-cookie'; | ||
import getAllCookies from './getAllCookies'; | ||
import getAllCookies, { Region } from './getAllCookies'; | ||
export { Region } from '../types'; | ||
@@ -19,6 +20,7 @@ jest.mock('js-cookie', () => ({ | ||
region: 'DEFAULT', | ||
consent: ['necessary', 'performance'], | ||
consent: ['necessary', 'performance', 'targeting'], | ||
}; | ||
const cookies = { | ||
cm_default_preferences: JSON.stringify(value), | ||
advertising_sharing_allowed: JSON.stringify({ value: true }), | ||
some_cookie: 'iamastring', | ||
@@ -28,5 +30,9 @@ another_cookie: '5', | ||
}; | ||
(navigator as any).globalPrivacyControl = false; | ||
mockGet.mockImplementation(jest.fn(() => cookies)); | ||
expect(getAllCookies({})).toEqual({ | ||
expect(getAllCookies(Region.DEFAULT, {})).toEqual({ | ||
cm_default_preferences: value, | ||
advertising_sharing_allowed: { value: true }, | ||
some_cookie: 'iamastring', | ||
@@ -37,2 +43,28 @@ another_cookie: 5, | ||
}); | ||
it('applies GCP to cookie values', () => { | ||
const mockGet = Cookies.get as jest.MockedFunction<typeof Cookies.get>; | ||
const value = { | ||
region: 'DEFAULT', | ||
consent: ['necessary', 'performance', 'targeting'], | ||
}; | ||
const cookies = { | ||
cm_default_preferences: JSON.stringify(value), | ||
advertising_sharing_allowed: JSON.stringify({ value: true }), | ||
some_cookie: 'iamastring', | ||
another_cookie: '5', | ||
array_cookie: JSON.stringify(['item1', 'item2']), | ||
}; | ||
(navigator as any).globalPrivacyControl = true; | ||
mockGet.mockImplementation(jest.fn(() => cookies)); | ||
expect(getAllCookies(Region.DEFAULT, {})).toEqual({ | ||
cm_default_preferences: { consent: ['necessary', 'performance'], region: 'DEFAULT' }, | ||
advertising_sharing_allowed: { value: false }, | ||
some_cookie: 'iamastring', | ||
another_cookie: 5, | ||
array_cookie: ['item1', 'item2'], | ||
}); | ||
}); | ||
}); |
import Cookies from 'js-cookie'; | ||
export const deserializeCookies = (cookies: Record<string, string>) => { | ||
export { Region } from '../types'; | ||
import { | ||
ADVERTISING_SHARING_ALLOWED, | ||
DEFAULT_CONSENT_PREFERENCES_COOKIE, | ||
EU_CONSENT_PREFERENCES_COOKIE, | ||
} from '../constants'; | ||
import { Region } from '../types'; | ||
import { applyGpcToAdPref } from './applyGpcToAdPref'; | ||
import { applyGpcToCookiePref } from './applyGpcToCookiePref'; | ||
export const deserializeCookies = (region: Region, cookies: Record<string, string>) => { | ||
const parsedCookies: Record<string, any> = {}; | ||
@@ -12,2 +23,3 @@ | ||
} | ||
parsedCookies[c] = filterCookieValue(region, c, parsedCookies[c]); | ||
}); | ||
@@ -17,9 +29,23 @@ return parsedCookies; | ||
export default function getAllCookies(initialCookies?: Record<string, string>) { | ||
export default function getAllCookies(region: Region, initialCookies?: Record<string, string>) { | ||
if (typeof window === 'undefined' && initialCookies) { | ||
return deserializeCookies(initialCookies); | ||
return deserializeCookies(region, initialCookies); | ||
} | ||
return deserializeCookies(Cookies.get() || {}); | ||
return deserializeCookies(region, Cookies.get() || {}); | ||
} | ||
// Apply in in memory filters to the cookie values. Currently we are just apply | ||
// Global Privacy Control (GPC) logic to ensure we are honoring GPC | ||
function filterCookieValue(region: Region, cookieName: string, cookieValue: any) { | ||
if (cookieName == ADVERTISING_SHARING_ALLOWED) { | ||
cookieValue = applyGpcToAdPref(region, cookieValue); | ||
} else if ( | ||
(region == Region.DEFAULT && cookieName == DEFAULT_CONSENT_PREFERENCES_COOKIE) || | ||
(region == Region.EU && cookieName == EU_CONSENT_PREFERENCES_COOKIE) | ||
) { | ||
cookieValue = applyGpcToCookiePref(cookieValue); | ||
} | ||
return cookieValue; | ||
} | ||
export function areRecordsEqual( | ||
@@ -26,0 +52,0 @@ record1: Record<string, any>, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
137020
105
2833