From d19bcbabe053e6b6eb80138818acaa0bed70ea7a Mon Sep 17 00:00:00 2001 From: Bartosz Hernas Date: Tue, 15 Sep 2020 00:09:09 +0200 Subject: [PATCH] Different method + fixed lint for tracker + added frame mode for shared URLs --- lib/web.js | 10 +++--- pages/share/[...id].js | 4 +-- tracker/index.js | 69 ++++++++++++++++++++++-------------------- 3 files changed, 42 insertions(+), 41 deletions(-) diff --git a/lib/web.js b/lib/web.js index 784a9e1e..4a8578f1 100644 --- a/lib/web.js +++ b/lib/web.js @@ -39,13 +39,11 @@ export const put = (url, params) => apiRequest('put', url, JSON.stringify(params export const hook = (_this, method, callback) => { const orig = _this[method]; - _this[method] = (...args) => { - callback.apply(null, args); - return orig.apply(_this, args); - }; - return () => { - _this[method] = orig; + return (...args) => { + callback.apply(null, args); + + return orig.apply(_this, args); }; }; diff --git a/pages/share/[...id].js b/pages/share/[...id].js index 1e0897a9..50467bf6 100644 --- a/pages/share/[...id].js +++ b/pages/share/[...id].js @@ -6,7 +6,7 @@ import useFetch from 'hooks/useFetch'; export default function SharePage() { const router = useRouter(); - const { id } = router.query; + const { id, frame } = router.query; const shareId = id?.[0]; const { data } = useFetch(shareId ? `/api/share/${shareId}` : null); @@ -15,7 +15,7 @@ export default function SharePage() { } return ( - + ); diff --git a/tracker/index.js b/tracker/index.js index 0b91d4e7..7bb1b7b3 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -1,6 +1,6 @@ import 'promise-polyfill/src/polyfill'; import 'unfetch/polyfill'; -import { post, hook, doNotTrack } from '../lib/web'; +import { doNotTrack, hook, post } from '../lib/web'; import { removeTrailingSlash } from '../lib/url'; (window => { @@ -31,25 +31,7 @@ import { removeTrailingSlash } from '../lib/url'; /* Collect metrics */ const collect = (type, params) => { - const payload = { - 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, - }); + return window.umamiTrack(website, type, params); }; const pageView = () => collect('pageview').then(() => setTimeout(loadEvents, 300)); @@ -73,8 +55,8 @@ import { removeTrailingSlash } from '../lib/url'; pageView(); }; - const pushStateUnhook = hook(history, 'pushState', handlePush); - const replaceStateUnhook = hook(history, 'replaceState', handlePush); + history.pushState = hook(history, 'pushState', handlePush); + history.replaceState = hook(history, 'replaceState', handlePush); /* Handle events */ @@ -86,7 +68,7 @@ import { removeTrailingSlash } from '../lib/url'; }; const loadEvents = () => { - document.querySelectorAll("[class*='umami--']").forEach(element => { + document.querySelectorAll('[class*=\'umami--\']').forEach(element => { element.className.split(' ').forEach(className => { if (/^umami--([a-z]+)--([a-z0-9_]+[a-z0-9-_]+)$/.test(className)) { 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) { window.umami = event_value => collect('event', { event_type: 'custom', event_value }); } - - if (!window.umamiUnregister) { - window.umamiUnregister = () => { - pushStateUnhook(); - replaceStateUnhook(); - removeEvents(); - }; - } })(window);