@guardian/commercial-core 4.4.0 → 4.5.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,3 +1,4 @@
1
+ import type { Breakpoint } from './lib/breakpoint';
1
2
  declare type AdSizeString = 'fluid' | `${number},${number}`;
2
3
  /**
3
4
  * Store ad sizes in a way that is compatible with google-tag but also accessible via
@@ -26,7 +27,6 @@ declare class AdSize extends Array<number> {
26
27
  }
27
28
  declare type SizeKeys = '160x600' | '300x1050' | '300x250' | '300x600' | '728x90' | '970x250' | 'billboard' | 'empty' | 'fabric' | 'fluid' | 'googleCard' | 'halfPage' | 'inlineMerchandising' | 'leaderboard' | 'merchandising' | 'merchandisingHigh' | 'merchandisingHighAdFeature' | 'mobilesticky' | 'mpu' | 'outOfPage' | 'outstreamDesktop' | 'outstreamGoogleDesktop' | 'outstreamMobile' | 'portrait' | 'skyscraper';
28
29
  declare type SlotName = 'right' | 'comments' | 'top-above-nav' | 'mostpop' | 'merchandising' | 'merchandising-high' | 'merchandising-high-lucky' | 'survey' | 'im' | 'inline' | 'mostpop' | 'comments' | 'top-above-nav' | 'carrot' | 'epic' | 'mobile-sticky' | 'crossword-banner';
29
- declare type Breakpoint = 'mobile' | 'desktop' | 'phablet' | 'tablet';
30
30
  declare type SizeMapping = Partial<Record<Breakpoint, AdSize[]>>;
31
31
  declare type SlotSizeMappings = Record<SlotName, SizeMapping>;
32
32
  declare const createAdSize: (width: number, height: number) => AdSize;
@@ -1,9 +1,18 @@
1
- import type { AdSize } from './ad-sizes';
1
+ import type { AdSize, SizeMapping } from './ad-sizes';
2
2
  declare type SlotName = 'im' | 'high-merch' | 'high-merch-lucky' | 'high-merch-paid' | 'inline' | 'mostpop' | 'comments' | 'top-above-nav' | 'carrot' | 'epic' | 'mobile-sticky';
3
3
  declare type CreateSlotOptions = {
4
4
  classes?: string;
5
5
  name?: string;
6
6
  sizes?: Record<string, AdSize[]>;
7
7
  };
8
- export declare const createAdSlot: (name: SlotName, options?: CreateSlotOptions) => HTMLElement;
9
- export {};
8
+ /**
9
+ * Given default size mappings and additional size mappings from
10
+ * the createAdSlot options parameter.
11
+ *
12
+ * 1. Check that the options size mappings use known device names
13
+ * 2. If so concat the size mappings
14
+ *
15
+ */
16
+ declare const concatSizeMappings: (defaultSizeMappings: SizeMapping, optionSizeMappings?: SizeMapping) => SizeMapping;
17
+ declare const createAdSlot: (name: SlotName, options?: CreateSlotOptions) => HTMLElement;
18
+ export { createAdSlot, concatSizeMappings };
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createAdSlot = void 0;
3
+ exports.concatSizeMappings = exports.createAdSlot = void 0;
4
4
  const ad_sizes_1 = require("./ad-sizes");
5
+ const breakpoint_1 = require("./lib/breakpoint");
5
6
  const adSlotIdPrefix = 'dfp-ad--';
6
7
  const adSlotConfigs = {
7
8
  im: {
@@ -102,24 +103,15 @@ const createClasses = (slotName, classes) => [...(classes?.split(' ') ?? []), sl
102
103
  * 2. If so concat the size mappings
103
104
  *
104
105
  */
105
- const concatSizeMappings = (defaultSizeMappings, optionSizeMappings) => {
106
- if (!optionSizeMappings)
107
- return defaultSizeMappings;
108
- const concatenatedSizeMappings = { ...defaultSizeMappings };
109
- const optionDevices = Object.keys(optionSizeMappings); // ['mobile', 'desktop']
110
- for (let i = 0; i < optionDevices.length; i++) {
111
- const device = optionDevices[i];
112
- if (optionSizeMappings[device] && defaultSizeMappings[device]) {
113
- const optionSizeMappingsForDevice = optionSizeMappings[device];
114
- const defaultSizeMappingsForDevice = defaultSizeMappings[device];
115
- if (defaultSizeMappingsForDevice && optionSizeMappingsForDevice) {
116
- // TODO can we do concatenatedSizeMappings[device]?.concat ?
117
- concatenatedSizeMappings[device] = (concatenatedSizeMappings[device] ?? []).concat(optionSizeMappingsForDevice);
118
- }
119
- }
106
+ const concatSizeMappings = (defaultSizeMappings, optionSizeMappings = {}) => Object.entries(optionSizeMappings).reduce((sizeMappings, [breakpoint, optionSizes]) => {
107
+ // Only perform concatenation if breakpoint is of the correct type
108
+ if ((0, breakpoint_1.isBreakpoint)(breakpoint)) {
109
+ // Concatenate the option sizes onto any existing sizes present for a given breakpoint
110
+ sizeMappings[breakpoint] = (sizeMappings[breakpoint] ?? []).concat(optionSizes);
120
111
  }
121
- return concatenatedSizeMappings;
122
- };
112
+ return sizeMappings;
113
+ }, { ...defaultSizeMappings });
114
+ exports.concatSizeMappings = concatSizeMappings;
123
115
  /**
124
116
  * Convert size mappings to a string that will be added to the ad slot
125
117
  * data attributes.
@@ -8,13 +8,15 @@ export { EventTimer } from './event-timer';
8
8
  export { bypassCommercialMetricsSampling, initCommercialMetrics, } from './send-commercial-metrics';
9
9
  export type { ThirdPartyTag } from './types';
10
10
  export { adSizes, getAdSize, slotSizeMappings, createAdSize } from './ad-sizes';
11
+ export { isBreakpoint } from './lib/breakpoint';
12
+ export type { Breakpoint } from './lib/breakpoint';
11
13
  export type { SizeKeys, AdSizeString, AdSize, SizeMapping, SlotSizeMappings, SlotName, } from './ad-sizes';
12
14
  export { isAdBlockInUse } from './detect-ad-blocker';
13
15
  export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments, } from './permutive';
14
16
  export { initTrackScrollDepth } from './track-scroll-depth';
15
17
  export { initTrackGpcSignal } from './track-gpc-signal';
16
18
  export { buildAdsConfigWithConsent, disabledAds } from './ad-targeting-youtube';
17
- export { createAdSlot } from './create-ad-slot';
19
+ export { createAdSlot, concatSizeMappings } from './create-ad-slot';
18
20
  export type { AdsConfig, AdsConfigBasic, AdsConfigDisabled, AdTargetingBuilder, CustomParams, } from './types';
19
21
  export * as constants from './constants';
20
22
  export type { ContentTargeting } from './targeting/content';
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.pickTargetingValues = exports.getViewportTargeting = exports.getSharedTargeting = exports.getSessionTargeting = exports.getPersonalisedTargeting = exports.getContentTargeting = exports.constants = exports.createAdSlot = exports.disabledAds = exports.buildAdsConfigWithConsent = exports.initTrackGpcSignal = exports.initTrackScrollDepth = exports.getPermutivePFPSegments = exports.getPermutiveSegments = exports.clearPermutiveSegments = exports.isAdBlockInUse = exports.createAdSize = exports.slotSizeMappings = exports.getAdSize = exports.adSizes = exports.initCommercialMetrics = exports.bypassCommercialMetricsSampling = exports.EventTimer = exports.remarketing = exports.inizio = exports.twitter = exports.fbPixel = exports.permutive = exports.ias = void 0;
27
+ 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.initTrackScrollDepth = exports.getPermutivePFPSegments = exports.getPermutiveSegments = exports.clearPermutiveSegments = exports.isAdBlockInUse = exports.isBreakpoint = exports.createAdSize = exports.slotSizeMappings = exports.getAdSize = exports.adSizes = exports.initCommercialMetrics = exports.bypassCommercialMetricsSampling = exports.EventTimer = exports.remarketing = exports.inizio = exports.twitter = exports.fbPixel = 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");
@@ -47,6 +47,8 @@ Object.defineProperty(exports, "adSizes", { enumerable: true, get: function () {
47
47
  Object.defineProperty(exports, "getAdSize", { enumerable: true, get: function () { return ad_sizes_1.getAdSize; } });
48
48
  Object.defineProperty(exports, "slotSizeMappings", { enumerable: true, get: function () { return ad_sizes_1.slotSizeMappings; } });
49
49
  Object.defineProperty(exports, "createAdSize", { enumerable: true, get: function () { return ad_sizes_1.createAdSize; } });
50
+ var breakpoint_1 = require("./lib/breakpoint");
51
+ Object.defineProperty(exports, "isBreakpoint", { enumerable: true, get: function () { return breakpoint_1.isBreakpoint; } });
50
52
  var detect_ad_blocker_1 = require("./detect-ad-blocker");
51
53
  Object.defineProperty(exports, "isAdBlockInUse", { enumerable: true, get: function () { return detect_ad_blocker_1.isAdBlockInUse; } });
52
54
  var permutive_2 = require("./permutive");
@@ -62,6 +64,7 @@ Object.defineProperty(exports, "buildAdsConfigWithConsent", { enumerable: true,
62
64
  Object.defineProperty(exports, "disabledAds", { enumerable: true, get: function () { return ad_targeting_youtube_1.disabledAds; } });
63
65
  var create_ad_slot_1 = require("./create-ad-slot");
64
66
  Object.defineProperty(exports, "createAdSlot", { enumerable: true, get: function () { return create_ad_slot_1.createAdSlot; } });
67
+ Object.defineProperty(exports, "concatSizeMappings", { enumerable: true, get: function () { return create_ad_slot_1.concatSizeMappings; } });
65
68
  exports.constants = __importStar(require("./constants"));
66
69
  var content_1 = require("./targeting/content");
67
70
  Object.defineProperty(exports, "getContentTargeting", { enumerable: true, get: function () { return content_1.getContentTargeting; } });
@@ -0,0 +1,4 @@
1
+ declare type Breakpoint = 'mobile' | 'desktop' | 'phablet' | 'tablet';
2
+ declare const isBreakpoint: (s: string) => s is Breakpoint;
3
+ export type { Breakpoint };
4
+ export { isBreakpoint };
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isBreakpoint = void 0;
4
+ const isBreakpoint = (s) => s === 'mobile' || s === 'phablet' || s === 'tablet' || s === 'desktop';
5
+ exports.isBreakpoint = isBreakpoint;
@@ -1,3 +1,4 @@
1
+ import type { Breakpoint } from './lib/breakpoint';
1
2
  declare type AdSizeString = 'fluid' | `${number},${number}`;
2
3
  /**
3
4
  * Store ad sizes in a way that is compatible with google-tag but also accessible via
@@ -26,7 +27,6 @@ declare class AdSize extends Array<number> {
26
27
  }
27
28
  declare type SizeKeys = '160x600' | '300x1050' | '300x250' | '300x600' | '728x90' | '970x250' | 'billboard' | 'empty' | 'fabric' | 'fluid' | 'googleCard' | 'halfPage' | 'inlineMerchandising' | 'leaderboard' | 'merchandising' | 'merchandisingHigh' | 'merchandisingHighAdFeature' | 'mobilesticky' | 'mpu' | 'outOfPage' | 'outstreamDesktop' | 'outstreamGoogleDesktop' | 'outstreamMobile' | 'portrait' | 'skyscraper';
28
29
  declare type SlotName = 'right' | 'comments' | 'top-above-nav' | 'mostpop' | 'merchandising' | 'merchandising-high' | 'merchandising-high-lucky' | 'survey' | 'im' | 'inline' | 'mostpop' | 'comments' | 'top-above-nav' | 'carrot' | 'epic' | 'mobile-sticky' | 'crossword-banner';
29
- declare type Breakpoint = 'mobile' | 'desktop' | 'phablet' | 'tablet';
30
30
  declare type SizeMapping = Partial<Record<Breakpoint, AdSize[]>>;
31
31
  declare type SlotSizeMappings = Record<SlotName, SizeMapping>;
32
32
  declare const createAdSize: (width: number, height: number) => AdSize;
@@ -1,9 +1,18 @@
1
- import type { AdSize } from './ad-sizes';
1
+ import type { AdSize, SizeMapping } from './ad-sizes';
2
2
  declare type SlotName = 'im' | 'high-merch' | 'high-merch-lucky' | 'high-merch-paid' | 'inline' | 'mostpop' | 'comments' | 'top-above-nav' | 'carrot' | 'epic' | 'mobile-sticky';
3
3
  declare type CreateSlotOptions = {
4
4
  classes?: string;
5
5
  name?: string;
6
6
  sizes?: Record<string, AdSize[]>;
7
7
  };
8
- export declare const createAdSlot: (name: SlotName, options?: CreateSlotOptions) => HTMLElement;
9
- export {};
8
+ /**
9
+ * Given default size mappings and additional size mappings from
10
+ * the createAdSlot options parameter.
11
+ *
12
+ * 1. Check that the options size mappings use known device names
13
+ * 2. If so concat the size mappings
14
+ *
15
+ */
16
+ declare const concatSizeMappings: (defaultSizeMappings: SizeMapping, optionSizeMappings?: SizeMapping) => SizeMapping;
17
+ declare const createAdSlot: (name: SlotName, options?: CreateSlotOptions) => HTMLElement;
18
+ export { createAdSlot, concatSizeMappings };
@@ -1,4 +1,5 @@
1
1
  import { slotSizeMappings } from './ad-sizes';
2
+ import { isBreakpoint } from './lib/breakpoint';
2
3
  const adSlotIdPrefix = 'dfp-ad--';
3
4
  const adSlotConfigs = {
4
5
  im: {
@@ -99,24 +100,14 @@ const createClasses = (slotName, classes) => [...(classes?.split(' ') ?? []), sl
99
100
  * 2. If so concat the size mappings
100
101
  *
101
102
  */
102
- const concatSizeMappings = (defaultSizeMappings, optionSizeMappings) => {
103
- if (!optionSizeMappings)
104
- return defaultSizeMappings;
105
- const concatenatedSizeMappings = { ...defaultSizeMappings };
106
- const optionDevices = Object.keys(optionSizeMappings); // ['mobile', 'desktop']
107
- for (let i = 0; i < optionDevices.length; i++) {
108
- const device = optionDevices[i];
109
- if (optionSizeMappings[device] && defaultSizeMappings[device]) {
110
- const optionSizeMappingsForDevice = optionSizeMappings[device];
111
- const defaultSizeMappingsForDevice = defaultSizeMappings[device];
112
- if (defaultSizeMappingsForDevice && optionSizeMappingsForDevice) {
113
- // TODO can we do concatenatedSizeMappings[device]?.concat ?
114
- concatenatedSizeMappings[device] = (concatenatedSizeMappings[device] ?? []).concat(optionSizeMappingsForDevice);
115
- }
116
- }
103
+ const concatSizeMappings = (defaultSizeMappings, optionSizeMappings = {}) => Object.entries(optionSizeMappings).reduce((sizeMappings, [breakpoint, optionSizes]) => {
104
+ // Only perform concatenation if breakpoint is of the correct type
105
+ if (isBreakpoint(breakpoint)) {
106
+ // Concatenate the option sizes onto any existing sizes present for a given breakpoint
107
+ sizeMappings[breakpoint] = (sizeMappings[breakpoint] ?? []).concat(optionSizes);
117
108
  }
118
- return concatenatedSizeMappings;
119
- };
109
+ return sizeMappings;
110
+ }, { ...defaultSizeMappings });
120
111
  /**
121
112
  * Convert size mappings to a string that will be added to the ad slot
122
113
  * data attributes.
@@ -135,7 +126,7 @@ const createDataAttributes = (attrs) => Object.entries(attrs).reduce((result, [k
135
126
  result[`data-${key}`] = value;
136
127
  return result;
137
128
  }, {});
138
- export const createAdSlot = (name, options = {}) => {
129
+ const createAdSlot = (name, options = {}) => {
139
130
  const adSlotConfig = adSlotConfigs[name];
140
131
  const slotName = options.name ?? adSlotConfig.name ?? name;
141
132
  const sizeMappings = concatSizeMappings(adSlotConfig.sizeMappings, options.sizes);
@@ -148,3 +139,4 @@ export const createAdSlot = (name, options = {}) => {
148
139
  }
149
140
  return createAdSlotElement(slotName, createDataAttributes(attributes), createClasses(slotName, options.classes));
150
141
  };
142
+ export { createAdSlot, concatSizeMappings };
@@ -8,13 +8,15 @@ export { EventTimer } from './event-timer';
8
8
  export { bypassCommercialMetricsSampling, initCommercialMetrics, } from './send-commercial-metrics';
9
9
  export type { ThirdPartyTag } from './types';
10
10
  export { adSizes, getAdSize, slotSizeMappings, createAdSize } from './ad-sizes';
11
+ export { isBreakpoint } from './lib/breakpoint';
12
+ export type { Breakpoint } from './lib/breakpoint';
11
13
  export type { SizeKeys, AdSizeString, AdSize, SizeMapping, SlotSizeMappings, SlotName, } from './ad-sizes';
12
14
  export { isAdBlockInUse } from './detect-ad-blocker';
13
15
  export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments, } from './permutive';
14
16
  export { initTrackScrollDepth } from './track-scroll-depth';
15
17
  export { initTrackGpcSignal } from './track-gpc-signal';
16
18
  export { buildAdsConfigWithConsent, disabledAds } from './ad-targeting-youtube';
17
- export { createAdSlot } from './create-ad-slot';
19
+ export { createAdSlot, concatSizeMappings } from './create-ad-slot';
18
20
  export type { AdsConfig, AdsConfigBasic, AdsConfigDisabled, AdTargetingBuilder, CustomParams, } from './types';
19
21
  export * as constants from './constants';
20
22
  export type { ContentTargeting } from './targeting/content';
package/dist/esm/index.js CHANGED
@@ -8,12 +8,13 @@ export { remarketing } from './third-party-tags/remarketing';
8
8
  export { EventTimer } from './event-timer';
9
9
  export { bypassCommercialMetricsSampling, initCommercialMetrics, } from './send-commercial-metrics';
10
10
  export { adSizes, getAdSize, slotSizeMappings, createAdSize } from './ad-sizes';
11
+ export { isBreakpoint } from './lib/breakpoint';
11
12
  export { isAdBlockInUse } from './detect-ad-blocker';
12
13
  export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments, } from './permutive';
13
14
  export { initTrackScrollDepth } from './track-scroll-depth';
14
15
  export { initTrackGpcSignal } from './track-gpc-signal';
15
16
  export { buildAdsConfigWithConsent, disabledAds } from './ad-targeting-youtube';
16
- export { createAdSlot } from './create-ad-slot';
17
+ export { createAdSlot, concatSizeMappings } from './create-ad-slot';
17
18
  import * as constants_1 from './constants';
18
19
  export { constants_1 as constants };
19
20
  export { getContentTargeting } from './targeting/content';
@@ -0,0 +1,4 @@
1
+ declare type Breakpoint = 'mobile' | 'desktop' | 'phablet' | 'tablet';
2
+ declare const isBreakpoint: (s: string) => s is Breakpoint;
3
+ export type { Breakpoint };
4
+ export { isBreakpoint };
@@ -0,0 +1,2 @@
1
+ const isBreakpoint = (s) => s === 'mobile' || s === 'phablet' || s === 'tablet' || s === 'desktop';
2
+ export { isBreakpoint };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/commercial-core",
3
- "version": "4.4.0",
3
+ "version": "4.5.0",
4
4
  "description": "Guardian advertising business logic",
5
5
  "homepage": "https://github.com/guardian/commercial-core#readme",
6
6
  "bugs": {