-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace Vercel Analytics with Friendly Analytics / Matomo (#110)
* feat: Friendly Analytics instead of Vercel Analytics
- Loading branch information
Showing
5 changed files
with
62 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"use client"; | ||
import { init, push } from "@socialgouv/matomo-next"; | ||
import { usePathname, useSearchParams } from "next/navigation"; | ||
import { Suspense, useEffect, useRef } from "react"; | ||
|
||
// matomo-next does not (yet) support next's app router natively | ||
// details see https://github.com/SocialGouv/matomo-next/issues/99 | ||
// hence we do some manual setup and event tracking in a small wrapper | ||
// loosely based on https://github.com/betagouv/reno/blob/master/utils/Matomo.tsx | ||
|
||
const MATOMO_URL = | ||
process.env.NEXT_PUBLIC_MATOMO_URL || "https://app.friendlyanalytics.ch"; | ||
const MATOMO_SITE_ID = process.env.NEXT_PUBLIC_MATOMO_SITE_ID || undefined; | ||
|
||
const FriendlyComponent = () => { | ||
const pathname = usePathname(), | ||
searchParams = useSearchParams(); | ||
const searchParamsString = searchParams.toString(); | ||
const isInitialLoad = useRef(true); | ||
|
||
useEffect(() => { | ||
init({ url: MATOMO_URL, siteId: MATOMO_SITE_ID ?? "" }); | ||
return () => push(["HeatmapSessionRecording::disable"]); | ||
}, []); | ||
|
||
useEffect(() => { | ||
if (isInitialLoad.current) { | ||
isInitialLoad.current = false; | ||
} else { | ||
if (pathname) { | ||
const url = | ||
pathname + (searchParamsString ? "?" + searchParamsString : ""); | ||
push(["setCustomUrl", pathname]); | ||
push(["disableCookies"]); | ||
push(["trackPageView"]); | ||
} | ||
} | ||
}, [pathname, searchParamsString]); | ||
|
||
return null; | ||
}; | ||
|
||
export default function Friendly() { | ||
return ( | ||
<Suspense> | ||
<FriendlyComponent /> | ||
</Suspense> | ||
); | ||
} |