Fixed comments

This commit is contained in:
Bartosz Hernas 2020-09-16 12:07:22 +02:00
parent 241905c5fe
commit 5318accc6f
3 changed files with 38 additions and 48 deletions

View File

@ -24,8 +24,7 @@ export default function TrackingCodeForm({ values, onClose }) {
rows={3}
cols={60}
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
/>
</FormRow>

View File

@ -3,13 +3,6 @@ import 'unfetch/polyfill';
import { doNotTrack, hook, post } from '../lib/web';
import { removeTrailingSlash } from '../lib/url';
function log(isDebug, messageFn) {
if (isDebug) {
return;
}
console.log(messageFn());
}
(window => {
const {
screen: { width, height },
@ -27,7 +20,6 @@ function log(isDebug, messageFn) {
const website = script.getAttribute('data-website-id');
const hostUrl = script.getAttribute('data-host-url');
const skipAuto = script.getAttribute('data-skip-auto');
const isDebug = script.getAttribute('data-debug');
const root = hostUrl
? removeTrailingSlash(hostUrl)
: new URL(script.src).href.split('/').slice(0, -1).join('/');
@ -37,26 +29,6 @@ function log(isDebug, messageFn) {
let currentUrl = `${pathname}${search}`;
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 */
const removeEvents = () => {
@ -71,7 +43,7 @@ function log(isDebug, messageFn) {
element.className.split(' ').forEach(className => {
if (/^umami--([a-z]+)--([a-z0-9_]+[a-z0-9-_]+)$/.test(className)) {
const [, type, value] = className.split('--');
const listener = () => window.umami.event(type, value);
const listener = () => event(type, value);
listeners.push([element, type, listener]);
element.addEventListener(type, listener, true);
@ -79,11 +51,7 @@ function log(isDebug, messageFn) {
});
});
};
const scheduledCalls = window.umami.calls;
window.umami = {};
window.umami.collect = (type, params, uuid) => {
const collect = (type, params, uuid) => {
const payload = {
website: uuid,
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`, {
type,
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,
referrer,
}, 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,
event_type,
event_value,
}, uuid);
window.umami.registerAutoEvents = () => {
const registerAutoEvents = () => {
history.pushState = hook(history, 'pushState', handlePush);
history.replaceState = hook(history, 'replaceState', handlePush);
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]) => {
log(isDebug, () => `Umami, calling ${fnName} fn with params: ${JSON.stringify(params)}`);
window.umami[fnName].apply(window.umami, params);
});
/* Start */
if (!skipAuto) {
window.umami.registerAutoEvents().catch(e => console.error(e));
registerAutoEvents().catch(e => console.error(e));
}
})(window);

View File

@ -19,7 +19,7 @@
let e = umami.methods[t];
umami[e] = umami.factory(e);
}
umami.load = function(umamiScript, umamiUUID, skipAuto, isDebug) {
umami.load = function(umamiScript, umamiUUID, skipAuto) {
const scriptElement = document.createElement('script');
scriptElement.type = 'text/javascript';
scriptElement.defer = true;
@ -28,15 +28,12 @@
if (skipAuto) {
scriptElement.setAttribute('data-skip-auto', 'true');
}
if (isDebug) {
scriptElement.setAttribute('data-debug', 'true');
}
scriptElement.src = umamiScript;
const otherScript = document.getElementsByTagName('script')[0];
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);