Skip to content

Commit 94e2ac3

Browse files
authored
Merge pull request #157 from ethereum/matomo-links
Implement Matomo tracking in Link component
2 parents 32ad1d5 + b094758 commit 94e2ac3

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

src/components/Link.tsx

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { FC, RefAttributes } from "react"
21
import { useRouter } from "next/router"
32
import { RxExternalLink } from "react-icons/rx"
43
import {
@@ -13,8 +12,8 @@ import {
1312
VisuallyHidden,
1413
} from "@chakra-ui/react"
1514

15+
import { type MatomoEventOptions, trackCustomEvent } from "@/lib/utils/matomo"
1616
import { getRelativePath } from "@/lib/utils/relativePath"
17-
// import { trackCustomEvent, MatomoEventOptions } from "@/lib/utils/matomo"
1817
import * as url from "@/lib/utils/url"
1918

2019
import { DISCORD_PATH, SITE_URL } from "@/lib/constants"
@@ -28,13 +27,11 @@ type BaseProps = {
2827
hideArrow?: boolean
2928
isPartiallyActive?: boolean
3029
activeStyle?: StyleProps
31-
// customEventOptions?: MatomoEventOptions
30+
customEventOptions?: MatomoEventOptions
3231
}
3332

3433
export type LinkProps = BaseProps & Omit<NextLinkProps, "href">
3534

36-
type LinkComponent = FC<RefAttributes<HTMLAnchorElement> & LinkProps>
37-
3835
/**
3936
* Link wrapper which handles:
4037
*
@@ -47,23 +44,25 @@ type LinkComponent = FC<RefAttributes<HTMLAnchorElement> & LinkProps>
4744
* - PDFs & static files (which open in a new tab)
4845
* e.g. <Link href="/eth-whitepaper.pdf">
4946
*/
50-
export const BaseLink: LinkComponent = forwardRef(function Link(props, ref) {
51-
const {
52-
href: hrefProp,
47+
export const BaseLink = forwardRef(function Link(
48+
{
5349
to,
50+
href: hrefProp,
5451
children,
5552
hideArrow,
5653
isPartiallyActive = true,
5754
activeStyle = { color: "primary.base" },
58-
...rest
59-
} = props
60-
61-
let href = (to ?? hrefProp)!
55+
customEventOptions,
56+
...props
57+
}: LinkProps,
58+
ref
59+
) {
60+
let href = (to ?? hrefProp) as string
6261

6362
const { asPath, locale } = useRouter()
6463
const { flipForRtl } = useRtlFlip()
65-
const isActive = url.isHrefActive(href, asPath, isPartiallyActive)
6664

65+
const isActive = url.isHrefActive(href, asPath, isPartiallyActive)
6766
const isDiscordInvite = url.isDiscordInvite(href)
6867
const isPdf = url.isPdf(href)
6968
const isExternal = url.isExternal(href)
@@ -81,14 +80,29 @@ export const BaseLink: LinkComponent = forwardRef(function Link(props, ref) {
8180

8281
const commonProps = {
8382
ref,
84-
href,
85-
...rest,
83+
...props,
8684
...(isActive && activeStyle),
85+
href,
8786
}
8887

8988
if (isInternalPdf || isExternal) {
9089
return (
91-
<ChakraLink {...commonProps} isExternal>
90+
<ChakraLink
91+
isExternal
92+
onClick={() =>
93+
trackCustomEvent(
94+
customEventOptions ?? {
95+
eventCategory: `Link`,
96+
eventAction: `Clicked`,
97+
eventName: `Clicked on ${
98+
isInternalPdf ? "internal PDF" : "external link"
99+
}`,
100+
eventValue: href,
101+
}
102+
)
103+
}
104+
{...commonProps}
105+
>
92106
{children}
93107
<VisuallyHidden>(opens in a new tab)</VisuallyHidden>
94108
{!hideArrow && (
@@ -106,13 +120,26 @@ export const BaseLink: LinkComponent = forwardRef(function Link(props, ref) {
106120
}
107121

108122
return (
109-
<NextLink locale={locale} {...commonProps}>
123+
<NextLink
124+
locale={locale}
125+
onClick={() =>
126+
trackCustomEvent(
127+
customEventOptions ?? {
128+
eventCategory: `Link`,
129+
eventAction: `Clicked`,
130+
eventName: `Clicked on internal link`,
131+
eventValue: href,
132+
}
133+
)
134+
}
135+
{...commonProps}
136+
>
110137
{children}
111138
</NextLink>
112139
)
113140
})
114141

115-
const InlineLink: FC<LinkProps> = forwardRef((props, ref) => {
142+
const InlineLink = forwardRef((props: LinkProps, ref) => {
116143
const { locale } = useRouter()
117144

118145
return <BaseLink data-inline-link ref={ref} locale={locale} {...props} />

0 commit comments

Comments
 (0)