plausible / plausible-tracker

Frontend library to interact with Plausible Analytics
https://github.com/plausible/plausible-tracker
MIT License
214 stars 47 forks source link

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

Open cosminstefanxp opened 2 years ago

cosminstefanxp commented 2 years ago

Versions

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 commented 2 years ago

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?

bfritscher commented 1 year ago

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');
        }
    }
}