@guardian/commercial-core 1.0.0 → 2.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.
@@ -23,14 +23,26 @@ 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
+ adSlotsInline?: number;
32
+ adSlotsTotal?: number;
33
+ sampling?: number;
34
+ }
26
35
  /**
27
36
  * A method to initialise metrics.
28
37
  * @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
29
38
  * @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
30
39
  * @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.
40
+ * @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
+ * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
32
44
  */
33
- export declare function initCommercialMetrics(pageViewId: string, browserId: string | undefined, isDev: boolean, adBlockerInUse?: boolean, sampling?: number): boolean;
45
+ export declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, adSlotsInline, adSlotsTotal, sampling, }: InitCommercialMetricsArgs): boolean;
34
46
  export declare const _: {
35
47
  Endpoints: typeof Endpoints;
36
48
  setEndpoint: (isDev: boolean) => Endpoints;
@@ -17,6 +17,7 @@ let commercialMetricsPayload = {
17
17
  };
18
18
  let devProperties = [];
19
19
  let adBlockerProperties = [];
20
+ let adSlotProperties = [];
20
21
  let initialised = false;
21
22
  let endpoint;
22
23
  const setEndpoint = (isDev) => (endpoint = isDev ? Endpoints.CODE : Endpoints.PROD);
@@ -34,6 +35,25 @@ const setAdBlockerProperties = (adBlockerInUse) => {
34
35
  ]
35
36
  : [];
36
37
  };
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
+ };
37
57
  const transformToObjectEntries = (eventTimerProperties) => {
38
58
  // Transforms object {key: value} pairs into an array of [key, value] arrays
39
59
  return Object.entries(eventTimerProperties);
@@ -68,7 +88,8 @@ function gatherMetricsOnPageUnload() {
68
88
  const mappedEventTimerProperties = mapEventTimerPropertiesToString(filteredEventTimerProperties);
69
89
  const properties = mappedEventTimerProperties
70
90
  .concat(devProperties)
71
- .concat(adBlockerProperties);
91
+ .concat(adBlockerProperties)
92
+ .concat(adSlotProperties);
72
93
  commercialMetricsPayload.properties = properties;
73
94
  const metrics = roundTimeStamp(eventTimer.events);
74
95
  commercialMetricsPayload.metrics = metrics;
@@ -108,14 +129,18 @@ exports.bypassCommercialMetricsSampling = bypassCommercialMetricsSampling;
108
129
  * @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
109
130
  * @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
110
131
  * @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.
132
+ * @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
+ * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
112
136
  */
113
- function initCommercialMetrics(pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100) {
137
+ function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, adSlotsInline, adSlotsTotal, sampling = 1 / 100, }) {
114
138
  commercialMetricsPayload.page_view_id = pageViewId;
115
139
  commercialMetricsPayload.browser_id = browserId;
116
140
  setEndpoint(isDev);
117
141
  setDevProperties(isDev);
118
142
  setAdBlockerProperties(adBlockerInUse);
143
+ setAdSlotProperties(adSlotsInline, adSlotsTotal);
119
144
  if (initialised) {
120
145
  return false;
121
146
  }
@@ -23,14 +23,26 @@ 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
+ adSlotsInline?: number;
32
+ adSlotsTotal?: number;
33
+ sampling?: number;
34
+ }
26
35
  /**
27
36
  * A method to initialise metrics.
28
37
  * @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
29
38
  * @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
30
39
  * @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.
40
+ * @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
+ * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
32
44
  */
33
- export declare function initCommercialMetrics(pageViewId: string, browserId: string | undefined, isDev: boolean, adBlockerInUse?: boolean, sampling?: number): boolean;
45
+ export declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, adSlotsInline, adSlotsTotal, sampling, }: InitCommercialMetricsArgs): boolean;
34
46
  export declare const _: {
35
47
  Endpoints: typeof Endpoints;
36
48
  setEndpoint: (isDev: boolean) => Endpoints;
@@ -14,6 +14,7 @@ let commercialMetricsPayload = {
14
14
  };
15
15
  let devProperties = [];
16
16
  let adBlockerProperties = [];
17
+ let adSlotProperties = [];
17
18
  let initialised = false;
18
19
  let endpoint;
19
20
  const setEndpoint = (isDev) => (endpoint = isDev ? Endpoints.CODE : Endpoints.PROD);
@@ -31,6 +32,25 @@ const setAdBlockerProperties = (adBlockerInUse) => {
31
32
  ]
32
33
  : [];
33
34
  };
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
+ };
34
54
  const transformToObjectEntries = (eventTimerProperties) => {
35
55
  // Transforms object {key: value} pairs into an array of [key, value] arrays
36
56
  return Object.entries(eventTimerProperties);
@@ -65,7 +85,8 @@ function gatherMetricsOnPageUnload() {
65
85
  const mappedEventTimerProperties = mapEventTimerPropertiesToString(filteredEventTimerProperties);
66
86
  const properties = mappedEventTimerProperties
67
87
  .concat(devProperties)
68
- .concat(adBlockerProperties);
88
+ .concat(adBlockerProperties)
89
+ .concat(adSlotProperties);
69
90
  commercialMetricsPayload.properties = properties;
70
91
  const metrics = roundTimeStamp(eventTimer.events);
71
92
  commercialMetricsPayload.metrics = metrics;
@@ -104,14 +125,18 @@ export function bypassCommercialMetricsSampling() {
104
125
  * @param init.pageViewId - identifies the page view. Usually available on `guardian.config.ophan.pageViewId`. Defaults to `null`
105
126
  * @param init.browserId - identifies the browser. Usually available via `getCookie({ name: 'bwid' })`. Defaults to `null`
106
127
  * @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.
128
+ * @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
+ * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
108
132
  */
109
- export function initCommercialMetrics(pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100) {
133
+ export function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, adSlotsInline, adSlotsTotal, sampling = 1 / 100, }) {
110
134
  commercialMetricsPayload.page_view_id = pageViewId;
111
135
  commercialMetricsPayload.browser_id = browserId;
112
136
  setEndpoint(isDev);
113
137
  setDevProperties(isDev);
114
138
  setAdBlockerProperties(adBlockerInUse);
139
+ setAdSlotProperties(adSlotsInline, adSlotsTotal);
115
140
  if (initialised) {
116
141
  return false;
117
142
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/commercial-core",
3
- "version": "1.0.0",
3
+ "version": "2.0.0",
4
4
  "description": "Guardian advertising business logic",
5
5
  "homepage": "https://github.com/guardian/commercial-core#readme",
6
6
  "bugs": {