@guardian/commercial-core 0.41.0 → 1.0.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/index.d.ts +1 -1
- package/dist/cjs/index.js +3 -2
- package/dist/cjs/sendCommercialMetrics.d.ts +43 -1
- package/dist/cjs/sendCommercialMetrics.js +142 -36
- package/dist/esm/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/sendCommercialMetrics.d.ts +43 -1
- package/dist/esm/sendCommercialMetrics.js +139 -34
- package/package.json +2 -1
package/dist/cjs/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { twitter } from './third-party-tags/twitter-uwt';
|
|
|
5
5
|
export { inizio } from './third-party-tags/inizio';
|
|
6
6
|
export { remarketing } from './third-party-tags/remarketing';
|
|
7
7
|
export { EventTimer } from './EventTimer';
|
|
8
|
-
export {
|
|
8
|
+
export { bypassCommercialMetricsSampling, initCommercialMetrics, } from './sendCommercialMetrics';
|
|
9
9
|
export type { ThirdPartyTag } from './types';
|
|
10
10
|
export { adSizes, getAdSize } from './ad-sizes';
|
|
11
11
|
export type { SizeKeys, AdSizeString, AdSize } from './ad-sizes';
|
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.
|
|
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;
|
|
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");
|
|
@@ -36,7 +36,8 @@ Object.defineProperty(exports, "remarketing", { enumerable: true, get: function
|
|
|
36
36
|
var EventTimer_1 = require("./EventTimer");
|
|
37
37
|
Object.defineProperty(exports, "EventTimer", { enumerable: true, get: function () { return EventTimer_1.EventTimer; } });
|
|
38
38
|
var sendCommercialMetrics_1 = require("./sendCommercialMetrics");
|
|
39
|
-
Object.defineProperty(exports, "
|
|
39
|
+
Object.defineProperty(exports, "bypassCommercialMetricsSampling", { enumerable: true, get: function () { return sendCommercialMetrics_1.bypassCommercialMetricsSampling; } });
|
|
40
|
+
Object.defineProperty(exports, "initCommercialMetrics", { enumerable: true, get: function () { return sendCommercialMetrics_1.initCommercialMetrics; } });
|
|
40
41
|
var ad_sizes_1 = require("./ad-sizes");
|
|
41
42
|
Object.defineProperty(exports, "adSizes", { enumerable: true, get: function () { return ad_sizes_1.adSizes; } });
|
|
42
43
|
Object.defineProperty(exports, "getAdSize", { enumerable: true, get: function () { return ad_sizes_1.getAdSize; } });
|
|
@@ -1 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
declare type Metric = {
|
|
2
|
+
name: string;
|
|
3
|
+
value: number;
|
|
4
|
+
};
|
|
5
|
+
declare type Property = {
|
|
6
|
+
name: string;
|
|
7
|
+
value: string;
|
|
8
|
+
};
|
|
9
|
+
declare type TimedEvent = {
|
|
10
|
+
name: string;
|
|
11
|
+
ts: number;
|
|
12
|
+
};
|
|
13
|
+
declare type EventProperties = {
|
|
14
|
+
type?: ConnectionType;
|
|
15
|
+
downlink?: number;
|
|
16
|
+
effectiveType?: string;
|
|
17
|
+
};
|
|
18
|
+
declare enum Endpoints {
|
|
19
|
+
CODE = "//performance-events.code.dev-guardianapis.com/commercial-metrics",
|
|
20
|
+
PROD = "//performance-events.guardianapis.com/commercial-metrics"
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A method to asynchronously send metrics after initialization.
|
|
24
|
+
*/
|
|
25
|
+
export declare function bypassCommercialMetricsSampling(): void;
|
|
26
|
+
/**
|
|
27
|
+
* A method to initialise metrics.
|
|
28
|
+
* @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
|
|
29
|
+
* @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
|
|
30
|
+
* @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.
|
|
32
|
+
*/
|
|
33
|
+
export declare function initCommercialMetrics(pageViewId: string, browserId: string | undefined, isDev: boolean, adBlockerInUse?: boolean, sampling?: number): boolean;
|
|
34
|
+
export declare const _: {
|
|
35
|
+
Endpoints: typeof Endpoints;
|
|
36
|
+
setEndpoint: (isDev: boolean) => Endpoints;
|
|
37
|
+
filterUndefinedProperties: <T>(transformedProperties: [string, T | undefined][]) => [string, T][];
|
|
38
|
+
mapEventTimerPropertiesToString: (properties: Array<[string, string | number]>) => Property[];
|
|
39
|
+
roundTimeStamp: (events: TimedEvent[]) => Metric[];
|
|
40
|
+
transformToObjectEntries: (eventTimerProperties: EventProperties) => Array<[string, string | number | undefined]>;
|
|
41
|
+
reset: () => void;
|
|
42
|
+
};
|
|
43
|
+
export type { Property, TimedEvent, Metric };
|
|
@@ -1,44 +1,150 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports._ = exports.initCommercialMetrics = exports.bypassCommercialMetricsSampling = void 0;
|
|
4
4
|
const libs_1 = require("@guardian/libs");
|
|
5
5
|
const EventTimer_1 = require("./EventTimer");
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
6
|
+
var Endpoints;
|
|
7
|
+
(function (Endpoints) {
|
|
8
|
+
Endpoints["CODE"] = "//performance-events.code.dev-guardianapis.com/commercial-metrics";
|
|
9
|
+
Endpoints["PROD"] = "//performance-events.guardianapis.com/commercial-metrics";
|
|
10
|
+
})(Endpoints || (Endpoints = {}));
|
|
11
|
+
let commercialMetricsPayload = {
|
|
12
|
+
page_view_id: undefined,
|
|
13
|
+
browser_id: undefined,
|
|
14
|
+
platform: 'NEXT_GEN',
|
|
15
|
+
metrics: [],
|
|
16
|
+
properties: [],
|
|
17
|
+
};
|
|
18
|
+
let devProperties = [];
|
|
19
|
+
let adBlockerProperties = [];
|
|
20
|
+
let initialised = false;
|
|
21
|
+
let endpoint;
|
|
22
|
+
const setEndpoint = (isDev) => (endpoint = isDev ? Endpoints.CODE : Endpoints.PROD);
|
|
23
|
+
const setDevProperties = (isDev) => (devProperties = isDev
|
|
24
|
+
? [{ name: 'isDev', value: window.location.hostname }]
|
|
25
|
+
: []);
|
|
26
|
+
const setAdBlockerProperties = (adBlockerInUse) => {
|
|
27
|
+
adBlockerProperties =
|
|
28
|
+
adBlockerInUse !== undefined
|
|
29
|
+
? [
|
|
30
|
+
{
|
|
31
|
+
name: 'adBlockerInUse',
|
|
32
|
+
value: adBlockerInUse.toString(),
|
|
33
|
+
},
|
|
34
|
+
]
|
|
35
|
+
: [];
|
|
36
|
+
};
|
|
37
|
+
const transformToObjectEntries = (eventTimerProperties) => {
|
|
38
|
+
// Transforms object {key: value} pairs into an array of [key, value] arrays
|
|
39
|
+
return Object.entries(eventTimerProperties);
|
|
40
|
+
};
|
|
41
|
+
const filterUndefinedProperties = (transformedProperties) => transformedProperties.reduce((acc, [key, value]) => {
|
|
42
|
+
if (typeof value !== 'undefined') {
|
|
43
|
+
acc.push([key, value]);
|
|
44
|
+
}
|
|
45
|
+
return acc;
|
|
46
|
+
}, []);
|
|
47
|
+
const mapEventTimerPropertiesToString = (properties) => {
|
|
48
|
+
return properties.map(([name, value]) => ({
|
|
49
|
+
name: String(name),
|
|
50
|
+
value: String(value),
|
|
51
|
+
}));
|
|
52
|
+
};
|
|
53
|
+
const roundTimeStamp = (events) => {
|
|
54
|
+
return events.map(({ name, ts }) => ({
|
|
31
55
|
name,
|
|
32
56
|
value: Math.ceil(ts),
|
|
33
57
|
}));
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
58
|
+
};
|
|
59
|
+
function sendMetrics() {
|
|
60
|
+
(0, libs_1.log)('commercial', 'About to send commercial metrics', commercialMetricsPayload);
|
|
61
|
+
return navigator.sendBeacon(endpoint, JSON.stringify(commercialMetricsPayload));
|
|
62
|
+
}
|
|
63
|
+
function gatherMetricsOnPageUnload() {
|
|
64
|
+
// Assemble commercial properties and metrics
|
|
65
|
+
const eventTimer = EventTimer_1.EventTimer.get();
|
|
66
|
+
const transformedEntries = transformToObjectEntries(eventTimer.properties);
|
|
67
|
+
const filteredEventTimerProperties = filterUndefinedProperties(transformedEntries);
|
|
68
|
+
const mappedEventTimerProperties = mapEventTimerPropertiesToString(filteredEventTimerProperties);
|
|
69
|
+
const properties = mappedEventTimerProperties
|
|
70
|
+
.concat(devProperties)
|
|
71
|
+
.concat(adBlockerProperties);
|
|
72
|
+
commercialMetricsPayload.properties = properties;
|
|
73
|
+
const metrics = roundTimeStamp(eventTimer.events);
|
|
74
|
+
commercialMetricsPayload.metrics = metrics;
|
|
75
|
+
sendMetrics();
|
|
76
|
+
}
|
|
77
|
+
const listener = (e) => {
|
|
78
|
+
switch (e.type) {
|
|
79
|
+
case 'visibilitychange':
|
|
80
|
+
if (document.visibilityState === 'hidden') {
|
|
81
|
+
gatherMetricsOnPageUnload();
|
|
82
|
+
}
|
|
83
|
+
return;
|
|
84
|
+
case 'pagehide':
|
|
85
|
+
gatherMetricsOnPageUnload();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
const addVisibilityListeners = () => {
|
|
90
|
+
// Report all available metrics when the page is unloaded or in background.
|
|
91
|
+
window.addEventListener('visibilitychange', listener);
|
|
92
|
+
// Safari does not reliably fire the `visibilitychange` on page unload.
|
|
93
|
+
window.addEventListener('pagehide', listener);
|
|
94
|
+
};
|
|
95
|
+
/**
|
|
96
|
+
* A method to asynchronously send metrics after initialization.
|
|
97
|
+
*/
|
|
98
|
+
function bypassCommercialMetricsSampling() {
|
|
99
|
+
if (!initialised) {
|
|
100
|
+
console.warn('initCommercialMetrics not yet initialised');
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
addVisibilityListeners();
|
|
104
|
+
}
|
|
105
|
+
exports.bypassCommercialMetricsSampling = bypassCommercialMetricsSampling;
|
|
106
|
+
/**
|
|
107
|
+
* A method to initialise metrics.
|
|
108
|
+
* @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
|
|
109
|
+
* @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
|
|
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.
|
|
112
|
+
*/
|
|
113
|
+
function initCommercialMetrics(pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100) {
|
|
114
|
+
commercialMetricsPayload.page_view_id = pageViewId;
|
|
115
|
+
commercialMetricsPayload.browser_id = browserId;
|
|
116
|
+
setEndpoint(isDev);
|
|
117
|
+
setDevProperties(isDev);
|
|
118
|
+
setAdBlockerProperties(adBlockerInUse);
|
|
119
|
+
if (initialised) {
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
initialised = true;
|
|
123
|
+
const userIsInSamplingGroup = Math.random() <= sampling;
|
|
124
|
+
if (isDev || userIsInSamplingGroup) {
|
|
125
|
+
addVisibilityListeners();
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
43
129
|
}
|
|
44
|
-
exports.
|
|
130
|
+
exports.initCommercialMetrics = initCommercialMetrics;
|
|
131
|
+
exports._ = {
|
|
132
|
+
Endpoints,
|
|
133
|
+
setEndpoint,
|
|
134
|
+
filterUndefinedProperties,
|
|
135
|
+
mapEventTimerPropertiesToString,
|
|
136
|
+
roundTimeStamp,
|
|
137
|
+
transformToObjectEntries,
|
|
138
|
+
reset: () => {
|
|
139
|
+
initialised = false;
|
|
140
|
+
commercialMetricsPayload = {
|
|
141
|
+
page_view_id: undefined,
|
|
142
|
+
browser_id: undefined,
|
|
143
|
+
platform: 'NEXT_GEN',
|
|
144
|
+
metrics: [],
|
|
145
|
+
properties: [],
|
|
146
|
+
};
|
|
147
|
+
removeEventListener('visibilitychange', listener);
|
|
148
|
+
removeEventListener('pagehide', listener);
|
|
149
|
+
},
|
|
150
|
+
};
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export { twitter } from './third-party-tags/twitter-uwt';
|
|
|
5
5
|
export { inizio } from './third-party-tags/inizio';
|
|
6
6
|
export { remarketing } from './third-party-tags/remarketing';
|
|
7
7
|
export { EventTimer } from './EventTimer';
|
|
8
|
-
export {
|
|
8
|
+
export { bypassCommercialMetricsSampling, initCommercialMetrics, } from './sendCommercialMetrics';
|
|
9
9
|
export type { ThirdPartyTag } from './types';
|
|
10
10
|
export { adSizes, getAdSize } from './ad-sizes';
|
|
11
11
|
export type { SizeKeys, AdSizeString, AdSize } from './ad-sizes';
|
package/dist/esm/index.js
CHANGED
|
@@ -6,7 +6,7 @@ export { twitter } from './third-party-tags/twitter-uwt';
|
|
|
6
6
|
export { inizio } from './third-party-tags/inizio';
|
|
7
7
|
export { remarketing } from './third-party-tags/remarketing';
|
|
8
8
|
export { EventTimer } from './EventTimer';
|
|
9
|
-
export {
|
|
9
|
+
export { bypassCommercialMetricsSampling, initCommercialMetrics, } from './sendCommercialMetrics';
|
|
10
10
|
export { adSizes, getAdSize } from './ad-sizes';
|
|
11
11
|
export { isAdBlockInUse } from './detectAdBlocker';
|
|
12
12
|
export { clearPermutiveSegments, getPermutiveSegments, getPermutivePFPSegments, } from './permutive';
|
|
@@ -1 +1,43 @@
|
|
|
1
|
-
|
|
1
|
+
declare type Metric = {
|
|
2
|
+
name: string;
|
|
3
|
+
value: number;
|
|
4
|
+
};
|
|
5
|
+
declare type Property = {
|
|
6
|
+
name: string;
|
|
7
|
+
value: string;
|
|
8
|
+
};
|
|
9
|
+
declare type TimedEvent = {
|
|
10
|
+
name: string;
|
|
11
|
+
ts: number;
|
|
12
|
+
};
|
|
13
|
+
declare type EventProperties = {
|
|
14
|
+
type?: ConnectionType;
|
|
15
|
+
downlink?: number;
|
|
16
|
+
effectiveType?: string;
|
|
17
|
+
};
|
|
18
|
+
declare enum Endpoints {
|
|
19
|
+
CODE = "//performance-events.code.dev-guardianapis.com/commercial-metrics",
|
|
20
|
+
PROD = "//performance-events.guardianapis.com/commercial-metrics"
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* A method to asynchronously send metrics after initialization.
|
|
24
|
+
*/
|
|
25
|
+
export declare function bypassCommercialMetricsSampling(): void;
|
|
26
|
+
/**
|
|
27
|
+
* A method to initialise metrics.
|
|
28
|
+
* @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
|
|
29
|
+
* @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
|
|
30
|
+
* @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.
|
|
32
|
+
*/
|
|
33
|
+
export declare function initCommercialMetrics(pageViewId: string, browserId: string | undefined, isDev: boolean, adBlockerInUse?: boolean, sampling?: number): boolean;
|
|
34
|
+
export declare const _: {
|
|
35
|
+
Endpoints: typeof Endpoints;
|
|
36
|
+
setEndpoint: (isDev: boolean) => Endpoints;
|
|
37
|
+
filterUndefinedProperties: <T>(transformedProperties: [string, T | undefined][]) => [string, T][];
|
|
38
|
+
mapEventTimerPropertiesToString: (properties: Array<[string, string | number]>) => Property[];
|
|
39
|
+
roundTimeStamp: (events: TimedEvent[]) => Metric[];
|
|
40
|
+
transformToObjectEntries: (eventTimerProperties: EventProperties) => Array<[string, string | number | undefined]>;
|
|
41
|
+
reset: () => void;
|
|
42
|
+
};
|
|
43
|
+
export type { Property, TimedEvent, Metric };
|
|
@@ -1,40 +1,145 @@
|
|
|
1
1
|
import { log } from '@guardian/libs';
|
|
2
2
|
import { EventTimer } from './EventTimer';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
3
|
+
var Endpoints;
|
|
4
|
+
(function (Endpoints) {
|
|
5
|
+
Endpoints["CODE"] = "//performance-events.code.dev-guardianapis.com/commercial-metrics";
|
|
6
|
+
Endpoints["PROD"] = "//performance-events.guardianapis.com/commercial-metrics";
|
|
7
|
+
})(Endpoints || (Endpoints = {}));
|
|
8
|
+
let commercialMetricsPayload = {
|
|
9
|
+
page_view_id: undefined,
|
|
10
|
+
browser_id: undefined,
|
|
11
|
+
platform: 'NEXT_GEN',
|
|
12
|
+
metrics: [],
|
|
13
|
+
properties: [],
|
|
14
|
+
};
|
|
15
|
+
let devProperties = [];
|
|
16
|
+
let adBlockerProperties = [];
|
|
17
|
+
let initialised = false;
|
|
18
|
+
let endpoint;
|
|
19
|
+
const setEndpoint = (isDev) => (endpoint = isDev ? Endpoints.CODE : Endpoints.PROD);
|
|
20
|
+
const setDevProperties = (isDev) => (devProperties = isDev
|
|
21
|
+
? [{ name: 'isDev', value: window.location.hostname }]
|
|
22
|
+
: []);
|
|
23
|
+
const setAdBlockerProperties = (adBlockerInUse) => {
|
|
24
|
+
adBlockerProperties =
|
|
25
|
+
adBlockerInUse !== undefined
|
|
26
|
+
? [
|
|
27
|
+
{
|
|
28
|
+
name: 'adBlockerInUse',
|
|
29
|
+
value: adBlockerInUse.toString(),
|
|
30
|
+
},
|
|
31
|
+
]
|
|
32
|
+
: [];
|
|
33
|
+
};
|
|
34
|
+
const transformToObjectEntries = (eventTimerProperties) => {
|
|
35
|
+
// Transforms object {key: value} pairs into an array of [key, value] arrays
|
|
36
|
+
return Object.entries(eventTimerProperties);
|
|
37
|
+
};
|
|
38
|
+
const filterUndefinedProperties = (transformedProperties) => transformedProperties.reduce((acc, [key, value]) => {
|
|
39
|
+
if (typeof value !== 'undefined') {
|
|
40
|
+
acc.push([key, value]);
|
|
41
|
+
}
|
|
42
|
+
return acc;
|
|
43
|
+
}, []);
|
|
44
|
+
const mapEventTimerPropertiesToString = (properties) => {
|
|
45
|
+
return properties.map(([name, value]) => ({
|
|
46
|
+
name: String(name),
|
|
47
|
+
value: String(value),
|
|
48
|
+
}));
|
|
49
|
+
};
|
|
50
|
+
const roundTimeStamp = (events) => {
|
|
51
|
+
return events.map(({ name, ts }) => ({
|
|
28
52
|
name,
|
|
29
53
|
value: Math.ceil(ts),
|
|
30
54
|
}));
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
55
|
+
};
|
|
56
|
+
function sendMetrics() {
|
|
57
|
+
log('commercial', 'About to send commercial metrics', commercialMetricsPayload);
|
|
58
|
+
return navigator.sendBeacon(endpoint, JSON.stringify(commercialMetricsPayload));
|
|
59
|
+
}
|
|
60
|
+
function gatherMetricsOnPageUnload() {
|
|
61
|
+
// Assemble commercial properties and metrics
|
|
62
|
+
const eventTimer = EventTimer.get();
|
|
63
|
+
const transformedEntries = transformToObjectEntries(eventTimer.properties);
|
|
64
|
+
const filteredEventTimerProperties = filterUndefinedProperties(transformedEntries);
|
|
65
|
+
const mappedEventTimerProperties = mapEventTimerPropertiesToString(filteredEventTimerProperties);
|
|
66
|
+
const properties = mappedEventTimerProperties
|
|
67
|
+
.concat(devProperties)
|
|
68
|
+
.concat(adBlockerProperties);
|
|
69
|
+
commercialMetricsPayload.properties = properties;
|
|
70
|
+
const metrics = roundTimeStamp(eventTimer.events);
|
|
71
|
+
commercialMetricsPayload.metrics = metrics;
|
|
72
|
+
sendMetrics();
|
|
73
|
+
}
|
|
74
|
+
const listener = (e) => {
|
|
75
|
+
switch (e.type) {
|
|
76
|
+
case 'visibilitychange':
|
|
77
|
+
if (document.visibilityState === 'hidden') {
|
|
78
|
+
gatherMetricsOnPageUnload();
|
|
79
|
+
}
|
|
80
|
+
return;
|
|
81
|
+
case 'pagehide':
|
|
82
|
+
gatherMetricsOnPageUnload();
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
const addVisibilityListeners = () => {
|
|
87
|
+
// Report all available metrics when the page is unloaded or in background.
|
|
88
|
+
window.addEventListener('visibilitychange', listener);
|
|
89
|
+
// Safari does not reliably fire the `visibilitychange` on page unload.
|
|
90
|
+
window.addEventListener('pagehide', listener);
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* A method to asynchronously send metrics after initialization.
|
|
94
|
+
*/
|
|
95
|
+
export function bypassCommercialMetricsSampling() {
|
|
96
|
+
if (!initialised) {
|
|
97
|
+
console.warn('initCommercialMetrics not yet initialised');
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
addVisibilityListeners();
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* A method to initialise metrics.
|
|
104
|
+
* @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
|
|
105
|
+
* @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
|
|
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.
|
|
108
|
+
*/
|
|
109
|
+
export function initCommercialMetrics(pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100) {
|
|
110
|
+
commercialMetricsPayload.page_view_id = pageViewId;
|
|
111
|
+
commercialMetricsPayload.browser_id = browserId;
|
|
112
|
+
setEndpoint(isDev);
|
|
113
|
+
setDevProperties(isDev);
|
|
114
|
+
setAdBlockerProperties(adBlockerInUse);
|
|
115
|
+
if (initialised) {
|
|
116
|
+
return false;
|
|
117
|
+
}
|
|
118
|
+
initialised = true;
|
|
119
|
+
const userIsInSamplingGroup = Math.random() <= sampling;
|
|
120
|
+
if (isDev || userIsInSamplingGroup) {
|
|
121
|
+
addVisibilityListeners();
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
return false;
|
|
40
125
|
}
|
|
126
|
+
export const _ = {
|
|
127
|
+
Endpoints,
|
|
128
|
+
setEndpoint,
|
|
129
|
+
filterUndefinedProperties,
|
|
130
|
+
mapEventTimerPropertiesToString,
|
|
131
|
+
roundTimeStamp,
|
|
132
|
+
transformToObjectEntries,
|
|
133
|
+
reset: () => {
|
|
134
|
+
initialised = false;
|
|
135
|
+
commercialMetricsPayload = {
|
|
136
|
+
page_view_id: undefined,
|
|
137
|
+
browser_id: undefined,
|
|
138
|
+
platform: 'NEXT_GEN',
|
|
139
|
+
metrics: [],
|
|
140
|
+
properties: [],
|
|
141
|
+
};
|
|
142
|
+
removeEventListener('visibilitychange', listener);
|
|
143
|
+
removeEventListener('pagehide', listener);
|
|
144
|
+
},
|
|
145
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@guardian/commercial-core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Guardian advertising business logic",
|
|
5
5
|
"homepage": "https://github.com/guardian/commercial-core#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"precommit:lint": "lint-staged",
|
|
27
27
|
"prepush:test": "jest --verbose --runInBand --onlyChanged",
|
|
28
28
|
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch",
|
|
29
30
|
"test:ci": "jest --coverage --ci",
|
|
30
31
|
"tsc": "tsc --noEmit",
|
|
31
32
|
"validate": "npm-run-all tsc lint test build",
|