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): expose the chart gallery experiment on chart menu open (#107602)

Sam Penningtone2ccec300c59
diff --git a/frontend/src/lib/components/ChartFilter/ChartFilter.tsx b/frontend/src/lib/components/ChartFilter/ChartFilter.tsx
index ef475badd7b5..9d11a48a0a92 100644
--- a/frontend/src/lib/components/ChartFilter/ChartFilter.tsx
+++ b/frontend/src/lib/components/ChartFilter/ChartFilter.tsx
@@ -38,9 +38,11 @@ function chartDisplayOptionToSelectOption(option: ChartDisplayOption): LemonSele
export function ChartFilter({
fullWidth = false,
disabledReason,
+ onOpen,
}: {
fullWidth?: boolean
disabledReason?: string
+ onOpen?: () => void
}): JSX.Element {
const { insightProps, editingDisabledReason } = useValues(insightLogic)
const { display } = useValues(insightVizDataLogic(insightProps))
@@ -71,6 +73,7 @@ export function ChartFilter({
optionTooltipPlacement="left"
dropdownMatchSelectWidth={false}
data-attr="chart-filter"
+ menu={{ onVisibilityChange: (visible) => visible && onOpen?.() }}
options={options}
size="small"
fullWidth={fullWidth}
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.test.tsx b/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.test.tsx
index d74579e2da47..3b272b84a9bd 100644
--- a/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.test.tsx
+++ b/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.test.tsx
@@ -1,7 +1,8 @@
import '@testing-library/jest-dom'
-import { cleanup, render, waitFor } from '@testing-library/react'
+import { cleanup, fireEvent, render, waitFor } from '@testing-library/react'
import { BindLogic, Provider } from 'kea'
+import posthog from 'posthog-js'
import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
@@ -141,4 +142,67 @@ describe('ChartAlternatives', () => {
expect(document.querySelector('[data-attr="chart-alternatives-all"]')).not.toBeInTheDocument()
)
})
+
+ it.each([
+ ['the gallery', 'test', 'chart-alternatives-all', 1],
+ ['the chart type dropdown', 'control', 'chart-filter', 1],
+ ])('reports an exposure when %s opens', async (_, variant, dataAttr, expected) => {
+ featureFlagLogic.actions.setFeatureFlags([FEATURE_FLAGS.PRODUCT_ANALYTICS_CHART_ALTERNATIVES], {
+ [FEATURE_FLAGS.PRODUCT_ANALYTICS_CHART_ALTERNATIVES]: variant,
+ })
+ setQuery(makeTrendsQuery())
+ alternativesLogic()
+ const capture = jest.spyOn(posthog, 'capture')
+
+ const menuButton = await waitFor(() => {
+ const button = document.querySelector(`[data-attr="${dataAttr}"]`)
+ expect(button).toBeInTheDocument()
+ return button!
+ })
+ fireEvent.click(menuButton)
+
+ await waitFor(() =>
+ expect(capture.mock.calls.filter(([event]) => event === 'insight chart type menu opened')).toHaveLength(
+ expected
+ )
+ )
+ jest.restoreAllMocks()
+ })
+
+ it('does not report an exposure when the chart type dropdown opens on a read-only insight', () => {
+ setQuery(makeTrendsQuery())
+ const capture = jest.spyOn(posthog, 'capture')
+
+ chartAlternativesLogic({ embedded: true, ...insightProps }).mount()
+ chartAlternativesLogic({ embedded: true, ...insightProps }).actions.reportChartMenuOpened()
+
+ expect(capture.mock.calls.map(([event]) => event)).not.toContain('insight chart type menu opened')
+ jest.restoreAllMocks()
+ })
+
+ it.each([
+ ['a read-only insight', true, NodeKind.TrendsQuery],
+ ['a non-trends insight', false, NodeKind.FunnelsQuery],
+ ])('does not read the experiment flag for %s', (_, embedded, kind) => {
+ const readFlags: string[] = []
+ const featureFlags = new Proxy(
+ {},
+ {
+ get: (_target, flag) => {
+ readFlags.push(String(flag))
+ return 'test'
+ },
+ }
+ )
+ jest.spyOn(featureFlagLogic.selectors, 'featureFlags').mockReturnValue(featureFlags)
+ builtInsightVizDataLogic.actions.updateQuerySource({ ...makeTrendsQuery(), kind } as unknown as TrendsQuery)
+
+ const logic = chartAlternativesLogic({ embedded, ...insightProps })
+ logic.mount()
+
+ expect(logic.values.canShowAlternatives).toBe(false)
+ expect(readFlags).not.toContain(FEATURE_FLAGS.PRODUCT_ANALYTICS_CHART_ALTERNATIVES)
+ logic.unmount()
+ jest.restoreAllMocks()
+ })
})
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.tsx b/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.tsx
index 4f1fe0dfc2f8..a09d25689304 100644
--- a/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.tsx
+++ b/products/product_analytics/frontend/insights/chartAlternatives/ChartAlternatives.tsx
@@ -28,11 +28,11 @@ export function ChartAlternatives({
const logic = useMountedLogic(chartAlternativesLogic(logicProps))
useMountedLogic(chartPreviewsLogic(logicProps))
const { canShowAlternatives, currentOption, galleryOpen, selectionDisabledReason } = useValues(logic)
- const { closeGallery, toggleGallery } = useActions(logic)
+ const { closeGallery, reportChartMenuOpened, toggleGallery } = useActions(logic)
const triggerRef = useRef<HTMLButtonElement>(null)
if (!canShowAlternatives) {
- return <ChartFilter />
+ return <ChartFilter onOpen={reportChartMenuOpened} />
}
return (
diff --git a/products/product_analytics/frontend/insights/chartAlternatives/chartAlternativesLogic.ts b/products/product_analytics/frontend/insights/chartAlternatives/chartAlternativesLogic.ts
index 34e35163e92a..acd918ae35ba 100644
--- a/products/product_analytics/frontend/insights/chartAlternatives/chartAlternativesLogic.ts
+++ b/products/product_analytics/frontend/insights/chartAlternatives/chartAlternativesLogic.ts
@@ -77,6 +77,7 @@ export interface chartAlternativesLogicValues {
galleryOpen: boolean
inSharedMode: boolean | undefined
isEditableSurface: boolean
+ isEligibleSurface: boolean
options: ChartDisplayOptionGroup[]
selectionDisabledReason: string | undefined
trendsSource: TrendsQuery | null
@@ -100,6 +101,9 @@ export interface chartAlternativesLogicActions {
openGallery: () => {
value: true
}
+ reportChartMenuOpened: () => {
+ value: true
+ }
selectChart: (
display: ChartDisplayType,
source: ChartAlternativeSource
@@ -145,13 +149,13 @@ export interface chartAlternativesLogicMeta {
currentDisplay: ChartDisplayType
) => ChartDisplayOption | undefined
isEditableSurface: (embedded: boolean, inSharedMode: boolean | undefined, canEditInsight: boolean) => boolean
- canShowAlternatives: (
- featureFlags: FeatureFlagsSet,
+ isEligibleSurface: (
isEditableSurface: boolean,
isTrends: boolean,
query: Node<Record<string, any>> | null,
trendsSource: TrendsQuery | null
) => boolean
+ canShowAlternatives: (featureFlags: FeatureFlagsSet, isEligibleSurface: boolean) => boolean
canSelectCharts: (canShowAlternatives: boolean, insightDataLoading: boolean) => boolean
selectionDisabledReason: (
canShowAlternatives: boolean,
@@ -187,6 +191,7 @@ export const chartAlternativesLogic = kea<chartAlternativesLogicType>([
openGallery: true,
closeGallery: true,
toggleGallery: true,
+ reportChartMenuOpened: true,
selectChart: (display: ChartDisplayType, source: ChartAlternativeSource) => ({ display, source }),
}),
reducers({
@@ -258,20 +263,21 @@ export const chartAlternativesLogic = kea<chartAlternativesLogicType>([
(embedded: boolean, inSharedMode: boolean | undefined, canEditInsight: boolean): boolean =>
!embedded && !inSharedMode && canEditInsight,
],
- canShowAlternatives: [
- (s) => [s.featureFlags, s.isEditableSurface, s.isTrends, s.query, s.trendsSource],
+ // Where either experiment arm offers a chart type choice, so where an opened menu counts as an exposure.
+ isEligibleSurface: [
+ (s) => [s.isEditableSurface, s.isTrends, s.query, s.trendsSource],
(
- featureFlags: FeatureFlagsSet,
isEditableSurface: boolean,
isTrends: boolean,
query: Node | null,
trendsSource: TrendsQuery | null
- ): boolean =>
- featureFlags[FEATURE_FLAGS.PRODUCT_ANALYTICS_CHART_ALTERNATIVES] === 'test' &&
- isEditableSurface &&
- isTrends &&
- !!query &&
- !!trendsSource,
+ ): boolean => isEditableSurface && isTrends && !!query && !!trendsSource,
+ ],
+ canShowAlternatives: [
+ (s) => [s.featureFlags, s.isEligibleSurface],
+ (featureFlags: FeatureFlagsSet, isEligibleSurface: boolean): boolean =>
+ // Reading the flag logs a flag call, so read it only where the gallery could show.
+ isEligibleSurface && featureFlags[FEATURE_FLAGS.PRODUCT_ANALYTICS_CHART_ALTERNATIVES] === 'test',
],
canSelectCharts: [
(s) => [s.canShowAlternatives, s.insightDataLoading],
@@ -299,6 +305,19 @@ export const chartAlternativesLogic = kea<chartAlternativesLogicType>([
openGallery: () => {
if (!values.canSelectCharts) {
actions.closeGallery()
+ return
+ }
+ actions.reportChartMenuOpened()
+ },
+ toggleGallery: () => {
+ if (values.galleryOpen) {
+ actions.reportChartMenuOpened()
+ }
+ },
+ reportChartMenuOpened: () => {
+ if (values.isEligibleSurface) {
+ // The chart gallery experiment uses this as its exposure event, for both arms.
+ posthog.capture('insight chart type menu opened', { current_display: values.currentDisplay })
}
},
selectChart: ({ display, source }) => {