Skip to content

Commit

Permalink
Ability to unregister umami
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszhernas committed Sep 14, 2020
1 parent 8727dbf commit 2ea91aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand Down
12 changes: 10 additions & 2 deletions tracker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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);

0 comments on commit 2ea91aa

Please sign in to comment.