Skip to content

An AI-powered search engine with a generative UI

feat(analytics): record whether the feedback control was offered (#968)

Yoshiki Miuradbba0f4acf05
diff --git a/components/__tests__/message-actions.test.tsx b/components/__tests__/message-actions.test.tsx
new file mode 100644
index 00000000..a95cefd6
--- /dev/null
+++ b/components/__tests__/message-actions.test.tsx
@@ -0,0 +1,143 @@
+import React from 'react'
+
+import { render } from '@testing-library/react'
+import { beforeEach, describe, expect, test, vi } from 'vitest'
+
+import { captureClient } from '@/lib/analytics/posthog-client'
+
+import { MessageActions } from '../message-actions'
+
+vi.mock('@/lib/analytics/posthog-client', () => ({
+ captureClient: vi.fn()
+}))
+
+vi.mock('../library/library-context', () => ({
+ useLibrary: () => ({
+ openLibrary: vi.fn(),
+ upsertCachedNote: vi.fn()
+ })
+}))
+
+describe('MessageActions', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ })
+
+ test('records one feedback control impression when the row becomes visible', () => {
+ const { rerender } = render(
+ <React.StrictMode>
+ <MessageActions
+ message="Answer"
+ messageId="visible-1"
+ traceId="trace-1"
+ chatId="chat-1"
+ isGuest
+ status="streaming"
+ visible={false}
+ />
+ </React.StrictMode>
+ )
+
+ expect(captureClient).not.toHaveBeenCalled()
+
+ rerender(
+ <React.StrictMode>
+ <MessageActions
+ message="Answer"
+ messageId="visible-1"
+ traceId="trace-1"
+ chatId="chat-1"
+ isGuest
+ status="ready"
+ visible
+ />
+ </React.StrictMode>
+ )
+
+ expect(captureClient).toHaveBeenCalledTimes(1)
+ expect(captureClient).toHaveBeenCalledWith('feedback_control_shown', {
+ hasTraceId: true,
+ hasFeedback: false,
+ chatId: 'chat-1',
+ isGuest: true
+ })
+
+ rerender(
+ <React.StrictMode>
+ <MessageActions
+ className="parent-state-changed"
+ message="Answer"
+ messageId="visible-1"
+ traceId="trace-1"
+ chatId="chat-1"
+ isGuest
+ status="ready"
+ visible
+ />
+ </React.StrictMode>
+ )
+
+ expect(captureClient).toHaveBeenCalledTimes(1)
+ })
+
+ test('does not record a second impression when the same message remounts', () => {
+ const props = {
+ message: 'Answer',
+ messageId: 'remount-1',
+ traceId: 'trace-1',
+ chatId: 'chat-1',
+ status: 'ready' as const,
+ visible: true
+ }
+
+ const first = render(<MessageActions {...props} />)
+ expect(captureClient).toHaveBeenCalledTimes(1)
+
+ // Reopening a conversation remounts every past answer.
+ first.unmount()
+ render(<MessageActions {...props} />)
+
+ expect(captureClient).toHaveBeenCalledTimes(1)
+ })
+
+ test('reports a message that already carries a score as such', () => {
+ render(
+ <MessageActions
+ message="Answer"
+ messageId="scored-1"
+ traceId="trace-1"
+ feedbackScore={-1}
+ chatId="chat-1"
+ status="ready"
+ visible
+ />
+ )
+
+ expect(captureClient).toHaveBeenCalledWith('feedback_control_shown', {
+ hasTraceId: true,
+ hasFeedback: true,
+ chatId: 'chat-1',
+ isGuest: false
+ })
+ })
+
+ test('records a missing trace id for a newly presented message', () => {
+ render(
+ <MessageActions
+ message="First answer"
+ messageId="no-trace-1"
+ chatId="chat-1"
+ status="ready"
+ visible
+ />
+ )
+
+ expect(captureClient).toHaveBeenCalledTimes(1)
+ expect(captureClient).toHaveBeenLastCalledWith('feedback_control_shown', {
+ hasTraceId: false,
+ hasFeedback: false,
+ chatId: 'chat-1',
+ isGuest: false
+ })
+ })
+})
diff --git a/components/message-actions.tsx b/components/message-actions.tsx
index fbde4ad1..cc0413f3 100644
--- a/components/message-actions.tsx
+++ b/components/message-actions.tsx
@@ -1,6 +1,6 @@
'use client'
-import { useMemo, useState } from 'react'
+import { useEffect, useMemo, useState } from 'react'
import Link from 'next/link'
import { UseChatHelpers } from '@ai-sdk/react'
@@ -33,6 +33,9 @@ import {
import { ChatShare } from './chat-share'
import { RetryButton } from './retry-button'
+// Module scope so the impression survives remounts of the same message.
+const reportedFeedbackControls = new Set<string>()
+
interface MessageActionsProps {
message: string
messageId: string
@@ -81,6 +84,25 @@ export function MessageActions({
const isLoading = status === 'submitted' || status === 'streaming'
const showSaveButton = libraryAvailable && (!isGuest || isCloudDeployment)
+ useEffect(() => {
+ if (!visible || isLoading || reportedFeedbackControls.has(messageId)) {
+ return
+ }
+
+ // A control that was never offered is otherwise indistinguishable from one
+ // that was offered and ignored, so the impression is what makes the click
+ // counts readable. It has to be one per message rather than one per mount:
+ // reopening a conversation remounts every past answer, and an answer that
+ // already carries a score cannot produce another one.
+ reportedFeedbackControls.add(messageId)
+ captureClient('feedback_control_shown', {
+ hasTraceId: Boolean(traceId),
+ hasFeedback: feedbackScore !== null,
+ chatId,
+ isGuest
+ })
+ }, [chatId, feedbackScore, isGuest, isLoading, messageId, traceId, visible])
+
// Keep the element mounted during loading to preserve layout; otherwise skip rendering.
if (!visible && !isLoading) {
return null