@guardian/commercial-core 2.0.0 → 3.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/EventTimer.d.ts +9 -5
- package/dist/cjs/EventTimer.js +3 -0
- package/dist/cjs/sendCommercialMetrics.d.ts +1 -5
- package/dist/cjs/sendCommercialMetrics.js +2 -26
- package/dist/esm/EventTimer.d.ts +9 -5
- package/dist/esm/EventTimer.js +3 -0
- package/dist/esm/sendCommercialMetrics.d.ts +1 -5
- package/dist/esm/sendCommercialMetrics.js +2 -26
- package/package.json +1 -1
package/dist/cjs/EventTimer.d.ts
CHANGED
|
@@ -23,6 +23,13 @@ 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
|
+
}
|
|
26
33
|
export declare class EventTimer {
|
|
27
34
|
private _events;
|
|
28
35
|
private static _externallyDefinedEventNames;
|
|
@@ -33,11 +40,7 @@ export declare class EventTimer {
|
|
|
33
40
|
page: PageEventStatus;
|
|
34
41
|
};
|
|
35
42
|
gaConfig: GAConfig;
|
|
36
|
-
properties:
|
|
37
|
-
type?: ConnectionType;
|
|
38
|
-
downlink?: number;
|
|
39
|
-
effectiveType?: string;
|
|
40
|
-
};
|
|
43
|
+
properties: EventTimerProperties;
|
|
41
44
|
/**
|
|
42
45
|
* Initialise the EventTimer class on page.
|
|
43
46
|
* Returns the singleton instance of the EventTimer class and binds
|
|
@@ -60,6 +63,7 @@ export declare class EventTimer {
|
|
|
60
63
|
*/
|
|
61
64
|
get events(): Event[];
|
|
62
65
|
private constructor();
|
|
66
|
+
setProperty(name: 'adSlotsInline' | 'adSlotsTotal', value: number): void;
|
|
63
67
|
/**
|
|
64
68
|
* Creates a new performance mark
|
|
65
69
|
* 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,9 @@ class EventTimer {
|
|
|
108
108
|
]
|
|
109
109
|
: this._events;
|
|
110
110
|
}
|
|
111
|
+
setProperty(name, value) {
|
|
112
|
+
this.properties[name] = value;
|
|
113
|
+
}
|
|
111
114
|
/**
|
|
112
115
|
* Creates a new performance mark
|
|
113
116
|
* For slot events also ensures each TYPE of event event is marked only once for 'first'
|
|
@@ -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
|
}
|
package/dist/esm/EventTimer.d.ts
CHANGED
|
@@ -23,6 +23,13 @@ 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
|
+
}
|
|
26
33
|
export declare class EventTimer {
|
|
27
34
|
private _events;
|
|
28
35
|
private static _externallyDefinedEventNames;
|
|
@@ -33,11 +40,7 @@ export declare class EventTimer {
|
|
|
33
40
|
page: PageEventStatus;
|
|
34
41
|
};
|
|
35
42
|
gaConfig: GAConfig;
|
|
36
|
-
properties:
|
|
37
|
-
type?: ConnectionType;
|
|
38
|
-
downlink?: number;
|
|
39
|
-
effectiveType?: string;
|
|
40
|
-
};
|
|
43
|
+
properties: EventTimerProperties;
|
|
41
44
|
/**
|
|
42
45
|
* Initialise the EventTimer class on page.
|
|
43
46
|
* Returns the singleton instance of the EventTimer class and binds
|
|
@@ -60,6 +63,7 @@ export declare class EventTimer {
|
|
|
60
63
|
*/
|
|
61
64
|
get events(): Event[];
|
|
62
65
|
private constructor();
|
|
66
|
+
setProperty(name: 'adSlotsInline' | 'adSlotsTotal', value: number): void;
|
|
63
67
|
/**
|
|
64
68
|
* Creates a new performance mark
|
|
65
69
|
* 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,9 @@ export class EventTimer {
|
|
|
105
105
|
]
|
|
106
106
|
: this._events;
|
|
107
107
|
}
|
|
108
|
+
setProperty(name, value) {
|
|
109
|
+
this.properties[name] = value;
|
|
110
|
+
}
|
|
108
111
|
/**
|
|
109
112
|
* Creates a new performance mark
|
|
110
113
|
* For slot events also ensures each TYPE of event event is marked only once for 'first'
|
|
@@ -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
|
}
|