-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmatomoTracking.js
More file actions
150 lines (126 loc) · 3.59 KB
/
Copy pathmatomoTracking.js
File metadata and controls
150 lines (126 loc) · 3.59 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
const matomoTrackingSelector = [
"button",
"[data-matomo-category]",
].join(", ");
const defaultTrackingCategory = "UI Interaction";
const defaultTrackingAction = "Click";
const handleMatomoTrackingDomReady = () => normalizeMatomoTrackableElements();
/**
* Normalize whitespace in tracking labels.
*
* @param {string | null | undefined} value The label to normalize.
* @returns {string} The normalized label.
*/
export function normalizeTrackingLabel(value) {
return (value || "").replace(/\s+/g, " ").trim();
}
/**
* Derive a Matomo tracking name for a CTA element.
*
* @param {Element} element The element to derive a name from.
* @returns {string} The derived tracking name.
*/
export function getMatomoTrackingName(element) {
return normalizeTrackingLabel(
element.getAttribute("data-matomo-name") ||
element.getAttribute("aria-label") ||
element.getAttribute("title") ||
element.innerText ||
element.textContent,
);
}
/**
* Apply default Matomo data attributes unless they are explicitly set.
*
* @param {HTMLElement} element The element to normalize.
* @returns {void}
*/
export function applyMatomoTrackingAttributes(element) {
const trackingName = getMatomoTrackingName(element);
if (!element.dataset.matomoCategory) {
element.dataset.matomoCategory = defaultTrackingCategory;
}
if (!element.dataset.matomoAction) {
element.dataset.matomoAction = defaultTrackingAction;
}
if (!element.dataset.matomoName && trackingName) {
element.dataset.matomoName = trackingName;
}
}
/**
* Find a trackable CTA element from an event target.
*
* @param {EventTarget | null} target The event target.
* @returns {HTMLElement | null} The closest trackable element.
*/
export function getMatomoTrackableElement(target) {
if (!(target instanceof Element)) {
return null;
}
const element = target.closest(matomoTrackingSelector);
return element instanceof HTMLElement ? element : null;
}
/**
* Normalize trackable elements in the current DOM.
*
* @returns {void}
*/
export function normalizeMatomoTrackableElements() {
document
.querySelectorAll(matomoTrackingSelector)
.forEach((element) =>
applyMatomoTrackingAttributes(element),
);
}
/**
* Handle delegated click tracking for Matomo.
*
* @param {MouseEvent} event The click event.
* @returns {void}
*/
export function handleMatomoTrackingClick(event) {
const trackableElement = getMatomoTrackableElement(event.target);
if (!trackableElement) {
return;
}
applyMatomoTrackingAttributes(trackableElement);
const trackingQueue = window._paq;
if (!trackingQueue || typeof trackingQueue.push !== "function") {
return;
}
const trackingName = trackableElement.dataset.matomoName || "";
if (!trackingName) {
return;
}
trackingQueue.push([
"trackEvent",
trackableElement.dataset.matomoCategory || defaultTrackingCategory,
trackableElement.dataset.matomoAction || defaultTrackingAction,
trackingName,
]);
}
/**
* Initialize global Matomo CTA tracking.
*
* @returns {() => void} Cleanup function for removing tracking listeners.
*/
export function initializeMatomoTracking() {
let hasDomReadyListener = false;
document.addEventListener("click", handleMatomoTrackingClick);
if (document.readyState === "loading") {
hasDomReadyListener = true;
document.addEventListener(
"DOMContentLoaded",
handleMatomoTrackingDomReady,
{ once: true },
);
} else {
normalizeMatomoTrackableElements();
}
return () => {
document.removeEventListener("click", handleMatomoTrackingClick);
if (hasDomReadyListener) {
document.removeEventListener("DOMContentLoaded", handleMatomoTrackingDomReady);
}
};
}