@guardian/commercial-core 3.3.0 → 3.4.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.
@@ -22,7 +22,7 @@ declare enum Endpoints {
22
22
  /**
23
23
  * A method to asynchronously send metrics after initialization.
24
24
  */
25
- declare function bypassCommercialMetricsSampling(): void;
25
+ declare function bypassCommercialMetricsSampling(): Promise<void>;
26
26
  interface InitCommercialMetricsArgs {
27
27
  pageViewId: string;
28
28
  browserId: string | undefined;
@@ -38,11 +38,10 @@ interface InitCommercialMetricsArgs {
38
38
  * @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
39
39
  * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
40
40
  */
41
- declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling, }: InitCommercialMetricsArgs): boolean;
41
+ declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling, }: InitCommercialMetricsArgs): Promise<boolean>;
42
42
  export declare const _: {
43
43
  Endpoints: typeof Endpoints;
44
44
  setEndpoint: (isDev: boolean) => Endpoints;
45
- filterUndefinedProperties: <T>(transformedProperties: [string, T | undefined][]) => [string, T][];
46
45
  mapEventTimerPropertiesToString: (properties: Array<[string, string | number]>) => Property[];
47
46
  roundTimeStamp: (events: TimedEvent[]) => Metric[];
48
47
  transformToObjectEntries: (eventTimerProperties: EventProperties) => Array<[string, string | number | undefined]>;
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.initCommercialMetrics = exports.bypassCommercialMetricsSampling = exports._ = void 0;
4
+ const consent_management_platform_1 = require("@guardian/consent-management-platform");
4
5
  const libs_1 = require("@guardian/libs");
5
6
  const event_timer_1 = require("./event-timer");
6
7
  var Endpoints;
@@ -38,12 +39,6 @@ const transformToObjectEntries = (eventTimerProperties) => {
38
39
  // Transforms object {key: value} pairs into an array of [key, value] arrays
39
40
  return Object.entries(eventTimerProperties);
40
41
  };
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
42
  const mapEventTimerPropertiesToString = (properties) => {
48
43
  return properties.map(([name, value]) => ({
49
44
  name: String(name),
@@ -64,7 +59,7 @@ function gatherMetricsOnPageUnload() {
64
59
  // Assemble commercial properties and metrics
65
60
  const eventTimer = event_timer_1.EventTimer.get();
66
61
  const transformedEntries = transformToObjectEntries(eventTimer.properties);
67
- const filteredEventTimerProperties = filterUndefinedProperties(transformedEntries);
62
+ const filteredEventTimerProperties = transformedEntries.filter((item) => typeof item[1] !== 'undefined');
68
63
  const mappedEventTimerProperties = mapEventTimerPropertiesToString(filteredEventTimerProperties);
69
64
  const properties = mappedEventTimerProperties
70
65
  .concat(devProperties)
@@ -92,15 +87,32 @@ const addVisibilityListeners = () => {
92
87
  // Safari does not reliably fire the `visibilitychange` on page unload.
93
88
  window.addEventListener('pagehide', listener);
94
89
  };
90
+ const checkConsent = async () => {
91
+ const consentState = await (0, consent_management_platform_1.onConsent)();
92
+ if (consentState.tcfv2) {
93
+ // TCFv2 mode - check for consent
94
+ const consents = consentState.tcfv2.consents;
95
+ const REQUIRED_CONSENTS = [7, 8];
96
+ return REQUIRED_CONSENTS.every((consent) => consents[consent]);
97
+ }
98
+ // non-TCFv2 mode - don't check for consent
99
+ return true;
100
+ };
95
101
  /**
96
102
  * A method to asynchronously send metrics after initialization.
97
103
  */
98
- function bypassCommercialMetricsSampling() {
104
+ async function bypassCommercialMetricsSampling() {
99
105
  if (!initialised) {
100
106
  console.warn('initCommercialMetrics not yet initialised');
101
107
  return;
102
108
  }
103
- addVisibilityListeners();
109
+ const consented = await checkConsent();
110
+ if (consented) {
111
+ addVisibilityListeners();
112
+ }
113
+ else {
114
+ (0, libs_1.log)('commercial', "Metrics won't be sent because consent wasn't given");
115
+ }
104
116
  }
105
117
  exports.bypassCommercialMetricsSampling = bypassCommercialMetricsSampling;
106
118
  /**
@@ -111,7 +123,7 @@ exports.bypassCommercialMetricsSampling = bypassCommercialMetricsSampling;
111
123
  * @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
112
124
  * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
113
125
  */
114
- function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100, }) {
126
+ async function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100, }) {
115
127
  commercialMetricsPayload.page_view_id = pageViewId;
116
128
  commercialMetricsPayload.browser_id = browserId;
117
129
  setEndpoint(isDev);
@@ -123,8 +135,14 @@ function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, s
123
135
  initialised = true;
124
136
  const userIsInSamplingGroup = Math.random() <= sampling;
125
137
  if (isDev || userIsInSamplingGroup) {
126
- addVisibilityListeners();
127
- return true;
138
+ const consented = await checkConsent();
139
+ if (consented) {
140
+ addVisibilityListeners();
141
+ return true;
142
+ }
143
+ else {
144
+ (0, libs_1.log)('commercial', "Metrics won't be sent because consent wasn't given");
145
+ }
128
146
  }
129
147
  return false;
130
148
  }
@@ -132,7 +150,6 @@ exports.initCommercialMetrics = initCommercialMetrics;
132
150
  exports._ = {
133
151
  Endpoints,
134
152
  setEndpoint,
135
- filterUndefinedProperties,
136
153
  mapEventTimerPropertiesToString,
137
154
  roundTimeStamp,
138
155
  transformToObjectEntries,
@@ -22,7 +22,7 @@ declare enum Endpoints {
22
22
  /**
23
23
  * A method to asynchronously send metrics after initialization.
24
24
  */
25
- declare function bypassCommercialMetricsSampling(): void;
25
+ declare function bypassCommercialMetricsSampling(): Promise<void>;
26
26
  interface InitCommercialMetricsArgs {
27
27
  pageViewId: string;
28
28
  browserId: string | undefined;
@@ -38,11 +38,10 @@ interface InitCommercialMetricsArgs {
38
38
  * @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
39
39
  * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
40
40
  */
41
- declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling, }: InitCommercialMetricsArgs): boolean;
41
+ declare function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling, }: InitCommercialMetricsArgs): Promise<boolean>;
42
42
  export declare const _: {
43
43
  Endpoints: typeof Endpoints;
44
44
  setEndpoint: (isDev: boolean) => Endpoints;
45
- filterUndefinedProperties: <T>(transformedProperties: [string, T | undefined][]) => [string, T][];
46
45
  mapEventTimerPropertiesToString: (properties: Array<[string, string | number]>) => Property[];
47
46
  roundTimeStamp: (events: TimedEvent[]) => Metric[];
48
47
  transformToObjectEntries: (eventTimerProperties: EventProperties) => Array<[string, string | number | undefined]>;
@@ -1,3 +1,4 @@
1
+ import { onConsent } from '@guardian/consent-management-platform';
1
2
  import { log } from '@guardian/libs';
2
3
  import { EventTimer } from './event-timer';
3
4
  var Endpoints;
@@ -35,12 +36,6 @@ const transformToObjectEntries = (eventTimerProperties) => {
35
36
  // Transforms object {key: value} pairs into an array of [key, value] arrays
36
37
  return Object.entries(eventTimerProperties);
37
38
  };
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
39
  const mapEventTimerPropertiesToString = (properties) => {
45
40
  return properties.map(([name, value]) => ({
46
41
  name: String(name),
@@ -61,7 +56,7 @@ function gatherMetricsOnPageUnload() {
61
56
  // Assemble commercial properties and metrics
62
57
  const eventTimer = EventTimer.get();
63
58
  const transformedEntries = transformToObjectEntries(eventTimer.properties);
64
- const filteredEventTimerProperties = filterUndefinedProperties(transformedEntries);
59
+ const filteredEventTimerProperties = transformedEntries.filter((item) => typeof item[1] !== 'undefined');
65
60
  const mappedEventTimerProperties = mapEventTimerPropertiesToString(filteredEventTimerProperties);
66
61
  const properties = mappedEventTimerProperties
67
62
  .concat(devProperties)
@@ -89,15 +84,32 @@ const addVisibilityListeners = () => {
89
84
  // Safari does not reliably fire the `visibilitychange` on page unload.
90
85
  window.addEventListener('pagehide', listener);
91
86
  };
87
+ const checkConsent = async () => {
88
+ const consentState = await onConsent();
89
+ if (consentState.tcfv2) {
90
+ // TCFv2 mode - check for consent
91
+ const consents = consentState.tcfv2.consents;
92
+ const REQUIRED_CONSENTS = [7, 8];
93
+ return REQUIRED_CONSENTS.every((consent) => consents[consent]);
94
+ }
95
+ // non-TCFv2 mode - don't check for consent
96
+ return true;
97
+ };
92
98
  /**
93
99
  * A method to asynchronously send metrics after initialization.
94
100
  */
95
- function bypassCommercialMetricsSampling() {
101
+ async function bypassCommercialMetricsSampling() {
96
102
  if (!initialised) {
97
103
  console.warn('initCommercialMetrics not yet initialised');
98
104
  return;
99
105
  }
100
- addVisibilityListeners();
106
+ const consented = await checkConsent();
107
+ if (consented) {
108
+ addVisibilityListeners();
109
+ }
110
+ else {
111
+ log('commercial', "Metrics won't be sent because consent wasn't given");
112
+ }
101
113
  }
102
114
  /**
103
115
  * A method to initialise metrics.
@@ -107,7 +119,7 @@ function bypassCommercialMetricsSampling() {
107
119
  * @param init.adBlockerInUse - indicates whether or not an adblocker is being used.
108
120
  * @param init.sampling - rate at which to sample commercial metrics - the default is to send for 1% of pageviews
109
121
  */
110
- function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100, }) {
122
+ async function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, sampling = 1 / 100, }) {
111
123
  commercialMetricsPayload.page_view_id = pageViewId;
112
124
  commercialMetricsPayload.browser_id = browserId;
113
125
  setEndpoint(isDev);
@@ -119,15 +131,20 @@ function initCommercialMetrics({ pageViewId, browserId, isDev, adBlockerInUse, s
119
131
  initialised = true;
120
132
  const userIsInSamplingGroup = Math.random() <= sampling;
121
133
  if (isDev || userIsInSamplingGroup) {
122
- addVisibilityListeners();
123
- return true;
134
+ const consented = await checkConsent();
135
+ if (consented) {
136
+ addVisibilityListeners();
137
+ return true;
138
+ }
139
+ else {
140
+ log('commercial', "Metrics won't be sent because consent wasn't given");
141
+ }
124
142
  }
125
143
  return false;
126
144
  }
127
145
  export const _ = {
128
146
  Endpoints,
129
147
  setEndpoint,
130
- filterUndefinedProperties,
131
148
  mapEventTimerPropertiesToString,
132
149
  roundTimeStamp,
133
150
  transformToObjectEntries,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/commercial-core",
3
- "version": "3.3.0",
3
+ "version": "3.4.0",
4
4
  "description": "Guardian advertising business logic",
5
5
  "homepage": "https://github.com/guardian/commercial-core#readme",
6
6
  "bugs": {
@@ -45,7 +45,7 @@
45
45
  "@commitlint/cli": "^16.1.0",
46
46
  "@commitlint/config-conventional": "^16.0.0",
47
47
  "@guardian/ab-core": "^2.0.0",
48
- "@guardian/consent-management-platform": "^10.1.0",
48
+ "@guardian/consent-management-platform": "^10.5.0",
49
49
  "@guardian/eslint-config-typescript": "^1.0.0",
50
50
  "@guardian/libs": "3.6.1",
51
51
  "@guardian/prettier": "^0.7.0",