plausible-tracker icon indicating copy to clipboard operation
plausible-tracker copied to clipboard

Page views not tracked when using History's `replaceState`

Open cosminstefanxp opened this issue 2 years ago • 2 comments

Versions

  • 0.3.1

Describe the bug

When using a router that makes use of History.replaceState, the page views are tracked. E.g. using Vue Router, such as $router.replace("/"), the mutation is not tracked.

Expected behavior

When replacing state, the page view should be tracked.

Steps to reproduce

Steps:

  1. Install normally
  2. Set it up
  3. User history.replaceState();

cosminstefanxp avatar Oct 14 '21 18:10 cosminstefanxp

The solution should be relatively easy: override the implementation for replaceState() similar to how it's done for pushState. @Maronato, am I missing something? Is there any reason why it wasn't done in the first place?

cosminstefanxp avatar Oct 14 '21 18:10 cosminstefanxp

Hi, Is there a design reason which makes it not desired to track replaceState? Would a pull request be accepted? Does it have to be behind a flag?

Here a proof of concept custom code to workaround

const originalReplaceState = window.history.replaceState;
if (originalReplaceState) {
    window.history.replaceState = function (data, title, url) {
        originalReplaceState.apply(this, [data, title, url]);
        if (window.plausible) {
            window.plausible('pageview');
        }
    }
}

bfritscher avatar Feb 21 '23 15:02 bfritscher