-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmatomoTracking.test.js
More file actions
152 lines (120 loc) · 3.81 KB
/
Copy pathmatomoTracking.test.js
File metadata and controls
152 lines (120 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* @jest-environment jsdom
*/
import {
getMatomoTrackableElement,
handleMatomoTrackingClick,
initializeMatomoTracking,
normalizeMatomoTrackableElements,
} from "./matomoTracking";
describe("matomoTracking", () => {
beforeEach(() => {
document.body.innerHTML = "";
window._paq = [];
});
afterEach(() => {
delete window._paq;
});
it("normalizes trackable elements with derived aria-label names", () => {
document.body.innerHTML = `
<button aria-label="Open menu">
<span>Ignored text</span>
</button>
`;
normalizeMatomoTrackableElements();
const button = document.querySelector("button");
expect(button.dataset.matomoCategory).toBe("UI Interaction");
expect(button.dataset.matomoAction).toBe("Click");
expect(button.dataset.matomoName).toBe("Open menu");
});
it("preserves explicitly provided matomo attributes", () => {
document.body.innerHTML = `
<a
href="/example"
data-matomo-category="Custom category"
data-matomo-action="Open"
data-matomo-name="Custom name"
>
Read more
</a>
`;
normalizeMatomoTrackableElements();
const link = document.querySelector("a");
expect(link.dataset.matomoCategory).toBe("Custom category");
expect(link.dataset.matomoAction).toBe("Open");
expect(link.dataset.matomoName).toBe("Custom name");
});
it("finds the closest trackable element for nested CTA content", () => {
document.body.innerHTML = `
<a
href="/example"
data-matomo-category="UI Interaction"
data-matomo-action="Click"
>
<span class="c-button__label-text">Read more</span>
</a>
`;
const target = document.querySelector(".c-button__label-text");
const trackableElement = getMatomoTrackableElement(target);
expect(trackableElement).toBe(document.querySelector("a"));
});
it("tracks clicks for delegated button-like links", () => {
document.body.innerHTML = `
<a
href="/example"
data-matomo-category="UI Interaction"
data-matomo-action="Click"
>
<span class="c-button__label-text">Read more</span>
</a>
`;
initializeMatomoTracking();
const target = document.querySelector(".c-button__label-text");
target.dispatchEvent(new MouseEvent("click", { bubbles: true }));
expect(window._paq).toEqual([
["trackEvent", "UI Interaction", "Click", "Read more"],
]);
expect(document.querySelector("a").dataset.matomoName).toBe("Read more");
});
it("does not track links without matomo tracking attributes", () => {
document.body.innerHTML = `
<a class="c-button" href="/example">
<span class="c-button__label-text">Read more</span>
</a>
`;
initializeMatomoTracking();
document
.querySelector(".c-button__label-text")
.dispatchEvent(new MouseEvent("click", { bubbles: true }));
expect(window._paq).toEqual([]);
});
it("tracks dynamically injected buttons through delegated listeners", () => {
initializeMatomoTracking();
const button = document.createElement("button");
button.textContent = "Dynamic CTA";
document.body.appendChild(button);
button.dispatchEvent(new MouseEvent("click", { bubbles: true }));
expect(window._paq).toEqual([
["trackEvent", "UI Interaction", "Click", "Dynamic CTA"],
]);
expect(button.dataset.matomoName).toBe("Dynamic CTA");
});
it("returns a cleanup function that removes delegated tracking", () => {
document.body.innerHTML = '<button>Track me</button>';
const cleanup = initializeMatomoTracking();
cleanup();
document
.querySelector("button")
.dispatchEvent(new MouseEvent("click", { bubbles: true }));
expect(window._paq).toEqual([]);
});
it("does not push events when the tracking queue is unavailable", () => {
document.body.innerHTML = '<button>Track me</button>';
delete window._paq;
expect(() =>
handleMatomoTrackingClick({
target: document.querySelector("button"),
}),
).not.toThrow();
});
});