@guardian/ophan-tracker-js 2.4.0 → 2.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.
package/NPM-dist/core.js CHANGED
@@ -9,6 +9,12 @@ let platform = null;
9
9
  const MaximumComponentNameLength = 50;
10
10
  init = function (servingPlatform, httpStatus) {
11
11
  platform = servingPlatform;
12
+ window.addEventListener('pageshow', function (event) {
13
+ if (event.persisted) {
14
+ transmit.bumpViewId();
15
+ sendInitialEvent(httpStatus, location.href, window.document.referrer, 'bfcache');
16
+ }
17
+ }, false);
12
18
  if (visibility.state() !== 'prerender') {
13
19
  return sendInitialEvent(httpStatus);
14
20
  }
@@ -65,7 +71,7 @@ const getNavigationType = () => {
65
71
  }
66
72
  return null;
67
73
  };
68
- sendInitialEvent = function (httpStatus, url = location.href, referrer = window.document.referrer) {
74
+ sendInitialEvent = function (httpStatus, url = location.href, referrer = window.document.referrer, loadType = 'normal') {
69
75
  const navigationType = getNavigationType();
70
76
  // send initial event
71
77
  const event = {
@@ -84,6 +90,9 @@ sendInitialEvent = function (httpStatus, url = location.href, referrer = window.
84
90
  // Add navigation type as metadata to the event if it's not null
85
91
  event.navigationType = navigationType;
86
92
  }
93
+ if (loadType !== 'normal') {
94
+ event.loadType = loadType;
95
+ }
87
96
  appendContentType(event);
88
97
  appendDataFromLocalStorageToEvent(event);
89
98
  transmit.sendInitial(event);
@@ -1,7 +1,11 @@
1
1
  import { _testExports } from '../core.js';
2
2
  import transmit from '../transmit';
3
3
  const mockSendMore = jest.fn();
4
+ const mockSendInitial = jest.fn();
5
+ const mockBumpViewId = jest.fn();
4
6
  transmit.sendMore = mockSendMore;
7
+ transmit.sendInitial = mockSendInitial;
8
+ transmit.bumpViewId = mockBumpViewId;
5
9
  const { getQueryParameterValue, appendComponentDataToEvent, trimComponentNamesToUrlSafeLength, onLoadCapture, onLoadCaptures, processCaptures, appendContentType, } = _testExports;
6
10
  describe('appendComponentDataToEvent', () => {
7
11
  beforeEach(() => {
@@ -159,3 +163,57 @@ describe('appendContentType', () => {
159
163
  expect(event.contentType).toBeUndefined();
160
164
  });
161
165
  });
166
+ describe('loadType pageshow handling', () => {
167
+ let mockAddEventListener;
168
+ let pageShowHandler;
169
+ let originalSendInitial;
170
+ beforeEach(() => {
171
+ jest.clearAllMocks();
172
+ mockAddEventListener = jest.fn();
173
+ window.addEventListener = mockAddEventListener;
174
+ originalSendInitial = transmit.sendInitial;
175
+ transmit.sendInitial = jest.fn();
176
+ const core = require('../core.js').default;
177
+ core.init('next-gen', 200);
178
+ const pageShowCall = mockAddEventListener.mock.calls.find(call => call[0] === 'pageshow');
179
+ pageShowHandler = pageShowCall ? pageShowCall[1] : null;
180
+ });
181
+ afterEach(() => {
182
+ transmit.sendInitial = originalSendInitial;
183
+ });
184
+ it('should register pageshow event listener during init', () => {
185
+ expect(mockAddEventListener).toHaveBeenCalledWith('pageshow', expect.any(Function), false);
186
+ });
187
+ it('should call sendInitialEvent with loadType=bfcache when pageshow event has persisted=true', () => {
188
+ expect(pageShowHandler).toBeTruthy();
189
+ transmit.sendInitial.mockClear();
190
+ // Simulate a bfcache restore event
191
+ const mockEvent = { persisted: true };
192
+ pageShowHandler(mockEvent);
193
+ expect(transmit.sendInitial).toHaveBeenCalledWith(expect.objectContaining({
194
+ loadType: 'bfcache'
195
+ }));
196
+ });
197
+ it('should call bumpViewId when pageshow event has persisted=true', () => {
198
+ expect(pageShowHandler).toBeTruthy();
199
+ transmit.bumpViewId.mockClear();
200
+ // Simulate a bfcache restore event
201
+ const mockEvent = { persisted: true };
202
+ pageShowHandler(mockEvent);
203
+ expect(transmit.bumpViewId).toHaveBeenCalledTimes(1);
204
+ });
205
+ it('should not call sendInitialEvent when pageshow event has persisted=false', () => {
206
+ expect(pageShowHandler).toBeTruthy();
207
+ transmit.sendInitial.mockClear();
208
+ const mockEvent = { persisted: false };
209
+ pageShowHandler(mockEvent);
210
+ expect(transmit.sendInitial).not.toHaveBeenCalled();
211
+ });
212
+ it('should handle pageshow event without persisted property', () => {
213
+ expect(pageShowHandler).toBeTruthy();
214
+ transmit.sendInitial.mockClear();
215
+ const mockEvent = {};
216
+ pageShowHandler(mockEvent);
217
+ expect(transmit.sendInitial).not.toHaveBeenCalled();
218
+ });
219
+ });
@@ -31,6 +31,8 @@ interface PageView {
31
31
  subscriptionType?: TSubscriptionType;
32
32
  membershipTier?: TMembershipTier;
33
33
  navigationType?: PerformanceNavigationTiming['type'];
34
+ /** The type of page load - normal or from cache */
35
+ loadType?: 'normal' | 'bfcache';
34
36
  from?: string;
35
37
  to?: string;
36
38
  tz?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guardian/ophan-tracker-js",
3
- "version": "2.4.0",
3
+ "version": "2.4.2",
4
4
  "type": "module",
5
5
  "engines": {
6
6
  "node": ">=16"