mirror of
https://github.com/kremalicious/umami.git
synced 2024-11-15 17:55:08 +01:00
QoL changes on tracker
- Remove redundant checks and returns - Add an additional check for local storage (some browsers still lack it) - Using some better practices - Use MutationObserver for events - Add initial events on DOM full render, send first trackView on complete readyState
This commit is contained in:
parent
92b205b7b7
commit
cbf64e561a
@ -44,17 +44,14 @@ export const hook = (_this, method, callback) => {
|
|||||||
export const doNotTrack = () => {
|
export const doNotTrack = () => {
|
||||||
const { doNotTrack, navigator, external } = window;
|
const { doNotTrack, navigator, external } = window;
|
||||||
|
|
||||||
|
const msTrackProtection = 'msTrackingProtectionEnabled';
|
||||||
const msTracking = () => {
|
const msTracking = () => {
|
||||||
return (
|
return external && msTrackProtection in external && external[msTrackProtection]();
|
||||||
external &&
|
|
||||||
typeof external.msTrackingProtectionEnabled === 'function' &&
|
|
||||||
external.msTrackingProtectionEnabled()
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const dnt = doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack || msTracking();
|
const dnt = doNotTrack || navigator.doNotTrack || navigator.msDoNotTrack || msTracking();
|
||||||
|
|
||||||
return dnt === true || dnt === 1 || dnt === 'yes' || dnt === '1';
|
return dnt == '1' || dnt === 'yes';
|
||||||
};
|
};
|
||||||
|
|
||||||
export const setItem = (key, data, session) => {
|
export const setItem = (key, data, session) => {
|
||||||
|
@ -16,28 +16,29 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
|
|
||||||
if (!script) return;
|
if (!script) return;
|
||||||
|
|
||||||
const attr = key => script && script.getAttribute(key);
|
const attr = script.getAttribute.bind(script);
|
||||||
const website = attr('data-website-id');
|
const website = attr('data-website-id');
|
||||||
const hostUrl = attr('data-host-url');
|
const hostUrl = attr('data-host-url');
|
||||||
const autoTrack = attr('data-auto-track') !== 'false';
|
const autoTrack = attr('data-auto-track') !== 'false';
|
||||||
const dnt = attr('data-do-not-track');
|
const dnt = attr('data-do-not-track');
|
||||||
const useCache = attr('data-cache');
|
const useCache = attr('data-cache');
|
||||||
const domains = attr('data-domains');
|
const domain = attr('data-domains') || '';
|
||||||
|
const domains = domain.split(',').map(n => n.trim());
|
||||||
|
|
||||||
const disableTracking =
|
const eventClass = /^umami--([a-z]+)--([\w]+[\w-]*)$/;
|
||||||
localStorage.getItem('umami.disabled') ||
|
const eventSelect = "[class*='umami--']";
|
||||||
|
const cacheKey = 'umami.cache';
|
||||||
|
|
||||||
|
const disableTracking = () =>
|
||||||
|
(localStorage && localStorage.getItem('umami.disabled')) ||
|
||||||
(dnt && doNotTrack()) ||
|
(dnt && doNotTrack()) ||
|
||||||
(domains &&
|
(domain && !domains.includes(hostname));
|
||||||
!domains
|
|
||||||
.split(',')
|
|
||||||
.map(n => n.trim())
|
|
||||||
.includes(hostname));
|
|
||||||
|
|
||||||
const root = hostUrl
|
const root = hostUrl
|
||||||
? removeTrailingSlash(hostUrl)
|
? removeTrailingSlash(hostUrl)
|
||||||
: script.src.split('/').slice(0, -1).join('/');
|
: script.src.split('/').slice(0, -1).join('/');
|
||||||
const screen = `${width}x${height}`;
|
const screen = `${width}x${height}`;
|
||||||
const listeners = [];
|
const listeners = {};
|
||||||
let currentUrl = `${pathname}${search}`;
|
let currentUrl = `${pathname}${search}`;
|
||||||
let currentRef = document.referrer;
|
let currentRef = document.referrer;
|
||||||
|
|
||||||
@ -50,7 +51,7 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
|
|
||||||
req.onreadystatechange = () => {
|
req.onreadystatechange = () => {
|
||||||
if (req.readyState === 4) {
|
if (req.readyState === 4) {
|
||||||
callback && callback(req.response);
|
callback(req.response);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -58,23 +59,19 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
};
|
};
|
||||||
|
|
||||||
const collect = (type, params, uuid) => {
|
const collect = (type, params, uuid) => {
|
||||||
if (disableTracking) return;
|
if (disableTracking()) return;
|
||||||
|
|
||||||
const key = 'umami.cache';
|
|
||||||
|
|
||||||
const payload = {
|
const payload = {
|
||||||
website: uuid,
|
website: uuid,
|
||||||
hostname,
|
hostname,
|
||||||
screen,
|
screen,
|
||||||
language,
|
language,
|
||||||
cache: useCache && sessionStorage.getItem(key),
|
cache: useCache && sessionStorage.getItem(cacheKey),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (params) {
|
|
||||||
Object.keys(params).forEach(key => {
|
Object.keys(params).forEach(key => {
|
||||||
payload[key] = params[key];
|
payload[key] = params[key];
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
|
||||||
post(
|
post(
|
||||||
`${root}/api/collect`,
|
`${root}/api/collect`,
|
||||||
@ -82,11 +79,11 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
type,
|
type,
|
||||||
payload,
|
payload,
|
||||||
},
|
},
|
||||||
res => useCache && sessionStorage.setItem(key, res),
|
res => useCache && sessionStorage.setItem(cacheKey, res),
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) =>
|
const trackView = (url = currentUrl, referrer = currentRef, uuid = website) => {
|
||||||
collect(
|
collect(
|
||||||
'pageview',
|
'pageview',
|
||||||
{
|
{
|
||||||
@ -95,8 +92,9 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
},
|
},
|
||||||
uuid,
|
uuid,
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const trackEvent = (event_value, event_type = 'custom', url = currentUrl, uuid = website) =>
|
const trackEvent = (event_value, event_type = 'custom', url = currentUrl, uuid = website) => {
|
||||||
collect(
|
collect(
|
||||||
'event',
|
'event',
|
||||||
{
|
{
|
||||||
@ -106,28 +104,29 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
},
|
},
|
||||||
uuid,
|
uuid,
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
/* Handle events */
|
/* Handle events */
|
||||||
|
|
||||||
const addEvents = () => {
|
const addEvent = element => {
|
||||||
document.querySelectorAll("[class*='umami--']").forEach(element => {
|
|
||||||
element.className.split(' ').forEach(className => {
|
element.className.split(' ').forEach(className => {
|
||||||
if (/^umami--([a-z]+)--([\w]+[\w-]*)$/.test(className)) {
|
if (!eventClass.test(className)) return;
|
||||||
const [, type, value] = className.split('--');
|
|
||||||
const listener = () => trackEvent(value, type);
|
const [, type, value] = className.split('--');
|
||||||
|
const listener = listeners[className]
|
||||||
|
? listeners[className]
|
||||||
|
: (listeners[className] = () => trackEvent(value, type));
|
||||||
|
|
||||||
listeners.push([element, type, listener]);
|
|
||||||
element.addEventListener(type, listener, true);
|
element.addEventListener(type, listener, true);
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const removeEvents = () => {
|
const monitorMutate = mutations => {
|
||||||
listeners.forEach(([element, type, listener]) => {
|
mutations.forEach(mutation => {
|
||||||
element && element.removeEventListener(type, listener, true);
|
const element = mutation.target;
|
||||||
|
addEvent(element);
|
||||||
|
element.querySelectorAll(eventSelect).forEach(addEvent);
|
||||||
});
|
});
|
||||||
listeners.length = 0;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Handle history changes */
|
/* Handle history changes */
|
||||||
@ -135,8 +134,6 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
const handlePush = (state, title, url) => {
|
const handlePush = (state, title, url) => {
|
||||||
if (!url) return;
|
if (!url) return;
|
||||||
|
|
||||||
removeEvents();
|
|
||||||
|
|
||||||
currentRef = currentUrl;
|
currentRef = currentUrl;
|
||||||
const newUrl = url.toString();
|
const newUrl = url.toString();
|
||||||
|
|
||||||
@ -147,32 +144,42 @@ import { removeTrailingSlash } from '../lib/url';
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (currentUrl !== currentRef) {
|
if (currentUrl !== currentRef) {
|
||||||
trackView(currentUrl, currentRef);
|
trackView();
|
||||||
}
|
}
|
||||||
|
|
||||||
setTimeout(addEvents, 300);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Global */
|
/* Global */
|
||||||
|
|
||||||
if (!window.umami) {
|
if (!window.umami) {
|
||||||
const umami = event_value => trackEvent(event_value);
|
const umami = eventValue => trackEvent(eventValue);
|
||||||
umami.trackView = trackView;
|
umami.trackView = trackView;
|
||||||
umami.trackEvent = trackEvent;
|
umami.trackEvent = trackEvent;
|
||||||
umami.addEvents = addEvents;
|
|
||||||
umami.removeEvents = removeEvents;
|
|
||||||
|
|
||||||
window.umami = umami;
|
window.umami = umami;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Start */
|
/* Start */
|
||||||
|
|
||||||
if (autoTrack && !disableTracking) {
|
if (autoTrack && !disableTracking()) {
|
||||||
history.pushState = hook(history, 'pushState', handlePush);
|
history.pushState = hook(history, 'pushState', handlePush);
|
||||||
history.replaceState = hook(history, 'replaceState', handlePush);
|
history.replaceState = hook(history, 'replaceState', handlePush);
|
||||||
|
|
||||||
trackView(currentUrl, currentRef);
|
const update = () => {
|
||||||
|
switch (document.readyState) {
|
||||||
addEvents();
|
/* DOM rendered, add event listeners */
|
||||||
|
case 'interactive': {
|
||||||
|
document.querySelectorAll(eventSelect).forEach(addEvent);
|
||||||
|
const observer = new MutationObserver(monitorMutate);
|
||||||
|
observer.observe(document, { childList: true, subtree: true });
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
/* Page loaded, track our view */
|
||||||
|
case 'complete':
|
||||||
|
trackView();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
document.addEventListener('readystatechange', update, true);
|
||||||
|
update();
|
||||||
}
|
}
|
||||||
})(window);
|
})(window);
|
||||||
|
Loading…
Reference in New Issue
Block a user