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(feature-flags): add NPS feedback buttons to the flags and experiments pages (#82311)

ruby childsdfb8d2ea768c
diff --git a/frontend/jest.setup.ts b/frontend/jest.setup.ts
index 67fac599af13..12411d0d7295 100644
--- a/frontend/jest.setup.ts
+++ b/frontend/jest.setup.ts
@@ -180,6 +180,10 @@ jest.mock('posthog-js', () => {
getSurveys: jest.fn(),
displaySurvey: jest.fn(),
onFeatureFlags: jest.fn(() => () => {}),
+ onSurveysLoaded: jest.fn((cb: (surveys: unknown[], context: { isLoaded: boolean }) => void) => {
+ cb([], { isLoaded: true })
+ return () => {}
+ }),
debug: jest.fn(),
get_session_id: jest.fn(),
get_session_replay_url: jest.fn(),
diff --git a/frontend/src/lib/components/FeedbackSurveyButton/FeedbackSurveyButton.test.tsx b/frontend/src/lib/components/FeedbackSurveyButton/FeedbackSurveyButton.test.tsx
new file mode 100644
index 000000000000..15e30bb02150
--- /dev/null
+++ b/frontend/src/lib/components/FeedbackSurveyButton/FeedbackSurveyButton.test.tsx
@@ -0,0 +1,61 @@
+import { cleanup, fireEvent, render } from '@testing-library/react'
+import posthog, { DisplaySurveyType } from 'posthog-js'
+
+import { FeedbackSurveyButton } from './FeedbackSurveyButton'
+
+const SURVEY_ID = '018bcec8-6cf5-0000-c724-a51a86a4e8b1'
+
+type SurveysLoadedCallback = (surveys: unknown[], context: { isLoaded: boolean }) => void
+
+function mockSurveysLoaded(context: { isLoaded: boolean } | null): void {
+ jest.mocked(posthog.onSurveysLoaded).mockImplementation(((cb: SurveysLoadedCallback) => {
+ if (context) {
+ cb([], context)
+ }
+ return () => {}
+ }) as typeof posthog.onSurveysLoaded)
+}
+
+function renderButton(): HTMLElement {
+ const { getByRole } = render(<FeedbackSurveyButton surveyId={SURVEY_ID} />)
+ return getByRole('button')
+}
+
+describe('FeedbackSurveyButton', () => {
+ beforeEach(() => {
+ jest.mocked(posthog.displaySurvey).mockClear()
+ })
+
+ afterEach(cleanup)
+
+ it('is disabled and does not display the survey before the surveys extension reports in', () => {
+ mockSurveysLoaded(null)
+ const button = renderButton()
+
+ expect(button.getAttribute('aria-disabled')).toBe('true')
+ fireEvent.click(button)
+ expect(posthog.displaySurvey).not.toHaveBeenCalled()
+ })
+
+ it('displays the survey with condition bypass once surveys load', () => {
+ mockSurveysLoaded({ isLoaded: true })
+ const button = renderButton()
+
+ expect(button.getAttribute('aria-disabled')).not.toBe('true')
+ fireEvent.click(button)
+ expect(posthog.displaySurvey).toHaveBeenCalledWith(SURVEY_ID, {
+ displayType: DisplaySurveyType.Popover,
+ ignoreConditions: true,
+ ignoreDelay: true,
+ })
+ })
+
+ it('stays disabled when the surveys extension reports a failed load', () => {
+ mockSurveysLoaded({ isLoaded: false })
+ const button = renderButton()
+
+ expect(button.getAttribute('aria-disabled')).toBe('true')
+ fireEvent.click(button)
+ expect(posthog.displaySurvey).not.toHaveBeenCalled()
+ })
+})
diff --git a/frontend/src/lib/components/FeedbackSurveyButton/FeedbackSurveyButton.tsx b/frontend/src/lib/components/FeedbackSurveyButton/FeedbackSurveyButton.tsx
new file mode 100644
index 000000000000..adae0b1837f4
--- /dev/null
+++ b/frontend/src/lib/components/FeedbackSurveyButton/FeedbackSurveyButton.tsx
@@ -0,0 +1,48 @@
+import posthog, { DisplaySurveyType } from 'posthog-js'
+import { useEffect, useState } from 'react'
+
+import { LemonButton } from 'lib/lemon-ui/LemonButton'
+
+export interface FeedbackSurveyButtonProps {
+ surveyId: string
+ 'data-attr'?: string
+}
+
+/**
+ * Opens an in-app survey as a popover, waiting for the surveys extension before allowing a click.
+ * The plain, iconless styling is the intended standard for in-app feedback buttons — new ones
+ * should use this component rather than adding another variant.
+ */
+export function FeedbackSurveyButton({ surveyId, 'data-attr': dataAttr }: FeedbackSurveyButtonProps): JSX.Element {
+ const [surveysLoaded, setSurveysLoaded] = useState(false)
+
+ useEffect(
+ () =>
+ posthog.onSurveysLoaded((_, context) => {
+ if (context) {
+ setSurveysLoaded(context.isLoaded)
+ }
+ }),
+ []
+ )
+
+ return (
+ <LemonButton
+ size="small"
+ data-attr={dataAttr}
+ tooltip="Have any questions or feedback?"
+ disabledReason={surveysLoaded ? undefined : 'Feedback is unavailable right now'}
+ onClick={() =>
+ // A deliberate click should always bring up the survey, so bypass the
+ // popover's URL/cohort/already-dismissed targeting.
+ posthog.displaySurvey(surveyId, {
+ displayType: DisplaySurveyType.Popover,
+ ignoreConditions: true,
+ ignoreDelay: true,
+ })
+ }
+ >
+ Feedback
+ </LemonButton>
+ )
+}
diff --git a/frontend/src/scenes/feature-flags/FeatureFlags.tsx b/frontend/src/scenes/feature-flags/FeatureFlags.tsx
index 4cf2a6335a57..572f0ef23a62 100644
--- a/frontend/src/scenes/feature-flags/FeatureFlags.tsx
+++ b/frontend/src/scenes/feature-flags/FeatureFlags.tsx
@@ -9,6 +9,7 @@ import api from 'lib/api'
import { AccessControlAction } from 'lib/components/AccessControlAction'
import { ActivityLog } from 'lib/components/ActivityLog/ActivityLog'
import { BulkUpdateTagsButton } from 'lib/components/BulkActions/BulkUpdateTagsButton'
+import { FeedbackSurveyButton } from 'lib/components/FeedbackSurveyButton/FeedbackSurveyButton'
import { ObjectTags } from 'lib/components/ObjectTags/ObjectTags'
import PropertyFiltersDisplay from 'lib/components/PropertyFilters/components/PropertyFiltersDisplay'
import { Shortcut } from 'lib/components/Shortcuts/Shortcut'
@@ -67,6 +68,10 @@ import { BULK_ARCHIVE_MAX_FLAGS, flagSelectionLogic } from './flagSelectionLogic
import { OverlayForNewFeatureFlagMenu } from './NewFeatureFlagMenu'
import ProjectsGrid from './projects-grid/ProjectsGrid'
+// "NPS - Feature Flags" in project 2: https://us.posthog.com/project/2/surveys/018bcec8-6cf5-0000-c724-a51a86a4e8b1
+// The survey also self-triggers as a popover on feature flag URLs; this is the on-demand path.
+const FEATURE_FLAGS_NPS_SURVEY_ID = '018bcec8-6cf5-0000-c724-a51a86a4e8b1'
+
function FlagDescription({ name }: { name: string }): JSX.Element {
const ref = useRef<HTMLDivElement | null>(null)
const [isTruncated, setIsTruncated] = useState(false)
@@ -764,39 +769,45 @@ export function FeatureFlags(): JSX.Element {
type: 'feature_flag',
}}
actions={
- <AccessControlAction
- resourceType={AccessControlResourceType.FeatureFlag}
- minAccessLevel={AccessControlLevel.Editor}
- >
- <Shortcut
- name="NewFeatureFlag"
- keybind={[keyBinds.new]}
- intent="New feature flag"
- interaction="click"
- scope={Scene.FeatureFlags}
+ <>
+ <FeedbackSurveyButton
+ surveyId={FEATURE_FLAGS_NPS_SURVEY_ID}
+ data-attr="feature-flags-feedback-button"
+ />
+ <AccessControlAction
+ resourceType={AccessControlResourceType.FeatureFlag}
+ minAccessLevel={AccessControlLevel.Editor}
>
- <LemonButton
- type="primary"
- to={newFeatureFlagUrl}
- data-attr="new-feature-flag"
- size="small"
- icon={<IconPlusSmall />}
- sideAction={{
- dropdown: {
- placement: 'bottom-end',
- className: 'new-feature-flag-overlay',
- actionable: true,
- closeOnClickInside: false,
- overlay: <OverlayForNewFeatureFlagMenu />,
- },
- 'data-attr': 'new-feature-flag-dropdown',
- }}
- tooltip="New feature flag"
+ <Shortcut
+ name="NewFeatureFlag"
+ keybind={[keyBinds.new]}
+ intent="New feature flag"
+ interaction="click"
+ scope={Scene.FeatureFlags}
>
- New
- </LemonButton>
- </Shortcut>
- </AccessControlAction>
+ <LemonButton
+ type="primary"
+ to={newFeatureFlagUrl}
+ data-attr="new-feature-flag"
+ size="small"
+ icon={<IconPlusSmall />}
+ sideAction={{
+ dropdown: {
+ placement: 'bottom-end',
+ className: 'new-feature-flag-overlay',
+ actionable: true,
+ closeOnClickInside: false,
+ overlay: <OverlayForNewFeatureFlagMenu />,
+ },
+ 'data-attr': 'new-feature-flag-dropdown',
+ }}
+ tooltip="New feature flag"
+ >
+ New
+ </LemonButton>
+ </Shortcut>
+ </AccessControlAction>
+ </>
}
/>
<LemonTabs
diff --git a/products/experiments/frontend/scenes/ExperimentsScene.tsx b/products/experiments/frontend/scenes/ExperimentsScene.tsx
index 4cbc5a1f78e9..811ddeafe917 100644
--- a/products/experiments/frontend/scenes/ExperimentsScene.tsx
+++ b/products/experiments/frontend/scenes/ExperimentsScene.tsx
@@ -7,6 +7,7 @@ import { LemonInput, LemonSelect, LemonTag, Tooltip, lemonToast } from '@posthog
import { AccessControlAction } from 'lib/components/AccessControlAction'
import { ActivityLog } from 'lib/components/ActivityLog/ActivityLog'
+import { FeedbackSurveyButton } from 'lib/components/FeedbackSurveyButton/FeedbackSurveyButton'
import { MemberMultiSelect } from 'lib/components/MemberMultiSelect'
import { Shortcut } from 'lib/components/Shortcuts/Shortcut'
import { keyBinds } from 'lib/components/Shortcuts/shortcuts'
@@ -73,6 +74,10 @@ import { ExperimentsHoldoutsScene } from 'products/experiments/frontend/scenes/E
import { ExperimentsSettingsScene } from 'products/experiments/frontend/scenes/ExperimentsSettingsScene'
import { ExperimentsSharedMetricsScene } from 'products/experiments/frontend/scenes/ExperimentsSharedMetricsScene'
+// "NPS - Experiments" in project 2: https://us.posthog.com/project/2/surveys/01902c1f-6675-0000-913b-686c40c3a957
+// The survey also self-triggers as a popover on experiment URLs; this is the on-demand path.
+const EXPERIMENTS_NPS_SURVEY_ID = '01902c1f-6675-0000-913b-686c40c3a957'
+
export const scene: SceneExport = {
component: ExperimentsScene,
logic: experimentsLogic,
@@ -570,60 +575,66 @@ export function ExperimentsScene(): JSX.Element {
type: 'experiment',
}}
actions={
- tab !== ExperimentsTabs.SharedMetrics && tab !== ExperimentsTabs.Holdouts ? (
- <AccessControlAction
- resourceType={AccessControlResourceType.Experiment}
- minAccessLevel={AccessControlLevel.Editor}
- >
- <div className="flex items-center gap-2">
- <MaxTool
- identifier="create_experiment"
- initialMaxPrompt="Create an experiment for "
- suggestions={[
- 'Create an experiment to test…',
- 'Set up an A/B test with a 70/30 split between control and test for…',
- ]}
- callback={(toolOutput: {
- experiment_id?: string | number
- experiment_name?: string
- feature_flag_key?: string
- error?: string
- }) => {
- if (toolOutput?.error || !toolOutput?.experiment_id) {
- lemonToast.error(
- `Failed to create experiment: ${toolOutput?.error || 'Unknown error'}`
- )
- return
- }
- // Refresh experiments list to show new experiment, then redirect to it
- loadExperiments()
- router.actions.push(urls.experiment(toolOutput.experiment_id))
- }}
- position="bottom-right"
- active={true}
- context={{}}
- >
- <Shortcut
- name="NewExperiment"
- keybind={[keyBinds.new]}
- intent="New experiment"
- interaction="click"
- scope={Scene.Experiments}
+ <>
+ <FeedbackSurveyButton
+ surveyId={EXPERIMENTS_NPS_SURVEY_ID}
+ data-attr="experiments-feedback-button"
+ />
+ {tab !== ExperimentsTabs.SharedMetrics && tab !== ExperimentsTabs.Holdouts ? (
+ <AccessControlAction
+ resourceType={AccessControlResourceType.Experiment}
+ minAccessLevel={AccessControlLevel.Editor}
+ >
+ <div className="flex items-center gap-2">
+ <MaxTool
+ identifier="create_experiment"
+ initialMaxPrompt="Create an experiment for "
+ suggestions={[
+ 'Create an experiment to test…',
+ 'Set up an A/B test with a 70/30 split between control and test for…',
+ ]}
+ callback={(toolOutput: {
+ experiment_id?: string | number
+ experiment_name?: string
+ feature_flag_key?: string
+ error?: string
+ }) => {
+ if (toolOutput?.error || !toolOutput?.experiment_id) {
+ lemonToast.error(
+ `Failed to create experiment: ${toolOutput?.error || 'Unknown error'}`
+ )
+ return
+ }
+ // Refresh experiments list to show new experiment, then redirect to it
+ loadExperiments()
+ router.actions.push(urls.experiment(toolOutput.experiment_id))
+ }}
+ position="bottom-right"
+ active={true}
+ context={{}}
>
- <LemonButton
- size="small"
- type="primary"
- data-attr="create-experiment"
- to={urls.experiment('new')}
- tooltip="New experiment"
+ <Shortcut
+ name="NewExperiment"
+ keybind={[keyBinds.new]}
+ intent="New experiment"
+ interaction="click"
+ scope={Scene.Experiments}
>
- <span className="pr-3">New experiment</span>
- </LemonButton>
- </Shortcut>
- </MaxTool>
- </div>
- </AccessControlAction>
- ) : undefined
+ <LemonButton
+ size="small"
+ type="primary"
+ data-attr="create-experiment"
+ to={urls.experiment('new')}
+ tooltip="New experiment"
+ >
+ <span className="pr-3">New experiment</span>
+ </LemonButton>
+ </Shortcut>
+ </MaxTool>
+ </div>
+ </AccessControlAction>
+ ) : undefined}
+ </>
}
/>
<LemonTabs