@guardian/ophan-tracker-js 2.3.2 → 2.4.1
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,11 @@ 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
|
+
sendInitialEvent(httpStatus, location.href, window.document.referrer, 'bfcache');
|
|
15
|
+
}
|
|
16
|
+
}, false);
|
|
12
17
|
if (visibility.state() !== 'prerender') {
|
|
13
18
|
return sendInitialEvent(httpStatus);
|
|
14
19
|
}
|
|
@@ -65,7 +70,7 @@ const getNavigationType = () => {
|
|
|
65
70
|
}
|
|
66
71
|
return null;
|
|
67
72
|
};
|
|
68
|
-
sendInitialEvent = function (httpStatus, url = location.href, referrer = window.document.referrer) {
|
|
73
|
+
sendInitialEvent = function (httpStatus, url = location.href, referrer = window.document.referrer, loadType = 'normal') {
|
|
69
74
|
const navigationType = getNavigationType();
|
|
70
75
|
// send initial event
|
|
71
76
|
const event = {
|
|
@@ -84,6 +89,9 @@ sendInitialEvent = function (httpStatus, url = location.href, referrer = window.
|
|
|
84
89
|
// Add navigation type as metadata to the event if it's not null
|
|
85
90
|
event.navigationType = navigationType;
|
|
86
91
|
}
|
|
92
|
+
if (loadType !== 'normal') {
|
|
93
|
+
event.loadType = loadType;
|
|
94
|
+
}
|
|
87
95
|
appendContentType(event);
|
|
88
96
|
appendDataFromLocalStorageToEvent(event);
|
|
89
97
|
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,49 @@ 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 not call sendInitialEvent when pageshow event has persisted=false', () => {
|
|
198
|
+
expect(pageShowHandler).toBeTruthy();
|
|
199
|
+
transmit.sendInitial.mockClear();
|
|
200
|
+
const mockEvent = { persisted: false };
|
|
201
|
+
pageShowHandler(mockEvent);
|
|
202
|
+
expect(transmit.sendInitial).not.toHaveBeenCalled();
|
|
203
|
+
});
|
|
204
|
+
it('should handle pageshow event without persisted property', () => {
|
|
205
|
+
expect(pageShowHandler).toBeTruthy();
|
|
206
|
+
transmit.sendInitial.mockClear();
|
|
207
|
+
const mockEvent = {};
|
|
208
|
+
pageShowHandler(mockEvent);
|
|
209
|
+
expect(transmit.sendInitial).not.toHaveBeenCalled();
|
|
210
|
+
});
|
|
211
|
+
});
|
|
@@ -63,7 +63,11 @@ export type TAction =
|
|
|
63
63
|
/**
|
|
64
64
|
* The component is returned to its original location
|
|
65
65
|
**/
|
|
66
|
-
| 'RETURN'
|
|
66
|
+
| 'RETURN'
|
|
67
|
+
/**
|
|
68
|
+
* The component has detected something
|
|
69
|
+
*/
|
|
70
|
+
| 'DETECT';
|
|
67
71
|
interface ComponentV2 {
|
|
68
72
|
componentType: TComponentType;
|
|
69
73
|
id?: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export type TComponentType =
|
|
1
|
+
export type TComponentType = 'READERS_QUESTIONS_ATOM' | 'QANDA_ATOM' | 'PROFILE_ATOM' | 'GUIDE_ATOM' | 'TIMELINE_ATOM' | 'NEWSLETTER_SUBSCRIPTION' | 'SURVEYS_QUESTIONS' | 'ACQUISITIONS_EPIC' | 'ACQUISITIONS_ENGAGEMENT_BANNER' | 'ACQUISITIONS_THANK_YOU_EPIC' | 'ACQUISITIONS_HEADER' | 'ACQUISITIONS_FOOTER' | 'ACQUISITIONS_INTERACTIVE_SLICE' | 'ACQUISITIONS_NUGGET' | 'ACQUISITIONS_STANDFIRST' | 'ACQUISITIONS_THRASHER' | 'ACQUISITIONS_EDITORIAL_LINK' | 'ACQUISITIONS_MANAGE_MY_ACCOUNT' | 'ACQUISITIONS_BUTTON' | 'ACQUISITIONS_OTHER' | 'APP_ADVERT' | 'APP_AUDIO' | 'APP_BUTTON' | 'APP_CARD' | 'APP_CROSSWORDS' | 'APP_ENGAGEMENT_BANNER' | 'APP_EPIC' | 'APP_GALLERY' | 'APP_LINK' | 'APP_NAVIGATION_ITEM' | 'APP_SCREEN' | 'APP_THRASHER' | 'APP_VIDEO' | 'AUDIO_ATOM' | 'CHART_ATOM' | 'ACQUISITIONS_MERCHANDISING' | 'ACQUISITIONS_HOUSE_ADS' | 'SIGN_IN_GATE' | 'ACQUISITIONS_SUBSCRIPTIONS_BANNER' | 'MOBILE_STICKY_AD' | 'IDENTITY_AUTHENTICATION' | 'RETENTION_ENGAGEMENT_BANNER' | 'ACQUISITION_SUPPORT_SITE' | 'RETENTION_EPIC' | 'CONSENT' | 'LIVE_BLOG_PINNED_POST' | 'STICKY_VIDEO' | 'KEY_EVENT_CARD' | 'RETENTION_HEADER' | 'SLIDESHOW' | 'APP_FEATURE' | 'CARD' | 'CAROUSEL' | 'CONTAINER' | 'MENU' | 'ACQUISITIONS_GUTTER' | 'INTERACTIVE_ATOM' | 'LOOP_VIDEO' | 'AD_BLOCK_RECOVERY';
|
|
@@ -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;
|