Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

With NextJs 13 error setTrackerUrl (setSiteId) is registered more than once #104

Open
rich-info opened this issue May 19, 2023 · 4 comments
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@rich-info
Copy link

The following error is displayed in the developer console:
The method setTrackerUrl (setSiteId) is registered more than once in "_paq" variable. Only the last call has an effect. Please have a look at the multiple Matomo trackers documentation.

Next.Js version 13.4.2

@tordans
Copy link

tordans commented Jun 13, 2023

I followed https://www.linkedin.com/pulse/preventing-useeffect-from-running-twice-strict-mode-useref-tingre/ and got rid of the warning with the code below.

It would be nicer to have an unInit function that can be called to cleanup the integration in the hooks return statement or something. Maybe init should be useInit and handle this kind of check internally.

  const matomoInitialized = useRef(false)

  useEffect(() => {
    if (MATOMO_URL && MATOMO_SITE_ID && matomoInitialized.current === false) {
      init({ url: MATOMO_URL, siteId: MATOMO_SITE_ID })
    }
    return () => {
      matomoInitialized.current = true
    }
  }, [])

@MaxHjarpe
Copy link

  const matomoInitialized = useRef(false)

  useEffect(() => {
    if (MATOMO_URL && MATOMO_SITE_ID && matomoInitialized.current === false) {
      init({ url: MATOMO_URL, siteId: MATOMO_SITE_ID })
    }
    return () => {
      matomoInitialized.current = true
    }
  }, [])

Thank you! This solved the console error and the issue I had with it registering every page view twice!

@revolunet
Copy link
Member

@tordans shouldn't the library just handle mutliple init calls properly ?

@laem
Copy link

laem commented Mar 18, 2024

In case you were wondering, like me, this snippet will let Matomo track the initial page load, but not the subsequent SPA page router changes. It's not enough and the library does not handle this natively for Next 13.

See #99 for WIP solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

5 participants