From 2ea91aaabfec51de37a1289e040de2792654b72b Mon Sep 17 00:00:00 2001 From: Bartosz Hernas Date: Mon, 14 Sep 2020 18:13:59 +0200 Subject: [PATCH] Ability to unregister umami --- lib/web.js | 8 +++++--- tracker/index.js | 12 ++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/web.js b/lib/web.js index 4a8578f1..784a9e1e 100644 --- a/lib/web.js +++ b/lib/web.js @@ -39,12 +39,14 @@ export const put = (url, params) => apiRequest('put', url, JSON.stringify(params export const hook = (_this, method, callback) => { const orig = _this[method]; - - return (...args) => { + _this[method] = (...args) => { callback.apply(null, args); - return orig.apply(_this, args); }; + + return () => { + _this[method] = orig; + }; }; export const doNotTrack = () => { diff --git a/tracker/index.js b/tracker/index.js index f7f00927..0b91d4e7 100644 --- a/tracker/index.js +++ b/tracker/index.js @@ -73,8 +73,8 @@ import { removeTrailingSlash } from '../lib/url'; pageView(); }; - history.pushState = hook(history, 'pushState', handlePush); - history.replaceState = hook(history, 'replaceState', handlePush); + const pushStateUnhook = hook(history, 'pushState', handlePush); + const replaceStateUnhook = hook(history, 'replaceState', handlePush); /* Handle events */ @@ -106,4 +106,12 @@ import { removeTrailingSlash } from '../lib/url'; if (!window.umami) { window.umami = event_value => collect('event', { event_type: 'custom', event_value }); } + + if (!window.umamiUnregister) { + window.umamiUnregister = () => { + pushStateUnhook(); + replaceStateUnhook(); + removeEvents(); + }; + } })(window);