Skip to content

Commit dd392e3

Browse files
committed
Add social media sharing tracking
1 parent 695f0c1 commit dd392e3

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

components/SocialShare/SocialShare.tsx

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
'use client';
22

3+
import { useAnalytics } from '@prezly/analytics-nextjs';
4+
import type { NewsroomGallery, Story } from '@prezly/sdk';
5+
36
import classNames from 'classnames';
47
import {
58
FacebookShareButton,
@@ -30,16 +33,34 @@ interface Props {
3033
socialNetworks: ThemeSettings['sharing_actions'];
3134
thumbnailUrl?: string;
3235
withLabels?: boolean;
36+
trackingContext: 'Story Page Header' | 'Story Page Footer' | 'Gallery';
37+
uuid: Story['uuid'] | NewsroomGallery['uuid'];
3338
}
3439

35-
export function SocialShare({ className, socialNetworks, thumbnailUrl, url, withLabels }: Props) {
40+
export function SocialShare({
41+
className,
42+
socialNetworks,
43+
thumbnailUrl,
44+
trackingContext,
45+
url,
46+
uuid,
47+
withLabels,
48+
}: Props) {
49+
const { track } = useAnalytics();
50+
3651
return (
3752
<div className={classNames(className, styles.social, { [styles.withLabels]: withLabels })}>
3853
{socialNetworks.includes(SocialNetwork.LINKEDIN) && (
3954
<LinkedinShareButton
4055
data-title="Share on Linkedin"
4156
className={styles.socialButton}
4257
url={url}
58+
onClick={() =>
59+
track(
60+
`Newsroom - ${trackingContext} - Share to ${SocialNetwork.LINKEDIN}`,
61+
{ id: uuid },
62+
)
63+
}
4364
>
4465
<IconLinkedin className={styles.socialIcon} />
4566
</LinkedinShareButton>
@@ -50,6 +71,12 @@ export function SocialShare({ className, socialNetworks, thumbnailUrl, url, with
5071
data-title="Share on Facebook"
5172
className={styles.socialButton}
5273
url={url}
74+
onClick={() =>
75+
track(
76+
`Newsroom - ${trackingContext} - Share to ${SocialNetwork.FACEBOOK}`,
77+
{ id: uuid },
78+
)
79+
}
5380
>
5481
<IconFacebook className={styles.socialIcon} />
5582
</FacebookShareButton>
@@ -60,6 +87,11 @@ export function SocialShare({ className, socialNetworks, thumbnailUrl, url, with
6087
data-title="Share on X"
6188
className={styles.socialButton}
6289
url={url}
90+
onClick={() =>
91+
track(`Newsroom - ${trackingContext} - Share to ${SocialNetwork.TWITTER}`, {
92+
id: uuid,
93+
})
94+
}
6395
>
6496
<IconTwitter className={styles.socialIcon} />
6597
</TwitterShareButton>
@@ -71,6 +103,12 @@ export function SocialShare({ className, socialNetworks, thumbnailUrl, url, with
71103
className={styles.socialButton}
72104
media={thumbnailUrl}
73105
url={url}
106+
onClick={() =>
107+
track(
108+
`Newsroom - ${trackingContext} - Share to ${SocialNetwork.PINTEREST}`,
109+
{ id: uuid },
110+
)
111+
}
74112
>
75113
<IconPinterest className={styles.socialIcon} />
76114
</PinterestShareButton>
@@ -81,6 +119,11 @@ export function SocialShare({ className, socialNetworks, thumbnailUrl, url, with
81119
data-title="Share on Reddit"
82120
className={styles.socialButton}
83121
url={url}
122+
onClick={() =>
123+
track(`Newsroom - ${trackingContext} - Share to ${SocialNetwork.REDDIT}`, {
124+
id: uuid,
125+
})
126+
}
84127
>
85128
<IconReddit className={styles.socialIcon} />
86129
</RedditShareButton>
@@ -97,6 +140,12 @@ export function SocialShare({ className, socialNetworks, thumbnailUrl, url, with
97140
data-title="Share on WhatsApp"
98141
className={styles.socialButton}
99142
url={url}
143+
onClick={() =>
144+
track(
145+
`Newsroom - ${trackingContext} - Share to ${SocialNetwork.WHATSAPP}`,
146+
{ id: uuid },
147+
)
148+
}
100149
>
101150
<IconWhatsApp className={styles.socialIcon} />
102151
</WhatsappShareButton>
@@ -107,6 +156,12 @@ export function SocialShare({ className, socialNetworks, thumbnailUrl, url, with
107156
data-title="Share on Telegram"
108157
className={styles.socialButton}
109158
url={url}
159+
onClick={() =>
160+
track(
161+
`Newsroom - ${trackingContext} - Share to ${SocialNetwork.TELEGRAM}`,
162+
{ id: uuid },
163+
)
164+
}
110165
>
111166
<IconTelegram className={styles.socialIcon} />
112167
</TelegramShareButton>

modules/Gallery/Gallery.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ export function Gallery({ localeCode, gallery, href, socialNetworks }: Props) {
3333
{downloadUrl && <DownloadLink localeCode={localeCode} href={downloadUrl} />}
3434
{socialNetworks.length > 0 && href && (
3535
<SocialShare
36+
className={styles.shareLinks}
3637
socialNetworks={socialNetworks}
38+
trackingContext="Gallery"
3739
url={href}
38-
className={styles.shareLinks}
40+
uuid={gallery.uuid}
3941
/>
4042
)}
4143
</div>

modules/Story/Share/Share.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ export function Share({
9090
<SocialShare
9191
socialNetworks={socialNetworks}
9292
url={url}
93+
uuid={uuid}
9394
thumbnailUrl={thumbnailUrl}
95+
trackingContext="Story Page Footer"
9496
withLabels={socialShareButtonsCount <= 2}
9597
/>
9698

modules/Story/Story.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export async function Story({ actions, sharingOptions, showDate, story, withHead
7373
<SocialShare
7474
socialNetworks={sharingOptions.sharing_actions}
7575
url={sharingUrl}
76+
uuid={uuid}
7677
thumbnailUrl={thumbnailUrl}
78+
trackingContext="Story Page Header"
7779
/>
7880
)}
7981
</div>

0 commit comments

Comments
 (0)