Different method + fixed lint for tracker + added frame mode for shared URLs

This commit is contained in:
Bartosz Hernas 2020-09-15 00:09:09 +02:00
parent 2ea91aaabf
commit d19bcbabe0
3 changed files with 42 additions and 41 deletions

View File

@ -39,13 +39,11 @@ export const put = (url, params) => apiRequest('put', url, JSON.stringify(params
export const hook = (_this, method, callback) => { export const hook = (_this, method, callback) => {
const orig = _this[method]; const orig = _this[method];
_this[method] = (...args) => {
callback.apply(null, args);
return orig.apply(_this, args);
};
return () => { return (...args) => {
_this[method] = orig; callback.apply(null, args);
return orig.apply(_this, args);
}; };
}; };

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 } = router.query; const { id, frame } = 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> <Layout header={!frame} footer={!frame}>
<WebsiteDetails websiteId={data.website_id} /> <WebsiteDetails websiteId={data.website_id} />
</Layout> </Layout>
); );

View File

@ -1,6 +1,6 @@
import 'promise-polyfill/src/polyfill'; import 'promise-polyfill/src/polyfill';
import 'unfetch/polyfill'; import 'unfetch/polyfill';
import { post, hook, doNotTrack } from '../lib/web'; import { doNotTrack, hook, post } from '../lib/web';
import { removeTrailingSlash } from '../lib/url'; import { removeTrailingSlash } from '../lib/url';
(window => { (window => {
@ -31,25 +31,7 @@ import { removeTrailingSlash } from '../lib/url';
/* Collect metrics */ /* Collect metrics */
const collect = (type, params) => { const collect = (type, params) => {
const payload = { return window.umamiTrack(website, type, params);
url: currentUrl,
referrer: currentRef,
website,
hostname,
screen,
language,
};
if (params) {
Object.keys(params).forEach(key => {
payload[key] = params[key];
});
}
return post(`${root}/api/collect`, {
type,
payload,
});
}; };
const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300)); const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300));
@ -73,8 +55,8 @@ import { removeTrailingSlash } from '../lib/url';
pageView(); pageView();
}; };
const pushStateUnhook = hook(history, 'pushState', handlePush); history.pushState = hook(history, 'pushState', handlePush);
const replaceStateUnhook = hook(history, 'replaceState', handlePush); history.replaceState = hook(history, 'replaceState', handlePush);
/* Handle events */ /* Handle events */
@ -86,7 +68,7 @@ import { removeTrailingSlash } from '../lib/url';
}; };
const loadEvents = () => { const loadEvents = () => {
document.querySelectorAll("[class*='umami--']").forEach(element => { document.querySelectorAll('[class*=\'umami--\']').forEach(element => {
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('--');
@ -99,19 +81,40 @@ import { removeTrailingSlash } from '../lib/url';
}); });
}; };
/* Start */ if (!window.umamiTrack) {
window.umamiTrack = (type, params, id) => {
if (!id) {
id = website
}
const payload = {
url: currentUrl,
referrer: currentRef,
website: id,
hostname,
screen,
language,
};
pageView(); if (params) {
Object.keys(params).forEach(key => {
payload[key] = params[key];
});
}
return post(`${root}/api/collect`, {
type,
payload,
});
};
}
/* Start */
const skipAuto = new URL(script.src).search.includes('auto=false');
if (!skipAuto) {
pageView();
}
if (!window.umami) { if (!window.umami) {
window.umami = event_value => collect('event', { event_type: 'custom', event_value }); window.umami = event_value => collect('event', { event_type: 'custom', event_value });
} }
if (!window.umamiUnregister) {
window.umamiUnregister = () => {
pushStateUnhook();
replaceStateUnhook();
removeEvents();
};
}
})(window); })(window);