@guardian/commercial-core 4.22.0 → 4.24.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.
@@ -15,7 +15,7 @@ export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments,
15
15
  export { initTrackScrollDepth } from './track-scroll-depth';
16
16
  export { initTrackLabsContainer } from './track-labs-container';
17
17
  export { initTrackGpcSignal } from './track-gpc-signal';
18
- export { buildAdsConfigWithConsent, disabledAds } from './ad-targeting-youtube';
18
+ export { buildAdsConfigWithConsent, disabledAds } from './targeting/youtube';
19
19
  export { createAdSlot, concatSizeMappings } from './create-ad-slot';
20
20
  export type { AdsConfig, AdsConfigBasic, AdsConfigDisabled, AdTargetingBuilder, CustomParams, } from './types';
21
21
  export * as constants from './constants';
package/dist/cjs/index.js CHANGED
@@ -61,9 +61,9 @@ var track_labs_container_1 = require("./track-labs-container");
61
61
  Object.defineProperty(exports, "initTrackLabsContainer", { enumerable: true, get: function () { return track_labs_container_1.initTrackLabsContainer; } });
62
62
  var track_gpc_signal_1 = require("./track-gpc-signal");
63
63
  Object.defineProperty(exports, "initTrackGpcSignal", { enumerable: true, get: function () { return track_gpc_signal_1.initTrackGpcSignal; } });
64
- var ad_targeting_youtube_1 = require("./ad-targeting-youtube");
65
- Object.defineProperty(exports, "buildAdsConfigWithConsent", { enumerable: true, get: function () { return ad_targeting_youtube_1.buildAdsConfigWithConsent; } });
66
- Object.defineProperty(exports, "disabledAds", { enumerable: true, get: function () { return ad_targeting_youtube_1.disabledAds; } });
64
+ var youtube_1 = require("./targeting/youtube");
65
+ Object.defineProperty(exports, "buildAdsConfigWithConsent", { enumerable: true, get: function () { return youtube_1.buildAdsConfigWithConsent; } });
66
+ Object.defineProperty(exports, "disabledAds", { enumerable: true, get: function () { return youtube_1.disabledAds; } });
67
67
  var create_ad_slot_1 = require("./create-ad-slot");
68
68
  Object.defineProperty(exports, "createAdSlot", { enumerable: true, get: function () { return create_ad_slot_1.createAdSlot; } });
69
69
  Object.defineProperty(exports, "concatSizeMappings", { enumerable: true, get: function () { return create_ad_slot_1.concatSizeMappings; } });
@@ -35,7 +35,10 @@ const isConsentlessKey = (key) => consentlessTargetingKeys.includes(key);
35
35
  * @returns ConsentlessPageTargeting
36
36
  */
37
37
  const buildPageTargetingConsentless = (consentState, adFree) => {
38
- const consentedPageTargeting = (0, build_page_targeting_1.buildPageTargeting)(consentState, adFree);
38
+ const consentedPageTargeting = (0, build_page_targeting_1.buildPageTargeting)({
39
+ consentState,
40
+ adFree,
41
+ });
39
42
  return Object.fromEntries(Object.entries(consentedPageTargeting).filter(([k]) => isConsentlessKey(k)));
40
43
  };
41
44
  exports.buildPageTargetingConsentless = buildPageTargetingConsentless;
@@ -34,6 +34,11 @@ declare type PageTargeting = PartialWithNulls<{
34
34
  [_: string]: string | string[];
35
35
  } & SharedTargeting>;
36
36
  declare const filterValues: (pageTargets: Record<string, unknown>) => Record<string, string | string[]>;
37
- declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean) => PageTargeting;
37
+ declare type BuildPageTargetingParams = {
38
+ consentState: ConsentState;
39
+ adFree: boolean;
40
+ youtube?: boolean;
41
+ };
42
+ declare const buildPageTargeting: ({ consentState, adFree, youtube, }: BuildPageTargetingParams) => Record<string, string | string[]>;
38
43
  export { buildPageTargeting, filterValues };
39
44
  export type { PageTargeting };
@@ -26,7 +26,7 @@ const filterValues = (pageTargets) => {
26
26
  return filtered;
27
27
  };
28
28
  exports.filterValues = filterValues;
29
- const buildPageTargeting = (consentState, adFree) => {
29
+ const buildPageTargeting = ({ consentState, adFree, youtube = false, }) => {
30
30
  const { page, isDotcomRendering } = window.guardian.config;
31
31
  const adFreeTargeting = adFree ? { af: 't' } : {};
32
32
  const contentTargeting = (0, content_1.getContentTargeting)({
@@ -64,7 +64,10 @@ const buildPageTargeting = (consentState, adFree) => {
64
64
  const sharedAdTargeting = page.sharedAdTargeting
65
65
  ? (0, shared_1.getSharedTargeting)(page.sharedAdTargeting)
66
66
  : {};
67
- const personalisedTargeting = (0, personalised_1.getPersonalisedTargeting)(consentState);
67
+ const personalisedTargeting = (0, personalised_1.getPersonalisedTargeting)({
68
+ state: consentState,
69
+ youtube,
70
+ });
68
71
  const pageTargets = {
69
72
  ...personalisedTargeting,
70
73
  ...sharedAdTargeting,
@@ -75,6 +75,10 @@ declare type PersonalisedTargeting = {
75
75
  */
76
76
  rdp: True | False | NotApplicable;
77
77
  };
78
- declare const getPersonalisedTargeting: (state: ConsentState) => PersonalisedTargeting;
78
+ declare type Personalised = {
79
+ state: ConsentState;
80
+ youtube: boolean;
81
+ };
82
+ declare const getPersonalisedTargeting: ({ state, youtube, }: Personalised) => PersonalisedTargeting;
79
83
  export { getPersonalisedTargeting };
80
84
  export type { PersonalisedTargeting, AdManagerGroup, Frequency };
@@ -124,17 +124,17 @@ const getAdManagerGroup = (state) => {
124
124
  ? existingGroup
125
125
  : createAdManagerGroup();
126
126
  };
127
- const getPermutiveWithState = (state) => {
128
- if (state.canTarget)
129
- return (0, permutive_1.getPermutiveSegments)();
127
+ const getPermutiveWithState = (state, youtube) => {
128
+ if (state.canTarget) {
129
+ return youtube ? (0, permutive_1.getPermutivePFPSegments)() : (0, permutive_1.getPermutiveSegments)();
130
+ }
130
131
  (0, permutive_1.clearPermutiveSegments)();
131
132
  return [];
132
133
  };
133
- /* -- Targeting -- */
134
- const getPersonalisedTargeting = (state) => ({
134
+ const getPersonalisedTargeting = ({ state, youtube, }) => ({
135
135
  amtgrp: getAdManagerGroup(state),
136
136
  fr: getFrequencyValue(state),
137
- permutive: getPermutiveWithState(state),
137
+ permutive: getPermutiveWithState(state, youtube),
138
138
  ...getCMPTargeting(state),
139
139
  });
140
140
  exports.getPersonalisedTargeting = getPersonalisedTargeting;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.buildImaAdTagUrl = void 0;
4
+ const libs_1 = require("@guardian/libs");
4
5
  const build_page_targeting_1 = require("./build-page-targeting");
5
6
  /**
6
7
  * @param {Record<string, MaybeArray<string|number|boolean>>
@@ -17,9 +18,28 @@ const encodeCustomParams = (params) => {
17
18
  .join('&');
18
19
  return encodedParams;
19
20
  };
20
- const buildImaAdTagUrl = (adUnit, customParams, consentState) => {
21
- const pageTargeting = (0, build_page_targeting_1.buildPageTargeting)(consentState, false);
21
+ const mergeCustomParamsWithTargeting = (customParams, consentState) => {
22
+ let pageTargeting = {};
23
+ try {
24
+ pageTargeting = (0, build_page_targeting_1.buildPageTargeting)({
25
+ consentState: consentState,
26
+ adFree: false,
27
+ });
28
+ }
29
+ catch (e) {
30
+ /**
31
+ * Defensive error handling in case YoutubeAtom is used in an
32
+ * environment where guardian.config, cookies, localstorage etc
33
+ * are not available
34
+ */
35
+ (0, libs_1.log)('commercial', 'Error building YouTube IMA custom params', e);
36
+ return customParams;
37
+ }
22
38
  const mergedCustomParams = { ...customParams, ...pageTargeting };
39
+ return mergedCustomParams;
40
+ };
41
+ const buildImaAdTagUrl = (adUnit, customParams, consentState) => {
42
+ const mergedCustomParams = mergeCustomParamsWithTargeting(customParams, consentState);
23
43
  const queryParams = {
24
44
  iu: adUnit,
25
45
  tfcd: '0',
@@ -1,5 +1,5 @@
1
1
  import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
2
- import type { AdsConfig, AdsConfigDisabled, CustomParams } from './types';
2
+ import type { AdsConfig, AdsConfigDisabled, CustomParams } from '../types';
3
3
  declare const disabledAds: AdsConfigDisabled;
4
4
  declare const buildAdsConfigWithConsent: (isAdFreeUser: boolean, adUnit: string, customParamsToMerge: CustomParams, consentState: ConsentState) => AdsConfig;
5
5
  export { buildAdsConfigWithConsent, disabledAds };
@@ -1,22 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.disabledAds = exports.buildAdsConfigWithConsent = void 0;
4
- const libs_1 = require("@guardian/libs");
5
- const can_use_dom_1 = require("./lib/can-use-dom");
6
- const construct_query_1 = require("./lib/construct-query");
7
- const permutive_1 = require("./permutive");
4
+ const construct_query_1 = require("../lib/construct-query");
5
+ const build_page_targeting_1 = require("./build-page-targeting");
8
6
  const disabledAds = { disableAds: true };
9
7
  exports.disabledAds = disabledAds;
10
- const buildCustomParamsFromCookies = () => (0, can_use_dom_1.canUseDom)()
11
- ? {
12
- permutive: (0, permutive_1.getPermutivePFPSegments)(),
13
- si: (0, libs_1.getCookie)({ name: 'GU_U' }) ? 't' : 'f',
14
- }
15
- : {};
16
8
  const buildAdsConfig = (cmpConsent, adUnit, customParams) => {
17
9
  const mergedCustomParams = {
18
10
  ...customParams,
19
- ...buildCustomParamsFromCookies(),
11
+ ...(0, build_page_targeting_1.buildPageTargeting)({
12
+ consentState: cmpConsent,
13
+ adFree: false,
14
+ youtube: true,
15
+ }),
20
16
  };
21
17
  const defaultAdsConfig = {
22
18
  adTagParameters: {
@@ -15,7 +15,7 @@ export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments,
15
15
  export { initTrackScrollDepth } from './track-scroll-depth';
16
16
  export { initTrackLabsContainer } from './track-labs-container';
17
17
  export { initTrackGpcSignal } from './track-gpc-signal';
18
- export { buildAdsConfigWithConsent, disabledAds } from './ad-targeting-youtube';
18
+ export { buildAdsConfigWithConsent, disabledAds } from './targeting/youtube';
19
19
  export { createAdSlot, concatSizeMappings } from './create-ad-slot';
20
20
  export type { AdsConfig, AdsConfigBasic, AdsConfigDisabled, AdTargetingBuilder, CustomParams, } from './types';
21
21
  export * as constants from './constants';
package/dist/esm/index.js CHANGED
@@ -13,7 +13,7 @@ export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments,
13
13
  export { initTrackScrollDepth } from './track-scroll-depth';
14
14
  export { initTrackLabsContainer } from './track-labs-container';
15
15
  export { initTrackGpcSignal } from './track-gpc-signal';
16
- export { buildAdsConfigWithConsent, disabledAds } from './ad-targeting-youtube';
16
+ export { buildAdsConfigWithConsent, disabledAds } from './targeting/youtube';
17
17
  export { createAdSlot, concatSizeMappings } from './create-ad-slot';
18
18
  import * as constants_1 from './constants';
19
19
  export { constants_1 as constants };
@@ -32,7 +32,10 @@ const isConsentlessKey = (key) => consentlessTargetingKeys.includes(key);
32
32
  * @returns ConsentlessPageTargeting
33
33
  */
34
34
  const buildPageTargetingConsentless = (consentState, adFree) => {
35
- const consentedPageTargeting = buildPageTargeting(consentState, adFree);
35
+ const consentedPageTargeting = buildPageTargeting({
36
+ consentState,
37
+ adFree,
38
+ });
36
39
  return Object.fromEntries(Object.entries(consentedPageTargeting).filter(([k]) => isConsentlessKey(k)));
37
40
  };
38
41
  export { buildPageTargetingConsentless };
@@ -34,6 +34,11 @@ declare type PageTargeting = PartialWithNulls<{
34
34
  [_: string]: string | string[];
35
35
  } & SharedTargeting>;
36
36
  declare const filterValues: (pageTargets: Record<string, unknown>) => Record<string, string | string[]>;
37
- declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean) => PageTargeting;
37
+ declare type BuildPageTargetingParams = {
38
+ consentState: ConsentState;
39
+ adFree: boolean;
40
+ youtube?: boolean;
41
+ };
42
+ declare const buildPageTargeting: ({ consentState, adFree, youtube, }: BuildPageTargetingParams) => Record<string, string | string[]>;
38
43
  export { buildPageTargeting, filterValues };
39
44
  export type { PageTargeting };
@@ -22,7 +22,7 @@ const filterValues = (pageTargets) => {
22
22
  }
23
23
  return filtered;
24
24
  };
25
- const buildPageTargeting = (consentState, adFree) => {
25
+ const buildPageTargeting = ({ consentState, adFree, youtube = false, }) => {
26
26
  const { page, isDotcomRendering } = window.guardian.config;
27
27
  const adFreeTargeting = adFree ? { af: 't' } : {};
28
28
  const contentTargeting = getContentTargeting({
@@ -60,7 +60,10 @@ const buildPageTargeting = (consentState, adFree) => {
60
60
  const sharedAdTargeting = page.sharedAdTargeting
61
61
  ? getSharedTargeting(page.sharedAdTargeting)
62
62
  : {};
63
- const personalisedTargeting = getPersonalisedTargeting(consentState);
63
+ const personalisedTargeting = getPersonalisedTargeting({
64
+ state: consentState,
65
+ youtube,
66
+ });
64
67
  const pageTargets = {
65
68
  ...personalisedTargeting,
66
69
  ...sharedAdTargeting,
@@ -75,6 +75,10 @@ declare type PersonalisedTargeting = {
75
75
  */
76
76
  rdp: True | False | NotApplicable;
77
77
  };
78
- declare const getPersonalisedTargeting: (state: ConsentState) => PersonalisedTargeting;
78
+ declare type Personalised = {
79
+ state: ConsentState;
80
+ youtube: boolean;
81
+ };
82
+ declare const getPersonalisedTargeting: ({ state, youtube, }: Personalised) => PersonalisedTargeting;
79
83
  export { getPersonalisedTargeting };
80
84
  export type { PersonalisedTargeting, AdManagerGroup, Frequency };
@@ -1,5 +1,5 @@
1
1
  import { storage } from '@guardian/libs';
2
- import { clearPermutiveSegments, getPermutiveSegments } from '../permutive';
2
+ import { clearPermutiveSegments, getPermutivePFPSegments, getPermutiveSegments, } from '../permutive';
3
3
  /* -- Types -- */
4
4
  const frequency = [
5
5
  '0',
@@ -121,17 +121,17 @@ const getAdManagerGroup = (state) => {
121
121
  ? existingGroup
122
122
  : createAdManagerGroup();
123
123
  };
124
- const getPermutiveWithState = (state) => {
125
- if (state.canTarget)
126
- return getPermutiveSegments();
124
+ const getPermutiveWithState = (state, youtube) => {
125
+ if (state.canTarget) {
126
+ return youtube ? getPermutivePFPSegments() : getPermutiveSegments();
127
+ }
127
128
  clearPermutiveSegments();
128
129
  return [];
129
130
  };
130
- /* -- Targeting -- */
131
- const getPersonalisedTargeting = (state) => ({
131
+ const getPersonalisedTargeting = ({ state, youtube, }) => ({
132
132
  amtgrp: getAdManagerGroup(state),
133
133
  fr: getFrequencyValue(state),
134
- permutive: getPermutiveWithState(state),
134
+ permutive: getPermutiveWithState(state, youtube),
135
135
  ...getCMPTargeting(state),
136
136
  });
137
137
  export { getPersonalisedTargeting };
@@ -1,3 +1,4 @@
1
+ import { log } from '@guardian/libs';
1
2
  import { buildPageTargeting, filterValues } from './build-page-targeting';
2
3
  /**
3
4
  * @param {Record<string, MaybeArray<string|number|boolean>>
@@ -14,9 +15,28 @@ const encodeCustomParams = (params) => {
14
15
  .join('&');
15
16
  return encodedParams;
16
17
  };
17
- const buildImaAdTagUrl = (adUnit, customParams, consentState) => {
18
- const pageTargeting = buildPageTargeting(consentState, false);
18
+ const mergeCustomParamsWithTargeting = (customParams, consentState) => {
19
+ let pageTargeting = {};
20
+ try {
21
+ pageTargeting = buildPageTargeting({
22
+ consentState: consentState,
23
+ adFree: false,
24
+ });
25
+ }
26
+ catch (e) {
27
+ /**
28
+ * Defensive error handling in case YoutubeAtom is used in an
29
+ * environment where guardian.config, cookies, localstorage etc
30
+ * are not available
31
+ */
32
+ log('commercial', 'Error building YouTube IMA custom params', e);
33
+ return customParams;
34
+ }
19
35
  const mergedCustomParams = { ...customParams, ...pageTargeting };
36
+ return mergedCustomParams;
37
+ };
38
+ const buildImaAdTagUrl = (adUnit, customParams, consentState) => {
39
+ const mergedCustomParams = mergeCustomParamsWithTargeting(customParams, consentState);
20
40
  const queryParams = {
21
41
  iu: adUnit,
22
42
  tfcd: '0',
@@ -1,5 +1,5 @@
1
1
  import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
2
- import type { AdsConfig, AdsConfigDisabled, CustomParams } from './types';
2
+ import type { AdsConfig, AdsConfigDisabled, CustomParams } from '../types';
3
3
  declare const disabledAds: AdsConfigDisabled;
4
4
  declare const buildAdsConfigWithConsent: (isAdFreeUser: boolean, adUnit: string, customParamsToMerge: CustomParams, consentState: ConsentState) => AdsConfig;
5
5
  export { buildAdsConfigWithConsent, disabledAds };
@@ -1,18 +1,14 @@
1
- import { getCookie } from '@guardian/libs';
2
- import { canUseDom } from './lib/can-use-dom';
3
- import { constructQuery } from './lib/construct-query';
4
- import { getPermutivePFPSegments } from './permutive';
1
+ import { constructQuery } from '../lib/construct-query';
2
+ import { buildPageTargeting } from './build-page-targeting';
5
3
  const disabledAds = { disableAds: true };
6
- const buildCustomParamsFromCookies = () => canUseDom()
7
- ? {
8
- permutive: getPermutivePFPSegments(),
9
- si: getCookie({ name: 'GU_U' }) ? 't' : 'f',
10
- }
11
- : {};
12
4
  const buildAdsConfig = (cmpConsent, adUnit, customParams) => {
13
5
  const mergedCustomParams = {
14
6
  ...customParams,
15
- ...buildCustomParamsFromCookies(),
7
+ ...buildPageTargeting({
8
+ consentState: cmpConsent,
9
+ adFree: false,
10
+ youtube: true,
11
+ }),
16
12
  };
17
13
  const defaultAdsConfig = {
18
14
  adTagParameters: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/commercial-core",
3
- "version": "4.22.0",
3
+ "version": "4.24.0",
4
4
  "description": "Guardian advertising business logic",
5
5
  "homepage": "https://github.com/guardian/commercial-core#readme",
6
6
  "bugs": {