@guardian/commercial-core 4.16.0 → 4.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/lib/get-locale.d.ts +8 -0
- package/dist/cjs/lib/get-locale.js +43 -0
- package/dist/cjs/targeting/build-page-targeting-consentless.d.ts +1 -3
- package/dist/cjs/targeting/build-page-targeting-consentless.js +2 -3
- package/dist/cjs/targeting/build-page-targeting.d.ts +1 -1
- package/dist/cjs/targeting/build-page-targeting.js +3 -2
- package/dist/cjs/types.d.ts +2 -1
- package/dist/esm/lib/get-locale.d.ts +8 -0
- package/dist/esm/lib/get-locale.js +38 -0
- package/dist/esm/targeting/build-page-targeting-consentless.d.ts +1 -3
- package/dist/esm/targeting/build-page-targeting-consentless.js +2 -3
- package/dist/esm/targeting/build-page-targeting.d.ts +1 -1
- package/dist/esm/targeting/build-page-targeting.js +3 -2
- package/dist/esm/types.d.ts +2 -1
- package/package.json +1 -1
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CountryCode } from '@guardian/libs';
|
|
2
|
+
export declare const __resetCachedValue: () => void;
|
|
3
|
+
/**
|
|
4
|
+
* Fetches the user's current location as an ISO 3166-1 alpha-2 string e.g. 'GB', 'AU' etc
|
|
5
|
+
* Note: This has been copied from guardian-libs and made syncronous by ommiting the call to
|
|
6
|
+
* the geolocation API
|
|
7
|
+
*/
|
|
8
|
+
export declare const getLocale: () => CountryCode;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getLocale = exports.__resetCachedValue = void 0;
|
|
4
|
+
const libs_1 = require("@guardian/libs");
|
|
5
|
+
const KEY = 'GU_geo_country';
|
|
6
|
+
const KEY_OVERRIDE = 'gu.geo.override';
|
|
7
|
+
const COUNTRY_REGEX = /^[A-Z]{2}$/;
|
|
8
|
+
// best guess that we have a valid code, without actually shipping the entire list
|
|
9
|
+
const isValidCountryCode = (country) => (0, libs_1.isString)(country) && COUNTRY_REGEX.test(country);
|
|
10
|
+
// we'll cache any successful lookups so we only have to do this once
|
|
11
|
+
let locale;
|
|
12
|
+
const editionToGeolocationMap = {
|
|
13
|
+
UK: 'GB',
|
|
14
|
+
US: 'US',
|
|
15
|
+
AU: 'AU',
|
|
16
|
+
};
|
|
17
|
+
const editionToGeolocation = (editionKey) => editionToGeolocationMap[editionKey];
|
|
18
|
+
// just used for tests
|
|
19
|
+
const __resetCachedValue = () => (locale = undefined);
|
|
20
|
+
exports.__resetCachedValue = __resetCachedValue;
|
|
21
|
+
/**
|
|
22
|
+
* Fetches the user's current location as an ISO 3166-1 alpha-2 string e.g. 'GB', 'AU' etc
|
|
23
|
+
* Note: This has been copied from guardian-libs and made syncronous by ommiting the call to
|
|
24
|
+
* the geolocation API
|
|
25
|
+
*/
|
|
26
|
+
const getLocale = () => {
|
|
27
|
+
if (locale)
|
|
28
|
+
return locale;
|
|
29
|
+
// return overridden geo from localStorage, used for changing geo only for development purposes
|
|
30
|
+
const geoOverride = libs_1.storage.local.get(KEY_OVERRIDE);
|
|
31
|
+
if (isValidCountryCode(geoOverride)) {
|
|
32
|
+
return (locale = geoOverride);
|
|
33
|
+
}
|
|
34
|
+
// return locale from cookie if it exists
|
|
35
|
+
const stored = (0, libs_1.getCookie)({ name: KEY });
|
|
36
|
+
if (stored && isValidCountryCode(stored)) {
|
|
37
|
+
return (locale = stored);
|
|
38
|
+
}
|
|
39
|
+
// return locale from edition
|
|
40
|
+
const editionCountryCode = editionToGeolocation(window.guardian.config.page.edition);
|
|
41
|
+
return (locale = editionCountryCode);
|
|
42
|
+
};
|
|
43
|
+
exports.getLocale = getLocale;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Participations } from '@guardian/ab-core';
|
|
2
2
|
import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
|
|
3
|
-
import type { CountryCode } from '@guardian/libs';
|
|
4
3
|
import type { PageTargeting } from './build-page-targeting';
|
|
5
4
|
declare const consentlessTargetingKeys: readonly ["ab", "at", "bl", "bp", "br", "cc", "ct", "dcre", "edition", "k", "rp", "s", "se", "sens", "sh", "si", "skinsize", "su", "tn", "url", "urlkw"];
|
|
6
5
|
declare type ConsentlessTargetingKeys = typeof consentlessTargetingKeys[number];
|
|
@@ -11,9 +10,8 @@ declare type ConsentlessPageTargeting = Partial<Pick<PageTargeting, ConsentlessT
|
|
|
11
10
|
*
|
|
12
11
|
* @param {ConsentState} consentState
|
|
13
12
|
* @param {boolean} adFree
|
|
14
|
-
* @param {CountryCode} countryCode
|
|
15
13
|
* @param {Participations} clientSideParticipations
|
|
16
14
|
* @returns ConsentlessPageTargeting
|
|
17
15
|
*/
|
|
18
|
-
declare const buildPageTargetingConsentless: (consentState: ConsentState, adFree: boolean,
|
|
16
|
+
declare const buildPageTargetingConsentless: (consentState: ConsentState, adFree: boolean, clientSideParticipations: Participations) => ConsentlessPageTargeting;
|
|
19
17
|
export { buildPageTargetingConsentless };
|
|
@@ -32,12 +32,11 @@ const isConsentlessKey = (key) => consentlessTargetingKeys.includes(key);
|
|
|
32
32
|
*
|
|
33
33
|
* @param {ConsentState} consentState
|
|
34
34
|
* @param {boolean} adFree
|
|
35
|
-
* @param {CountryCode} countryCode
|
|
36
35
|
* @param {Participations} clientSideParticipations
|
|
37
36
|
* @returns ConsentlessPageTargeting
|
|
38
37
|
*/
|
|
39
|
-
const buildPageTargetingConsentless = (consentState, adFree,
|
|
40
|
-
const consentedPageTargeting = (0, build_page_targeting_1.buildPageTargeting)(consentState, adFree,
|
|
38
|
+
const buildPageTargetingConsentless = (consentState, adFree, clientSideParticipations) => {
|
|
39
|
+
const consentedPageTargeting = (0, build_page_targeting_1.buildPageTargeting)(consentState, adFree, clientSideParticipations);
|
|
41
40
|
return Object.fromEntries(Object.entries(consentedPageTargeting).filter(([k]) => isConsentlessKey(k)));
|
|
42
41
|
};
|
|
43
42
|
exports.buildPageTargetingConsentless = buildPageTargetingConsentless;
|
|
@@ -34,6 +34,6 @@ declare type PageTargeting = PartialWithNulls<{
|
|
|
34
34
|
vl: string;
|
|
35
35
|
[_: string]: string | string[];
|
|
36
36
|
} & SharedTargeting>;
|
|
37
|
-
declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean,
|
|
37
|
+
declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean, clientSideParticipations: Participations) => PageTargeting;
|
|
38
38
|
export { buildPageTargeting };
|
|
39
39
|
export type { PageTargeting };
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.buildPageTargeting = void 0;
|
|
4
4
|
const consent_management_platform_1 = require("@guardian/consent-management-platform");
|
|
5
5
|
const libs_1 = require("@guardian/libs");
|
|
6
|
+
const get_locale_1 = require("../lib/get-locale");
|
|
6
7
|
const content_1 = require("./content");
|
|
7
8
|
const personalised_1 = require("./personalised");
|
|
8
9
|
const session_1 = require("./session");
|
|
@@ -23,7 +24,7 @@ const filterEmptyValues = (pageTargets) => {
|
|
|
23
24
|
}
|
|
24
25
|
return filtered;
|
|
25
26
|
};
|
|
26
|
-
const buildPageTargeting = (consentState, adFree,
|
|
27
|
+
const buildPageTargeting = (consentState, adFree, clientSideParticipations) => {
|
|
27
28
|
const { page, isDotcomRendering } = window.guardian.config;
|
|
28
29
|
const adFreeTargeting = adFree ? { af: 't' } : {};
|
|
29
30
|
const contentTargeting = (0, content_1.getContentTargeting)({
|
|
@@ -39,7 +40,7 @@ const buildPageTargeting = (consentState, adFree, countryCode, clientSidePartici
|
|
|
39
40
|
const getReferrer = () => document.referrer || '';
|
|
40
41
|
const sessionTargeting = (0, session_1.getSessionTargeting)({
|
|
41
42
|
adTest: (0, libs_1.getCookie)({ name: 'adtest', shouldMemoize: true }),
|
|
42
|
-
countryCode,
|
|
43
|
+
countryCode: (0, get_locale_1.getLocale)(),
|
|
43
44
|
isSignedIn: !!(0, libs_1.getCookie)({ name: 'GU_U' }),
|
|
44
45
|
pageViewId: window.guardian.config.ophan.pageViewId,
|
|
45
46
|
participations: {
|
package/dist/cjs/types.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export declare type GetThirdPartyTag = (arg0: {
|
|
|
21
21
|
export declare type GuardianAnalyticsConfig = {
|
|
22
22
|
trackers: Record<string, string>;
|
|
23
23
|
};
|
|
24
|
+
export declare type Edition = 'UK' | 'AU' | 'US';
|
|
24
25
|
export declare type GuardianWindowConfig = {
|
|
25
26
|
googleAnalytics?: GuardianAnalyticsConfig;
|
|
26
27
|
isDotcomRendering: boolean;
|
|
@@ -30,11 +31,11 @@ export declare type GuardianWindowConfig = {
|
|
|
30
31
|
};
|
|
31
32
|
page: {
|
|
32
33
|
sharedAdTargeting?: Record<string, string | string[]>;
|
|
33
|
-
pageAdTargeting?: Record<string, string | string[]>;
|
|
34
34
|
isSensitive: boolean;
|
|
35
35
|
pageId: string;
|
|
36
36
|
section: string;
|
|
37
37
|
videoDuration: number;
|
|
38
|
+
edition: Edition;
|
|
38
39
|
};
|
|
39
40
|
tests?: {
|
|
40
41
|
[key: `${string}Control`]: 'control';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { CountryCode } from '@guardian/libs';
|
|
2
|
+
export declare const __resetCachedValue: () => void;
|
|
3
|
+
/**
|
|
4
|
+
* Fetches the user's current location as an ISO 3166-1 alpha-2 string e.g. 'GB', 'AU' etc
|
|
5
|
+
* Note: This has been copied from guardian-libs and made syncronous by ommiting the call to
|
|
6
|
+
* the geolocation API
|
|
7
|
+
*/
|
|
8
|
+
export declare const getLocale: () => CountryCode;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { getCookie, isString, storage } from '@guardian/libs';
|
|
2
|
+
const KEY = 'GU_geo_country';
|
|
3
|
+
const KEY_OVERRIDE = 'gu.geo.override';
|
|
4
|
+
const COUNTRY_REGEX = /^[A-Z]{2}$/;
|
|
5
|
+
// best guess that we have a valid code, without actually shipping the entire list
|
|
6
|
+
const isValidCountryCode = (country) => isString(country) && COUNTRY_REGEX.test(country);
|
|
7
|
+
// we'll cache any successful lookups so we only have to do this once
|
|
8
|
+
let locale;
|
|
9
|
+
const editionToGeolocationMap = {
|
|
10
|
+
UK: 'GB',
|
|
11
|
+
US: 'US',
|
|
12
|
+
AU: 'AU',
|
|
13
|
+
};
|
|
14
|
+
const editionToGeolocation = (editionKey) => editionToGeolocationMap[editionKey];
|
|
15
|
+
// just used for tests
|
|
16
|
+
export const __resetCachedValue = () => (locale = undefined);
|
|
17
|
+
/**
|
|
18
|
+
* Fetches the user's current location as an ISO 3166-1 alpha-2 string e.g. 'GB', 'AU' etc
|
|
19
|
+
* Note: This has been copied from guardian-libs and made syncronous by ommiting the call to
|
|
20
|
+
* the geolocation API
|
|
21
|
+
*/
|
|
22
|
+
export const getLocale = () => {
|
|
23
|
+
if (locale)
|
|
24
|
+
return locale;
|
|
25
|
+
// return overridden geo from localStorage, used for changing geo only for development purposes
|
|
26
|
+
const geoOverride = storage.local.get(KEY_OVERRIDE);
|
|
27
|
+
if (isValidCountryCode(geoOverride)) {
|
|
28
|
+
return (locale = geoOverride);
|
|
29
|
+
}
|
|
30
|
+
// return locale from cookie if it exists
|
|
31
|
+
const stored = getCookie({ name: KEY });
|
|
32
|
+
if (stored && isValidCountryCode(stored)) {
|
|
33
|
+
return (locale = stored);
|
|
34
|
+
}
|
|
35
|
+
// return locale from edition
|
|
36
|
+
const editionCountryCode = editionToGeolocation(window.guardian.config.page.edition);
|
|
37
|
+
return (locale = editionCountryCode);
|
|
38
|
+
};
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { Participations } from '@guardian/ab-core';
|
|
2
2
|
import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
|
|
3
|
-
import type { CountryCode } from '@guardian/libs';
|
|
4
3
|
import type { PageTargeting } from './build-page-targeting';
|
|
5
4
|
declare const consentlessTargetingKeys: readonly ["ab", "at", "bl", "bp", "br", "cc", "ct", "dcre", "edition", "k", "rp", "s", "se", "sens", "sh", "si", "skinsize", "su", "tn", "url", "urlkw"];
|
|
6
5
|
declare type ConsentlessTargetingKeys = typeof consentlessTargetingKeys[number];
|
|
@@ -11,9 +10,8 @@ declare type ConsentlessPageTargeting = Partial<Pick<PageTargeting, ConsentlessT
|
|
|
11
10
|
*
|
|
12
11
|
* @param {ConsentState} consentState
|
|
13
12
|
* @param {boolean} adFree
|
|
14
|
-
* @param {CountryCode} countryCode
|
|
15
13
|
* @param {Participations} clientSideParticipations
|
|
16
14
|
* @returns ConsentlessPageTargeting
|
|
17
15
|
*/
|
|
18
|
-
declare const buildPageTargetingConsentless: (consentState: ConsentState, adFree: boolean,
|
|
16
|
+
declare const buildPageTargetingConsentless: (consentState: ConsentState, adFree: boolean, clientSideParticipations: Participations) => ConsentlessPageTargeting;
|
|
19
17
|
export { buildPageTargetingConsentless };
|
|
@@ -29,12 +29,11 @@ const isConsentlessKey = (key) => consentlessTargetingKeys.includes(key);
|
|
|
29
29
|
*
|
|
30
30
|
* @param {ConsentState} consentState
|
|
31
31
|
* @param {boolean} adFree
|
|
32
|
-
* @param {CountryCode} countryCode
|
|
33
32
|
* @param {Participations} clientSideParticipations
|
|
34
33
|
* @returns ConsentlessPageTargeting
|
|
35
34
|
*/
|
|
36
|
-
const buildPageTargetingConsentless = (consentState, adFree,
|
|
37
|
-
const consentedPageTargeting = buildPageTargeting(consentState, adFree,
|
|
35
|
+
const buildPageTargetingConsentless = (consentState, adFree, clientSideParticipations) => {
|
|
36
|
+
const consentedPageTargeting = buildPageTargeting(consentState, adFree, clientSideParticipations);
|
|
38
37
|
return Object.fromEntries(Object.entries(consentedPageTargeting).filter(([k]) => isConsentlessKey(k)));
|
|
39
38
|
};
|
|
40
39
|
export { buildPageTargetingConsentless };
|
|
@@ -34,6 +34,6 @@ declare type PageTargeting = PartialWithNulls<{
|
|
|
34
34
|
vl: string;
|
|
35
35
|
[_: string]: string | string[];
|
|
36
36
|
} & SharedTargeting>;
|
|
37
|
-
declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean,
|
|
37
|
+
declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean, clientSideParticipations: Participations) => PageTargeting;
|
|
38
38
|
export { buildPageTargeting };
|
|
39
39
|
export type { PageTargeting };
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { cmp } from '@guardian/consent-management-platform';
|
|
2
2
|
import { getCookie, isString } from '@guardian/libs';
|
|
3
|
+
import { getLocale } from '../lib/get-locale';
|
|
3
4
|
import { getContentTargeting } from './content';
|
|
4
5
|
import { getPersonalisedTargeting } from './personalised';
|
|
5
6
|
import { getSessionTargeting } from './session';
|
|
@@ -20,7 +21,7 @@ const filterEmptyValues = (pageTargets) => {
|
|
|
20
21
|
}
|
|
21
22
|
return filtered;
|
|
22
23
|
};
|
|
23
|
-
const buildPageTargeting = (consentState, adFree,
|
|
24
|
+
const buildPageTargeting = (consentState, adFree, clientSideParticipations) => {
|
|
24
25
|
const { page, isDotcomRendering } = window.guardian.config;
|
|
25
26
|
const adFreeTargeting = adFree ? { af: 't' } : {};
|
|
26
27
|
const contentTargeting = getContentTargeting({
|
|
@@ -36,7 +37,7 @@ const buildPageTargeting = (consentState, adFree, countryCode, clientSidePartici
|
|
|
36
37
|
const getReferrer = () => document.referrer || '';
|
|
37
38
|
const sessionTargeting = getSessionTargeting({
|
|
38
39
|
adTest: getCookie({ name: 'adtest', shouldMemoize: true }),
|
|
39
|
-
countryCode,
|
|
40
|
+
countryCode: getLocale(),
|
|
40
41
|
isSignedIn: !!getCookie({ name: 'GU_U' }),
|
|
41
42
|
pageViewId: window.guardian.config.ophan.pageViewId,
|
|
42
43
|
participations: {
|
package/dist/esm/types.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export declare type GetThirdPartyTag = (arg0: {
|
|
|
21
21
|
export declare type GuardianAnalyticsConfig = {
|
|
22
22
|
trackers: Record<string, string>;
|
|
23
23
|
};
|
|
24
|
+
export declare type Edition = 'UK' | 'AU' | 'US';
|
|
24
25
|
export declare type GuardianWindowConfig = {
|
|
25
26
|
googleAnalytics?: GuardianAnalyticsConfig;
|
|
26
27
|
isDotcomRendering: boolean;
|
|
@@ -30,11 +31,11 @@ export declare type GuardianWindowConfig = {
|
|
|
30
31
|
};
|
|
31
32
|
page: {
|
|
32
33
|
sharedAdTargeting?: Record<string, string | string[]>;
|
|
33
|
-
pageAdTargeting?: Record<string, string | string[]>;
|
|
34
34
|
isSensitive: boolean;
|
|
35
35
|
pageId: string;
|
|
36
36
|
section: string;
|
|
37
37
|
videoDuration: number;
|
|
38
|
+
edition: Edition;
|
|
38
39
|
};
|
|
39
40
|
tests?: {
|
|
40
41
|
[key: `${string}Control`]: 'control';
|