: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(mcp): add usefulness feedback to the explain-chart app (#126181)
Sam Penningtonbcb098470850
diff --git a/services/mcp/src/ui-apps/analytics/posthog.ts b/services/mcp/src/ui-apps/analytics/posthog.tsindex be2a4832e5d4..e7a36f5f5434 100644--- a/services/mcp/src/ui-apps/analytics/posthog.ts+++ b/services/mcp/src/ui-apps/analytics/posthog.ts@@ -49,14 +49,14 @@ export function initPostHog(appName: string, appVersion: string): void {client.register({$mcp_app_name: appName,$mcp_app_version: appVersion,- $mcp_app_instance_id: newInstanceId(),+ $mcp_app_instance_id: newId(),})} catch (error) {log('PostHog register failed', error)}}-function newInstanceId(): string {+export function newId(): string {if (typeof crypto !== 'undefined' && typeof crypto.randomUUID === 'function') {return crypto.randomUUID()}@@ -90,6 +90,10 @@ export function identifyUser(distinctId: string, toolName?: string, mcpClientNam}}+export function isAnalyticsEnabled(): boolean {+ return client !== null+}+/*** Capture a custom event.*/diff --git a/services/mcp/src/ui-apps/components/ExplainChart.tsx b/services/mcp/src/ui-apps/components/ExplainChart.tsxindex 1dce062e1fcb..abf5fecc675c 100644--- a/services/mcp/src/ui-apps/components/ExplainChart.tsx+++ b/services/mcp/src/ui-apps/components/ExplainChart.tsx@@ -10,6 +10,7 @@ import { ExplainChartView } from 'products/product_analytics/mcp/apps/explainChaimport { capture } from '../analytics/posthog'import { useEvidenceCharts } from '../hooks/useEvidenceCharts'+import { ExplainChartFeedback } from './ExplainChartFeedback'import { TrendsVisualizer } from './TrendsVisualizer'import type { TrendsQuery, TrendsResult } from './types'@@ -79,6 +80,7 @@ export function ExplainChart({ data, openLink, app = null }: ExplainChartProps):})}/>+ <ExplainChartFeedback /></CardContent></Card>)diff --git a/services/mcp/src/ui-apps/components/ExplainChartFeedback.tsx b/services/mcp/src/ui-apps/components/ExplainChartFeedback.tsxnew file mode 100644index 000000000000..38bcad3f3330--- /dev/null+++ b/services/mcp/src/ui-apps/components/ExplainChartFeedback.tsx@@ -0,0 +1,128 @@+import { ThumbsDown, ThumbsUp, X } from 'lucide-react'+import { type ReactElement, useEffect, useId, useState } from 'react'++import { Button, Field, FieldLabel, Text, Textarea } from '@posthog/quill'++import { capture, isAnalyticsEnabled, newId } from '../analytics/posthog'++// An API survey in the PostHog project that receives the app's analytics. The app sends the responses itself.+const SURVEY_ID = '01a18c51-4f07-0000-03ff-d05744424486'+const RATING_QUESTION_ID = 'ea2206c2-1e43-41f8-ba87-86d6281ed76d'+const NOTE_QUESTION_ID = '6631e694-32f6-4add-897c-a23029a4d284'+const SURVEY_PROPERTIES = { $survey_id: SURVEY_ID, $survey_name: 'Explain chart usefulness' }++// A two-point thumbs rating stores 1 for thumbs up and 2 for thumbs down.+const RATINGS = [+ { value: '1', label: 'Useful', icon: <ThumbsUp className="size-3.5" /> },+ { value: '2', label: 'Not useful', icon: <ThumbsDown className="size-3.5" /> },+]++export function ExplainChartFeedback(): ReactElement | null {+ const questionId = useId()+ const noteId = useId()+ const [enabled] = useState(isAnalyticsEnabled)+ const [submissionId] = useState(newId)+ const [rating, setRating] = useState<string | null>(null)+ const [note, setNote] = useState('')+ const [state, setState] = useState<'asking' | 'sent' | 'dismissed'>('asking')++ useEffect(() => {+ if (enabled) {+ capture('survey shown', SURVEY_PROPERTIES)+ }+ }, [enabled])++ if (!enabled || state === 'dismissed') {+ return null+ }++ const send = (value: string, completed: boolean): void => {+ const trimmed = note.trim()+ capture('survey sent', {+ ...SURVEY_PROPERTIES,+ $survey_submission_id: submissionId,+ $survey_completed: completed,+ [`$survey_response_${RATING_QUESTION_ID}`]: value,+ ...(completed && trimmed ? { [`$survey_response_${NOTE_QUESTION_ID}`]: trimmed } : {}),+ })+ }++ const rate = (value: string): void => {+ setRating(value)+ send(value, false)+ }++ const dismiss = (): void => {+ capture('survey dismissed', SURVEY_PROPERTIES)+ setState('dismissed')+ }++ return (+ <div className="mt-3 flex flex-col gap-2 border-t border-border pt-3" data-attr="explain-chart-feedback">+ {state === 'sent' ? (+ <Text size="sm" variant="muted" role="status">+ Thanks for your feedback.+ </Text>+ ) : rating ? (+ <>+ <Field>+ <FieldLabel htmlFor={noteId}>Any other feedback?</FieldLabel>+ <Textarea+ id={noteId}+ value={note}+ onChange={(event) => setNote(event.target.value)}+ rows={2}+ maxLength={2000}+ autoFocus+ data-attr="explain-chart-feedback-detail"+ />+ </Field>+ <Button+ variant="outline"+ size="sm"+ className="self-start"+ onClick={() => {+ send(rating, true)+ setState('sent')+ }}+ data-attr="explain-chart-feedback-submit"+ >+ {note.trim() ? 'Send feedback' : 'Done'}+ </Button>+ </>+ ) : (+ <div className="flex items-center justify-between gap-2">+ <Text id={questionId} size="sm">+ Was this explanation useful?+ </Text>+ <div className="flex items-center gap-1">+ <div className="flex items-center gap-1" role="group" aria-labelledby={questionId}>+ {RATINGS.map(({ value, label, icon }) => (+ <Button+ key={value}+ variant="outline"+ size="icon-sm"+ aria-label={label}+ title={label}+ onClick={() => rate(value)}+ data-attr="explain-chart-feedback-answer"+ >+ {icon}+ </Button>+ ))}+ </div>+ <Button+ size="icon-sm"+ aria-label="Dismiss"+ title="Dismiss"+ onClick={dismiss}+ data-attr="explain-chart-feedback-dismiss"+ >+ <X className="size-3.5" />+ </Button>+ </div>+ </div>+ )}+ </div>+ )+}