@guardian/ophan-tracker-js 2.2.4 → 2.2.5
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.
|
@@ -41,28 +41,43 @@ const getDataLinkNames = (el, dataLinkNames = []) => {
|
|
|
41
41
|
};
|
|
42
42
|
if (typeof document.addEventListener === 'function') {
|
|
43
43
|
document.addEventListener('click', function (e) {
|
|
44
|
-
let
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
44
|
+
let target = e.target;
|
|
45
|
+
let info;
|
|
46
|
+
// For anchors, use validAncestorAnchorElement
|
|
47
|
+
const anchorTarget = validAncestorAnchorElement(target);
|
|
48
|
+
const nonTrackableElements = [
|
|
49
|
+
'div',
|
|
50
|
+
'span',
|
|
51
|
+
'p',
|
|
52
|
+
'section',
|
|
53
|
+
'article',
|
|
54
|
+
'main',
|
|
55
|
+
'header',
|
|
56
|
+
'footer',
|
|
57
|
+
'nav',
|
|
58
|
+
];
|
|
59
|
+
if (nonTrackableElements.includes(target.tagName.toLowerCase()) &&
|
|
60
|
+
!anchorTarget) {
|
|
48
61
|
return;
|
|
49
62
|
}
|
|
63
|
+
// If it's an anchor, use anchorTarget, otherwise use the original target
|
|
64
|
+
const trackingTarget = anchorTarget || target;
|
|
50
65
|
info = {
|
|
51
66
|
from: [location.protocol, '//', location.host, location.pathname].join(''),
|
|
52
|
-
to:
|
|
53
|
-
referringComponent: getContainingComponent(
|
|
54
|
-
referringDataLinkNames: getDataLinkNames(
|
|
67
|
+
to: trackingTarget.href || undefined,
|
|
68
|
+
referringComponent: getContainingComponent(trackingTarget),
|
|
69
|
+
referringDataLinkNames: getDataLinkNames(trackingTarget),
|
|
55
70
|
refPlatform: ophan.servingPlatform(),
|
|
56
71
|
refViewId: ophan.viewId,
|
|
57
72
|
};
|
|
58
|
-
if (info.referringDataLinkNames
|
|
73
|
+
if (info.referringDataLinkNames && info.referringDataLinkNames.length > 0) {
|
|
59
74
|
transmit.sendMore({
|
|
60
75
|
clickComponent: info.referringComponent,
|
|
61
76
|
clickLinkNames: info.referringDataLinkNames,
|
|
62
77
|
});
|
|
63
78
|
}
|
|
64
|
-
if (anchorTarget
|
|
65
|
-
//
|
|
79
|
+
if (anchorTarget) {
|
|
80
|
+
// If it's an anchor, we may be about to navigate away from this page, so store to send on next request
|
|
66
81
|
return ophan.storeDataToSendOnNextEvent(info);
|
|
67
82
|
}
|
|
68
83
|
}, false);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import clickPathCapture, { getContainingComponent, validAncestorAnchorElement } from '../click-path-capture.js';
|
|
1
|
+
import clickPathCapture, { getContainingComponent, validAncestorAnchorElement, } from '../click-path-capture.js';
|
|
2
2
|
import ophan from '../core.js';
|
|
3
3
|
import transmit from '../transmit.js';
|
|
4
4
|
const { getDataLinkNames } = clickPathCapture;
|
|
@@ -11,6 +11,8 @@ describe('click-path-capture.js', () => {
|
|
|
11
11
|
<span id="testElement1"></span>
|
|
12
12
|
</a>
|
|
13
13
|
<span id="nonLinkElement">Non-link element</span>
|
|
14
|
+
<button id="testButton" data-link-name="buttonLink" data-component="buttonComponent">Click Me</button>
|
|
15
|
+
<label id="testLabel" data-link-name="labelLink" data-component="labelComponent">Label Text</label>
|
|
14
16
|
</div>
|
|
15
17
|
</div>
|
|
16
18
|
<div id="testElement2" data-component="component2"></div>
|
|
@@ -46,7 +48,7 @@ describe('click-path-capture.js', () => {
|
|
|
46
48
|
</div>
|
|
47
49
|
`;
|
|
48
50
|
const element = document.getElementById('testElement1');
|
|
49
|
-
expect(getContainingComponent(element)).toBe(
|
|
51
|
+
expect(getContainingComponent(element)).toBe('nested-component');
|
|
50
52
|
});
|
|
51
53
|
});
|
|
52
54
|
describe('getDataLinkNames', () => {
|
|
@@ -62,8 +64,13 @@ describe('click-path-capture.js', () => {
|
|
|
62
64
|
describe('click event handling', () => {
|
|
63
65
|
let transmitSendMoreSpy, ophanStoreDataToSendOnNextEventSpy;
|
|
64
66
|
beforeEach(() => {
|
|
65
|
-
|
|
66
|
-
|
|
67
|
+
jest.clearAllMocks();
|
|
68
|
+
transmitSendMoreSpy = jest
|
|
69
|
+
.spyOn(transmit, 'sendMore')
|
|
70
|
+
.mockImplementation(() => { });
|
|
71
|
+
ophanStoreDataToSendOnNextEventSpy = jest
|
|
72
|
+
.spyOn(ophan, 'storeDataToSendOnNextEvent')
|
|
73
|
+
.mockImplementation(() => { });
|
|
67
74
|
});
|
|
68
75
|
afterEach(() => {
|
|
69
76
|
jest.clearAllMocks();
|
|
@@ -71,9 +78,36 @@ describe('click-path-capture.js', () => {
|
|
|
71
78
|
test('should send request and store data when a link is clicked', () => {
|
|
72
79
|
const element = document.getElementById('testElement1');
|
|
73
80
|
element.click();
|
|
74
|
-
expect(transmitSendMoreSpy).
|
|
81
|
+
expect(transmitSendMoreSpy).toHaveBeenCalledWith({
|
|
82
|
+
clickComponent: 'component1',
|
|
83
|
+
clickLinkNames: ['link1'],
|
|
84
|
+
});
|
|
75
85
|
expect(ophanStoreDataToSendOnNextEventSpy).toHaveBeenCalled();
|
|
76
86
|
});
|
|
87
|
+
test('should send request but not store data when a button is clicked', () => {
|
|
88
|
+
const element = document.getElementById('testButton');
|
|
89
|
+
element.click();
|
|
90
|
+
expect(transmitSendMoreSpy).toHaveBeenCalledWith({
|
|
91
|
+
clickComponent: 'buttonComponent',
|
|
92
|
+
clickLinkNames: ['buttonLink'],
|
|
93
|
+
});
|
|
94
|
+
expect(ophanStoreDataToSendOnNextEventSpy).not.toHaveBeenCalled();
|
|
95
|
+
});
|
|
96
|
+
test('should send request but not store data when a label is clicked', () => {
|
|
97
|
+
const element = document.getElementById('testLabel');
|
|
98
|
+
element.click();
|
|
99
|
+
expect(transmitSendMoreSpy).toHaveBeenCalledWith({
|
|
100
|
+
clickComponent: 'labelComponent',
|
|
101
|
+
clickLinkNames: ['labelLink'],
|
|
102
|
+
});
|
|
103
|
+
expect(ophanStoreDataToSendOnNextEventSpy).not.toHaveBeenCalled();
|
|
104
|
+
});
|
|
105
|
+
test('should not send request when a non-trackable element is clicked', () => {
|
|
106
|
+
const element = document.getElementById('nonLinkElement');
|
|
107
|
+
element.click();
|
|
108
|
+
expect(transmitSendMoreSpy).not.toHaveBeenCalled();
|
|
109
|
+
expect(ophanStoreDataToSendOnNextEventSpy).not.toHaveBeenCalled();
|
|
110
|
+
});
|
|
77
111
|
test('should not send request when a non-link element within a container is clicked', () => {
|
|
78
112
|
const element = document.getElementById('testElement4');
|
|
79
113
|
element.click();
|