-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMatomo.tsx
40 lines (35 loc) · 1.09 KB
/
Matomo.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use client'
import { init, push } from '@socialgouv/matomo-next'
import { usePathname, useSearchParams } from 'next/navigation'
import { Suspense, useEffect, useState } from 'react'
const MATOMO_URL = 'https://stats.beta.gouv.fr'
const MATOMO_SITE_ID = '101'
const MatomoComponent = () => {
const [initialised, setInitialised] = useState(false)
useEffect(() => {
if (MATOMO_URL && MATOMO_SITE_ID && !initialised) {
init({ url: MATOMO_URL, siteId: MATOMO_SITE_ID })
}
return () => {
setInitialised(true)
}
}, [initialised, setInitialised])
const searchParams = useSearchParams(),
pathname = usePathname()
const searchParamsString = searchParams.toString()
useEffect(() => {
if (!pathname) return
// may be necessary to decodeURIComponent searchParamsString ?
const url = pathname + (searchParamsString ? '?' + searchParamsString : '')
push(['setCustomUrl', url])
push(['trackPageView'])
}, [pathname, searchParamsString])
return null
}
export default function Matomo() {
return (
<Suspense>
<MatomoComponent />
</Suspense>
)
}