@guardian/commercial-core 1.0.0 → 3.1.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.
@@ -23,6 +23,14 @@ interface PageEventStatus {
23
23
  commercialBaseModulesLoaded: boolean;
24
24
  commercialModulesLoaded: boolean;
25
25
  }
26
+ interface EventTimerProperties {
27
+ type?: ConnectionType;
28
+ downlink?: number;
29
+ effectiveType?: string;
30
+ adSlotsInline?: number;
31
+ adSlotsTotal?: number;
32
+ pageHeightVH?: number;
33
+ }
26
34
  export declare class EventTimer {
27
35
  private _events;
28
36
  private static _externallyDefinedEventNames;
@@ -33,11 +41,7 @@ export declare class EventTimer {
33
41
  page: PageEventStatus;
34
42
  };
35
43
  gaConfig: GAConfig;
36
- properties: {
37
- type?: ConnectionType;
38
- downlink?: number;
39
- effectiveType?: string;
40
- };
44
+ properties: EventTimerProperties;
41
45
  /**
42
46
  * Initialise the EventTimer class on page.
43
47
  * Returns the singleton instance of the EventTimer class and binds
@@ -60,6 +64,13 @@ export declare class EventTimer {
60
64
  */
61
65
  get events(): Event[];
62
66
  private constructor();
67
+ /**
68
+ * Adds an event timer property
69
+ *
70
+ * @param {string} name - the property's name
71
+ * @param {value} number - the property's value
72
+ */
73
+ setProperty(name: 'adSlotsInline' | 'adSlotsTotal' | 'pageHeightVH', value: number): void;
63
74
  /**
64
75
  * Creates a new performance mark
65
76
  * For slot events also ensures each TYPE of event event is marked only once for 'first'
@@ -108,6 +108,15 @@ class EventTimer {
108
108
  ]
109
109
  : this._events;
110
110
  }
111
+ /**
112
+ * Adds an event timer property
113
+ *
114
+ * @param {string} name - the property's name
115
+ * @param {value} number - the property's value
116
+ */
117
+ setProperty(name, value) {
118
+ this.properties[name] = value;
119
+ }
111
120
  /**
112
121
  * Creates a new performance mark
113
122
  * For slot events also ensures each TYPE of event event is marked only once for 'first'
@@ -11,6 +11,7 @@ export { adSizes, getAdSize } from './ad-sizes';
11
11
  export type { SizeKeys, AdSizeString, AdSize } from './ad-sizes';
12
12
  export { isAdBlockInUse } from './detectAdBlocker';
13
13
  export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments, } from './permutive';
14
+ export { initTrackScrollDepth } from './track-scroll-depth';
14
15
  export { buildAdsConfigWithConsent, disabledAds } from './ad-targeting-youtube';
15
16
  export type { AdsConfig, AdsConfigBasic, AdsConfigDisabled, AdTargetingBuilder, CustomParams, } from './types';
16
17
  export * as constants from './constants';
package/dist/cjs/index.js CHANGED
@@ -20,7 +20,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
20
20
  return result;
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.pickTargetingValues = exports.getViewportTargeting = exports.getSharedTargeting = exports.getSessionTargeting = exports.getPersonalisedTargeting = exports.getContentTargeting = exports.constants = exports.disabledAds = exports.buildAdsConfigWithConsent = exports.getPermutivePFPSegments = exports.getPermutiveSegments = exports.clearPermutiveSegments = exports.isAdBlockInUse = exports.getAdSize = exports.adSizes = exports.initCommercialMetrics = exports.bypassCommercialMetricsSampling = exports.EventTimer = exports.remarketing = exports.inizio = exports.twitter = exports.fbPixel = exports.permutive = exports.ias = void 0;
23
+ exports.pickTargetingValues = exports.getViewportTargeting = exports.getSharedTargeting = exports.getSessionTargeting = exports.getPersonalisedTargeting = exports.getContentTargeting = exports.constants = exports.disabledAds = exports.buildAdsConfigWithConsent = exports.initTrackScrollDepth = exports.getPermutivePFPSegments = exports.getPermutiveSegments = exports.clearPermutiveSegments = exports.isAdBlockInUse = exports.getAdSize = exports.adSizes = exports.initCommercialMetrics = exports.bypassCommercialMetricsSampling = exports.EventTimer = exports.remarketing = exports.inizio = exports.twitter = exports.fbPixel = exports.permutive = exports.ias = void 0;
24
24
  var ias_1 = require("./third-party-tags/ias");
25
25
  Object.defineProperty(exports, "ias", { enumerable: true, get: function () { return ias_1.ias; } });
26
26
  var permutive_1 = require("./third-party-tags/permutive");
@@ -47,6 +47,8 @@ var permutive_2 = require("./permutive");
47
47
  Object.defineProperty(exports, "clearPermutiveSegments", { enumerable: true, get: function () { return permutive_2.clearPermutiveSegments; } });
48
48
  Object.defineProperty(exports, "getPermutiveSegments", { enumerable: true, get: function () { return permutive_2.getPermutiveSegments; } });
49
49
  Object.defineProperty(exports, "getPermutivePFPSegments", { enumerable: true, get: function () { return permutive_2.getPermutivePFPSegments; } });
50
+ var track_scroll_depth_1 = require("./track-scroll-depth");
51
+ Object.defineProperty(exports, "initTrackScrollDepth", { enumerable: true, get: function () { return track_scroll_depth_1.initTrackScrollDepth; } });
50
52
  var ad_targeting_youtube_1 = require("./ad-targeting-youtube");
51
53
  Object.defineProperty(exports, "buildAdsConfigWithConsent", { enumerable: true, get: function () { return ad_targeting_youtube_1.buildAdsConfigWithConsent; } });
52
54
  Object.defineProperty(exports, "disabledAds", { enumerable: true, get: function () { return ad_targeting_youtube_1.disabledAds; } });
@@ -23,14 +23,22 @@ declare enum Endpoints {
23
23
  * A method to asynchronously send metrics after initialization.
24
24
  */
25
25
  export declare function bypassCommercialMetricsSampling(): void;
26
+ interface InitCommercialMetricsArgs {
27
+ pageViewId: string;
28
+ browserId: string | undefined;
29
+ isDev: boolean;
30
+ adBlockerInUse?: boolean;
31
+ sampling?: number;
32
+ }
26
33
  /**
27
34
  * A method to initialise metrics.
28
35
  * @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
29
36
  * @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
30
37
  * @param init.isDev - used to determine whether to use CODE or PROD endpoints.
31
- * @param init.adBlockerInUse - indicates whether or not ann adblocker is being used.
38
+ * @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
39
+ * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
32
40
  */
33
- export declare function initCommercialMetrics(pageViewId: string, browserId: string | undefined, isDev: boolean, adBlockerInUse?: boolean, sampling?: number): boolean;
41
+ export declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling, }: InitCommercialMetricsArgs): boolean;
34
42
  export declare const _: {
35
43
  Endpoints: typeof Endpoints;
36
44
  setEndpoint: (isDev: boolean) => Endpoints;
@@ -108,9 +108,10 @@ exports.bypassCommercialMetricsSampling = bypassCommercialMetricsSampling;
108
108
  * @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
109
109
  * @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
110
110
  * @param init.isDev - used to determine whether to use CODE or PROD endpoints.
111
- * @param init.adBlockerInUse - indicates whether or not ann adblocker is being used.
111
+ * @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
112
+ * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
112
113
  */
113
- function initCommercialMetrics(pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100) {
114
+ function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100, }) {
114
115
  commercialMetricsPayload.page_view_id = pageViewId;
115
116
  commercialMetricsPayload.browser_id = browserId;
116
117
  setEndpoint(isDev);
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Collect commercial metrics on scroll depth
3
+ * Insert hidden elements at intervals of 1 viewport height
4
+ * then use an intersection observer to mark the time when the viewport intersects with these elements.
5
+ * Approach inspired by https://gist.github.com/bgreater/2412517f5a3f9c6fc4cafeb1ca71384f
6
+ */
7
+ declare const initTrackScrollDepth: () => void;
8
+ export { initTrackScrollDepth };
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.initTrackScrollDepth = void 0;
4
+ const libs_1 = require("@guardian/libs");
5
+ const EventTimer_1 = require("./EventTimer");
6
+ /**
7
+ * Collect commercial metrics on scroll depth
8
+ * Insert hidden elements at intervals of 1 viewport height
9
+ * then use an intersection observer to mark the time when the viewport intersects with these elements.
10
+ * Approach inspired by https://gist.github.com/bgreater/2412517f5a3f9c6fc4cafeb1ca71384f
11
+ */
12
+ const initTrackScrollDepth = () => {
13
+ const pageHeight = document.body.offsetHeight;
14
+ const intViewportHeight = window.innerHeight;
15
+ // how many viewports tall is the page?
16
+ const pageHeightVH = Math.floor(pageHeight / intViewportHeight);
17
+ const eventTimer = EventTimer_1.EventTimer.get();
18
+ eventTimer.setProperty('pageHeightVH', pageHeightVH);
19
+ const observer = new IntersectionObserver(
20
+ /* istanbul ignore next */
21
+ (entries) => {
22
+ entries.forEach((entry) => {
23
+ if (entry.isIntersecting) {
24
+ const currentDepthVH = String(entry.target.getAttribute('data-depth'));
25
+ (0, libs_1.log)('commercial', `current scroll depth ${currentDepthVH}`);
26
+ eventTimer.trigger(`scroll-depth-vh-${currentDepthVH}`);
27
+ observer.unobserve(entry.target);
28
+ }
29
+ });
30
+ });
31
+ for (let depth = 1; depth <= pageHeightVH; depth++) {
32
+ const div = document.createElement('div');
33
+ div.dataset.depth = String(depth);
34
+ div.style.top = String(100 * depth) + '%';
35
+ div.style.position = 'absolute';
36
+ div.className = 'scroll-depth-marker';
37
+ document.body.appendChild(div);
38
+ observer.observe(div);
39
+ }
40
+ };
41
+ exports.initTrackScrollDepth = initTrackScrollDepth;
@@ -23,6 +23,14 @@ interface PageEventStatus {
23
23
  commercialBaseModulesLoaded: boolean;
24
24
  commercialModulesLoaded: boolean;
25
25
  }
26
+ interface EventTimerProperties {
27
+ type?: ConnectionType;
28
+ downlink?: number;
29
+ effectiveType?: string;
30
+ adSlotsInline?: number;
31
+ adSlotsTotal?: number;
32
+ pageHeightVH?: number;
33
+ }
26
34
  export declare class EventTimer {
27
35
  private _events;
28
36
  private static _externallyDefinedEventNames;
@@ -33,11 +41,7 @@ export declare class EventTimer {
33
41
  page: PageEventStatus;
34
42
  };
35
43
  gaConfig: GAConfig;
36
- properties: {
37
- type?: ConnectionType;
38
- downlink?: number;
39
- effectiveType?: string;
40
- };
44
+ properties: EventTimerProperties;
41
45
  /**
42
46
  * Initialise the EventTimer class on page.
43
47
  * Returns the singleton instance of the EventTimer class and binds
@@ -60,6 +64,13 @@ export declare class EventTimer {
60
64
  */
61
65
  get events(): Event[];
62
66
  private constructor();
67
+ /**
68
+ * Adds an event timer property
69
+ *
70
+ * @param {string} name - the property's name
71
+ * @param {value} number - the property's value
72
+ */
73
+ setProperty(name: 'adSlotsInline' | 'adSlotsTotal' | 'pageHeightVH', value: number): void;
63
74
  /**
64
75
  * Creates a new performance mark
65
76
  * For slot events also ensures each TYPE of event event is marked only once for 'first'
@@ -105,6 +105,15 @@ export class EventTimer {
105
105
  ]
106
106
  : this._events;
107
107
  }
108
+ /**
109
+ * Adds an event timer property
110
+ *
111
+ * @param {string} name - the property's name
112
+ * @param {value} number - the property's value
113
+ */
114
+ setProperty(name, value) {
115
+ this.properties[name] = value;
116
+ }
108
117
  /**
109
118
  * Creates a new performance mark
110
119
  * For slot events also ensures each TYPE of event event is marked only once for 'first'
@@ -11,6 +11,7 @@ export { adSizes, getAdSize } from './ad-sizes';
11
11
  export type { SizeKeys, AdSizeString, AdSize } from './ad-sizes';
12
12
  export { isAdBlockInUse } from './detectAdBlocker';
13
13
  export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments, } from './permutive';
14
+ export { initTrackScrollDepth } from './track-scroll-depth';
14
15
  export { buildAdsConfigWithConsent, disabledAds } from './ad-targeting-youtube';
15
16
  export type { AdsConfig, AdsConfigBasic, AdsConfigDisabled, AdTargetingBuilder, CustomParams, } from './types';
16
17
  export * as constants from './constants';
package/dist/esm/index.js CHANGED
@@ -10,6 +10,7 @@ export { bypassCommercialMetricsSampling, initCommercialMetrics, } from './sendC
10
10
  export { adSizes, getAdSize } from './ad-sizes';
11
11
  export { isAdBlockInUse } from './detectAdBlocker';
12
12
  export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments, } from './permutive';
13
+ export { initTrackScrollDepth } from './track-scroll-depth';
13
14
  export { buildAdsConfigWithConsent, disabledAds } from './ad-targeting-youtube';
14
15
  import * as constants_1 from './constants';
15
16
  export { constants_1 as constants };
@@ -23,14 +23,22 @@ declare enum Endpoints {
23
23
  * A method to asynchronously send metrics after initialization.
24
24
  */
25
25
  export declare function bypassCommercialMetricsSampling(): void;
26
+ interface InitCommercialMetricsArgs {
27
+ pageViewId: string;
28
+ browserId: string | undefined;
29
+ isDev: boolean;
30
+ adBlockerInUse?: boolean;
31
+ sampling?: number;
32
+ }
26
33
  /**
27
34
  * A method to initialise metrics.
28
35
  * @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
29
36
  * @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
30
37
  * @param init.isDev - used to determine whether to use CODE or PROD endpoints.
31
- * @param init.adBlockerInUse - indicates whether or not ann adblocker is being used.
38
+ * @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
39
+ * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
32
40
  */
33
- export declare function initCommercialMetrics(pageViewId: string, browserId: string | undefined, isDev: boolean, adBlockerInUse?: boolean, sampling?: number): boolean;
41
+ export declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling, }: InitCommercialMetricsArgs): boolean;
34
42
  export declare const _: {
35
43
  Endpoints: typeof Endpoints;
36
44
  setEndpoint: (isDev: boolean) => Endpoints;
@@ -104,9 +104,10 @@ export function bypassCommercialMetricsSampling() {
104
104
  * @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
105
105
  * @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
106
106
  * @param init.isDev - used to determine whether to use CODE or PROD endpoints.
107
- * @param init.adBlockerInUse - indicates whether or not ann adblocker is being used.
107
+ * @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
108
+ * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
108
109
  */
109
- export function initCommercialMetrics(pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100) {
110
+ export function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100, }) {
110
111
  commercialMetricsPayload.page_view_id = pageViewId;
111
112
  commercialMetricsPayload.browser_id = browserId;
112
113
  setEndpoint(isDev);
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Collect commercial metrics on scroll depth
3
+ * Insert hidden elements at intervals of 1 viewport height
4
+ * then use an intersection observer to mark the time when the viewport intersects with these elements.
5
+ * Approach inspired by https://gist.github.com/bgreater/2412517f5a3f9c6fc4cafeb1ca71384f
6
+ */
7
+ declare const initTrackScrollDepth: () => void;
8
+ export { initTrackScrollDepth };
@@ -0,0 +1,38 @@
1
+ import { log } from '@guardian/libs';
2
+ import { EventTimer } from './EventTimer';
3
+ /**
4
+ * Collect commercial metrics on scroll depth
5
+ * Insert hidden elements at intervals of 1 viewport height
6
+ * then use an intersection observer to mark the time when the viewport intersects with these elements.
7
+ * Approach inspired by https://gist.github.com/bgreater/2412517f5a3f9c6fc4cafeb1ca71384f
8
+ */
9
+ const initTrackScrollDepth = () => {
10
+ const pageHeight = document.body.offsetHeight;
11
+ const intViewportHeight = window.innerHeight;
12
+ // how many viewports tall is the page?
13
+ const pageHeightVH = Math.floor(pageHeight / intViewportHeight);
14
+ const eventTimer = EventTimer.get();
15
+ eventTimer.setProperty('pageHeightVH', pageHeightVH);
16
+ const observer = new IntersectionObserver(
17
+ /* istanbul ignore next */
18
+ (entries) => {
19
+ entries.forEach((entry) => {
20
+ if (entry.isIntersecting) {
21
+ const currentDepthVH = String(entry.target.getAttribute('data-depth'));
22
+ log('commercial', `current scroll depth ${currentDepthVH}`);
23
+ eventTimer.trigger(`scroll-depth-vh-${currentDepthVH}`);
24
+ observer.unobserve(entry.target);
25
+ }
26
+ });
27
+ });
28
+ for (let depth = 1; depth <= pageHeightVH; depth++) {
29
+ const div = document.createElement('div');
30
+ div.dataset.depth = String(depth);
31
+ div.style.top = String(100 * depth) + '%';
32
+ div.style.position = 'absolute';
33
+ div.className = 'scroll-depth-marker';
34
+ document.body.appendChild(div);
35
+ observer.observe(div);
36
+ }
37
+ };
38
+ export { initTrackScrollDepth };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/commercial-core",
3
- "version": "1.0.0",
3
+ "version": "3.1.0",
4
4
  "description": "Guardian advertising business logic",
5
5
  "homepage": "https://github.com/guardian/commercial-core#readme",
6
6
  "bugs": {