mirror of
https://github.com/umami-software/umami.git
synced 2025-12-06 01:18:00 +01:00
Merge pull request #3381 from YektaDev/script-simplification
Script Simplification
This commit is contained in:
commit
3e9a7ee9ad
1 changed files with 56 additions and 130 deletions
|
|
@ -8,19 +8,19 @@
|
||||||
top,
|
top,
|
||||||
doNotTrack,
|
doNotTrack,
|
||||||
} = window;
|
} = window;
|
||||||
const { hostname, href, origin } = location;
|
|
||||||
const { currentScript, referrer } = document;
|
const { currentScript, referrer } = document;
|
||||||
const localStorage = href.startsWith('data:') ? undefined : window.localStorage;
|
|
||||||
|
|
||||||
if (!currentScript) return;
|
if (!currentScript) return;
|
||||||
|
|
||||||
|
const { hostname, href, origin } = location;
|
||||||
|
const localStorage = href.startsWith('data:') ? undefined : window.localStorage;
|
||||||
|
|
||||||
const _data = 'data-';
|
const _data = 'data-';
|
||||||
const _false = 'false';
|
const _false = 'false';
|
||||||
const _true = 'true';
|
const _true = 'true';
|
||||||
const attr = currentScript.getAttribute.bind(currentScript);
|
const attr = currentScript.getAttribute.bind(currentScript);
|
||||||
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 tag = attr(_data + 'tag');
|
const tag = attr(_data + 'tag') || undefined;
|
||||||
const autoTrack = attr(_data + 'auto-track') !== _false;
|
const autoTrack = attr(_data + 'auto-track') !== _false;
|
||||||
const dnt = attr(_data + 'do-not-track') === _true;
|
const dnt = attr(_data + 'do-not-track') === _true;
|
||||||
const excludeSearch = attr(_data + 'exclude-search') === _true;
|
const excludeSearch = attr(_data + 'exclude-search') === _true;
|
||||||
|
|
@ -41,11 +41,11 @@
|
||||||
website,
|
website,
|
||||||
screen,
|
screen,
|
||||||
language,
|
language,
|
||||||
title,
|
title: document.title,
|
||||||
hostname,
|
hostname,
|
||||||
url: currentUrl,
|
url: currentUrl,
|
||||||
referrer: currentRef,
|
referrer: currentRef,
|
||||||
tag: tag ? tag : undefined,
|
tag,
|
||||||
id: identity ? identity : undefined,
|
id: identity ? identity : undefined,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -56,20 +56,14 @@
|
||||||
|
|
||||||
/* Event handlers */
|
/* Event handlers */
|
||||||
|
|
||||||
const handlePush = (state, title, url) => {
|
const handlePush = (_state, _title, url) => {
|
||||||
if (!url) return;
|
if (!url) return;
|
||||||
|
|
||||||
currentRef = currentUrl;
|
currentRef = currentUrl;
|
||||||
currentUrl = new URL(url, location.href);
|
currentUrl = new URL(url, location.href);
|
||||||
|
|
||||||
if (excludeSearch) {
|
if (excludeSearch) currentUrl.search = '';
|
||||||
currentUrl.search = '';
|
if (excludeHash) currentUrl.hash = '';
|
||||||
}
|
|
||||||
|
|
||||||
if (excludeHash) {
|
|
||||||
currentUrl.hash = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
currentUrl = currentUrl.toString();
|
currentUrl = currentUrl.toString();
|
||||||
|
|
||||||
if (currentUrl !== currentRef) {
|
if (currentUrl !== currentRef) {
|
||||||
|
|
@ -80,10 +74,8 @@
|
||||||
const handlePathChanges = () => {
|
const handlePathChanges = () => {
|
||||||
const hook = (_this, method, callback) => {
|
const hook = (_this, method, callback) => {
|
||||||
const orig = _this[method];
|
const orig = _this[method];
|
||||||
|
|
||||||
return (...args) => {
|
return (...args) => {
|
||||||
callback.apply(null, args);
|
callback.apply(null, args);
|
||||||
|
|
||||||
return orig.apply(_this, args);
|
return orig.apply(_this, args);
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
@ -92,96 +84,47 @@
|
||||||
history.replaceState = hook(history, 'replaceState', handlePush);
|
history.replaceState = hook(history, 'replaceState', handlePush);
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleTitleChanges = () => {
|
|
||||||
const observer = new MutationObserver(([entry]) => {
|
|
||||||
title = entry && entry.target ? entry.target.text : undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
const node = document.querySelector('head > title');
|
|
||||||
|
|
||||||
if (node) {
|
|
||||||
observer.observe(node, {
|
|
||||||
subtree: true,
|
|
||||||
characterData: true,
|
|
||||||
childList: true,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleClicks = () => {
|
const handleClicks = () => {
|
||||||
document.addEventListener(
|
|
||||||
'click',
|
|
||||||
async e => {
|
|
||||||
const isSpecialTag = tagName => ['BUTTON', 'A'].includes(tagName);
|
|
||||||
|
|
||||||
const trackElement = async el => {
|
const trackElement = async el => {
|
||||||
const attr = el.getAttribute.bind(el);
|
const eventName = el.getAttribute(eventNameAttribute);
|
||||||
const eventName = attr(eventNameAttribute);
|
|
||||||
|
|
||||||
if (eventName) {
|
if (eventName) {
|
||||||
const eventData = {};
|
const eventData = {};
|
||||||
|
|
||||||
el.getAttributeNames().forEach(name => {
|
el.getAttributeNames().forEach(name => {
|
||||||
const match = name.match(eventRegex);
|
const match = name.match(eventRegex);
|
||||||
|
if (match) eventData[match[1]] = el.getAttribute(name);
|
||||||
if (match) {
|
|
||||||
eventData[match[1]] = attr(name);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
return track(eventName, eventData);
|
return track(eventName, eventData);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
const onClick = async e => {
|
||||||
const findParentTag = (rootElem, maxSearchDepth) => {
|
|
||||||
let currentElement = rootElem;
|
|
||||||
for (let i = 0; i < maxSearchDepth; i++) {
|
|
||||||
if (isSpecialTag(currentElement.tagName)) {
|
|
||||||
return currentElement;
|
|
||||||
}
|
|
||||||
currentElement = currentElement.parentElement;
|
|
||||||
if (!currentElement) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const el = e.target;
|
const el = e.target;
|
||||||
const parentElement = isSpecialTag(el.tagName) ? el : findParentTag(el, 10);
|
const parentElement = el.closest('a,button');
|
||||||
|
if (!parentElement) return trackElement(el);
|
||||||
|
|
||||||
if (parentElement) {
|
|
||||||
const { href, target } = parentElement;
|
const { href, target } = parentElement;
|
||||||
const eventName = parentElement.getAttribute(eventNameAttribute);
|
if (!parentElement.getAttribute(eventNameAttribute)) return;
|
||||||
|
|
||||||
if (eventName) {
|
if (parentElement.tagName === 'BUTTON') {
|
||||||
if (parentElement.tagName === 'A') {
|
return trackElement(parentElement);
|
||||||
|
}
|
||||||
|
if (parentElement.tagName === 'A' && href) {
|
||||||
const external =
|
const external =
|
||||||
target === '_blank' ||
|
target === '_blank' ||
|
||||||
e.ctrlKey ||
|
e.ctrlKey ||
|
||||||
e.shiftKey ||
|
e.shiftKey ||
|
||||||
e.metaKey ||
|
e.metaKey ||
|
||||||
(e.button && e.button === 1);
|
(e.button && e.button === 1);
|
||||||
|
if (!external) e.preventDefault();
|
||||||
if (eventName && href) {
|
|
||||||
if (!external) {
|
|
||||||
e.preventDefault();
|
|
||||||
}
|
|
||||||
return trackElement(parentElement).then(() => {
|
return trackElement(parentElement).then(() => {
|
||||||
if (!external) {
|
if (!external) {
|
||||||
(target === '_top' ? top.location : location).href = href;
|
(target === '_top' ? top.location : location).href = href;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else if (parentElement.tagName === 'BUTTON') {
|
};
|
||||||
return trackElement(parentElement);
|
document.addEventListener('click', onClick, true);
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return trackElement(el);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
true,
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Tracking functions */
|
/* Tracking functions */
|
||||||
|
|
@ -195,56 +138,40 @@
|
||||||
|
|
||||||
const send = async (payload, type = 'event') => {
|
const send = async (payload, type = 'event') => {
|
||||||
if (trackingDisabled()) return;
|
if (trackingDisabled()) return;
|
||||||
|
|
||||||
const headers = {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
};
|
|
||||||
|
|
||||||
if (typeof cache !== 'undefined') {
|
|
||||||
headers['x-umami-cache'] = cache;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch(endpoint, {
|
const res = await fetch(endpoint, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({ type, payload }),
|
body: JSON.stringify({ type, payload }),
|
||||||
headers,
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...(typeof cache !== 'undefined' && { 'x-umami-cache': cache }),
|
||||||
|
},
|
||||||
credentials: 'omit',
|
credentials: 'omit',
|
||||||
});
|
});
|
||||||
|
|
||||||
const data = await res.json();
|
const data = await res.json();
|
||||||
|
|
||||||
if (data) {
|
if (data) {
|
||||||
disabled = !!data.disabled;
|
disabled = !!data.disabled;
|
||||||
cache = data.cache;
|
cache = data.cache;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
/* empty */
|
/* no-op */
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const init = () => {
|
const init = () => {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
|
initialized = true;
|
||||||
track();
|
track();
|
||||||
handlePathChanges();
|
handlePathChanges();
|
||||||
handleTitleChanges();
|
|
||||||
handleClicks();
|
handleClicks();
|
||||||
initialized = true;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const track = (name, data) => {
|
const track = (obj, data) => {
|
||||||
if (typeof name === 'string') {
|
if (typeof obj === 'string') return send({ ...getPayload(), name: obj, data });
|
||||||
return send({
|
if (typeof obj === 'object') return send(obj);
|
||||||
...getPayload(),
|
if (typeof obj === 'function') return send(obj(getPayload()));
|
||||||
name,
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
} else if (typeof name === 'object') {
|
|
||||||
return send({ ...name });
|
|
||||||
} else if (typeof name === 'function') {
|
|
||||||
return send(name(getPayload()));
|
|
||||||
}
|
|
||||||
return send(getPayload());
|
return send(getPayload());
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -274,10 +201,9 @@
|
||||||
|
|
||||||
let currentUrl = href;
|
let currentUrl = href;
|
||||||
let currentRef = referrer.startsWith(origin) ? '' : referrer;
|
let currentRef = referrer.startsWith(origin) ? '' : referrer;
|
||||||
let title = document.title;
|
let initialized = false;
|
||||||
let cache;
|
|
||||||
let initialized;
|
|
||||||
let disabled = false;
|
let disabled = false;
|
||||||
|
let cache;
|
||||||
let identity;
|
let identity;
|
||||||
|
|
||||||
if (autoTrack && !trackingDisabled()) {
|
if (autoTrack && !trackingDisabled()) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue