mirror of
https://github.com/kremalicious/umami.git
synced 2025-02-14 21:10:34 +01:00
Fixed comments
This commit is contained in:
parent
241905c5fe
commit
5318accc6f
@ -24,8 +24,7 @@ export default function TrackingCodeForm({ values, onClose }) {
|
|||||||
rows={3}
|
rows={3}
|
||||||
cols={60}
|
cols={60}
|
||||||
spellCheck={false}
|
spellCheck={false}
|
||||||
defaultValue={`!function(){"use strict";!function(e){var t=e.umami=e.umami||[];if(!t.registerAutoEvents)if(t.invoked)e.console&&console.error&&console.error("Umami snippet included twice.");else{t.invoked=!0,t.calls=[],t.methods=["registerAutoEvents","event","pageView"],t.factory=function(e){return function(){var r=Array.prototype.slice.call(arguments);return r.unshift(e),t.calls.push(r),t}};for(var r=0;r<t.methods.length;r++){var i=t.methods[r];t[i]=t.factory(i)}t.load=function(e,t,r,i){var o=document.createElement("script");o.type="text/javascript",o.defer=!0,o.async=!0,o.setAttribute("data-website-id",t),r&&o.setAttribute("data-skip-auto","true"),i&&o.setAttribute("data-debug","true"),o.src=e;var a=document.getElementsByTagName("script")[0];a.parentNode.insertBefore(o,a)},t.load("${document.location.origin}/umami.js","${values.website_uuid}",!1,!1)}}(window)}();
|
defaultValue={`!function(){"use strict";!function(e){var t=e.umami=e.umami||[];if(!t.registerAutoEvents)if(t.invoked)e.console&&console.error&&console.error("Umami snippet included twice.");else{t.invoked=!0,t.calls=[],t.methods=["registerAutoEvents","event","pageView"],t.factory=function(e){return function(){var r=Array.prototype.slice.call(arguments);return r.unshift(e),t.calls.push(r),t}};for(var r=0;r<t.methods.length;r++){var i=t.methods[r];t[i]=t.factory(i)}t.load=function(e,t,r){var i=document.createElement("script");i.type="text/javascript",i.defer=!0,i.async=!0,i.setAttribute("data-website-id",t),r&&i.setAttribute("data-skip-auto","true"),i.src=e;var o=document.getElementsByTagName("script")[0];o.parentNode.insertBefore(i,o)},t.load("${document.location.origin}/umami.js","${values.website_uuid}",!1)}}(window)}();`}
|
||||||
`}
|
|
||||||
readOnly
|
readOnly
|
||||||
/>
|
/>
|
||||||
</FormRow>
|
</FormRow>
|
||||||
|
@ -3,13 +3,6 @@ import 'unfetch/polyfill';
|
|||||||
import { doNotTrack, hook, post } from '../lib/web';
|
import { doNotTrack, hook, post } from '../lib/web';
|
||||||
import { removeTrailingSlash } from '../lib/url';
|
import { removeTrailingSlash } from '../lib/url';
|
||||||
|
|
||||||
function log(isDebug, messageFn) {
|
|
||||||
if (isDebug) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
console.log(messageFn());
|
|
||||||
}
|
|
||||||
|
|
||||||
(window => {
|
(window => {
|
||||||
const {
|
const {
|
||||||
screen: { width, height },
|
screen: { width, height },
|
||||||
@ -27,7 +20,6 @@ function log(isDebug, messageFn) {
|
|||||||
const website = script.getAttribute('data-website-id');
|
const website = script.getAttribute('data-website-id');
|
||||||
const hostUrl = script.getAttribute('data-host-url');
|
const hostUrl = script.getAttribute('data-host-url');
|
||||||
const skipAuto = script.getAttribute('data-skip-auto');
|
const skipAuto = script.getAttribute('data-skip-auto');
|
||||||
const isDebug = script.getAttribute('data-debug');
|
|
||||||
const root = hostUrl
|
const root = hostUrl
|
||||||
? removeTrailingSlash(hostUrl)
|
? removeTrailingSlash(hostUrl)
|
||||||
: new URL(script.src).href.split('/').slice(0, -1).join('/');
|
: new URL(script.src).href.split('/').slice(0, -1).join('/');
|
||||||
@ -37,26 +29,6 @@ function log(isDebug, messageFn) {
|
|||||||
let currentUrl = `${pathname}${search}`;
|
let currentUrl = `${pathname}${search}`;
|
||||||
let currentRef = document.referrer;
|
let currentRef = document.referrer;
|
||||||
|
|
||||||
/* Collect metrics */
|
|
||||||
const pageViewWithAutoEvents = (url, referrer) => window.umami.pageView(url, referrer).then(() => setTimeout(loadEvents, 300));
|
|
||||||
|
|
||||||
/* Handle history */
|
|
||||||
|
|
||||||
const handlePush = (state, title, url) => {
|
|
||||||
removeEvents();
|
|
||||||
currentRef = currentUrl;
|
|
||||||
const newUrl = url.toString();
|
|
||||||
|
|
||||||
if (newUrl.substring(0, 4) === 'http') {
|
|
||||||
const { pathname, search } = new URL(newUrl);
|
|
||||||
currentUrl = `${pathname}${search}`;
|
|
||||||
} else {
|
|
||||||
currentUrl = newUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
pageViewWithAutoEvents(currentUrl, currentRef);
|
|
||||||
};
|
|
||||||
|
|
||||||
/* Handle events */
|
/* Handle events */
|
||||||
|
|
||||||
const removeEvents = () => {
|
const removeEvents = () => {
|
||||||
@ -71,7 +43,7 @@ function log(isDebug, messageFn) {
|
|||||||
element.className.split(' ').forEach(className => {
|
element.className.split(' ').forEach(className => {
|
||||||
if (/^umami--([a-z]+)--([a-z0-9_]+[a-z0-9-_]+)$/.test(className)) {
|
if (/^umami--([a-z]+)--([a-z0-9_]+[a-z0-9-_]+)$/.test(className)) {
|
||||||
const [, type, value] = className.split('--');
|
const [, type, value] = className.split('--');
|
||||||
const listener = () => window.umami.event(type, value);
|
const listener = () => event(type, value);
|
||||||
|
|
||||||
listeners.push([element, type, listener]);
|
listeners.push([element, type, listener]);
|
||||||
element.addEventListener(type, listener, true);
|
element.addEventListener(type, listener, true);
|
||||||
@ -79,11 +51,7 @@ function log(isDebug, messageFn) {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
const collect = (type, params, uuid) => {
|
||||||
|
|
||||||
const scheduledCalls = window.umami.calls;
|
|
||||||
window.umami = {};
|
|
||||||
window.umami.collect = (type, params, uuid) => {
|
|
||||||
const payload = {
|
const payload = {
|
||||||
website: uuid,
|
website: uuid,
|
||||||
hostname,
|
hostname,
|
||||||
@ -97,35 +65,61 @@ function log(isDebug, messageFn) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
log(isDebug, () => `Umami, collect ${type} with payload: ${JSON.stringify(payload, null, 2)}`);
|
|
||||||
return post(`${root}/api/collect`, {
|
return post(`${root}/api/collect`, {
|
||||||
type,
|
type,
|
||||||
payload,
|
payload,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
window.umami.pageView = (url = currentUrl, referrer = currentRef, uuid = website) => window.umami.collect('pageview', {
|
const pageView = (url = currentUrl, referrer = currentRef, uuid = website) => collect('pageview', {
|
||||||
url,
|
url,
|
||||||
referrer,
|
referrer,
|
||||||
}, uuid);
|
}, uuid);
|
||||||
window.umami.event = (event_type, event_value, url = currentUrl, uuid = website) => window.umami.collect('event', {
|
|
||||||
|
/* Collect metrics */
|
||||||
|
const pageViewWithAutoEvents = (url, referrer) => pageView(url, referrer).then(() => setTimeout(loadEvents, 300));
|
||||||
|
|
||||||
|
/* Handle history */
|
||||||
|
const handlePush = (state, title, url) => {
|
||||||
|
removeEvents();
|
||||||
|
currentRef = currentUrl;
|
||||||
|
const newUrl = url.toString();
|
||||||
|
|
||||||
|
if (newUrl.substring(0, 4) === 'http') {
|
||||||
|
const { pathname, search } = new URL(newUrl);
|
||||||
|
currentUrl = `${pathname}${search}`;
|
||||||
|
} else {
|
||||||
|
currentUrl = newUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
pageViewWithAutoEvents(currentUrl, currentRef);
|
||||||
|
};
|
||||||
|
|
||||||
|
const event = (event_type, event_value, url = currentUrl, uuid = website) => collect('event', {
|
||||||
url,
|
url,
|
||||||
event_type,
|
event_type,
|
||||||
event_value,
|
event_value,
|
||||||
}, uuid);
|
}, uuid);
|
||||||
window.umami.registerAutoEvents = () => {
|
|
||||||
|
const registerAutoEvents = () => {
|
||||||
history.pushState = hook(history, 'pushState', handlePush);
|
history.pushState = hook(history, 'pushState', handlePush);
|
||||||
history.replaceState = hook(history, 'replaceState', handlePush);
|
history.replaceState = hook(history, 'replaceState', handlePush);
|
||||||
return pageViewWithAutoEvents(currentUrl, currentRef);
|
return pageViewWithAutoEvents(currentUrl, currentRef);
|
||||||
};
|
};
|
||||||
|
|
||||||
log(isDebug, () => 'Umami, calling scheduled invocations');
|
|
||||||
|
const scheduledCalls = window.umami.calls;
|
||||||
|
window.umami = {
|
||||||
|
collect,
|
||||||
|
pageView,
|
||||||
|
event,
|
||||||
|
registerAutoEvents
|
||||||
|
};
|
||||||
scheduledCalls.forEach(([fnName, ...params]) => {
|
scheduledCalls.forEach(([fnName, ...params]) => {
|
||||||
log(isDebug, () => `Umami, calling ${fnName} fn with params: ${JSON.stringify(params)}`);
|
|
||||||
window.umami[fnName].apply(window.umami, params);
|
window.umami[fnName].apply(window.umami, params);
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Start */
|
/* Start */
|
||||||
if (!skipAuto) {
|
if (!skipAuto) {
|
||||||
window.umami.registerAutoEvents().catch(e => console.error(e));
|
registerAutoEvents().catch(e => console.error(e));
|
||||||
}
|
}
|
||||||
})(window);
|
})(window);
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
let e = umami.methods[t];
|
let e = umami.methods[t];
|
||||||
umami[e] = umami.factory(e);
|
umami[e] = umami.factory(e);
|
||||||
}
|
}
|
||||||
umami.load = function(umamiScript, umamiUUID, skipAuto, isDebug) {
|
umami.load = function(umamiScript, umamiUUID, skipAuto) {
|
||||||
const scriptElement = document.createElement('script');
|
const scriptElement = document.createElement('script');
|
||||||
scriptElement.type = 'text/javascript';
|
scriptElement.type = 'text/javascript';
|
||||||
scriptElement.defer = true;
|
scriptElement.defer = true;
|
||||||
@ -28,15 +28,12 @@
|
|||||||
if (skipAuto) {
|
if (skipAuto) {
|
||||||
scriptElement.setAttribute('data-skip-auto', 'true');
|
scriptElement.setAttribute('data-skip-auto', 'true');
|
||||||
}
|
}
|
||||||
if (isDebug) {
|
|
||||||
scriptElement.setAttribute('data-debug', 'true');
|
|
||||||
}
|
|
||||||
scriptElement.src = umamiScript;
|
scriptElement.src = umamiScript;
|
||||||
const otherScript = document.getElementsByTagName('script')[0];
|
const otherScript = document.getElementsByTagName('script')[0];
|
||||||
otherScript.parentNode.insertBefore(scriptElement, otherScript);
|
otherScript.parentNode.insertBefore(scriptElement, otherScript);
|
||||||
};
|
};
|
||||||
|
|
||||||
umami.load('${document.location.origin}/umami.js', '${values.website_uuid}', false, false);
|
umami.load('${document.location.origin}/umami.js', '${values.website_uuid}', false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})(window);
|
})(window);
|
Loading…
x
Reference in New Issue
Block a user