Skip to content

Commit 574a6c6

Browse files
committed
feat: add analytics tracking for mark as read action and refactor related functions
1 parent 872f752 commit 574a6c6

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

src/components/Elements/CardWithActions/CardItemWithActions.tsx

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@ import React, { useCallback, useEffect, useState } from 'react'
33
import { BiBookmarkMinus, BiBookmarkPlus, BiCheckDouble, BiShareAlt } from 'react-icons/bi'
44
import { ShareModal } from 'src/features/shareModal'
55
import { ShareModalData } from 'src/features/shareModal/types'
6-
import { Attributes, trackLinkBookmark, trackLinkUnBookmark } from 'src/lib/analytics'
6+
import {
7+
Attributes,
8+
trackLinkBookmark,
9+
trackLinkUnBookmark,
10+
trackMarkAsRead,
11+
} from 'src/lib/analytics'
712
import { useBookmarks } from 'src/stores/bookmarks'
813
import { useReadPosts } from 'src/stores/readPosts'
914
import { useShallow } from 'zustand/shallow'
@@ -46,15 +51,25 @@ export const CardItemWithActions = ({
4651
userBookmarks.some((bm) => bm.source === source && bm.url === item.url)
4752
)
4853

49-
const onMarkAsReadClick = useCallback(() => {
54+
const onMarkAsReadClicked = useCallback(() => {
5055
if (isRead) {
5156
markAsUnread(item.id)
5257
} else {
5358
markAsRead(item.id)
5459
}
60+
61+
if (isRead) {
62+
const analyticsAttrs = {
63+
[Attributes.TRIGERED_FROM]: 'card',
64+
[Attributes.TITLE]: item.title,
65+
[Attributes.LINK]: item.url,
66+
[Attributes.SOURCE]: source,
67+
}
68+
trackMarkAsRead(analyticsAttrs)
69+
}
5570
}, [isRead, item.id])
5671

57-
const onBookmarkClick = useCallback(() => {
72+
const onBookmarkClicked = useCallback(() => {
5873
const itemToBookmark = {
5974
title: item.title,
6075
url: item.url,
@@ -113,15 +128,15 @@ export const CardItemWithActions = ({
113128
{showBookmarkAction && (
114129
<button
115130
className={`blockActionButton ${isBookmarked ? 'active' : ''}`}
116-
onClick={onBookmarkClick}
131+
onClick={onBookmarkClicked}
117132
aria-label="Bookmark item">
118133
{!isBookmarked ? <BiBookmarkPlus /> : <BiBookmarkMinus />}
119134
</button>
120135
)}
121136

122137
<button
123138
className={`blockActionButton ${isRead ? 'active' : ''}`}
124-
onClick={onMarkAsReadClick}
139+
onClick={onMarkAsReadClicked}
125140
aria-label={isRead ? 'Mark as unread' : 'Mark as read'}>
126141
<BiCheckDouble />
127142
</button>

src/lib/analytics.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ enum Verbs {
3737
TARGET = 'Target',
3838
BOOKMARK = 'Bookmark',
3939
UNBOOKMARK = 'Unbookmark',
40+
MARK_AS_READ = 'Mark as Read',
4041
REMOVE = 'Remove',
4142
START = 'Start',
4243
FINISH = 'Finish',
@@ -226,6 +227,14 @@ export const trackLinkUnBookmark = (attributes: { [P: string]: string }) => {
226227
})
227228
}
228229

230+
export const trackMarkAsRead = (attributes: { [P: string]: string }) => {
231+
trackEvent({
232+
object: Objects.LINK,
233+
verb: Verbs.MARK_AS_READ,
234+
attributes,
235+
})
236+
}
237+
229238
export const trackLinkOpen = (attributes: { [P: string]: string | number | undefined }) => {
230239
trackEvent({
231240
object: Objects.LINK,

0 commit comments

Comments
 (0)