Skip to content

Commit cdd7273

Browse files
committed
Added css-events tracker parameter. Closes umami-software#1018.
1 parent 5ca6f76 commit cdd7273

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

tracker/index.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ import { removeTrailingSlash } from '../lib/url';
2222
const autoTrack = attr('data-auto-track') !== 'false';
2323
const dnt = attr('data-do-not-track');
2424
const useCache = attr('data-cache');
25+
const cssEvents = attr('data-css-events') !== 'false';
2526
const domain = attr('data-domains') || '';
2627
const domains = domain.split(',').map(n => n.trim());
2728

2829
const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/;
2930
const eventSelect = "[class*='umami--']";
3031
const cacheKey = 'umami.cache';
3132

32-
const disableTracking = () =>
33+
const trackingDisabled = () =>
3334
(localStorage && localStorage.getItem('umami.disabled')) ||
3435
(dnt && doNotTrack()) ||
3536
(domain && !domains.includes(hostname));
@@ -75,7 +76,7 @@ import { removeTrailingSlash } from '../lib/url';
7576
};
7677

7778
const collect = (type, payload) => {
78-
if (disableTracking()) return;
79+
if (trackingDisabled()) return;
7980

8081
post(
8182
`${root}/api/collect`,
@@ -150,14 +151,6 @@ import { removeTrailingSlash } from '../lib/url';
150151
});
151152
};
152153

153-
const monitorMutate = mutations => {
154-
mutations.forEach(mutation => {
155-
const element = mutation.target;
156-
addEvent(element);
157-
addEvents(element);
158-
});
159-
};
160-
161154
/* Handle history changes */
162155

163156
const handlePush = (state, title, url) => {
@@ -177,6 +170,19 @@ import { removeTrailingSlash } from '../lib/url';
177170
}
178171
};
179172

173+
const observeDocument = () => {
174+
const monitorMutate = mutations => {
175+
mutations.forEach(mutation => {
176+
const element = mutation.target;
177+
addEvent(element);
178+
addEvents(element);
179+
});
180+
};
181+
182+
const observer = new MutationObserver(monitorMutate);
183+
observer.observe(document, { childList: true, subtree: true });
184+
};
185+
180186
/* Global */
181187

182188
if (!window.umami) {
@@ -189,17 +195,18 @@ import { removeTrailingSlash } from '../lib/url';
189195

190196
/* Start */
191197

192-
if (autoTrack && !disableTracking()) {
198+
if (autoTrack && !trackingDisabled()) {
193199
history.pushState = hook(history, 'pushState', handlePush);
194200
history.replaceState = hook(history, 'replaceState', handlePush);
195201

196202
const update = () => {
197203
if (document.readyState === 'complete') {
198-
addEvents(document);
199204
trackView();
200205

201-
const observer = new MutationObserver(monitorMutate);
202-
observer.observe(document, { childList: true, subtree: true });
206+
if (cssEvents) {
207+
addEvents(document);
208+
observeDocument();
209+
}
203210
}
204211
};
205212

0 commit comments

Comments
 (0)