Skip to content

Commit

Permalink
feat: add analytics to icons-block (#561)
Browse files Browse the repository at this point in the history
* feat: add analytics to icons-block

* fix: separate events for icons

* fix: add analyticsEvents to schema

* fix: fix after review
  • Loading branch information
Marginy605 authored Sep 14, 2023
1 parent e8a2567 commit 75bafc4
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
14 changes: 12 additions & 2 deletions src/blocks/Icons/Icons.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import React, {Fragment, useContext} from 'react';
import React, {Fragment, useCallback, useContext} from 'react';

import {Image, Title} from '../../components';
import {LocationContext} from '../../context/locationContext';
import {IconsBlockProps} from '../../models';
import {useAnalytics} from '../../hooks';
import {IconsBlockItemProps, IconsBlockProps} from '../../models';
import {block, getLinkProps} from '../../utils';

import './Icons.scss';
Expand All @@ -18,6 +19,14 @@ const getItemContent = (item: IconsBlockProps['items'][number]) => (

const Icons = ({title, size = 's', items}: IconsBlockProps) => {
const {hostname} = useContext(LocationContext);
const handleAnalytics = useAnalytics();

const onClick = useCallback(
({analyticsEvents, url}: IconsBlockItemProps) => {
handleAnalytics(analyticsEvents, {url});
},
[handleAnalytics],
);

return (
<div className={b({size})}>
Expand All @@ -33,6 +42,7 @@ const Icons = ({title, size = 's', items}: IconsBlockProps) => {
aria-label={text}
title={text}
{...getLinkProps(url, hostname)}
onClick={() => onClick(item)}
>
{itemContent}
</a>
Expand Down
14 changes: 14 additions & 0 deletions src/blocks/Icons/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {AnimatableProps, BlockBaseProps} from '../../schema/validators/common';
import {AnalyticsEventSchema} from '../../schema/validators/event';

export const IconsProps = {
additionalProperties: false,
Expand Down Expand Up @@ -33,6 +34,19 @@ export const IconsProps = {
type: 'string',
},
},
analyticsEvents: {
oneOf: [
{
...AnalyticsEventSchema,
optionName: 'single',
},
{
type: 'array',
items: AnalyticsEventSchema,
optionName: 'list',
},
],
},
},
},
},
Expand Down
13 changes: 8 additions & 5 deletions src/models/constructor-items/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {ButtonSize} from '@gravity-ui/uikit';

import {GridColumnSize, GridColumnSizesType} from '../../grid/types';
import {ThemeSupporting} from '../../utils';
import {AnalyticsEventsBase} from '../common';

import {
AnchorProps,
Expand Down Expand Up @@ -312,14 +313,16 @@ export interface FilterBlockProps extends Animatable, LoadableChildren {
centered?: boolean;
}

export interface IconsBlockItemProps extends AnalyticsEventsBase {
url: string;
text: string;
src: string;
}

export interface IconsBlockProps {
title?: string;
size?: 's' | 'm' | 'l';
items: {
url: string;
text: string;
src: string;
}[];
items: IconsBlockItemProps[];
}

interface ContentLayoutBlockParams {
Expand Down

0 comments on commit 75bafc4

Please sign in to comment.