Skip to content

: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.

feat(insights): ask for feedback in the data checks popover (#120884)

Sam Pennington1c3405c823c4
diff --git a/products/product_analytics/frontend/insights/dataChecks/DataChecksFeedback.tsx b/products/product_analytics/frontend/insights/dataChecks/DataChecksFeedback.tsx
new file mode 100644
index 000000000000..fb9a42216ccd
--- /dev/null
+++ b/products/product_analytics/frontend/insights/dataChecks/DataChecksFeedback.tsx
@@ -0,0 +1,99 @@
+import { useActions, useValues } from 'kea'
+import { useEffect, useId } from 'react'
+
+import { IconThumbsDown, IconThumbsUp, IconX } from '@posthog/icons'
+import { Button, Field, FieldLabel, Text, Textarea } from '@posthog/quill'
+
+import { DATA_CHECKS_FEEDBACK_RATINGS, dataChecksFeedbackLogic } from './dataChecksFeedbackLogic'
+
+const RATINGS = [
+ { value: DATA_CHECKS_FEEDBACK_RATINGS.USEFUL, label: 'Useful', icon: <IconThumbsUp /> },
+ { value: DATA_CHECKS_FEEDBACK_RATINGS.NOT_USEFUL, label: 'Not useful', icon: <IconThumbsDown /> },
+]
+
+export function DataChecksFeedback({ userId }: { userId: string }): JSX.Element | null {
+ const questionId = useId()
+ const noteId = useId()
+ const logic = dataChecksFeedbackLogic({ userId })
+ const { survey, visible, shownReported, rating, note, noteSent } = useValues(logic)
+ const { reportShown, rate, setNote, submitNote, dismiss } = useActions(logic)
+
+ useEffect(() => {
+ if (visible && !shownReported) {
+ reportShown()
+ }
+ }, [visible, shownReported, reportShown])
+
+ if (!visible || !survey) {
+ return null
+ }
+
+ const [ratingQuestion, noteQuestion] = survey.questions
+
+ return (
+ <div className="flex flex-col gap-2 border-t border-(--border) pt-2" data-attr="insight-data-checks-feedback">
+ {noteSent ? (
+ <Text size={null} variant="muted" role="status">
+ Thanks for your feedback.
+ </Text>
+ ) : rating ? (
+ <>
+ <Field>
+ <FieldLabel htmlFor={noteId}>{noteQuestion?.question}</FieldLabel>
+ <Textarea
+ id={noteId}
+ value={note}
+ onChange={(event) => setNote(event.target.value)}
+ rows={2}
+ maxLength={2000}
+ autoFocus
+ data-attr="insight-data-checks-feedback-detail"
+ />
+ </Field>
+ <Button
+ variant="outline"
+ size="sm"
+ className="self-start"
+ onClick={submitNote}
+ data-attr="insight-data-checks-feedback-submit"
+ >
+ {note.trim() ? 'Send feedback' : 'Done'}
+ </Button>
+ </>
+ ) : (
+ <div className="flex items-center justify-between gap-2">
+ <Text id={questionId} size={null}>
+ {ratingQuestion?.question}
+ </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}
+ tooltip={label}
+ onClick={() => rate(value)}
+ data-attr="insight-data-checks-feedback-answer"
+ data-feedback-rating={value}
+ >
+ {icon}
+ </Button>
+ ))}
+ </div>
+ <Button
+ size="icon-sm"
+ aria-label="Dismiss"
+ tooltip="Dismiss"
+ onClick={dismiss}
+ data-attr="insight-data-checks-feedback-dismiss"
+ >
+ <IconX />
+ </Button>
+ </div>
+ </div>
+ )}
+ </div>
+ )
+}
diff --git a/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsx b/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsx
index 627aadb2f113..c65a2be83ac7 100644
--- a/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsx
+++ b/products/product_analytics/frontend/insights/dataChecks/DataChecksPopover.tsx
@@ -1,15 +1,18 @@
-import { useActions, useValues } from 'kea'
+import { useActions, useMountedLogic, useValues } from 'kea'
import { useId } from 'react'
import { Accordion, Button, Dot, Popover, PopoverContent, PopoverTrigger, Text } from '@posthog/quill'
import { humanFriendlyNumber } from 'lib/utils/numbers'
import { pluralize } from 'lib/utils/strings'
+import { userLogic } from 'scenes/userLogic'
import type { InsightLogicProps } from '~/types'
import type { InsightDataChecksCoverageApi } from 'products/product_analytics/frontend/generated/api.schemas'
+import { DataChecksFeedback } from './DataChecksFeedback'
+import { dataChecksFeedbackLogic } from './dataChecksFeedbackLogic'
import { DATA_CHECK_NAMES } from './dataCheckText'
import { InsightDataCheckRow } from './InsightDataCheckRow'
import { insightDataChecksLogic } from './insightDataChecksLogic'
@@ -45,6 +48,10 @@ export function DataChecksPopover({ insightProps, trigger, children }: DataCheck
const logic = insightDataChecksLogic(insightProps)
const { visibleChecks, checkedQuery, result, status } = useValues(logic)
const { loadDataChecks, reportDataChecksViewed } = useActions(logic)
+ const { user } = useValues(userLogic)
+ const userId = user?.uuid ?? ''
+ // Mounted outside the popover content, so a half-written note survives the popover closing.
+ useMountedLogic(dataChecksFeedbackLogic({ userId }))
const titleId = useId()
return (
@@ -103,6 +110,9 @@ export function DataChecksPopover({ insightProps, trigger, children }: DataCheck
Try again
</Button>
)}
+ {(status === 'passed' || status === 'warning' || status === 'problem') && (
+ <DataChecksFeedback userId={userId} />
+ )}
</PopoverContent>
</Popover>
)
diff --git a/products/product_analytics/frontend/insights/dataChecks/dataChecksFeedbackLogic.test.ts b/products/product_analytics/frontend/insights/dataChecks/dataChecksFeedbackLogic.test.ts
new file mode 100644
index 000000000000..76125e59a550
--- /dev/null
+++ b/products/product_analytics/frontend/insights/dataChecks/dataChecksFeedbackLogic.test.ts
@@ -0,0 +1,128 @@
+import posthog, { CaptureResult, Survey, SurveyQuestionType, SurveyType } from 'posthog-js'
+
+import { initKeaTests } from '~/test/init'
+
+import {
+ DATA_CHECKS_FEEDBACK_RATINGS,
+ DATA_CHECKS_FEEDBACK_SURVEY_ID,
+ dataChecksFeedbackLogic,
+} from './dataChecksFeedbackLogic'
+
+describe('dataChecksFeedbackLogic', () => {
+ const survey: Survey = {
+ id: DATA_CHECKS_FEEDBACK_SURVEY_ID,
+ name: 'Insight data checks usefulness',
+ type: SurveyType.API,
+ start_date: '2026-01-01T00:00:00Z',
+ feature_flag_keys: null,
+ linked_flag_key: null,
+ targeting_flag_key: null,
+ internal_targeting_flag_key: null,
+ appearance: null,
+ conditions: null,
+ end_date: null,
+ current_iteration: null,
+ current_iteration_start_date: null,
+ questions: [
+ {
+ id: 'example-rating',
+ type: SurveyQuestionType.Rating,
+ question: 'Were these checks useful?',
+ display: 'emoji',
+ scale: 2,
+ lowerBoundLabel: '',
+ upperBoundLabel: '',
+ },
+ {
+ id: 'example-note',
+ type: SurveyQuestionType.Open,
+ question: 'What would make it better?',
+ optional: true,
+ },
+ ],
+ }
+
+ const mountWithSurveys = (surveys: Survey[]): ReturnType<typeof dataChecksFeedbackLogic.build> => {
+ posthog.getActiveMatchingSurveys = jest.fn((callback) => callback(surveys))
+ const logic = dataChecksFeedbackLogic({ userId: 'example-user' })
+ logic.mount()
+ return logic
+ }
+
+ beforeEach(() => {
+ localStorage.clear()
+ initKeaTests(false)
+ jest.mocked(posthog.onSurveysLoaded).mockImplementation(() => () => {})
+ jest.mocked(posthog.onFeatureFlags).mockImplementation((callback) => {
+ callback([], {})
+ return () => {}
+ })
+ jest.mocked(posthog.capture)
+ .mockReset()
+ .mockReturnValue({} as CaptureResult)
+ })
+
+ it.each([
+ { name: 'no live survey', surveys: [], visible: false },
+ { name: 'another survey', surveys: [{ ...survey, id: 'another-survey' }], visible: false },
+ { name: 'its survey', surveys: [survey], visible: true },
+ ])('shows the prompt only while its survey is live for the person: $name', ({ surveys, visible }) => {
+ expect(mountWithSurveys(surveys).values.visible).toBe(visible)
+ })
+
+ it('counts a rating and its note as one response', () => {
+ const logic = mountWithSurveys([survey])
+
+ logic.actions.rate(DATA_CHECKS_FEEDBACK_RATINGS.NOT_USEFUL)
+ logic.actions.setNote(' Slow on big charts ')
+ logic.actions.submitNote()
+
+ const sent = jest.mocked(posthog.capture).mock.calls.filter(([event]) => event === 'survey sent')
+ expect(sent.map(([, properties]) => properties)).toEqual([
+ expect.objectContaining({
+ $survey_id: DATA_CHECKS_FEEDBACK_SURVEY_ID,
+ $survey_submission_id: logic.values.submissionId,
+ $survey_completed: false,
+ '$survey_response_example-rating': '2',
+ }),
+ expect.objectContaining({
+ $survey_submission_id: logic.values.submissionId,
+ $survey_completed: true,
+ '$survey_response_example-rating': '2',
+ '$survey_response_example-note': 'Slow on big charts',
+ }),
+ ])
+ })
+
+ it('keeps the note box open when a flag reload drops the rated survey', () => {
+ const logic = mountWithSurveys([survey])
+ logic.actions.rate(DATA_CHECKS_FEEDBACK_RATINGS.USEFUL)
+
+ posthog.getActiveMatchingSurveys = jest.fn((callback) => callback([]))
+ jest.mocked(posthog.onFeatureFlags).mock.lastCall![0]([], {})
+
+ expect(logic.values.visible).toBe(true)
+ })
+
+ it.each([
+ { choice: 'rate', accepted: true, asksAgain: false },
+ { choice: 'rate', accepted: false, asksAgain: true },
+ { choice: 'dismiss', accepted: true, asksAgain: false },
+ ] as const)(
+ 'after $choice with capture accepted $accepted, asks again: $asksAgain',
+ ({ choice, accepted, asksAgain }) => {
+ if (!accepted) {
+ jest.mocked(posthog.capture).mockReturnValue(undefined)
+ }
+ const logic = mountWithSurveys([survey])
+ if (choice === 'rate') {
+ logic.actions.rate(DATA_CHECKS_FEEDBACK_RATINGS.USEFUL)
+ } else {
+ logic.actions.dismiss()
+ }
+ logic.unmount()
+
+ expect(mountWithSurveys([survey]).values.visible).toBe(asksAgain)
+ }
+ )
+})
diff --git a/products/product_analytics/frontend/insights/dataChecks/dataChecksFeedbackLogic.ts b/products/product_analytics/frontend/insights/dataChecks/dataChecksFeedbackLogic.ts
new file mode 100644
index 000000000000..d27d882b3cf7
--- /dev/null
+++ b/products/product_analytics/frontend/insights/dataChecks/dataChecksFeedbackLogic.ts
@@ -0,0 +1,189 @@
+import {
+ MakeLogicType,
+ LogicWrapper,
+ actions,
+ afterMount,
+ kea,
+ key,
+ listeners,
+ path,
+ props,
+ reducers,
+ selectors,
+} from 'kea'
+import posthog, { Survey } from 'posthog-js'
+
+import { lemonToast } from '@posthog/lemon-ui'
+
+import { uuid } from 'lib/utils/dom'
+import { getSurveyIdBasedResponseKey } from 'scenes/surveys/utils'
+
+import { SurveyEventName, SurveyEventProperties } from '~/types'
+
+export const DATA_CHECKS_FEEDBACK_SURVEY_ID = '01a15d5f-3117-0000-9b74-1712010c28ca'
+
+// A two-point thumbs rating stores 1 for thumbs up and 2 for thumbs down.
+export const DATA_CHECKS_FEEDBACK_RATINGS = { USEFUL: '1', NOT_USEFUL: '2' } as const
+export type DataChecksFeedbackRating = (typeof DATA_CHECKS_FEEDBACK_RATINGS)[keyof typeof DATA_CHECKS_FEEDBACK_RATINGS]
+
+export interface DataChecksFeedbackLogicProps {
+ userId: string
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface dataChecksFeedbackLogicValues {
+ finished: boolean
+ note: string
+ noteSent: boolean
+ rating: DataChecksFeedbackRating | null
+ shownReported: boolean
+ submissionId: string
+ survey: Survey | null
+ surveyProperties: Record<string, unknown>
+ visible: boolean
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface dataChecksFeedbackLogicActions {
+ dismiss: () => {
+ value: true
+ }
+ rate: (rating: DataChecksFeedbackRating) => {
+ rating: DataChecksFeedbackRating
+ submissionId: string
+ }
+ rateFailure: () => {
+ value: true
+ }
+ rateSuccess: () => {
+ value: true
+ }
+ reportShown: () => {
+ value: true
+ }
+ setNote: (note: string) => {
+ note: string
+ }
+ setSurvey: (survey: Survey | null) => {
+ survey: Survey | null
+ }
+ submitNote: () => {
+ value: true
+ }
+ submitNoteSuccess: () => {
+ value: true
+ }
+}
+
+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
+export interface dataChecksFeedbackLogicMeta {
+ key: string
+ __keaTypeGenInternalSelectorTypes: {
+ visible: (survey: Survey | null, finished: boolean, rating: DataChecksFeedbackRating | null) => boolean
+ surveyProperties: (survey: Survey | null) => Record<string, unknown>
+ }
+}
+
+export type dataChecksFeedbackLogicType = MakeLogicType<
+ dataChecksFeedbackLogicValues,
+ dataChecksFeedbackLogicActions,
+ DataChecksFeedbackLogicProps,
+ dataChecksFeedbackLogicMeta
+>
+
+export const dataChecksFeedbackLogic: LogicWrapper<dataChecksFeedbackLogicType> = kea<dataChecksFeedbackLogicType>([
+ props({} as DataChecksFeedbackLogicProps),
+ key(({ userId }) => userId),
+ path((key) => ['products', 'product_analytics', 'insights', 'dataChecks', 'dataChecksFeedbackLogic', key]),
+ actions({
+ setSurvey: (survey: Survey | null) => ({ survey }),
+ reportShown: true,
+ rate: (rating: DataChecksFeedbackRating) => ({ rating, submissionId: uuid() }),
+ rateSuccess: true,
+ rateFailure: true,
+ setNote: (note: string) => ({ note }),
+ submitNote: true,
+ submitNoteSuccess: true,
+ dismiss: true,
+ }),
+ reducers(({ props }) => ({
+ survey: [null as Survey | null, { setSurvey: (_, { survey }) => survey }],
+ shownReported: [false, { reportShown: () => true }],
+ rating: [null as DataChecksFeedbackRating | null, { rate: (_, { rating }) => rating, rateFailure: () => null }],
+ submissionId: ['', { rate: (_, { submissionId }) => submissionId }],
+ note: ['', { setNote: (_, { note }) => note }],
+ noteSent: [false, { submitNoteSuccess: () => true }],
+ // pinned: localStorage key, renaming it asks everyone again
+ finished: [
+ false,
+ { persist: true, storageKey: `insight-data-checks-feedback-finished:${props.userId}` },
+ { rateSuccess: () => true, dismiss: () => true },
+ ],
+ })),
+ selectors({
+ visible: [
+ (s) => [s.survey, s.finished, s.rating],
+ (survey: Survey | null, finished: boolean, rating: DataChecksFeedbackRating | null): boolean =>
+ !!survey && (!finished || rating !== null),
+ ],
+ surveyProperties: [
+ (s) => [s.survey],
+ (survey: Survey | null): Record<string, unknown> => ({
+ [SurveyEventProperties.SURVEY_ID]: survey?.id,
+ $survey_name: survey?.name,
+ }),
+ ],
+ }),
+ listeners(({ actions, values }) => ({
+ reportShown: () => {
+ posthog.capture(SurveyEventName.SHOWN, values.surveyProperties)
+ },
+ rate: ({ rating, submissionId }) => {
+ const [ratingQuestion] = values.survey?.questions ?? []
+ const sent = posthog.capture(SurveyEventName.SENT, {
+ ...values.surveyProperties,
+ [SurveyEventProperties.SURVEY_SUBMISSION_ID]: submissionId,
+ [SurveyEventProperties.SURVEY_COMPLETED]: false,
+ ...(ratingQuestion?.id ? { [getSurveyIdBasedResponseKey(ratingQuestion.id)]: rating } : {}),
+ })
+ if (sent) {
+ actions.rateSuccess()
+ } else {
+ actions.rateFailure()
+ lemonToast.error('Could not send your feedback. Try again.')
+ }
+ },
+ submitNote: () => {
+ const [ratingQuestion, noteQuestion] = values.survey?.questions ?? []
+ const note = values.note.trim()
+ const sent = posthog.capture(SurveyEventName.SENT, {
+ ...values.surveyProperties,
+ [SurveyEventProperties.SURVEY_SUBMISSION_ID]: values.submissionId,
+ [SurveyEventProperties.SURVEY_COMPLETED]: true,
+ ...(ratingQuestion?.id ? { [getSurveyIdBasedResponseKey(ratingQuestion.id)]: values.rating } : {}),
+ ...(noteQuestion?.id && note ? { [getSurveyIdBasedResponseKey(noteQuestion.id)]: note } : {}),
+ })
+ if (sent) {
+ actions.submitNoteSuccess()
+ } else {
+ lemonToast.error('Could not send your feedback. Try again.')
+ }
+ },
+ dismiss: () => {
+ posthog.capture(SurveyEventName.DISMISSED, values.surveyProperties)
+ },
+ })),
+ afterMount(({ actions, values, cache }) => {
+ // The survey links to a feature flag, so it can only match once both surveys and flags have loaded.
+ // A sent rating marks the survey as answered, so keep it after the rating while the person writes a note.
+ const findSurvey = (): void => {
+ posthog.getActiveMatchingSurveys((surveys) => {
+ if (values.rating === null) {
+ actions.setSurvey(surveys.find(({ id }) => id === DATA_CHECKS_FEEDBACK_SURVEY_ID) ?? null)
+ }
+ })
+ }
+ cache.disposables.add(() => posthog.onSurveysLoaded(findSurvey), 'surveysLoaded')
+ cache.disposables.add(() => posthog.onFeatureFlags(findSurvey), 'flagsLoaded')
+ }),
+])