Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 20 additions & 40 deletions src/components/ui/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const BaseLink = forwardRef<HTMLAnchorElement, LinkProps>(function Link(
isPartiallyActive = true,
activeClassName = "text-primary",
customEventOptions,
onClick,
...props
}: LinkProps,
ref
Expand Down Expand Up @@ -98,25 +99,29 @@ export const BaseLink = forwardRef<HTMLAnchorElement, LinkProps>(function Link(
href,
}

// Create click handler that tracks events and calls any passed onClick
const createClickHandler =
(eventName: string) => (e: React.MouseEvent<HTMLAnchorElement>) => {
trackCustomEvent(
customEventOptions ?? {
eventCategory: "Link",
eventAction: "Clicked",
eventName: `${eventName} - ${href}`,
}
)
onClick?.(e)
}

if (isExternal) {
const { className, ...rest } = commonProps

return (
<a
target="_blank"
rel="noopener noreferrer"
onClick={() =>
trackCustomEvent(
customEventOptions ?? {
eventCategory: `Link`,
eventAction: `Clicked`,
eventName: "Clicked on external link",
eventValue: href,
}
)
}
className={cn("relative", className)}
{...rest}
onClick={createClickHandler("Clicked on external link")}
className={cn("relative", className)}
>
{isMailto ? (
<span className="text-nowrap">
Expand All @@ -141,17 +146,8 @@ export const BaseLink = forwardRef<HTMLAnchorElement, LinkProps>(function Link(
<NextLink
target="_blank"
rel="noopener noreferrer"
onClick={() =>
trackCustomEvent(
customEventOptions ?? {
eventCategory: `Link`,
eventAction: `Clicked`,
eventName: "Clicked on internal PDF",
eventValue: href,
}
)
}
{...commonProps}
onClick={createClickHandler("Clicked on internal PDF")}
>
{children}
</NextLink>
Expand All @@ -165,18 +161,11 @@ export const BaseLink = forwardRef<HTMLAnchorElement, LinkProps>(function Link(
// as a same-page scroll rather than a route change)
return (
<I18nLink
{...commonProps}
onClick={(e) => {
e.stopPropagation()
trackCustomEvent(
customEventOptions ?? {
eventCategory: "Link",
eventAction: "Clicked",
eventName: "Clicked on hash link",
eventValue: href,
}
)
createClickHandler("Clicked on hash link")(e)
}}
{...commonProps}
>
{children}
</I18nLink>
Expand All @@ -185,17 +174,8 @@ export const BaseLink = forwardRef<HTMLAnchorElement, LinkProps>(function Link(

return (
<I18nLink
onClick={() =>
trackCustomEvent(
customEventOptions ?? {
eventCategory: `Link`,
eventAction: `Clicked`,
eventName: `Clicked on internal link`,
eventValue: href,
}
)
}
{...commonProps}
onClick={createClickHandler("Clicked on internal link")}
>
{children}
</I18nLink>
Expand Down
9 changes: 2 additions & 7 deletions src/components/ui/buttons/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,6 @@ const ButtonLink = React.forwardRef<HTMLAnchorElement, ButtonLinkProps>(
},
ref
) => {
const handleClick: React.MouseEventHandler<HTMLAnchorElement> = (
...args
) => {
customEventOptions && trackCustomEvent(customEventOptions)
onClick?.(...args)
}
return (
<Button
asChild
Expand All @@ -173,8 +167,9 @@ const ButtonLink = React.forwardRef<HTMLAnchorElement, ButtonLinkProps>(
className
)}
activeClassName=""
customEventOptions={customEventOptions}
onClick={onClick}
{...linkProps}
onClick={handleClick}
>
{children}
</BaseLink>
Expand Down