Comments fixed

pull/162/head
Bartosz Hernas 2020-09-15 12:03:34 +02:00
parent d6d1c68e6d
commit d5e4914153
2 changed files with 24 additions and 24 deletions

View File

@ -6,7 +6,7 @@ import useFetch from 'hooks/useFetch';
export default function SharePage() { export default function SharePage() {
const router = useRouter(); const router = useRouter();
const { id, frame } = router.query; const { id } = router.query;
const shareId = id?.[0]; const shareId = id?.[0];
const { data } = useFetch(shareId ? `/api/share/${shareId}` : null); const { data } = useFetch(shareId ? `/api/share/${shareId}` : null);
@ -15,7 +15,7 @@ export default function SharePage() {
} }
return ( return (
<Layout header={!frame} footer={!frame}> <Layout>
<WebsiteDetails websiteId={data.website_id} /> <WebsiteDetails websiteId={data.website_id} />
</Layout> </Layout>
); );

View File

@ -19,6 +19,7 @@ import { removeTrailingSlash } from '../lib/url';
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 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('/');
@ -29,14 +30,7 @@ import { removeTrailingSlash } from '../lib/url';
let currentRef = document.referrer; let currentRef = document.referrer;
/* Collect metrics */ /* Collect metrics */
const pageViewWithAutoEvents = (url, referrer) => window.umami.pageView(url, referrer).then(() => setTimeout(loadEvents, 300));
const collect = (type, params) => {
return window.umamiTrack(website, type, params);
};
const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300));
const pageEvent = (event_type, event_value) => collect('event', { event_type, event_value });
/* Handle history */ /* Handle history */
@ -52,7 +46,7 @@ import { removeTrailingSlash } from '../lib/url';
currentUrl = newUrl; currentUrl = newUrl;
} }
pageView(); pageViewWithAutoEvents(currentUrl, currentRef);
}; };
/* Handle events */ /* Handle events */
@ -69,7 +63,7 @@ import { removeTrailingSlash } from '../lib/url';
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 = () => pageEvent(type, value); const listener = () => window.umami.event(type, value);
listeners.push([element, type, listener]); listeners.push([element, type, listener]);
element.addEventListener(type, listener, true); element.addEventListener(type, listener, true);
@ -78,14 +72,13 @@ import { removeTrailingSlash } from '../lib/url';
}); });
}; };
if (!window.umamiTrack) { if (!window.umami) {
window.umamiTrack = (type, params, id) => { window.umami = event_value => window.umami.event('custom', event_value, currentUrl);
window.umami.collect = (type, params, id) => {
if (!id) { if (!id) {
id = website; id = website;
} }
const payload = { const payload = {
url: currentUrl,
referrer: currentRef,
website: id, website: id,
hostname, hostname,
screen, screen,
@ -103,17 +96,24 @@ import { removeTrailingSlash } from '../lib/url';
payload, payload,
}); });
}; };
window.umami.pageView = (url = currentUrl, referrer = currentRef) => window.umami.collect('pageview', {
url,
referrer,
});
window.umami.event = (event_type, event_value, url = currentUrl) => window.umami.collect('event', {
url,
event_type,
event_value,
});
window.umami.registerAutoEvents = () => {
history.pushState = hook(history, 'pushState', handlePush);
history.replaceState = hook(history, 'replaceState', handlePush);
return pageViewWithAutoEvents(currentUrl, currentRef);
};
} }
/* Start */ /* Start */
const skipAuto = new URL(script.src).search.includes('auto=false');
if (!skipAuto) { if (!skipAuto) {
history.pushState = hook(history, 'pushState', handlePush); window.umami.registerAutoEvents().catch(e => console.error(e));
history.replaceState = hook(history, 'replaceState', handlePush);
pageView();
}
if (!window.umami) {
window.umami = event_value => collect('event', { event_type: 'custom', event_value });
} }
})(window); })(window);