@guardian/commercial-core 2.0.0 → 3.2.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/dist/cjs/EventTimer.d.ts +17 -5
- package/dist/cjs/EventTimer.js +9 -0
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +3 -1
- package/dist/cjs/sendCommercialMetrics.d.ts +1 -5
- package/dist/cjs/sendCommercialMetrics.js +2 -26
- package/dist/cjs/track-scroll-depth.d.ts +8 -0
- package/dist/cjs/track-scroll-depth.js +41 -0
- package/dist/esm/EventTimer.d.ts +17 -5
- package/dist/esm/EventTimer.js +9 -0
- package/dist/esm/index.d.ts +1 -0
- package/dist/esm/index.js +1 -0
- package/dist/esm/sendCommercialMetrics.d.ts +1 -5
- package/dist/esm/sendCommercialMetrics.js +2 -26
- package/dist/esm/track-scroll-depth.d.ts +8 -0
- package/dist/esm/track-scroll-depth.js +38 -0
- package/package.json +1 -1
package/dist/cjs/EventTimer.d.ts
CHANGED
|
@@ -23,6 +23,15 @@ 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
|
+
lazyLoadMarginPercent?: number;
|
|
34
|
+
}
|
|
26
35
|
export declare class EventTimer {
|
|
27
36
|
private _events;
|
|
28
37
|
private static _externallyDefinedEventNames;
|
|
@@ -33,11 +42,7 @@ export declare class EventTimer {
|
|
|
33
42
|
page: PageEventStatus;
|
|
34
43
|
};
|
|
35
44
|
gaConfig: GAConfig;
|
|
36
|
-
properties:
|
|
37
|
-
type?: ConnectionType;
|
|
38
|
-
downlink?: number;
|
|
39
|
-
effectiveType?: string;
|
|
40
|
-
};
|
|
45
|
+
properties: EventTimerProperties;
|
|
41
46
|
/**
|
|
42
47
|
* Initialise the EventTimer class on page.
|
|
43
48
|
* Returns the singleton instance of the EventTimer class and binds
|
|
@@ -60,6 +65,13 @@ export declare class EventTimer {
|
|
|
60
65
|
*/
|
|
61
66
|
get events(): Event[];
|
|
62
67
|
private constructor();
|
|
68
|
+
/**
|
|
69
|
+
* Adds an event timer property
|
|
70
|
+
*
|
|
71
|
+
* @param {string} name - the property's name
|
|
72
|
+
* @param {value} number - the property's value
|
|
73
|
+
*/
|
|
74
|
+
setProperty(name: 'adSlotsInline' | 'adSlotsTotal' | 'pageHeightVH' | 'lazyLoadMarginPercent', value: number): void;
|
|
63
75
|
/**
|
|
64
76
|
* Creates a new performance mark
|
|
65
77
|
* For slot events also ensures each TYPE of event event is marked only once for 'first'
|
package/dist/cjs/EventTimer.js
CHANGED
|
@@ -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'
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -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; } });
|
|
@@ -28,8 +28,6 @@ interface InitCommercialMetricsArgs {
|
|
|
28
28
|
browserId: string | undefined;
|
|
29
29
|
isDev: boolean;
|
|
30
30
|
adBlockerInUse?: boolean;
|
|
31
|
-
adSlotsInline?: number;
|
|
32
|
-
adSlotsTotal?: number;
|
|
33
31
|
sampling?: number;
|
|
34
32
|
}
|
|
35
33
|
/**
|
|
@@ -38,11 +36,9 @@ interface InitCommercialMetricsArgs {
|
|
|
38
36
|
* @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
|
|
39
37
|
* @param init.isDev - used to determine whether to use CODE or PROD endpoints.
|
|
40
38
|
* @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
|
|
41
|
-
* @param init.adSlotsInline - the number of inline ad slots on the page
|
|
42
|
-
* @param init.adSlotsTotal - the total number of ad slots on the page
|
|
43
39
|
* @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
|
|
44
40
|
*/
|
|
45
|
-
export declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse,
|
|
41
|
+
export declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling, }: InitCommercialMetricsArgs): boolean;
|
|
46
42
|
export declare const _: {
|
|
47
43
|
Endpoints: typeof Endpoints;
|
|
48
44
|
setEndpoint: (isDev: boolean) => Endpoints;
|
|
@@ -17,7 +17,6 @@ let commercialMetricsPayload = {
|
|
|
17
17
|
};
|
|
18
18
|
let devProperties = [];
|
|
19
19
|
let adBlockerProperties = [];
|
|
20
|
-
let adSlotProperties = [];
|
|
21
20
|
let initialised = false;
|
|
22
21
|
let endpoint;
|
|
23
22
|
const setEndpoint = (isDev) => (endpoint = isDev ? Endpoints.CODE : Endpoints.PROD);
|
|
@@ -35,25 +34,6 @@ const setAdBlockerProperties = (adBlockerInUse) => {
|
|
|
35
34
|
]
|
|
36
35
|
: [];
|
|
37
36
|
};
|
|
38
|
-
const setAdSlotProperties = (adSlotsInline, adSlotsTotal) => {
|
|
39
|
-
const adSlotsInlineProperties = adSlotsInline !== undefined
|
|
40
|
-
? [
|
|
41
|
-
{
|
|
42
|
-
name: 'adSlotsInline',
|
|
43
|
-
value: adSlotsInline.toString(),
|
|
44
|
-
},
|
|
45
|
-
]
|
|
46
|
-
: [];
|
|
47
|
-
const adSlotsTotalProperties = adSlotsTotal !== undefined
|
|
48
|
-
? [
|
|
49
|
-
{
|
|
50
|
-
name: 'adSlotsTotal',
|
|
51
|
-
value: adSlotsTotal.toString(),
|
|
52
|
-
},
|
|
53
|
-
]
|
|
54
|
-
: [];
|
|
55
|
-
adSlotProperties = adSlotsInlineProperties.concat(adSlotsTotalProperties);
|
|
56
|
-
};
|
|
57
37
|
const transformToObjectEntries = (eventTimerProperties) => {
|
|
58
38
|
// Transforms object {key: value} pairs into an array of [key, value] arrays
|
|
59
39
|
return Object.entries(eventTimerProperties);
|
|
@@ -88,8 +68,7 @@ function gatherMetricsOnPageUnload() {
|
|
|
88
68
|
const mappedEventTimerProperties = mapEventTimerPropertiesToString(filteredEventTimerProperties);
|
|
89
69
|
const properties = mappedEventTimerProperties
|
|
90
70
|
.concat(devProperties)
|
|
91
|
-
.concat(adBlockerProperties)
|
|
92
|
-
.concat(adSlotProperties);
|
|
71
|
+
.concat(adBlockerProperties);
|
|
93
72
|
commercialMetricsPayload.properties = properties;
|
|
94
73
|
const metrics = roundTimeStamp(eventTimer.events);
|
|
95
74
|
commercialMetricsPayload.metrics = metrics;
|
|
@@ -130,17 +109,14 @@ exports.bypassCommercialMetricsSampling = bypassCommercialMetricsSampling;
|
|
|
130
109
|
* @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
|
|
131
110
|
* @param init.isDev - used to determine whether to use CODE or PROD endpoints.
|
|
132
111
|
* @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
|
|
133
|
-
* @param init.adSlotsInline - the number of inline ad slots on the page
|
|
134
|
-
* @param init.adSlotsTotal - the total number of ad slots on the page
|
|
135
112
|
* @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
|
|
136
113
|
*/
|
|
137
|
-
function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse,
|
|
114
|
+
function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100, }) {
|
|
138
115
|
commercialMetricsPayload.page_view_id = pageViewId;
|
|
139
116
|
commercialMetricsPayload.browser_id = browserId;
|
|
140
117
|
setEndpoint(isDev);
|
|
141
118
|
setDevProperties(isDev);
|
|
142
119
|
setAdBlockerProperties(adBlockerInUse);
|
|
143
|
-
setAdSlotProperties(adSlotsInline, adSlotsTotal);
|
|
144
120
|
if (initialised) {
|
|
145
121
|
return false;
|
|
146
122
|
}
|
|
@@ -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;
|
package/dist/esm/EventTimer.d.ts
CHANGED
|
@@ -23,6 +23,15 @@ 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
|
+
lazyLoadMarginPercent?: number;
|
|
34
|
+
}
|
|
26
35
|
export declare class EventTimer {
|
|
27
36
|
private _events;
|
|
28
37
|
private static _externallyDefinedEventNames;
|
|
@@ -33,11 +42,7 @@ export declare class EventTimer {
|
|
|
33
42
|
page: PageEventStatus;
|
|
34
43
|
};
|
|
35
44
|
gaConfig: GAConfig;
|
|
36
|
-
properties:
|
|
37
|
-
type?: ConnectionType;
|
|
38
|
-
downlink?: number;
|
|
39
|
-
effectiveType?: string;
|
|
40
|
-
};
|
|
45
|
+
properties: EventTimerProperties;
|
|
41
46
|
/**
|
|
42
47
|
* Initialise the EventTimer class on page.
|
|
43
48
|
* Returns the singleton instance of the EventTimer class and binds
|
|
@@ -60,6 +65,13 @@ export declare class EventTimer {
|
|
|
60
65
|
*/
|
|
61
66
|
get events(): Event[];
|
|
62
67
|
private constructor();
|
|
68
|
+
/**
|
|
69
|
+
* Adds an event timer property
|
|
70
|
+
*
|
|
71
|
+
* @param {string} name - the property's name
|
|
72
|
+
* @param {value} number - the property's value
|
|
73
|
+
*/
|
|
74
|
+
setProperty(name: 'adSlotsInline' | 'adSlotsTotal' | 'pageHeightVH' | 'lazyLoadMarginPercent', value: number): void;
|
|
63
75
|
/**
|
|
64
76
|
* Creates a new performance mark
|
|
65
77
|
* For slot events also ensures each TYPE of event event is marked only once for 'first'
|
package/dist/esm/EventTimer.js
CHANGED
|
@@ -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'
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -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 };
|
|
@@ -28,8 +28,6 @@ interface InitCommercialMetricsArgs {
|
|
|
28
28
|
browserId: string | undefined;
|
|
29
29
|
isDev: boolean;
|
|
30
30
|
adBlockerInUse?: boolean;
|
|
31
|
-
adSlotsInline?: number;
|
|
32
|
-
adSlotsTotal?: number;
|
|
33
31
|
sampling?: number;
|
|
34
32
|
}
|
|
35
33
|
/**
|
|
@@ -38,11 +36,9 @@ interface InitCommercialMetricsArgs {
|
|
|
38
36
|
* @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
|
|
39
37
|
* @param init.isDev - used to determine whether to use CODE or PROD endpoints.
|
|
40
38
|
* @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
|
|
41
|
-
* @param init.adSlotsInline - the number of inline ad slots on the page
|
|
42
|
-
* @param init.adSlotsTotal - the total number of ad slots on the page
|
|
43
39
|
* @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
|
|
44
40
|
*/
|
|
45
|
-
export declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse,
|
|
41
|
+
export declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling, }: InitCommercialMetricsArgs): boolean;
|
|
46
42
|
export declare const _: {
|
|
47
43
|
Endpoints: typeof Endpoints;
|
|
48
44
|
setEndpoint: (isDev: boolean) => Endpoints;
|
|
@@ -14,7 +14,6 @@ let commercialMetricsPayload = {
|
|
|
14
14
|
};
|
|
15
15
|
let devProperties = [];
|
|
16
16
|
let adBlockerProperties = [];
|
|
17
|
-
let adSlotProperties = [];
|
|
18
17
|
let initialised = false;
|
|
19
18
|
let endpoint;
|
|
20
19
|
const setEndpoint = (isDev) => (endpoint = isDev ? Endpoints.CODE : Endpoints.PROD);
|
|
@@ -32,25 +31,6 @@ const setAdBlockerProperties = (adBlockerInUse) => {
|
|
|
32
31
|
]
|
|
33
32
|
: [];
|
|
34
33
|
};
|
|
35
|
-
const setAdSlotProperties = (adSlotsInline, adSlotsTotal) => {
|
|
36
|
-
const adSlotsInlineProperties = adSlotsInline !== undefined
|
|
37
|
-
? [
|
|
38
|
-
{
|
|
39
|
-
name: 'adSlotsInline',
|
|
40
|
-
value: adSlotsInline.toString(),
|
|
41
|
-
},
|
|
42
|
-
]
|
|
43
|
-
: [];
|
|
44
|
-
const adSlotsTotalProperties = adSlotsTotal !== undefined
|
|
45
|
-
? [
|
|
46
|
-
{
|
|
47
|
-
name: 'adSlotsTotal',
|
|
48
|
-
value: adSlotsTotal.toString(),
|
|
49
|
-
},
|
|
50
|
-
]
|
|
51
|
-
: [];
|
|
52
|
-
adSlotProperties = adSlotsInlineProperties.concat(adSlotsTotalProperties);
|
|
53
|
-
};
|
|
54
34
|
const transformToObjectEntries = (eventTimerProperties) => {
|
|
55
35
|
// Transforms object {key: value} pairs into an array of [key, value] arrays
|
|
56
36
|
return Object.entries(eventTimerProperties);
|
|
@@ -85,8 +65,7 @@ function gatherMetricsOnPageUnload() {
|
|
|
85
65
|
const mappedEventTimerProperties = mapEventTimerPropertiesToString(filteredEventTimerProperties);
|
|
86
66
|
const properties = mappedEventTimerProperties
|
|
87
67
|
.concat(devProperties)
|
|
88
|
-
.concat(adBlockerProperties)
|
|
89
|
-
.concat(adSlotProperties);
|
|
68
|
+
.concat(adBlockerProperties);
|
|
90
69
|
commercialMetricsPayload.properties = properties;
|
|
91
70
|
const metrics = roundTimeStamp(eventTimer.events);
|
|
92
71
|
commercialMetricsPayload.metrics = metrics;
|
|
@@ -126,17 +105,14 @@ export function bypassCommercialMetricsSampling() {
|
|
|
126
105
|
* @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
|
|
127
106
|
* @param init.isDev - used to determine whether to use CODE or PROD endpoints.
|
|
128
107
|
* @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
|
|
129
|
-
* @param init.adSlotsInline - the number of inline ad slots on the page
|
|
130
|
-
* @param init.adSlotsTotal - the total number of ad slots on the page
|
|
131
108
|
* @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
|
|
132
109
|
*/
|
|
133
|
-
export function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse,
|
|
110
|
+
export function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100, }) {
|
|
134
111
|
commercialMetricsPayload.page_view_id = pageViewId;
|
|
135
112
|
commercialMetricsPayload.browser_id = browserId;
|
|
136
113
|
setEndpoint(isDev);
|
|
137
114
|
setDevProperties(isDev);
|
|
138
115
|
setAdBlockerProperties(adBlockerInUse);
|
|
139
|
-
setAdSlotProperties(adSlotsInline, adSlotsTotal);
|
|
140
116
|
if (initialised) {
|
|
141
117
|
return false;
|
|
142
118
|
}
|
|
@@ -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 };
|