-
Notifications
You must be signed in to change notification settings - Fork 5.1k
/
Copy pathMatomo.tsx
56 lines (47 loc) · 1.49 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"use client"
import { useEffect, useState } from "react"
import { usePathname } from "next/navigation"
import { init, push } from "@socialgouv/matomo-next"
export default function Matomo() {
const pathname = usePathname()
const [inited, setInited] = useState(false)
const [previousPath, setPreviousPath] = useState("")
useEffect(() => {
if (!process.env.IS_PREVIEW_DEPLOY && !inited) {
init({
url: process.env.NEXT_PUBLIC_MATOMO_URL!,
siteId: process.env.NEXT_PUBLIC_MATOMO_SITE_ID!,
})
setInited(true)
}
}, [inited])
/**
* The @socialgouv/matomo-next does not work with next 13
* Code from https://github.com/SocialGouv/matomo-next/issues/99
*/
useEffect(() => {
if (!pathname) {
return
}
if (!previousPath) {
return setPreviousPath(pathname)
}
push(["setReferrerUrl", `${previousPath}`])
push(["setCustomUrl", pathname])
push(["deleteCustomVariables", "page"])
setPreviousPath(pathname)
// In order to ensure that the page title had been updated,
// we delayed pushing the tracking to the next tick.
setTimeout(() => {
push(["setDocumentTitle", document.title])
push(["trackPageView"])
})
/**
* This is because we don't want to track previousPath
* could be a if (previousPath === pathname) return; instead
* But be sure to not send the tracking twice
*/
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pathname])
return <></>
}