Open rich-info opened 1 year ago
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
}
}, [])
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!
@tordans shouldn't the library just handle mutliple init
calls properly ?
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 https://github.com/SocialGouv/matomo-next/issues/99 for WIP solution.
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