An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
feat(analytics): record thumb clicks and their outcome (#981)
Yoshiki Miurad4c3ccd16191
diff --git a/components/__tests__/message-actions.test.tsx b/components/__tests__/message-actions.test.tsxindex a95cefd6..feba43f5 100644--- a/components/__tests__/message-actions.test.tsx+++ b/components/__tests__/message-actions.test.tsx@@ -1,7 +1,7 @@import React from 'react'-import { render } from '@testing-library/react'-import { beforeEach, describe, expect, test, vi } from 'vitest'+import { fireEvent, render, screen, waitFor } from '@testing-library/react'+import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'import { captureClient } from '@/lib/analytics/posthog-client'@@ -19,8 +19,15 @@ vi.mock('../library/library-context', () => ({}))describe('MessageActions', () => {+ const originalFetch = global.fetch+beforeEach(() => {vi.clearAllMocks()+ global.fetch = vi.fn()+ })++ afterEach(() => {+ global.fetch = originalFetch})test('records one feedback control impression when the row becomes visible', () => {@@ -140,4 +147,116 @@ describe('MessageActions', () => {isGuest: false})})++ test('records a thumbs up click and successful feedback response', async () => {+ vi.mocked(global.fetch).mockResolvedValue({ ok: true } as Response)+ render(+ <MessageActions+ message="Answer"+ messageId="feedback-success-1"+ traceId="trace-1"+ chatId="chat-1"+ isGuest+ status="ready"+ visible+ />+ )+ vi.mocked(captureClient).mockClear()++ fireEvent.click(screen.getByRole('button', { name: 'Good response' }))++ await waitFor(() => {+ expect(captureClient).toHaveBeenCalledTimes(2)+ })+ expect(captureClient).toHaveBeenNthCalledWith(+ 1,+ 'feedback_control_clicked',+ {+ score: 1,+ chatId: 'chat-1',+ isGuest: true+ }+ )+ expect(captureClient).toHaveBeenNthCalledWith(2, 'feedback_recorded', {+ score: 1,+ chatId: 'chat-1',+ isGuest: true+ })+ })++ test('records a failed feedback response', async () => {+ vi.mocked(global.fetch).mockResolvedValue({+ ok: false,+ status: 503+ } as Response)+ render(+ <MessageActions+ message="Answer"+ messageId="feedback-response-failure-1"+ traceId="trace-1"+ chatId="chat-1"+ status="ready"+ visible+ />+ )+ vi.mocked(captureClient).mockClear()++ fireEvent.click(screen.getByRole('button', { name: 'Good response' }))++ await waitFor(() => {+ expect(captureClient).toHaveBeenCalledTimes(2)+ })+ expect(captureClient).toHaveBeenNthCalledWith(+ 1,+ 'feedback_control_clicked',+ {+ score: 1,+ chatId: 'chat-1',+ isGuest: false+ }+ )+ expect(captureClient).toHaveBeenNthCalledWith(2, 'feedback_failed', {+ score: 1,+ chatId: 'chat-1',+ isGuest: false,+ status: 503,+ reason: 'response'+ })+ })++ test('records an exception when feedback submission throws', async () => {+ vi.mocked(global.fetch).mockRejectedValue(new Error('Network error'))+ render(+ <MessageActions+ message="Answer"+ messageId="feedback-exception-1"+ traceId="trace-1"+ chatId="chat-1"+ status="ready"+ visible+ />+ )+ vi.mocked(captureClient).mockClear()++ fireEvent.click(screen.getByRole('button', { name: 'Good response' }))++ await waitFor(() => {+ expect(captureClient).toHaveBeenCalledTimes(2)+ })+ expect(captureClient).toHaveBeenNthCalledWith(+ 1,+ 'feedback_control_clicked',+ {+ score: 1,+ chatId: 'chat-1',+ isGuest: false+ }+ )+ expect(captureClient).toHaveBeenNthCalledWith(2, 'feedback_failed', {+ score: 1,+ chatId: 'chat-1',+ isGuest: false,+ reason: 'exception'+ })+ })})diff --git a/components/message-actions.tsx b/components/message-actions.tsxindex cc0413f3..431d40e7 100644--- a/components/message-actions.tsx+++ b/components/message-actions.tsx@@ -185,6 +185,7 @@ export function MessageActions({async function handleFeedback(score: number) {if (isSubmittingFeedback || !traceId) return+ captureClient('feedback_control_clicked', { score, chatId, isGuest })setIsSubmittingFeedback(true)try {const response = await fetch('/api/feedback', {@@ -198,6 +199,9 @@ export function MessageActions({})if (response.ok) {+ // The route answers 200 without writing a score when tracing is off, so+ // this records that the request succeeded, not that a score exists.+ captureClient('feedback_recorded', { score, chatId, isGuest })setFeedbackScore(score)toast.success(score === 1@@ -205,10 +209,23 @@ export function MessageActions({: 'Thanks for letting us know!')} else {+ captureClient('feedback_failed', {+ score,+ chatId,+ isGuest,+ status: response.status,+ reason: 'response'+ })console.error('Failed to submit feedback')toast.error('Failed to submit feedback')}} catch (error) {+ captureClient('feedback_failed', {+ score,+ chatId,+ isGuest,+ reason: 'exception'+ })console.error('Error submitting feedback:', error)toast.error('Failed to submit feedback')} finally {@@ -246,6 +263,7 @@ export function MessageActions({onClick={() => handleFeedback(1)}disabled={isSubmittingFeedback || feedbackScore === 1}className="rounded-full"+ aria-label="Good response"><ThumbsUpsize={14}@@ -260,6 +278,7 @@ export function MessageActions({onClick={() => handleFeedback(-1)}disabled={isSubmittingFeedback || feedbackScore === -1}className="rounded-full"+ aria-label="Bad response"><ThumbsDownsize={14}