@guardian/commercial-core 5.4.1 → 5.4.2

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.
@@ -28,7 +28,6 @@ interface EventTimerProperties {
28
28
  type?: ConnectionType;
29
29
  downlink?: number;
30
30
  effectiveType?: string;
31
- offlineCount?: number;
32
31
  adSlotsInline?: number;
33
32
  adSlotsTotal?: number;
34
33
  pageHeightVH?: number;
@@ -93,9 +93,7 @@ class EventTimer {
93
93
  },
94
94
  ],
95
95
  };
96
- this.properties = {
97
- offlineCount: window.guardian.offlineCount,
98
- };
96
+ this.properties = {};
99
97
  if (window.navigator.connection) {
100
98
  this.properties.type = window.navigator.connection.type;
101
99
  this.properties.downlink = window.navigator.connection.downlink;
@@ -15,7 +15,6 @@ type EventProperties = {
15
15
  type?: ConnectionType;
16
16
  downlink?: number;
17
17
  effectiveType?: string;
18
- offlineCount?: number;
19
18
  };
20
19
  declare enum Endpoints {
21
20
  CODE = "//performance-events.code.dev-guardianapis.com/commercial-metrics",
@@ -55,6 +55,23 @@ function sendMetrics() {
55
55
  (0, libs_1.log)('commercial', 'About to send commercial metrics', commercialMetricsPayload);
56
56
  return navigator.sendBeacon(endpoint, JSON.stringify(commercialMetricsPayload));
57
57
  }
58
+ /**
59
+ * Gather how many times the user has experienced the “offline” event
60
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event
61
+ *
62
+ * This value should be fetched as late as possible in the page lifecycle,
63
+ * to get an accurate value.
64
+ *
65
+ * Relevant for an @guardian/open-journalism investigation.
66
+ */
67
+ const getOfflineCount = () => window.guardian.offlineCount
68
+ ? [
69
+ {
70
+ name: 'offlineCount',
71
+ value: window.guardian.offlineCount,
72
+ },
73
+ ]
74
+ : [];
58
75
  function gatherMetricsOnPageUnload() {
59
76
  // Assemble commercial properties and metrics
60
77
  const eventTimer = event_timer_1.EventTimer.get();
@@ -65,7 +82,7 @@ function gatherMetricsOnPageUnload() {
65
82
  .concat(devProperties)
66
83
  .concat(adBlockerProperties);
67
84
  commercialMetricsPayload.properties = properties;
68
- const metrics = roundTimeStamp(eventTimer.events);
85
+ const metrics = roundTimeStamp(eventTimer.events).concat(getOfflineCount());
69
86
  commercialMetricsPayload.metrics = metrics;
70
87
  sendMetrics();
71
88
  }
@@ -28,7 +28,6 @@ interface EventTimerProperties {
28
28
  type?: ConnectionType;
29
29
  downlink?: number;
30
30
  effectiveType?: string;
31
- offlineCount?: number;
32
31
  adSlotsInline?: number;
33
32
  adSlotsTotal?: number;
34
33
  pageHeightVH?: number;
@@ -90,9 +90,7 @@ class EventTimer {
90
90
  },
91
91
  ],
92
92
  };
93
- this.properties = {
94
- offlineCount: window.guardian.offlineCount,
95
- };
93
+ this.properties = {};
96
94
  if (window.navigator.connection) {
97
95
  this.properties.type = window.navigator.connection.type;
98
96
  this.properties.downlink = window.navigator.connection.downlink;
@@ -15,7 +15,6 @@ type EventProperties = {
15
15
  type?: ConnectionType;
16
16
  downlink?: number;
17
17
  effectiveType?: string;
18
- offlineCount?: number;
19
18
  };
20
19
  declare enum Endpoints {
21
20
  CODE = "//performance-events.code.dev-guardianapis.com/commercial-metrics",
@@ -52,6 +52,23 @@ function sendMetrics() {
52
52
  log('commercial', 'About to send commercial metrics', commercialMetricsPayload);
53
53
  return navigator.sendBeacon(endpoint, JSON.stringify(commercialMetricsPayload));
54
54
  }
55
+ /**
56
+ * Gather how many times the user has experienced the “offline” event
57
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Window/offline_event
58
+ *
59
+ * This value should be fetched as late as possible in the page lifecycle,
60
+ * to get an accurate value.
61
+ *
62
+ * Relevant for an @guardian/open-journalism investigation.
63
+ */
64
+ const getOfflineCount = () => window.guardian.offlineCount
65
+ ? [
66
+ {
67
+ name: 'offlineCount',
68
+ value: window.guardian.offlineCount,
69
+ },
70
+ ]
71
+ : [];
55
72
  function gatherMetricsOnPageUnload() {
56
73
  // Assemble commercial properties and metrics
57
74
  const eventTimer = EventTimer.get();
@@ -62,7 +79,7 @@ function gatherMetricsOnPageUnload() {
62
79
  .concat(devProperties)
63
80
  .concat(adBlockerProperties);
64
81
  commercialMetricsPayload.properties = properties;
65
- const metrics = roundTimeStamp(eventTimer.events);
82
+ const metrics = roundTimeStamp(eventTimer.events).concat(getOfflineCount());
66
83
  commercialMetricsPayload.metrics = metrics;
67
84
  sendMetrics();
68
85
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/commercial-core",
3
- "version": "5.4.1",
3
+ "version": "5.4.2",
4
4
  "description": "Guardian advertising business logic",
5
5
  "homepage": "https://github.com/guardian/commercial-core#readme",
6
6
  "bugs": {