@guardian/ophan-tracker-js 4.0.0 → 4.0.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.
@@ -17,6 +17,7 @@ const EVENTS = [
17
17
  'focus',
18
18
  'click',
19
19
  'scroll',
20
+ 'wheel',
20
21
  'mousemove',
21
22
  'touchstart',
22
23
  'touchend',
@@ -26,6 +27,7 @@ const EVENTS = [
26
27
  'keyup',
27
28
  'keydown',
28
29
  ];
30
+ const USECAPTURE = true;
29
31
  const REPORTINGINTERVAL = 10000;
30
32
  // total attention time so far on this page
31
33
  let totalAttentionMs = 0;
@@ -106,7 +108,7 @@ const initComponent = (name, el, visibilityThreshold = 0.5, isTrackingVideo = fa
106
108
  };
107
109
  const initAttention = (visibility) => {
108
110
  EVENTS.forEach((event) => {
109
- window.addEventListener(event, makeActive);
111
+ window.addEventListener(event, makeActive, USECAPTURE);
110
112
  });
111
113
  document.addEventListener(visibility.changeEvent, () => {
112
114
  if (visibility.state() === 'visible') {
@@ -71,6 +71,30 @@ describe('attention.js', () => {
71
71
  mockTimeouts = [];
72
72
  mockIntervals = [];
73
73
  });
74
+ test('should register user interaction listeners in the capture phase', () => {
75
+ const mockVisibility = {
76
+ changeEvent: 'visibilitychange',
77
+ state: jest.fn(() => 'visible'),
78
+ };
79
+ attention.init(mockVisibility);
80
+ const interactionEvents = [
81
+ 'focus',
82
+ 'click',
83
+ 'mousemove',
84
+ 'scroll',
85
+ 'touchstart',
86
+ 'touchend',
87
+ 'touchcancel',
88
+ 'touchleave',
89
+ 'touchmove',
90
+ 'wheel',
91
+ 'keyup',
92
+ 'keydown',
93
+ ];
94
+ for (const event of interactionEvents) {
95
+ expect(window.addEventListener).toHaveBeenCalledWith(event, expect.any(Function), true);
96
+ }
97
+ });
74
98
  describe('initComponent', () => {
75
99
  test('should register component with default parameters', () => {
76
100
  const mockElement = document.createElement('div');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/ophan-tracker-js",
3
- "version": "4.0.0",
3
+ "version": "4.0.2",
4
4
  "repository": {
5
5
  "url": "git+https://github.com/guardian/ophan.git",
6
6
  "directory": "tracker-js"