:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(notifications): capture panel opens and notification clicks (#89987)
Alex Lider315f5ec16b7e
diff --git a/frontend/src/layout/navigation-3000/sidepanel/panels/activity/sidePanelNotificationsLogic.tsx b/frontend/src/layout/navigation-3000/sidepanel/panels/activity/sidePanelNotificationsLogic.tsxindex 944c0baafa50..4a15467f9f29 100644--- a/frontend/src/layout/navigation-3000/sidepanel/panels/activity/sidePanelNotificationsLogic.tsx+++ b/frontend/src/layout/navigation-3000/sidepanel/panels/activity/sidePanelNotificationsLogic.tsx@@ -319,6 +319,9 @@ export interface sidePanelNotificationsLogicActions {navigateToNotification: (notification: InAppNotification) => {notification: InAppNotification}+ notificationClicked: (notification: InAppNotification) => {+ notification: InAppNotification+ }notificationReceived: (notification: InAppNotification) => {notification: InAppNotification}@@ -472,6 +475,7 @@ export const sidePanelNotificationsLogic = kea<sidePanelNotificationsLogicType>(loadMoreArchivedSuccess: (count: number) => ({ count }),loadArchivedGroupChildren: (group: NotificationGroup) => ({ group }),navigateToNotification: (notification: InAppNotification) => ({ notification }),+ notificationClicked: (notification: InAppNotification) => ({ notification }),loadMoreNotifications: true,loadMoreNotificationsSuccess: (count: number) => ({ count }),loadGroupChildren: (group: NotificationGroup) => ({ group }),@@ -941,6 +945,17 @@ export const sidePanelNotificationsLogic = kea<sidePanelNotificationsLogicType>(cache.disposables.dispose('sseConnection')cache.nextStopReason = null},+ notificationClicked: ({ notification }) => {+ posthog.capture('notification clicked', {+ notification_type: notification.notification_type,+ resource_type: notification.resource_type,+ was_unread: !notification.read,+ // How stale a notification is by the time anyone acts on it+ age_seconds: dayjs().diff(dayjs(notification.created_at), 'second'),+ has_navigation_target: !!values.sourcePathForNotification(notification),+ is_other_project: notification.team_id !== null && notification.team_id !== values.currentTeamId,+ })+ },navigateToNotification: ({ notification }) => {const path = values.sourcePathForNotification(notification)if (!path) {diff --git a/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsx b/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsxindex 32a8ea851561..9dbc8b347d9e 100644--- a/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsx+++ b/frontend/src/lib/components/NotificationsMenu/NotificationRow.tsx@@ -141,7 +141,7 @@ export function NotificationRow({onNavigate?: () => voidreadOnly?: boolean}): JSX.Element {- const { navigateToNotification, toggleRead, markAsRead, archiveNotification } =+ const { navigateToNotification, notificationClicked, toggleRead, markAsRead, archiveNotification } =useActions(sidePanelNotificationsLogic)const { projectNameForNotification, sourcePathForNotification, manuallyToggledIds, archivingEnabled } =useValues(sidePanelNotificationsLogic)@@ -158,6 +158,9 @@ export function NotificationRow({const hasNavigationTarget = !!sourcePathForNotification(notification)const handleOpen = (): void => {+ // Sits outside the navigation guard below: a click on a notification with no target+ // is still engagement+ notificationClicked(notification)// Clicking the card marks it read and navigates to its sourceif (!notification.read) {toggleRead(notification.id)diff --git a/frontend/src/lib/components/NotificationsMenu/NotificationsMenu.tsx b/frontend/src/lib/components/NotificationsMenu/NotificationsMenu.tsxindex 34cebb4a4e4b..c9e1b6aaa084 100644--- a/frontend/src/lib/components/NotificationsMenu/NotificationsMenu.tsx+++ b/frontend/src/lib/components/NotificationsMenu/NotificationsMenu.tsx@@ -3,17 +3,17 @@ import { useEffect, useRef, useState } from 'react'import { IconNotification } from '@posthog/icons'+import { notificationsMenuLogic } from 'lib/components/NotificationsMenu/notificationsMenuLogic'import { IconWithCount } from 'lib/lemon-ui/icons'import { ButtonPrimitive } from 'lib/ui/Button/ButtonPrimitives'import { MenuOpenIndicator } from 'lib/ui/Menus/Menus'import { cn } from 'lib/utils/css-classes'import { sidePanelNotificationsLogic } from '~/layout/navigation-3000/sidepanel/panels/activity/sidePanelNotificationsLogic'-import { panelLayoutLogic } from '~/layout/panel-layout/panelLayoutLogic'export const NotificationsMenu = ({ iconOnly = false }: { iconOnly?: boolean }): JSX.Element => {- const { activePanelIdentifier, isLayoutPanelVisible } = useValues(panelLayoutLogic)- const { setActivePanelIdentifier, showLayoutPanel, closePanel } = useActions(panelLayoutLogic)+ const { isNotificationsPanelActive } = useValues(notificationsMenuLogic)+ const { toggleNotificationsPanel } = useActions(notificationsMenuLogic)const { inAppUnreadCount } = useValues(sidePanelNotificationsLogic)const [badgePulse, setBadgePulse] = useState(false)const prevCountRef = useRef(inAppUnreadCount)@@ -27,17 +27,6 @@ export const NotificationsMenu = ({ iconOnly = false }: { iconOnly?: boolean }):}}, [inAppUnreadCount])- const isActive = isLayoutPanelVisible && activePanelIdentifier === 'Notifications'-- const handleClick = (): void => {- if (isActive) {- closePanel()- } else {- setActivePanelIdentifier('Notifications')- showLayoutPanel(true)- }- }-return (<ButtonPrimitivetooltip={iconOnly ? 'Notifications' : undefined}@@ -45,8 +34,8 @@ export const NotificationsMenu = ({ iconOnly = false }: { iconOnly?: boolean }):tooltipCloseDelayMs={0}iconOnly={iconOnly}menuItem={!iconOnly}- active={isActive}- onClick={handleClick}+ active={isNotificationsPanelActive}+ onClick={() => toggleNotificationsPanel('bell')}className="group"data-attr="notifications-menu-button">diff --git a/frontend/src/lib/components/NotificationsMenu/notificationsMenuLogic.test.ts b/frontend/src/lib/components/NotificationsMenu/notificationsMenuLogic.test.tsnew file mode 100644index 000000000000..c7459c3886a6--- /dev/null+++ b/frontend/src/lib/components/NotificationsMenu/notificationsMenuLogic.test.ts@@ -0,0 +1,80 @@+import posthog from 'posthog-js'++import { sidePanelNotificationsLogic } from '~/layout/navigation-3000/sidepanel/panels/activity/sidePanelNotificationsLogic'+import { panelLayoutLogic } from '~/layout/panel-layout/panelLayoutLogic'+import { initKeaTests } from '~/test/init'++import { notificationsMenuLogic } from './notificationsMenuLogic'++describe('notificationsMenuLogic', () => {+ let logic: ReturnType<typeof notificationsMenuLogic.build>+ let captureSpy: jest.SpyInstance++ beforeEach(() => {+ initKeaTests()+ captureSpy = jest.spyOn(posthog, 'capture').mockImplementation()+ logic = notificationsMenuLogic()+ logic.mount()+ // panelLayoutLogic persists panel visibility, so each test would otherwise start with+ // whichever panel the previous one left open+ panelLayoutLogic.actions.closePanel()+ captureSpy.mockClear()+ })++ afterEach(() => {+ captureSpy.mockRestore()+ logic.unmount()+ })++ it('toggles the side panel open and closed from the bell', () => {+ logic.actions.toggleNotificationsPanel('bell')+ expect(logic.values.isNotificationsPanelActive).toBe(true)++ // Clicking the bell while the panel is open closes it again, rather than reopening it+ logic.actions.toggleNotificationsPanel('bell')+ expect(logic.values.isNotificationsPanelActive).toBe(false)+ })++ it('reports the unread count the badge was showing when the panel opens', () => {+ sidePanelNotificationsLogic.actions.setInAppUnreadCount(3)++ logic.actions.toggleNotificationsPanel('bell')++ expect(captureSpy).toHaveBeenCalledWith('notification panel opened', {+ source: 'bell',+ unread_count: 3,+ had_unread: true,+ })+ })++ it('attributes an open from a critical toast to the toast, not the bell', () => {+ logic.actions.openToUnread()++ expect(captureSpy).toHaveBeenCalledWith(+ 'notification panel opened',+ expect.objectContaining({ source: 'critical_toast' })+ )+ expect(logic.values.activeTab).toEqual('unread')+ })++ it('does not report an open when a critical toast fires with the panel already open', () => {+ logic.actions.toggleNotificationsPanel('bell')+ captureSpy.mockClear()++ logic.actions.openToUnread()++ // The toast still switches to the unread tab, but no panel opened, so nothing to report+ expect(captureSpy).not.toHaveBeenCalled()+ expect(logic.values.activeTab).toEqual('unread')+ })++ it('does not report an open when the bell closes the panel', () => {+ logic.actions.toggleNotificationsPanel('bell')+ captureSpy.mockClear()++ logic.actions.toggleNotificationsPanel('bell')++ expect(captureSpy).not.toHaveBeenCalled()+ expect(panelLayoutLogic.values.isLayoutPanelVisible).toBe(false)+ })+})diff --git a/frontend/src/lib/components/NotificationsMenu/notificationsMenuLogic.tsx b/frontend/src/lib/components/NotificationsMenu/notificationsMenuLogic.tsxindex a17a9be64175..637549ad485f 100644--- a/frontend/src/lib/components/NotificationsMenu/notificationsMenuLogic.tsx+++ b/frontend/src/lib/components/NotificationsMenu/notificationsMenuLogic.tsx@@ -1,4 +1,5 @@-import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'+import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'+import posthog from 'posthog-js'import { sidePanelNotificationsLogic } from '~/layout/navigation-3000/sidepanel/panels/activity/sidePanelNotificationsLogic'import { panelLayoutLogic } from '~/layout/panel-layout/panelLayoutLogic'@@ -7,14 +8,24 @@ import type { PanelLayoutNavIdentifier } from '../../../layout/panel-layout/paneexport type NotificationTab = 'all' | 'unread' | 'archived'+/** Which surface the user opened the notifications panel from. */+export type NotificationsPanelSource = 'bell' | 'critical_toast'+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface notificationsMenuLogicValues {+ activePanelIdentifier: string // panelLayoutLogic+ isLayoutPanelVisible: boolean // panelLayoutLogic+ inAppUnreadCount: number // sidePanelNotificationsLogicactiveTab: NotificationTabisNotificationsMenuOpen: boolean+ isNotificationsPanelActive: boolean}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface notificationsMenuLogicActions {+ closePanel: () => {+ value: true+ } // panelLayoutLogicsetActivePanelIdentifier: (identifier: PanelLayoutNavIdentifier) => {identifier: PanelLayoutNavIdentifier} // panelLayoutLogic@@ -24,6 +35,9 @@ export interface notificationsMenuLogicActions {loadArchivedNotifications: () => {value: true} // sidePanelNotificationsLogic+ openNotificationsPanel: (source: NotificationsPanelSource) => {+ source: NotificationsPanelSource+ }openToUnread: () => {value: true}@@ -36,9 +50,24 @@ export interface notificationsMenuLogicActions {toggleNotificationsMenu: () => {value: true}+ toggleNotificationsPanel: (source: NotificationsPanelSource) => {+ source: NotificationsPanelSource+ }}-export type notificationsMenuLogicType = MakeLogicType<notificationsMenuLogicValues, notificationsMenuLogicActions>+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface notificationsMenuLogicMeta {+ __keaTypeGenInternalSelectorTypes: {+ isNotificationsPanelActive: (isLayoutPanelVisible: boolean, activePanelIdentifier: string) => boolean+ }+}++export type notificationsMenuLogicType = MakeLogicType<+ notificationsMenuLogicValues,+ notificationsMenuLogicActions,+ Record<string, any>,+ notificationsMenuLogicMeta+>export const notificationsMenuLogic = kea<notificationsMenuLogicType>([path(['lib', 'components', 'NotificationsMenu', 'notificationsMenuLogic']),@@ -47,7 +76,13 @@ export const notificationsMenuLogic = kea<notificationsMenuLogicType>([sidePanelNotificationsLogic,['loadArchivedNotifications'],panelLayoutLogic,- ['setActivePanelIdentifier', 'showLayoutPanel'],+ ['setActivePanelIdentifier', 'showLayoutPanel', 'closePanel'],+ ],+ values: [+ sidePanelNotificationsLogic,+ ['inAppUnreadCount'],+ panelLayoutLogic,+ ['activePanelIdentifier', 'isLayoutPanelVisible'],],})),actions({@@ -55,6 +90,8 @@ export const notificationsMenuLogic = kea<notificationsMenuLogicType>([toggleNotificationsMenu: true,setActiveTab: (tab: NotificationTab) => ({ tab }),openToUnread: true,+ openNotificationsPanel: (source: NotificationsPanelSource) => ({ source }),+ toggleNotificationsPanel: (source: NotificationsPanelSource) => ({ source }),}),reducers({isNotificationsMenuOpen: [@@ -73,7 +110,14 @@ export const notificationsMenuLogic = kea<notificationsMenuLogicType>([},],}),- listeners(({ actions }) => ({+ selectors({+ isNotificationsPanelActive: [+ (s) => [s.isLayoutPanelVisible, s.activePanelIdentifier],+ (isLayoutPanelVisible: boolean, activePanelIdentifier: string): boolean =>+ isLayoutPanelVisible && activePanelIdentifier === 'Notifications',+ ],+ }),+ listeners(({ actions, values }) => ({setActiveTab: ({ tab }) => {// Archived notifications are a separate data source loaded on demand. Refetch every time// the tab is opened so items just archived from the other tabs always show up.@@ -81,11 +125,33 @@ export const notificationsMenuLogic = kea<notificationsMenuLogicType>([actions.loadArchivedNotifications()}},- openToUnread: () => {+ toggleNotificationsPanel: ({ source }) => {+ if (values.isNotificationsPanelActive) {+ actions.closePanel()+ return+ }+ actions.openNotificationsPanel(source)+ },+ openNotificationsPanel: ({ source }) => {+ // Captured before the panel renders, so `unread_count` is what the badge was showing.+ // Reading it after opening would race the auto-mark-read pass over the visible rows.+ posthog.capture('notification panel opened', {+ source,+ unread_count: values.inAppUnreadCount,+ had_unread: values.inAppUnreadCount > 0,+ })// The Notifications side panel's visibility is owned by panelLayoutLogic, so opening it- // (e.g. from a critical-notification toast button) has to go through these actions.+ // has to go through these actions.actions.setActivePanelIdentifier('Notifications')actions.showLayoutPanel(true)},+ openToUnread: () => {+ // A critical toast can be clicked while the panel is already open. That only switches+ // to the unread tab (the reducer above), so it must not report a panel open.+ if (values.isNotificationsPanelActive) {+ return+ }+ actions.openNotificationsPanel('critical_toast')+ },})),])