matomo-next icon indicating copy to clipboard operation
matomo-next copied to clipboard

Duplicate Page View Tracking in SPA

Open rtrindade08 opened this issue 1 year ago • 1 comments

Hello,

I am currently working on a Single Page Application (SPA) and facing an issue with tracking query strings during page navigation. My goal is to accurately track page views, including the full URL with query strings, whenever the user navigates from one page to another.

I have implemented the following code in my _app file to integrate Matomo tracking:

I've added this in my _app :

const appConfig = useAppConfig()
  useEffect(() => {
    if (appConfig?.matomoUrl && appConfig?.matomoSiteId) {
      init({
        url: appConfig?.matomoUrl,
        siteId: appConfig?.matomoSiteId,
        onRouteChangeComplete: (path) => {
          const fullUrl = window.location.origin + path
          window._paq.push(['setCustomUrl', fullUrl])
          window._paq.push(['trackPageView'])
        },
      })
    }
  }, [appConfig?.matomoUrl, appConfig?.matomoSiteId])

This setup successfully tracks the full URL, including query strings, as intended. However, I have encountered an issue where each page view is being tracked twice.

I am reaching out to see what adjustments in my setup I need to do to ensure that page views are only tracked once per navigation event.

Thank you.

rtrindade08 avatar Dec 07 '23 15:12 rtrindade08