@guardian/commercial-core 4.20.0 → 4.22.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.
@@ -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 };
@@ -35,7 +35,7 @@ interface EventTimerProperties {
35
35
  hasLabsContainer?: boolean;
36
36
  labsUrl?: string;
37
37
  }
38
- export declare class EventTimer {
38
+ declare class EventTimer {
39
39
  private _events;
40
40
  private static _externallyDefinedEventNames;
41
41
  startTS: DOMHighResTimeStamp;
@@ -87,4 +87,4 @@ export declare class EventTimer {
87
87
  private mark;
88
88
  private trackInGA;
89
89
  }
90
- export {};
90
+ export { EventTimer };
@@ -2,11 +2,15 @@
2
2
  import type { GoogleTagParams, GoogleTrackConversionObject, GuardianWindowConfig } from './types';
3
3
  import type { EventTimer } from '.';
4
4
  declare global {
5
+ type ConnectionType = 'bluetooth' | 'cellular' | 'ethernet' | 'mixed' | 'none' | 'other' | 'unknown' | 'wifi';
5
6
  interface NetworkInformation extends EventTarget {
6
- readonly type: ConnectionType;
7
+ readonly type?: ConnectionType;
7
8
  readonly downlink?: number;
8
9
  readonly effectiveType?: string;
9
10
  }
11
+ interface Navigator {
12
+ readonly connection: NetworkInformation;
13
+ }
10
14
  interface Window {
11
15
  google_trackConversion?: (arg0: GoogleTrackConversionObject) => void;
12
16
  google_tag_params?: GoogleTagParams;
@@ -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; } });
@@ -33,6 +33,7 @@ declare type PageTargeting = PartialWithNulls<{
33
33
  vl: string;
34
34
  [_: string]: string | string[];
35
35
  } & SharedTargeting>;
36
+ declare const filterValues: (pageTargets: Record<string, unknown>) => Record<string, string | string[]>;
36
37
  declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean) => PageTargeting;
37
- export { buildPageTargeting };
38
+ export { buildPageTargeting, filterValues };
38
39
  export type { PageTargeting };
@@ -1,6 +1,6 @@
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
6
  const ab_localstorage_1 = require("../lib/ab-localstorage");
@@ -10,7 +10,7 @@ const personalised_1 = require("./personalised");
10
10
  const session_1 = require("./session");
11
11
  const shared_1 = require("./shared");
12
12
  const viewport_1 = require("./viewport");
13
- const filterEmptyValues = (pageTargets) => {
13
+ const filterValues = (pageTargets) => {
14
14
  const filtered = {};
15
15
  for (const key in pageTargets) {
16
16
  const value = pageTargets[key];
@@ -25,6 +25,7 @@ const filterEmptyValues = (pageTargets) => {
25
25
  }
26
26
  return filtered;
27
27
  };
28
+ exports.filterValues = filterValues;
28
29
  const buildPageTargeting = (consentState, adFree) => {
29
30
  const { page, isDotcomRendering } = window.guardian.config;
30
31
  const adFreeTargeting = adFree ? { af: 't' } : {};
@@ -72,8 +73,8 @@ const buildPageTargeting = (consentState, adFree) => {
72
73
  ...sessionTargeting,
73
74
  ...viewportTargeting,
74
75
  };
75
- // filter out empty values
76
- const pageTargeting = filterEmptyValues(pageTargets);
76
+ // filter !(string | string[]) and empty values
77
+ const pageTargeting = filterValues(pageTargets);
77
78
  return pageTargeting;
78
79
  };
79
80
  exports.buildPageTargeting = buildPageTargeting;
@@ -21,5 +21,5 @@ declare type ValidTargetingObject<Base> = ConditionalExcept<Base, null | undefin
21
21
  * clean.invalid
22
22
  * ```
23
23
  */
24
- export declare const pickTargetingValues: <T extends Record<string, string | readonly string[] | undefined>>(obj: T) => import("type-fest").Except<T, NonNullable<{ [Key in keyof T]: T[Key] extends number | boolean | "" | never[] | readonly [] | readonly [""] | null | undefined ? Key : never; }[keyof T]>>;
24
+ export declare const pickTargetingValues: <T extends Record<string, string | readonly string[] | undefined>>(obj: T) => import("type-fest").Except<T, import("type-fest").ConditionalKeys<T, number | boolean | "" | never[] | readonly [] | readonly [""] | null | undefined>>;
25
25
  export {};
@@ -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,4 @@
1
+ import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
2
+ import type { CustomParams } from '../types';
3
+ declare const buildImaAdTagUrl: (adUnit: string, customParams: CustomParams, consentState: ConsentState) => string;
4
+ export { buildImaAdTagUrl };
@@ -0,0 +1,49 @@
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 encodeCustomParams = (params) => {
10
+ const encodedParams = Object.entries(params)
11
+ .map(([key, value]) => {
12
+ const queryValue = Array.isArray(value)
13
+ ? value.join(',')
14
+ : String(value);
15
+ return `${key}=${encodeURIComponent(queryValue)}`;
16
+ })
17
+ .join('&');
18
+ return encodedParams;
19
+ };
20
+ const buildImaAdTagUrl = (adUnit, customParams, consentState) => {
21
+ const pageTargeting = (0, build_page_targeting_1.buildPageTargeting)(consentState, false);
22
+ const mergedCustomParams = { ...customParams, ...pageTargeting };
23
+ const queryParams = {
24
+ iu: adUnit,
25
+ tfcd: '0',
26
+ npa: '0',
27
+ sz: '480x360|480x361|400x300',
28
+ gdfp_req: '1',
29
+ output: 'vast',
30
+ unviewed_position_start: '1',
31
+ env: 'vp',
32
+ impl: 's',
33
+ vad_type: 'linear',
34
+ vpos: 'preroll',
35
+ /**
36
+ * cust_params string is encoded
37
+ * cust_params values are also encoded so they will get double encoded
38
+ * this ensures any values with separator chars (=&,) do not conflict with the main string
39
+ */
40
+ cust_params: encodeURIComponent(encodeCustomParams((0, build_page_targeting_1.filterValues)(mergedCustomParams))),
41
+ };
42
+ const queryParamsArray = [];
43
+ for (const [k, v] of Object.entries(queryParams)) {
44
+ queryParamsArray.push(`${k}=${v}`);
45
+ }
46
+ return ('https://pubads.g.doubleclick.net/gampad/ads?' +
47
+ queryParamsArray.join('&'));
48
+ };
49
+ 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 };
@@ -35,7 +35,7 @@ interface EventTimerProperties {
35
35
  hasLabsContainer?: boolean;
36
36
  labsUrl?: string;
37
37
  }
38
- export declare class EventTimer {
38
+ declare class EventTimer {
39
39
  private _events;
40
40
  private static _externallyDefinedEventNames;
41
41
  startTS: DOMHighResTimeStamp;
@@ -87,4 +87,4 @@ export declare class EventTimer {
87
87
  private mark;
88
88
  private trackInGA;
89
89
  }
90
- export {};
90
+ export { EventTimer };
@@ -5,7 +5,7 @@ class Event {
5
5
  this.ts = mark.startTime;
6
6
  }
7
7
  }
8
- export class EventTimer {
8
+ class EventTimer {
9
9
  constructor() {
10
10
  this._events = [];
11
11
  this.startTS = window.performance.now();
@@ -177,3 +177,4 @@ EventTimer._externallyDefinedEventNames = [
177
177
  'cmp-aus-ui-displayed',
178
178
  'cmp-aus-got-consent',
179
179
  ];
180
+ export { EventTimer };
@@ -2,11 +2,15 @@
2
2
  import type { GoogleTagParams, GoogleTrackConversionObject, GuardianWindowConfig } from './types';
3
3
  import type { EventTimer } from '.';
4
4
  declare global {
5
+ type ConnectionType = 'bluetooth' | 'cellular' | 'ethernet' | 'mixed' | 'none' | 'other' | 'unknown' | 'wifi';
5
6
  interface NetworkInformation extends EventTarget {
6
- readonly type: ConnectionType;
7
+ readonly type?: ConnectionType;
7
8
  readonly downlink?: number;
8
9
  readonly effectiveType?: string;
9
10
  }
11
+ interface Navigator {
12
+ readonly connection: NetworkInformation;
13
+ }
10
14
  interface Window {
11
15
  google_trackConversion?: (arg0: GoogleTrackConversionObject) => void;
12
16
  google_tag_params?: GoogleTagParams;
@@ -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';
@@ -33,6 +33,7 @@ declare type PageTargeting = PartialWithNulls<{
33
33
  vl: string;
34
34
  [_: string]: string | string[];
35
35
  } & SharedTargeting>;
36
+ declare const filterValues: (pageTargets: Record<string, unknown>) => Record<string, string | string[]>;
36
37
  declare const buildPageTargeting: (consentState: ConsentState, adFree: boolean) => PageTargeting;
37
- export { buildPageTargeting };
38
+ export { buildPageTargeting, filterValues };
38
39
  export type { PageTargeting };
@@ -7,7 +7,7 @@ import { getPersonalisedTargeting } from './personalised';
7
7
  import { getSessionTargeting } from './session';
8
8
  import { getSharedTargeting } from './shared';
9
9
  import { getViewportTargeting } from './viewport';
10
- const filterEmptyValues = (pageTargets) => {
10
+ const filterValues = (pageTargets) => {
11
11
  const filtered = {};
12
12
  for (const key in pageTargets) {
13
13
  const value = pageTargets[key];
@@ -69,8 +69,8 @@ const buildPageTargeting = (consentState, adFree) => {
69
69
  ...sessionTargeting,
70
70
  ...viewportTargeting,
71
71
  };
72
- // filter out empty values
73
- const pageTargeting = filterEmptyValues(pageTargets);
72
+ // filter !(string | string[]) and empty values
73
+ const pageTargeting = filterValues(pageTargets);
74
74
  return pageTargeting;
75
75
  };
76
- export { buildPageTargeting };
76
+ export { buildPageTargeting, filterValues };
@@ -21,5 +21,5 @@ declare type ValidTargetingObject<Base> = ConditionalExcept<Base, null | undefin
21
21
  * clean.invalid
22
22
  * ```
23
23
  */
24
- export declare const pickTargetingValues: <T extends Record<string, string | readonly string[] | undefined>>(obj: T) => import("type-fest").Except<T, NonNullable<{ [Key in keyof T]: T[Key] extends number | boolean | "" | never[] | readonly [] | readonly [""] | null | undefined ? Key : never; }[keyof T]>>;
24
+ export declare const pickTargetingValues: <T extends Record<string, string | readonly string[] | undefined>>(obj: T) => import("type-fest").Except<T, import("type-fest").ConditionalKeys<T, number | boolean | "" | never[] | readonly [] | readonly [""] | null | undefined>>;
25
25
  export {};
@@ -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,4 @@
1
+ import type { ConsentState } from '@guardian/consent-management-platform/dist/types';
2
+ import type { CustomParams } from '../types';
3
+ declare const buildImaAdTagUrl: (adUnit: string, customParams: CustomParams, consentState: ConsentState) => string;
4
+ export { buildImaAdTagUrl };
@@ -0,0 +1,46 @@
1
+ import { buildPageTargeting, 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 encodeCustomParams = (params) => {
7
+ const encodedParams = Object.entries(params)
8
+ .map(([key, value]) => {
9
+ const queryValue = Array.isArray(value)
10
+ ? value.join(',')
11
+ : String(value);
12
+ return `${key}=${encodeURIComponent(queryValue)}`;
13
+ })
14
+ .join('&');
15
+ return encodedParams;
16
+ };
17
+ const buildImaAdTagUrl = (adUnit, customParams, consentState) => {
18
+ const pageTargeting = buildPageTargeting(consentState, false);
19
+ const mergedCustomParams = { ...customParams, ...pageTargeting };
20
+ const queryParams = {
21
+ iu: adUnit,
22
+ tfcd: '0',
23
+ npa: '0',
24
+ sz: '480x360|480x361|400x300',
25
+ gdfp_req: '1',
26
+ output: 'vast',
27
+ unviewed_position_start: '1',
28
+ env: 'vp',
29
+ impl: 's',
30
+ vad_type: 'linear',
31
+ vpos: 'preroll',
32
+ /**
33
+ * cust_params string is encoded
34
+ * cust_params values are also encoded so they will get double encoded
35
+ * this ensures any values with separator chars (=&,) do not conflict with the main string
36
+ */
37
+ cust_params: encodeURIComponent(encodeCustomParams(filterValues(mergedCustomParams))),
38
+ };
39
+ const queryParamsArray = [];
40
+ for (const [k, v] of Object.entries(queryParams)) {
41
+ queryParamsArray.push(`${k}=${v}`);
42
+ }
43
+ return ('https://pubads.g.doubleclick.net/gampad/ads?' +
44
+ queryParamsArray.join('&'));
45
+ };
46
+ export { buildImaAdTagUrl };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/commercial-core",
3
- "version": "4.20.0",
3
+ "version": "4.22.0",
4
4
  "description": "Guardian advertising business logic",
5
5
  "homepage": "https://github.com/guardian/commercial-core#readme",
6
6
  "bugs": {