From b1d8a58e22cea37ec14fbbd90246c59944b79e16 Mon Sep 17 00:00:00 2001 From: Mike Cao Date: Mon, 1 Feb 2021 22:49:00 -0800 Subject: [PATCH] Update tracker script. --- tracker/index.js | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/tracker/index.js b/tracker/index.js index 8f58fae3..04266a68 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -133,26 +133,24 @@ import { removeTrailingSlash } from '../lib/url'; /* Handle history changes */ const handlePush = (state, title, url) => { - removeEvents(); - // url is not guaranteed - // thus calling `url.toString()` might causes exception, then break the pjax update - // track only if url is validate - if (url) { - currentRef = currentUrl; - const newUrl = url.toString(); + if (!url) return; - if (newUrl.substring(0, 4) === 'http') { - currentUrl = '/' + newUrl.split('/').splice(3).join('/'); - } else { - currentUrl = newUrl; - } - // this function under some framework might be called multiple times - // thus only track if currentUrl and currentRef are not same - if (currentUrl !== currentRef) { - trackView(currentUrl, currentRef); - setTimeout(addEvents, 300); - } + removeEvents(); + + currentRef = currentUrl; + const newUrl = url.toString(); + + if (newUrl.substring(0, 4) === 'http') { + currentUrl = '/' + newUrl.split('/').splice(3).join('/'); + } else { + currentUrl = newUrl; } + + if (currentUrl !== currentRef) { + trackView(currentUrl, currentRef); + } + + setTimeout(addEvents, 300); }; /* Global */