Skip to content

Commit d409b65

Browse files
Valent1325Maksim Malofeev
andauthored
feat(NavigationLink): add handleAnalytics (#1198)
Co-authored-by: Maksim Malofeev <[email protected]>
1 parent b98717c commit d409b65

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

src/models/navigation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {ThemeSupporting} from '../utils';
22

33
import {ButtonProps, ImageProps} from './constructor-items';
4+
import {AnalyticsEventsBase} from './common';
45

56
export enum NavigationItemType {
67
Link = 'link',
@@ -41,7 +42,7 @@ export interface NavigationGithubButton extends Omit<NavigationItemBase, 'icon'>
4142
size?: string;
4243
}
4344

44-
export interface NavigationLinkItem extends Omit<NavigationItemBase, 'url'> {
45+
export interface NavigationLinkItem extends Omit<NavigationItemBase, 'url'>, AnalyticsEventsBase {
4546
type: NavigationItemType.Link;
4647
url: string;
4748
urlTitle?: string;

src/navigation/components/NavigationItem/components/NavigationLink/NavigationLink.tsx

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import * as React from 'react';
33
import {RouterLink} from '../../../../../components';
44
import {getMediaImage} from '../../../../../components/Media/Image/utils';
55
import {LocationContext} from '../../../../../context/locationContext';
6+
import {useAnalytics} from '../../../../../hooks';
67
import {NavigationArrow} from '../../../../../icons';
7-
import {NavigationLinkItem} from '../../../../../models';
8+
import {DefaultEventNames, NavigationLinkItem} from '../../../../../models';
89
import {block, getLinkProps} from '../../../../../utils';
910
import {NavigationItemProps} from '../../../../models';
1011
import {ContentWrapper} from '../ContentWrapper/ContentWrapper';
@@ -17,9 +18,26 @@ type NavigationLinkProps = NavigationItemProps & NavigationLinkItem;
1718

1819
export const NavigationLink: React.FC<NavigationLinkProps> = (props) => {
1920
const {hostname, Link} = React.useContext(LocationContext);
20-
const {url, text, icon, arrow, target, className, iconSize, urlTitle, ...rest} = props;
21+
const {
22+
url,
23+
text,
24+
icon,
25+
arrow,
26+
target,
27+
className,
28+
iconSize,
29+
urlTitle,
30+
analyticsEvents,
31+
...rest
32+
} = props;
33+
2134
const linkExtraProps = getLinkProps(url, hostname, target);
2235
const iconData = icon && getMediaImage(icon);
36+
const handleAnalytics = useAnalytics(DefaultEventNames.Link, url);
37+
38+
const onClick = () => {
39+
handleAnalytics(analyticsEvents);
40+
};
2341

2442
const classes = b(null, className);
2543
const content = (
@@ -35,6 +53,7 @@ export const NavigationLink: React.FC<NavigationLinkProps> = (props) => {
3553
href={url}
3654
title={urlTitle || text}
3755
className={classes}
56+
onClick={onClick}
3857
{...rest}
3958
{...linkExtraProps}
4059
>
@@ -44,7 +63,15 @@ export const NavigationLink: React.FC<NavigationLinkProps> = (props) => {
4463
} else {
4564
return (
4665
<RouterLink href={url} passHref>
47-
<a title={urlTitle || text} {...rest} className={classes}>
66+
<a
67+
role="link"
68+
title={urlTitle || text}
69+
{...rest}
70+
className={classes}
71+
tabIndex={0}
72+
onClick={onClick}
73+
onKeyDown={(e) => e.key === 'Enter' && onClick()}
74+
>
4875
{content}
4976
</a>
5077
</RouterLink>

0 commit comments

Comments
 (0)