@guardian/commercial-core 4.19.0 → 4.21.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/README.md CHANGED
@@ -27,12 +27,6 @@
27
27
  yarn add @guardian/commercial-core
28
28
  ```
29
29
 
30
- or
31
-
32
- ```bash
33
- npm install @guardian/commercial-core
34
- ```
35
-
36
30
  ### Bundling
37
31
 
38
32
  This package uses `ES2020`.
@@ -1,6 +1,5 @@
1
1
  import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
2
- import type { AdsConfig, AdsConfigDisabled, MaybeArray } from './types';
3
- declare type CustomParams = Record<string, MaybeArray<string | number | boolean>>;
2
+ import type { AdsConfig, AdsConfigDisabled, CustomParams } from './types';
4
3
  declare const disabledAds: AdsConfigDisabled;
5
4
  declare const buildAdsConfigWithConsent: (isAdFreeUser: boolean, adUnit: string, customParamsToMerge: CustomParams, consentState: ConsentState) => AdsConfig;
6
5
  export { buildAdsConfigWithConsent, disabledAds };
@@ -40,3 +40,4 @@ export { a9Apstag } from './__vendor/a9-apstag';
40
40
  export { ipsosMoriStub } from './__vendor/ipsos-mori';
41
41
  export { launchpad } from './__vendor/launchpad';
42
42
  export { pubmatic } from './__vendor/pubmatic';
43
+ export { buildImaAdTagUrl } from './targeting/youtube-ima';
package/dist/cjs/index.js CHANGED
@@ -24,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  return result;
25
25
  };
26
26
  Object.defineProperty(exports, "__esModule", { value: true });
27
- exports.pubmatic = exports.launchpad = exports.ipsosMoriStub = exports.a9Apstag = exports.buildPageTargetingConsentless = exports.buildPageTargeting = exports.postMessage = exports.initMessenger = exports.pickTargetingValues = exports.getViewportTargeting = exports.getSharedTargeting = exports.getSessionTargeting = exports.getPersonalisedTargeting = exports.getContentTargeting = exports.constants = exports.concatSizeMappings = exports.createAdSlot = exports.disabledAds = exports.buildAdsConfigWithConsent = exports.initTrackGpcSignal = exports.initTrackLabsContainer = exports.initTrackScrollDepth = exports.getPermutivePFPSegments = exports.getPermutiveSegments = exports.clearPermutiveSegments = exports.isAdBlockInUse = exports.isBreakpoint = exports.standardAdSizes = exports.slotSizeMappings = exports.outstreamSizes = exports.getAdSize = exports.createAdSize = exports.adSizes = exports.initCommercialMetrics = exports.bypassCommercialMetricsSampling = exports.EventTimer = exports.remarketing = exports.inizio = exports.twitter = exports.permutive = exports.ias = void 0;
27
+ exports.buildImaAdTagUrl = exports.pubmatic = exports.launchpad = exports.ipsosMoriStub = exports.a9Apstag = exports.buildPageTargetingConsentless = exports.buildPageTargeting = exports.postMessage = exports.initMessenger = exports.pickTargetingValues = exports.getViewportTargeting = exports.getSharedTargeting = exports.getSessionTargeting = exports.getPersonalisedTargeting = exports.getContentTargeting = exports.constants = exports.concatSizeMappings = exports.createAdSlot = exports.disabledAds = exports.buildAdsConfigWithConsent = exports.initTrackGpcSignal = exports.initTrackLabsContainer = exports.initTrackScrollDepth = exports.getPermutivePFPSegments = exports.getPermutiveSegments = exports.clearPermutiveSegments = exports.isAdBlockInUse = exports.isBreakpoint = exports.standardAdSizes = exports.slotSizeMappings = exports.outstreamSizes = exports.getAdSize = exports.createAdSize = exports.adSizes = exports.initCommercialMetrics = exports.bypassCommercialMetricsSampling = exports.EventTimer = exports.remarketing = exports.inizio = exports.twitter = exports.permutive = exports.ias = void 0;
28
28
  var ias_1 = require("./third-party-tags/ias");
29
29
  Object.defineProperty(exports, "ias", { enumerable: true, get: function () { return ias_1.ias; } });
30
30
  var permutive_1 = require("./third-party-tags/permutive");
@@ -97,3 +97,5 @@ var launchpad_1 = require("./__vendor/launchpad");
97
97
  Object.defineProperty(exports, "launchpad", { enumerable: true, get: function () { return launchpad_1.launchpad; } });
98
98
  var pubmatic_1 = require("./__vendor/pubmatic");
99
99
  Object.defineProperty(exports, "pubmatic", { enumerable: true, get: function () { return pubmatic_1.pubmatic; } });
100
+ var youtube_ima_1 = require("./targeting/youtube-ima");
101
+ Object.defineProperty(exports, "buildImaAdTagUrl", { enumerable: true, get: function () { return youtube_ima_1.buildImaAdTagUrl; } });
@@ -0,0 +1,2 @@
1
+ import type { Participations } from '@guardian/ab-core';
2
+ export declare const getParticipationsFromLocalStorage: () => Participations;
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getParticipationsFromLocalStorage = void 0;
4
+ const libs_1 = require("@guardian/libs");
5
+ const participationsKey = 'gu.ab.participations';
6
+ const isParticipations = (participations) => {
7
+ return ((0, libs_1.isObject)(participations) &&
8
+ Object.values(participations).every((participation) => (0, libs_1.isObject)(participation) && (0, libs_1.isString)(participation.variant)));
9
+ };
10
+ const getParticipationsFromLocalStorage = () => {
11
+ const participations = libs_1.storage.local.get(participationsKey);
12
+ return isParticipations(participations) ? participations : {};
13
+ };
14
+ exports.getParticipationsFromLocalStorage = getParticipationsFromLocalStorage;
@@ -1,4 +1,3 @@
1
- import type { Participations } from '@guardian/ab-core';
2
1
  import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
3
2
  import type { PageTargeting } from './build-page-targeting';
4
3
  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"];
@@ -10,8 +9,7 @@ declare type ConsentlessPageTargeting = Partial<Pick<PageTargeting, ConsentlessT
10
9
  *
11
10
  * @param {ConsentState} consentState
12
11
  * @param {boolean} adFree
13
- * @param {Participations} clientSideParticipations
14
12
  * @returns ConsentlessPageTargeting
15
13
  */
16
- declare const buildPageTargetingConsentless: (consentState: ConsentState, adFree: boolean, clientSideParticipations: Participations) => ConsentlessPageTargeting;
14
+ declare const buildPageTargetingConsentless: (consentState: ConsentState, adFree: boolean) => ConsentlessPageTargeting;
17
15
  export { buildPageTargetingConsentless };
@@ -32,11 +32,10 @@ const isConsentlessKey = (key) => consentlessTargetingKeys.includes(key);
32
32
  *
33
33
  * @param {ConsentState} consentState
34
34
  * @param {boolean} adFree
35
- * @param {Participations} clientSideParticipations
36
35
  * @returns ConsentlessPageTargeting
37
36
  */
38
- const buildPageTargetingConsentless = (consentState, adFree, clientSideParticipations) => {
39
- const consentedPageTargeting = (0, build_page_targeting_1.buildPageTargeting)(consentState, adFree, clientSideParticipations);
37
+ const buildPageTargetingConsentless = (consentState, adFree) => {
38
+ const consentedPageTargeting = (0, build_page_targeting_1.buildPageTargeting)(consentState, adFree);
40
39
  return Object.fromEntries(Object.entries(consentedPageTargeting).filter(([k]) => isConsentlessKey(k)));
41
40
  };
42
41
  exports.buildPageTargetingConsentless = buildPageTargetingConsentless;
@@ -1,4 +1,3 @@
1
- import type { Participations } from '@guardian/ab-core';
2
1
  import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
3
2
  import type { CountryCode } from '@guardian/libs';
4
3
  import type { False, True } from '../types';
@@ -34,6 +33,7 @@ declare type PageTargeting = PartialWithNulls<{
34
33
  vl: string;
35
34
  [_: string]: string | string[];
36
35
  } & SharedTargeting>;
37
- declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean, clientSideParticipations: Participations) => PageTargeting;
38
- export { buildPageTargeting };
36
+ declare const filterValues: (pageTargets: Record<string, unknown>) => Record<string, string | string[]>;
37
+ declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean) => PageTargeting;
38
+ export { buildPageTargeting, filterValues };
39
39
  export type { PageTargeting };
@@ -1,15 +1,16 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildPageTargeting = void 0;
3
+ exports.filterValues = 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 ab_localstorage_1 = require("../lib/ab-localstorage");
6
7
  const get_locale_1 = require("../lib/get-locale");
7
8
  const content_1 = require("./content");
8
9
  const personalised_1 = require("./personalised");
9
10
  const session_1 = require("./session");
10
11
  const shared_1 = require("./shared");
11
12
  const viewport_1 = require("./viewport");
12
- const filterEmptyValues = (pageTargets) => {
13
+ const filterValues = (pageTargets) => {
13
14
  const filtered = {};
14
15
  for (const key in pageTargets) {
15
16
  const value = pageTargets[key];
@@ -24,7 +25,8 @@ const filterEmptyValues = (pageTargets) => {
24
25
  }
25
26
  return filtered;
26
27
  };
27
- const buildPageTargeting = (consentState, adFree, clientSideParticipations) => {
28
+ exports.filterValues = filterValues;
29
+ const buildPageTargeting = (consentState, adFree) => {
28
30
  const { page, isDotcomRendering } = window.guardian.config;
29
31
  const adFreeTargeting = adFree ? { af: 't' } : {};
30
32
  const contentTargeting = (0, content_1.getContentTargeting)({
@@ -44,7 +46,7 @@ const buildPageTargeting = (consentState, adFree, clientSideParticipations) => {
44
46
  isSignedIn: !!(0, libs_1.getCookie)({ name: 'GU_U' }),
45
47
  pageViewId: window.guardian.config.ophan.pageViewId,
46
48
  participations: {
47
- clientSideParticipations,
49
+ clientSideParticipations: (0, ab_localstorage_1.getParticipationsFromLocalStorage)(),
48
50
  serverSideParticipations: window.guardian.config.tests ?? {},
49
51
  },
50
52
  referrer: getReferrer(),
@@ -71,8 +73,8 @@ const buildPageTargeting = (consentState, adFree, clientSideParticipations) => {
71
73
  ...sessionTargeting,
72
74
  ...viewportTargeting,
73
75
  };
74
- // filter out empty values
75
- const pageTargeting = filterEmptyValues(pageTargets);
76
+ // filter !(string | string[]) and empty values
77
+ const pageTargeting = filterValues(pageTargets);
76
78
  return pageTargeting;
77
79
  };
78
80
  exports.buildPageTargeting = buildPageTargeting;
@@ -97,6 +97,7 @@ declare type AllParticipations = {
97
97
  [key: `${string}Variant`]: 'variant';
98
98
  };
99
99
  };
100
+ declare const experimentsTargeting: ({ clientSideParticipations, serverSideParticipations, }: AllParticipations) => SessionTargeting['ab'];
100
101
  declare type Session = {
101
102
  adTest: SessionTargeting['at'];
102
103
  countryCode: CountryCode;
@@ -107,4 +108,4 @@ declare type Session = {
107
108
  };
108
109
  declare const getSessionTargeting: ({ adTest, countryCode, isSignedIn, pageViewId, participations, referrer, }: Session) => SessionTargeting;
109
110
  export type { SessionTargeting, AllParticipations };
110
- export { getSessionTargeting };
111
+ export { getSessionTargeting, experimentsTargeting };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getSessionTargeting = void 0;
3
+ exports.experimentsTargeting = exports.getSessionTargeting = void 0;
4
4
  const libs_1 = require("@guardian/libs");
5
5
  /* -- Types -- */
6
6
  const referrers = [
@@ -49,6 +49,7 @@ const experimentsTargeting = ({ clientSideParticipations, serverSideParticipatio
49
49
  }
50
50
  return [...clientSideExperiment, ...serverSideExperiments];
51
51
  };
52
+ exports.experimentsTargeting = experimentsTargeting;
52
53
  const getSessionTargeting = ({ adTest, countryCode, isSignedIn, pageViewId, participations, referrer, }) => ({
53
54
  ab: experimentsTargeting(participations),
54
55
  at: adTest,
@@ -0,0 +1,3 @@
1
+ import type { CustomParams } from '../types';
2
+ declare const buildImaAdTagUrl: (adUnit: string, customParams: CustomParams) => string;
3
+ export { buildImaAdTagUrl };
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildImaAdTagUrl = void 0;
4
+ const build_page_targeting_1 = require("./build-page-targeting");
5
+ /**
6
+ * @param {Record<string, MaybeArray<string|number|boolean>>
7
+ * Follows https://support.google.com/admanager/answer/1080597
8
+ */
9
+ const encodeVastTagKeyValues = (query) => {
10
+ const unencodedUrl = Object.entries(query)
11
+ .map(([key, value]) => {
12
+ const queryValue = Array.isArray(value)
13
+ ? value.join(',')
14
+ : String(value);
15
+ return `${key}=${queryValue}`;
16
+ })
17
+ .join('&');
18
+ return unencodedUrl
19
+ .replace(/=/g, '%3D')
20
+ .replace(/&/g, '%26')
21
+ .replace(/,/g, '%2C');
22
+ };
23
+ const buildImaAdTagUrl = (adUnit, customParams) => {
24
+ // TODO: Add regular build-page-targeting to
25
+ // get all targeting including ab participations.
26
+ // For now hardcode adtest (at)
27
+ const mergedCustomParams = { ...customParams, at: 'fixed-puppies' };
28
+ const queryParams = {
29
+ iu: adUnit,
30
+ description_url: '[placeholder]',
31
+ tfcd: '0',
32
+ npa: '0',
33
+ sz: '480x360|480x361|400x300',
34
+ gdfp_req: '1',
35
+ output: 'vast',
36
+ unviewed_position_start: '1',
37
+ env: 'vp',
38
+ impl: 's',
39
+ correlator: '',
40
+ vad_type: 'linear',
41
+ vpos: 'preroll',
42
+ cust_params: encodeVastTagKeyValues((0, build_page_targeting_1.filterValues)(mergedCustomParams)),
43
+ };
44
+ const queryParamsArray = [];
45
+ for (const [k, v] of Object.entries(queryParams)) {
46
+ queryParamsArray.push(`${k}=${v}`);
47
+ }
48
+ return ('https://pubads.g.doubleclick.net/gampad/ads?' +
49
+ queryParamsArray.join('&'));
50
+ };
51
+ exports.buildImaAdTagUrl = buildImaAdTagUrl;
@@ -1,6 +1,5 @@
1
1
  import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
2
- import type { AdsConfig, AdsConfigDisabled, MaybeArray } from './types';
3
- declare type CustomParams = Record<string, MaybeArray<string | number | boolean>>;
2
+ import type { AdsConfig, AdsConfigDisabled, CustomParams } from './types';
4
3
  declare const disabledAds: AdsConfigDisabled;
5
4
  declare const buildAdsConfigWithConsent: (isAdFreeUser: boolean, adUnit: string, customParamsToMerge: CustomParams, consentState: ConsentState) => AdsConfig;
6
5
  export { buildAdsConfigWithConsent, disabledAds };
@@ -40,3 +40,4 @@ export { a9Apstag } from './__vendor/a9-apstag';
40
40
  export { ipsosMoriStub } from './__vendor/ipsos-mori';
41
41
  export { launchpad } from './__vendor/launchpad';
42
42
  export { pubmatic } from './__vendor/pubmatic';
43
+ export { buildImaAdTagUrl } from './targeting/youtube-ima';
package/dist/esm/index.js CHANGED
@@ -32,3 +32,4 @@ export { a9Apstag } from './__vendor/a9-apstag';
32
32
  export { ipsosMoriStub } from './__vendor/ipsos-mori';
33
33
  export { launchpad } from './__vendor/launchpad';
34
34
  export { pubmatic } from './__vendor/pubmatic';
35
+ export { buildImaAdTagUrl } from './targeting/youtube-ima';
@@ -0,0 +1,2 @@
1
+ import type { Participations } from '@guardian/ab-core';
2
+ export declare const getParticipationsFromLocalStorage: () => Participations;
@@ -0,0 +1,10 @@
1
+ import { isObject, isString, storage } from '@guardian/libs';
2
+ const participationsKey = 'gu.ab.participations';
3
+ const isParticipations = (participations) => {
4
+ return (isObject(participations) &&
5
+ Object.values(participations).every((participation) => isObject(participation) && isString(participation.variant)));
6
+ };
7
+ export const getParticipationsFromLocalStorage = () => {
8
+ const participations = storage.local.get(participationsKey);
9
+ return isParticipations(participations) ? participations : {};
10
+ };
@@ -1,4 +1,3 @@
1
- import type { Participations } from '@guardian/ab-core';
2
1
  import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
3
2
  import type { PageTargeting } from './build-page-targeting';
4
3
  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"];
@@ -10,8 +9,7 @@ declare type ConsentlessPageTargeting = Partial<Pick<PageTargeting, ConsentlessT
10
9
  *
11
10
  * @param {ConsentState} consentState
12
11
  * @param {boolean} adFree
13
- * @param {Participations} clientSideParticipations
14
12
  * @returns ConsentlessPageTargeting
15
13
  */
16
- declare const buildPageTargetingConsentless: (consentState: ConsentState, adFree: boolean, clientSideParticipations: Participations) => ConsentlessPageTargeting;
14
+ declare const buildPageTargetingConsentless: (consentState: ConsentState, adFree: boolean) => ConsentlessPageTargeting;
17
15
  export { buildPageTargetingConsentless };
@@ -29,11 +29,10 @@ const isConsentlessKey = (key) => consentlessTargetingKeys.includes(key);
29
29
  *
30
30
  * @param {ConsentState} consentState
31
31
  * @param {boolean} adFree
32
- * @param {Participations} clientSideParticipations
33
32
  * @returns ConsentlessPageTargeting
34
33
  */
35
- const buildPageTargetingConsentless = (consentState, adFree, clientSideParticipations) => {
36
- const consentedPageTargeting = buildPageTargeting(consentState, adFree, clientSideParticipations);
34
+ const buildPageTargetingConsentless = (consentState, adFree) => {
35
+ const consentedPageTargeting = buildPageTargeting(consentState, adFree);
37
36
  return Object.fromEntries(Object.entries(consentedPageTargeting).filter(([k]) => isConsentlessKey(k)));
38
37
  };
39
38
  export { buildPageTargetingConsentless };
@@ -1,4 +1,3 @@
1
- import type { Participations } from '@guardian/ab-core';
2
1
  import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
3
2
  import type { CountryCode } from '@guardian/libs';
4
3
  import type { False, True } from '../types';
@@ -34,6 +33,7 @@ declare type PageTargeting = PartialWithNulls<{
34
33
  vl: string;
35
34
  [_: string]: string | string[];
36
35
  } & SharedTargeting>;
37
- declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean, clientSideParticipations: Participations) => PageTargeting;
38
- export { buildPageTargeting };
36
+ declare const filterValues: (pageTargets: Record<string, unknown>) => Record<string, string | string[]>;
37
+ declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean) => PageTargeting;
38
+ export { buildPageTargeting, filterValues };
39
39
  export type { PageTargeting };
@@ -1,12 +1,13 @@
1
1
  import { cmp } from '@guardian/consent-management-platform';
2
2
  import { getCookie, isString } from '@guardian/libs';
3
+ import { getParticipationsFromLocalStorage } from '../lib/ab-localstorage';
3
4
  import { getLocale } from '../lib/get-locale';
4
5
  import { getContentTargeting } from './content';
5
6
  import { getPersonalisedTargeting } from './personalised';
6
7
  import { getSessionTargeting } from './session';
7
8
  import { getSharedTargeting } from './shared';
8
9
  import { getViewportTargeting } from './viewport';
9
- const filterEmptyValues = (pageTargets) => {
10
+ const filterValues = (pageTargets) => {
10
11
  const filtered = {};
11
12
  for (const key in pageTargets) {
12
13
  const value = pageTargets[key];
@@ -21,7 +22,7 @@ const filterEmptyValues = (pageTargets) => {
21
22
  }
22
23
  return filtered;
23
24
  };
24
- const buildPageTargeting = (consentState, adFree, clientSideParticipations) => {
25
+ const buildPageTargeting = (consentState, adFree) => {
25
26
  const { page, isDotcomRendering } = window.guardian.config;
26
27
  const adFreeTargeting = adFree ? { af: 't' } : {};
27
28
  const contentTargeting = getContentTargeting({
@@ -41,7 +42,7 @@ const buildPageTargeting = (consentState, adFree, clientSideParticipations) => {
41
42
  isSignedIn: !!getCookie({ name: 'GU_U' }),
42
43
  pageViewId: window.guardian.config.ophan.pageViewId,
43
44
  participations: {
44
- clientSideParticipations,
45
+ clientSideParticipations: getParticipationsFromLocalStorage(),
45
46
  serverSideParticipations: window.guardian.config.tests ?? {},
46
47
  },
47
48
  referrer: getReferrer(),
@@ -68,8 +69,8 @@ const buildPageTargeting = (consentState, adFree, clientSideParticipations) => {
68
69
  ...sessionTargeting,
69
70
  ...viewportTargeting,
70
71
  };
71
- // filter out empty values
72
- const pageTargeting = filterEmptyValues(pageTargets);
72
+ // filter !(string | string[]) and empty values
73
+ const pageTargeting = filterValues(pageTargets);
73
74
  return pageTargeting;
74
75
  };
75
- export { buildPageTargeting };
76
+ export { buildPageTargeting, filterValues };
@@ -97,6 +97,7 @@ declare type AllParticipations = {
97
97
  [key: `${string}Variant`]: 'variant';
98
98
  };
99
99
  };
100
+ declare const experimentsTargeting: ({ clientSideParticipations, serverSideParticipations, }: AllParticipations) => SessionTargeting['ab'];
100
101
  declare type Session = {
101
102
  adTest: SessionTargeting['at'];
102
103
  countryCode: CountryCode;
@@ -107,4 +108,4 @@ declare type Session = {
107
108
  };
108
109
  declare const getSessionTargeting: ({ adTest, countryCode, isSignedIn, pageViewId, participations, referrer, }: Session) => SessionTargeting;
109
110
  export type { SessionTargeting, AllParticipations };
110
- export { getSessionTargeting };
111
+ export { getSessionTargeting, experimentsTargeting };
@@ -54,4 +54,4 @@ const getSessionTargeting = ({ adTest, countryCode, isSignedIn, pageViewId, part
54
54
  ref: getReferrer(referrer),
55
55
  si: isSignedIn ? 't' : 'f',
56
56
  });
57
- export { getSessionTargeting };
57
+ export { getSessionTargeting, experimentsTargeting };
@@ -0,0 +1,3 @@
1
+ import type { CustomParams } from '../types';
2
+ declare const buildImaAdTagUrl: (adUnit: string, customParams: CustomParams) => string;
3
+ export { buildImaAdTagUrl };
@@ -0,0 +1,48 @@
1
+ import { filterValues } from './build-page-targeting';
2
+ /**
3
+ * @param {Record<string, MaybeArray<string|number|boolean>>
4
+ * Follows https://support.google.com/admanager/answer/1080597
5
+ */
6
+ const encodeVastTagKeyValues = (query) => {
7
+ const unencodedUrl = Object.entries(query)
8
+ .map(([key, value]) => {
9
+ const queryValue = Array.isArray(value)
10
+ ? value.join(',')
11
+ : String(value);
12
+ return `${key}=${queryValue}`;
13
+ })
14
+ .join('&');
15
+ return unencodedUrl
16
+ .replace(/=/g, '%3D')
17
+ .replace(/&/g, '%26')
18
+ .replace(/,/g, '%2C');
19
+ };
20
+ const buildImaAdTagUrl = (adUnit, customParams) => {
21
+ // TODO: Add regular build-page-targeting to
22
+ // get all targeting including ab participations.
23
+ // For now hardcode adtest (at)
24
+ const mergedCustomParams = { ...customParams, at: 'fixed-puppies' };
25
+ const queryParams = {
26
+ iu: adUnit,
27
+ description_url: '[placeholder]',
28
+ tfcd: '0',
29
+ npa: '0',
30
+ sz: '480x360|480x361|400x300',
31
+ gdfp_req: '1',
32
+ output: 'vast',
33
+ unviewed_position_start: '1',
34
+ env: 'vp',
35
+ impl: 's',
36
+ correlator: '',
37
+ vad_type: 'linear',
38
+ vpos: 'preroll',
39
+ cust_params: encodeVastTagKeyValues(filterValues(mergedCustomParams)),
40
+ };
41
+ const queryParamsArray = [];
42
+ for (const [k, v] of Object.entries(queryParams)) {
43
+ queryParamsArray.push(`${k}=${v}`);
44
+ }
45
+ return ('https://pubads.g.doubleclick.net/gampad/ads?' +
46
+ queryParamsArray.join('&'));
47
+ };
48
+ export { buildImaAdTagUrl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/commercial-core",
3
- "version": "4.19.0",
3
+ "version": "4.21.0",
4
4
  "description": "Guardian advertising business logic",
5
5
  "homepage": "https://github.com/guardian/commercial-core#readme",
6
6
  "bugs": {