From d851a9e88bcc540566d7a69eb8021c73e98380a0 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Tue, 4 Apr 2023 00:27:25 -0700 Subject: [PATCH 1/2] Fixed test console error. --- components/pages/console/TestConsole.js | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/components/pages/console/TestConsole.js b/components/pages/console/TestConsole.js index f1571807..685f5458 100644 --- a/components/pages/console/TestConsole.js +++ b/components/pages/console/TestConsole.js @@ -49,15 +49,13 @@ export default function TestConsole() { return null; } - const websiteId = id?.[0]; + const [websiteId] = id || []; const website = data.find(({ id }) => websiteId === id); return ( - - {router.query.id[2] ? `Umami Console page ${router.query.id[2]}` : 'Umami Console Select'} - + {website ? `${website.name} | Umami Console` : 'Umami Console'} @@ -74,10 +72,10 @@ export default function TestConsole() {
Page links
- page one + page one
- page two + page two
From 84894a5b805d959b0e946eebe7d9a44a504c0cbc Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Tue, 4 Apr 2023 00:34:07 -0700 Subject: [PATCH 2/2] Updated tracker to handle links. --- tracker/index.js | 53 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/tracker/index.js b/tracker/index.js index c18c8754..4d680c7e 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -28,6 +28,7 @@ const endpoint = `${root}/api/send`; const screen = `${width}x${height}`; const eventRegex = /data-umami-event-([\w-_]+)/; + const eventNameAttribute = _data + 'umami-event'; /* Helper functions */ @@ -90,15 +91,14 @@ }; const handleClick = () => { - const callback = e => { - const t = e.target; - const attr = t.getAttribute.bind(t); - const eventName = attr(_data + 'umami-event'); + const trackElement = el => { + const attr = el.getAttribute.bind(el); + const eventName = attr(eventNameAttribute); if (eventName) { const eventData = {}; - t.getAttributeNames().forEach(name => { + el.getAttributeNames().forEach(name => { const match = name.match(eventRegex); if (match) { @@ -106,23 +106,40 @@ } }); - if (t.tagName === 'A') { - const href = attr('href'); - const target = attr('target'); + return track(eventName, { data: eventData }); + } + return Promise.resolve(); + }; - if ( - href && - target !== '_blank' && - !(e.ctrlKey || e.shiftKey || e.metaKey || (e.button && e.button === 1)) - ) { + const callback = e => { + const el = e.target; + const anchor = + el.tagName === 'A' + ? el + : el.parentElement && el.parentElement.tagName === 'A' + ? el.parentElement + : null; + + if (anchor) { + const { href, target } = anchor; + const external = + target === '_blank' || + e.ctrlKey || + e.shiftKey || + e.metaKey || + (e.button && e.button === 1); + const eventName = anchor.getAttribute(eventNameAttribute); + + if (eventName && href) { + if (!external) { e.preventDefault(); - return track(eventName, { data: eventData }).then(() => { - location.href = href; - }); } + return trackElement(anchor).then(() => { + if (!external) location.href = href; + }); } - - track(eventName, { data: eventData }); + } else { + trackElement(el); } };