fix: support for tracking link clicks

This commit is contained in:
Christel Westerberg
2024-07-12 09:59:39 +02:00
parent 546679387f
commit edb6005a72
8 changed files with 107 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
"use client"
import NextLink from "next/link"
import { usePathname } from "next/navigation"
import { useEffect } from "react"
import { linkVariants } from "./variants"
@@ -16,6 +17,7 @@ export default function Link({
scroll = true,
prefetch,
variant,
trackingId,
...props
}: LinkProps) {
const currentPageSlug = usePathname()
@@ -31,6 +33,18 @@ export default function Link({
size,
variant,
})
useEffect(() => {
if (trackingId) {
document.getElementById(trackingId)?.addEventListener("click", () => {})
return () => {
document
.getElementById(trackingId)
?.removeEventListener("click", () => {})
}
}
}, [trackingId])
return (
<NextLink
scroll={scroll}