plausible / plausible-tracker

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

Pageviews is duplicated when using hashMode and browser back button #58

Open eferroni opened 1 year ago

eferroni commented 1 year ago

I'm using the hashMode property as true and the enableAutoPageviews method to start plausible tracker in my index.tsx file. It's working as expect when I navigate between pages with hashes, except when I use the browser back button. When I use the browser back button, plausible tracker is tracking the destination page twice. Example: from /test#tab to /test Probably because of:

    const page = () => trackPageview();
    // Attach pushState and popState listeners
    const originalPushState = history.pushState;
    if (originalPushState) {
      // eslint-disable-next-line functional/immutable-data
      history.pushState = function (data, title, url) {
        originalPushState.apply(this, [data, title, url]);
        page();
      };
      addEventListener('popstate', page); // track the browser back behavior
    }

    // Attach hashchange listener
    if (defaults && defaults.hashMode) {
      addEventListener('hashchange', page); // track when the hash changes from #tab to no hash fragment
    }