@guardian/ophan-tracker-js 2.2.1 → 2.2.3
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.
|
@@ -43,6 +43,10 @@ if (typeof document.addEventListener === 'function') {
|
|
|
43
43
|
document.addEventListener('click', function (e) {
|
|
44
44
|
let anchorTarget, info;
|
|
45
45
|
anchorTarget = validAncestorAnchorElement(e.target);
|
|
46
|
+
if (!anchorTarget) {
|
|
47
|
+
// If no anchor element was clicked, exit early.
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
46
50
|
info = {
|
|
47
51
|
from: [location.protocol, '//', location.host, location.pathname].join(''),
|
|
48
52
|
to: anchorTarget ? anchorTarget.href : void 0,
|
|
@@ -1,12 +1,17 @@
|
|
|
1
|
-
import clickPathCapture, {
|
|
1
|
+
import clickPathCapture, { getContainingComponent, validAncestorAnchorElement } from '../click-path-capture.js';
|
|
2
|
+
import ophan from '../core.js';
|
|
3
|
+
import transmit from '../transmit.js';
|
|
2
4
|
const { getDataLinkNames } = clickPathCapture;
|
|
3
5
|
describe('click-path-capture.js', () => {
|
|
4
|
-
|
|
6
|
+
beforeEach(() => {
|
|
5
7
|
document.body.innerHTML = `
|
|
6
8
|
<div data-component="component1">
|
|
7
|
-
<
|
|
8
|
-
<
|
|
9
|
-
|
|
9
|
+
<div id="testElement4">
|
|
10
|
+
<a href="#" data-link-name="link1" id="link1">
|
|
11
|
+
<span id="testElement1"></span>
|
|
12
|
+
</a>
|
|
13
|
+
<span id="nonLinkElement">Non-link element</span>
|
|
14
|
+
</div>
|
|
10
15
|
</div>
|
|
11
16
|
<div id="testElement2" data-component="component2"></div>
|
|
12
17
|
`;
|
|
@@ -30,6 +35,19 @@ describe('click-path-capture.js', () => {
|
|
|
30
35
|
const element = document.createElement('div');
|
|
31
36
|
expect(getContainingComponent(element)).toBeNull();
|
|
32
37
|
});
|
|
38
|
+
test('should return the closest ancestor element with data-component attribute', () => {
|
|
39
|
+
document.body.innerHTML = `
|
|
40
|
+
<div data-component="outer-component">
|
|
41
|
+
<div data-component="nested-component">
|
|
42
|
+
<a href="#" data-link-name="link1" id="link1">
|
|
43
|
+
<span id="testElement1"></span>
|
|
44
|
+
</a>
|
|
45
|
+
</div>
|
|
46
|
+
</div>
|
|
47
|
+
`;
|
|
48
|
+
const element = document.getElementById('testElement1');
|
|
49
|
+
expect(getContainingComponent(element)).toBe("nested-component");
|
|
50
|
+
});
|
|
33
51
|
});
|
|
34
52
|
describe('getDataLinkNames', () => {
|
|
35
53
|
test('should collect data-link-name attributes from ancestors', () => {
|
|
@@ -41,4 +59,26 @@ describe('click-path-capture.js', () => {
|
|
|
41
59
|
expect(getDataLinkNames(element)).toEqual([]);
|
|
42
60
|
});
|
|
43
61
|
});
|
|
62
|
+
describe('click event handling', () => {
|
|
63
|
+
let transmitSendMoreSpy, ophanStoreDataToSendOnNextEventSpy;
|
|
64
|
+
beforeEach(() => {
|
|
65
|
+
transmitSendMoreSpy = jest.spyOn(transmit, 'sendMore').mockImplementation(() => { });
|
|
66
|
+
ophanStoreDataToSendOnNextEventSpy = jest.spyOn(ophan, 'storeDataToSendOnNextEvent').mockImplementation(() => { });
|
|
67
|
+
});
|
|
68
|
+
afterEach(() => {
|
|
69
|
+
jest.clearAllMocks();
|
|
70
|
+
});
|
|
71
|
+
test('should send request and store data when a link is clicked', () => {
|
|
72
|
+
const element = document.getElementById('testElement1');
|
|
73
|
+
element.click();
|
|
74
|
+
expect(transmitSendMoreSpy).toHaveBeenCalled();
|
|
75
|
+
expect(ophanStoreDataToSendOnNextEventSpy).toHaveBeenCalled();
|
|
76
|
+
});
|
|
77
|
+
test('should not send request when a non-link element within a container is clicked', () => {
|
|
78
|
+
const element = document.getElementById('testElement4');
|
|
79
|
+
element.click();
|
|
80
|
+
expect(transmitSendMoreSpy).not.toHaveBeenCalled();
|
|
81
|
+
expect(ophanStoreDataToSendOnNextEventSpy).not.toHaveBeenCalled();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
44
84
|
});
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -42,7 +42,7 @@ You can see example usage of the library on theguardian.com [here](https://githu
|
|
|
42
42
|
|
|
43
43
|
### Usage with Typescript
|
|
44
44
|
|
|
45
|
-
Refer to the type definitions within the library for the structure of valid tracking events. The `ophan.record()` function accepts a type of `EventPayload` found [here]("
|
|
45
|
+
Refer to the type definitions within the library for the structure of valid tracking events. The `ophan.record()` function accepts a type of `EventPayload` found [here]("https://github.com/guardian/ophan/blob/main/tracker-js/src/types/event.ts#L90").
|
|
46
46
|
|
|
47
47
|
#### Example Usage
|
|
48
48
|
|