1
- import { FC , RefAttributes } from "react"
2
1
import { useRouter } from "next/router"
3
2
import { RxExternalLink } from "react-icons/rx"
4
3
import {
@@ -13,8 +12,8 @@ import {
13
12
VisuallyHidden ,
14
13
} from "@chakra-ui/react"
15
14
15
+ import { type MatomoEventOptions , trackCustomEvent } from "@/lib/utils/matomo"
16
16
import { getRelativePath } from "@/lib/utils/relativePath"
17
- // import { trackCustomEvent, MatomoEventOptions } from "@/lib/utils/matomo"
18
17
import * as url from "@/lib/utils/url"
19
18
20
19
import { DISCORD_PATH , SITE_URL } from "@/lib/constants"
@@ -28,13 +27,11 @@ type BaseProps = {
28
27
hideArrow ?: boolean
29
28
isPartiallyActive ?: boolean
30
29
activeStyle ?: StyleProps
31
- // customEventOptions?: MatomoEventOptions
30
+ customEventOptions ?: MatomoEventOptions
32
31
}
33
32
34
33
export type LinkProps = BaseProps & Omit < NextLinkProps , "href" >
35
34
36
- type LinkComponent = FC < RefAttributes < HTMLAnchorElement > & LinkProps >
37
-
38
35
/**
39
36
* Link wrapper which handles:
40
37
*
@@ -47,23 +44,25 @@ type LinkComponent = FC<RefAttributes<HTMLAnchorElement> & LinkProps>
47
44
* - PDFs & static files (which open in a new tab)
48
45
* e.g. <Link href="/eth-whitepaper.pdf">
49
46
*/
50
- export const BaseLink : LinkComponent = forwardRef ( function Link ( props , ref ) {
51
- const {
52
- href : hrefProp ,
47
+ export const BaseLink = forwardRef ( function Link (
48
+ {
53
49
to,
50
+ href : hrefProp ,
54
51
children,
55
52
hideArrow,
56
53
isPartiallyActive = true ,
57
54
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
62
61
63
62
const { asPath, locale } = useRouter ( )
64
63
const { flipForRtl } = useRtlFlip ( )
65
- const isActive = url . isHrefActive ( href , asPath , isPartiallyActive )
66
64
65
+ const isActive = url . isHrefActive ( href , asPath , isPartiallyActive )
67
66
const isDiscordInvite = url . isDiscordInvite ( href )
68
67
const isPdf = url . isPdf ( href )
69
68
const isExternal = url . isExternal ( href )
@@ -81,14 +80,29 @@ export const BaseLink: LinkComponent = forwardRef(function Link(props, ref) {
81
80
82
81
const commonProps = {
83
82
ref,
84
- href,
85
- ...rest ,
83
+ ...props ,
86
84
...( isActive && activeStyle ) ,
85
+ href,
87
86
}
88
87
89
88
if ( isInternalPdf || isExternal ) {
90
89
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
+ >
92
106
{ children }
93
107
< VisuallyHidden > (opens in a new tab)</ VisuallyHidden >
94
108
{ ! hideArrow && (
@@ -106,13 +120,26 @@ export const BaseLink: LinkComponent = forwardRef(function Link(props, ref) {
106
120
}
107
121
108
122
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
+ >
110
137
{ children }
111
138
</ NextLink >
112
139
)
113
140
} )
114
141
115
- const InlineLink : FC < LinkProps > = forwardRef ( ( props , ref ) => {
142
+ const InlineLink = forwardRef ( ( props : LinkProps , ref ) => {
116
143
const { locale } = useRouter ( )
117
144
118
145
return < BaseLink data-inline-link ref = { ref } locale = { locale } { ...props } />
0 commit comments