Skip to content
This repository has been archived by the owner on Jul 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #923 from prezly/feature/dev-12744-rework-how-ifra…
Browse files Browse the repository at this point in the history
…mely-embeds-are-rendered

[DEV-12744] Feature - Track when embeds and videos are played for the first time
  • Loading branch information
kudlajz authored Apr 8, 2024
2 parents 68fc812 + 13f342b commit af96289
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 9 deletions.
6 changes: 5 additions & 1 deletion components/ContentRenderer/ContentRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { Node } from '@prezly/story-content-format';
import {
AttachmentNode,
ButtonBlockNode,
EmbedNode,
GalleryNode,
HeadingNode,
HtmlNode,
Expand All @@ -15,6 +16,7 @@ import {
QuoteNode,
StoryBookmarkNode,
VariableNode,
VideoNode,
} from '@prezly/story-content-format';
import { useEffect } from 'react';

Expand All @@ -29,7 +31,7 @@ import {
Quote,
} from '@/components/RichText';

import { Attachment, Gallery, Image, StoryBookmark, Variable } from './components';
import { Attachment, Embed, Gallery, Image, StoryBookmark, Variable, Video } from './components';

import styles from './ContentRenderer.module.scss';

Expand All @@ -54,6 +56,7 @@ export default function ContentRenderer({ nodes }: Props) {
match={ButtonBlockNode.isButtonBlockNode}
component={Elements.ButtonBlock}
/>
<Component match={EmbedNode.isEmbedNode} component={Embed} />
<Component match={GalleryNode.isGalleryNode} component={Gallery} />
{/* Title and Subtitle heading rules must be defined above the general Heading */}
<Component match={HeadingNode.isTitleHeadingNode} component={Elements.Ignore} />
Expand All @@ -72,6 +75,7 @@ export default function ContentRenderer({ nodes }: Props) {
component={StoryBookmark}
/>
<Component match={VariableNode.isVariableNode} component={Variable} />
<Component match={VideoNode.isVideoNode} component={Video} />
</Renderer>
</div>
);
Expand Down
24 changes: 24 additions & 0 deletions components/ContentRenderer/components/Embed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use client';

import { MEDIA, useAnalytics } from '@prezly/analytics-nextjs';
import { Elements } from '@prezly/content-renderer-react-js';
import type { EmbedNode } from '@prezly/story-content-format';
import { useCallback, useRef } from 'react';

interface Props {
node: EmbedNode;
}

export function Embed({ node }: Props) {
const { track } = useAnalytics();
const isEventTracked = useRef(false);

const handlePlay = useCallback(() => {
if (!isEventTracked.current) {
track(MEDIA.PLAY);
isEventTracked.current = true;
}
}, [track]);

return <Elements.Embed node={node} onPlay={handlePlay} />;
}
24 changes: 24 additions & 0 deletions components/ContentRenderer/components/Video.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
'use client';

import { MEDIA, useAnalytics } from '@prezly/analytics-nextjs';
import { Elements } from '@prezly/content-renderer-react-js';
import type { VideoNode } from '@prezly/story-content-format';
import { useCallback, useRef } from 'react';

interface Props {
node: VideoNode;
}

export function Video({ node }: Props) {
const { track } = useAnalytics();
const isEventTracked = useRef(false);

const handlePlay = useCallback(() => {
if (!isEventTracked.current) {
track(MEDIA.PLAY);
isEventTracked.current = true;
}
}, [track]);

return <Elements.Video node={node} onPlay={handlePlay} />;
}
2 changes: 2 additions & 0 deletions components/ContentRenderer/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export { Attachment } from './Attachment';
export { Embed } from './Embed';
export { Gallery } from './Gallery';
export { Image } from './Image';
export { StoryBookmark } from './StoryBookmark';
export { Variable } from './Variable';
export { Video } from './Video';
26 changes: 19 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@headlessui/react": "1.7.18",
"@playwright/test": "^1.33.0",
"@prezly/analytics-nextjs": "2.0.1",
"@prezly/content-renderer-react-js": "0.35.0",
"@prezly/content-renderer-react-js": "0.36.1",
"@prezly/sdk": "18.3.0",
"@prezly/story-content-format": "0.64.0",
"@prezly/theme-kit-core": "7.1.0",
Expand Down

0 comments on commit af96289

Please sign in to comment.