Skip to content

Commit 85510cc

Browse files
authored
feat: add analytics to video block (#376)
* feat: add analytics to video block
1 parent 65e3168 commit 85510cc

File tree

4 files changed

+25
-4
lines changed

4 files changed

+25
-4
lines changed

src/components/Media/Media.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export const Media = (props: MediaAllProps) => {
9696
previewImg={previewImg}
9797
height={height}
9898
fullscreen={fullscreen}
99+
analyticsEvents={analyticsEvents}
99100
/>
100101
);
101102
}

src/components/VideoBlock/VideoBlock.tsx

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import {Icon} from '@gravity-ui/uikit';
44
import _ from 'lodash';
55
import {v4 as uuidv4} from 'uuid';
66

7+
import {useAnalytics} from '../../hooks/useAnalytics';
78
import {PlayVideo} from '../../icons';
9+
import {AnalyticsEventsBase, DefaultEventNames} from '../../models/common';
810
import {block, getPageSearchParams} from '../../utils';
911
import Image from '../Image/Image';
1012

@@ -47,7 +49,7 @@ export function getHeight(width: number): number {
4749
return (width / 16) * 9;
4850
}
4951

50-
export interface VideoBlockProps {
52+
export interface VideoBlockProps extends AnalyticsEventsBase {
5153
id?: string;
5254
stream?: string;
5355
record?: string;
@@ -60,8 +62,20 @@ export interface VideoBlockProps {
6062
}
6163

6264
const VideoBlock = (props: VideoBlockProps) => {
63-
const {stream, record, attributes, className, id, previewImg, playButton, height, fullscreen} =
64-
props;
65+
const {
66+
stream,
67+
record,
68+
attributes,
69+
className,
70+
id,
71+
previewImg,
72+
playButton,
73+
height,
74+
fullscreen,
75+
analyticsEvents,
76+
} = props;
77+
const handleAnalytics = useAnalytics(DefaultEventNames.VideoPreview);
78+
6579
const src = getVideoSrc(stream, record);
6680
const ref = useRef<HTMLDivElement>(null);
6781
const iframeRef = useRef<HTMLIFrameElement>();
@@ -70,6 +84,8 @@ const VideoBlock = (props: VideoBlockProps) => {
7084
const [currentHeight, setCurrentHeight] = useState(height || undefined);
7185
const fullId = id || uuidv4();
7286
const onPreviewClick = useCallback(() => {
87+
handleAnalytics(analyticsEvents);
88+
7389
if (iframeRef.current) {
7490
iframeRef.current.src = `${src}?${getPageSearchParams({
7591
...AUTOPLAY_ATTRIBUTES,
@@ -78,7 +94,7 @@ const VideoBlock = (props: VideoBlockProps) => {
7894
}
7995

8096
setTimeout(() => setHidePreview(true), AUTOPLAY_DELAY);
81-
}, [src, attributes]);
97+
}, [handleAnalytics, analyticsEvents, src, attributes]);
8298

8399
useEffect(() => {
84100
const updateSize = _.debounce(() => {

src/models/common.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export enum DefaultEventNames {
9090
QuoteButton = 'quote-button-click',
9191
BackLink = 'back-link-click',
9292
Breadcrumb = 'breadcrumb-click',
93+
VideoPreview = 'video-preview-click',
9394
}
9495

9596
export type AnalyticsCounters = {

src/schema/validators/common.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,6 +515,9 @@ export const MediaProps = {
515515
fullscreen: {
516516
type: 'boolean',
517517
},
518+
analyticsEvents: {
519+
anyOf: [AnalyticsEventSchema, {type: 'array', items: AnalyticsEventSchema}],
520+
},
518521
};
519522

520523
export const YMapMarkerLabel = {

0 commit comments

Comments
 (0)