: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(experiments): Add a warehouse-native exposure to the new experiment wizard (#130745)
Juraj Majerik7d0e71842672
diff --git a/frontend/snapshots.yml b/frontend/snapshots.ymlindex 1da218324a44..db90a87537cd 100644--- a/frontend/snapshots.yml+++ b/frontend/snapshots.yml@@ -3472,6 +3472,10 @@ snapshots:hash: v1.k794b7964.47d21cdf33f368b06633d317125603fc69b5675d9389c038b40c824e73f1a405.0ufZrtGmQpexxX1aaLjAVmf3-YVGmHd7p8-HB9cbQjkexperiments-varianttimeseriestooltip--significant-day--light:hash: v1.k794b7964.6e850075cec396cda57b4d44acd24b1fb4deb1e00c640db0049d172e8d99f2bb.Im0tdmWO-QcJ_sukw7HEC5eSF0ZBsTJgYvKKf-KxnLA+ experiments-warehousenativeexposurefields--exposure-query--dark:+ hash: v1.k794b7964.a96f5ac94c6cb920f58f915915997326da9909ee1f63d3594ed5f1523ed67703.JYINlKygF3apUHNXcy4wTSDiUH-Lk4i37NIDmGKW_40+ experiments-warehousenativeexposurefields--exposure-query--light:+ hash: v1.k794b7964.08e0e84bdd7b58f447d8c926fab99eae1490daa25f8d99415786ad0b3d500300.f558Oxg-xOtb4B33R24kFozgNca_Q9O5KXMN-7xZImQexperiments-warehousenativemetricfields--shared-ratio-metric--dark:hash: v1.k794b7964.725376bdb2387961defbc6e14cb786936236c46383110b60684b0d34bb8810a5.ecVYEPkV-p8nvLY497gnhBIaCyiN8b8QiYtDswjYsr4experiments-warehousenativemetricfields--shared-ratio-metric--light:diff --git a/frontend/src/lib/utils/eventUsageLogic.ts b/frontend/src/lib/utils/eventUsageLogic.tsindex 61fa4408b6b9..418099d391ab 100644--- a/frontend/src/lib/utils/eventUsageLogic.ts+++ b/frontend/src/lib/utils/eventUsageLogic.ts@@ -9,6 +9,7 @@ import { objectClean } from 'lib/utils/objects'import { retryImport } from 'lib/utils/retryImport'import { BillingUsageInteractionProps } from 'scenes/billing/types'import type { ExperimentWizardStepNavigation } from 'scenes/experiments/ExperimentWizard/experimentWizardLogic'+import type { ExposureSource } from 'scenes/experiments/exposureContract'import { SharedMetric } from 'scenes/experiments/SharedMetrics/sharedMetricLogic'import { ProductTourEvent } from 'scenes/product-tours/constants'import { SURVEY_CREATED_SOURCE } from 'scenes/surveys/constants'@@ -1042,6 +1043,9 @@ export interface eventUsageLogicActions {reportExperimentWizardAskAiClicked: (currentStep: string) => {currentStep: string}+ reportExperimentWizardExposureSourceSelected: (exposureSource: ExposureSource) => {+ exposureSource: ExposureSource+ }reportExperimentWizardStarted: () => {value: true}@@ -1550,6 +1554,7 @@ export const eventUsageLogic = kea<eventUsageLogicType>([reportInsightDraftDiscarded: (draftAgeSeconds: number) => ({ draftAgeSeconds }),reportExperimentWizardStarted: true,reportExperimentWizardAskAiClicked: (currentStep: string) => ({ currentStep }),+ reportExperimentWizardExposureSourceSelected: (exposureSource: ExposureSource) => ({ exposureSource }),reportExperimentsListAiBadgeClicked: true,reportExperimentsListViewed: (listView: {experimentsShown: number@@ -2031,6 +2036,9 @@ export const eventUsageLogic = kea<eventUsageLogicType>([reportExperimentWizardAskAiClicked: ({ currentStep }) => {posthog.capture('experiment wizard ask ai clicked', { current_step: currentStep })},+ reportExperimentWizardExposureSourceSelected: ({ exposureSource }) => {+ posthog.capture('experiment wizard exposure source selected', { exposure_source: exposureSource })+ },reportExperimentsListAiBadgeClicked: () => {posthog.capture('experiments list ai badge clicked')},diff --git a/frontend/src/scenes/experiments/ExperimentForm/ExposureCriteriaPanel.test.tsx b/frontend/src/scenes/experiments/ExperimentForm/ExposureCriteriaPanel.test.tsxnew file mode 100644index 000000000000..1d4ff97d0c7f--- /dev/null+++ b/frontend/src/scenes/experiments/ExperimentForm/ExposureCriteriaPanel.test.tsx@@ -0,0 +1,80 @@+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'+import userEvent from '@testing-library/user-event'++import { FEATURE_FLAGS } from 'lib/constants'+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'++import { NodeKind } from '~/queries/schema/schema-general'+import { initKeaTests } from '~/test/init'+import type { Experiment } from '~/types'++import { NEW_EXPERIMENT } from 'products/experiments/frontend/constants'++import { ExposureCriteriaPanel } from './ExposureCriteriaPanel'++jest.mock('lib/components/TestAccountFiltersSwitch', () => ({ TestAccountFilterSwitch: () => null }))++describe('ExposureCriteriaPanel', () => {+ const WAREHOUSE_NATIVE_OPTION = '[data-attr="experiment-wizard-exposure-warehouse-native"]'++ const renderPanel = (onChange: jest.Mock, metricDataSources = { warehouseNative: false, posthog: false }): void => {+ render(+ <ExposureCriteriaPanel+ experiment={{ ...NEW_EXPERIMENT } as Experiment}+ featureFlag={null}+ onChange={onChange}+ metricDataSources={metricDataSources}+ compact+ />+ )+ }++ const openExposureSelect = async (): Promise<void> => {+ await userEvent.click(screen.getByText('Feature flag is called'))+ }++ beforeEach(() => {+ initKeaTests()+ featureFlagLogic.mount()+ })++ afterEach(() => {+ cleanup()+ })++ it('hides the warehouse-native exposure while the project is not flagged in', async () => {+ renderPanel(jest.fn())++ await openExposureSelect()++ expect(screen.getByText('Custom event')).toBeInTheDocument()+ expect(document.querySelector(WAREHOUSE_NATIVE_OPTION)).not.toBeInTheDocument()+ })++ it.each([+ ['starts an empty warehouse-native exposure when the draft has no metrics', false, 1],+ ['blocks the warehouse-native exposure while the draft has PostHog metrics', true, 0],+ ])('%s', async (_, hasPosthogMetrics, expectedChanges) => {+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.BIGQUERY_DIRECT_QUERY]: true })+ const onChange = jest.fn()+ renderPanel(onChange, { warehouseNative: false, posthog: hasPosthogMetrics })++ await openExposureSelect()+ await userEvent.click(document.querySelector(WAREHOUSE_NATIVE_OPTION)!)++ expect(onChange).toHaveBeenCalledTimes(expectedChanges)+ if (expectedChanges) {+ expect(onChange).toHaveBeenCalledWith({+ exposure_config: {+ kind: NodeKind.ExperimentWarehouseNativeExposureConfig,+ connection_id: '',+ unit: { unit_type: '', id_namespace: '' },+ query: '',+ },+ activation_config: undefined,+ })+ }+ })+})diff --git a/frontend/src/scenes/experiments/ExperimentForm/ExposureCriteriaPanel.tsx b/frontend/src/scenes/experiments/ExperimentForm/ExposureCriteriaPanel.tsxindex f5fa0db6b479..8b628ea5acc7 100644--- a/frontend/src/scenes/experiments/ExperimentForm/ExposureCriteriaPanel.tsx+++ b/frontend/src/scenes/experiments/ExperimentForm/ExposureCriteriaPanel.tsx@@ -10,12 +10,24 @@ import { SeriesActionFilter } from 'scenes/insights/filters/ActionFilter/ActionFimport { MathAvailability } from 'scenes/insights/filters/ActionFilter/ActionFilterRow/types'import { teamLogic } from 'scenes/teamLogic'-import { ExperimentEventExposureConfig, ExperimentExposureCriteria, NodeKind } from '~/queries/schema/schema-general'+import {+ ExperimentEventExposureConfig,+ ExperimentExposureCriteria,+ ExperimentWarehouseNativeExposureConfig,+ NodeKind,+} from '~/queries/schema/schema-general'import { ChartDisplayCategory, type Experiment, type FeatureFlagBasicType } from '~/types'import { NegatedEventFilterNote } from 'products/experiments/frontend/components/NegatedEventFilterNote'import { SelectableCard } from 'products/experiments/frontend/components/SelectableCard'import { isLaunched } from 'products/experiments/frontend/experimentStatus'+import { WarehouseNativeExposureFields } from 'products/experiments/frontend/warehouseNative/WarehouseNativeExposureFields'+import {+ EMPTY_WAREHOUSE_NATIVE_BINDING,+ type MetricDataSources,+ getExposureSourceBlockedReasons,+ getWarehouseNativeExposure,+} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'import {EXPERIMENT_EXPOSURE_EVENT,@@ -40,11 +52,19 @@ const DEFAULT_ACTIVATION_CONFIG: ExperimentEventExposureConfig = {properties: [],}+const DEFAULT_WAREHOUSE_NATIVE_EXPOSURE_CONFIG: ExperimentWarehouseNativeExposureConfig = {+ kind: NodeKind.ExperimentWarehouseNativeExposureConfig,+ ...EMPTY_WAREHOUSE_NATIVE_BINDING,+ query: '',+}+type ExposureCriteriaPanelProps = {experiment: Experiment/** The flag the experiment runs on. A wizard draft keeps a linked flag outside `experiment`. */featureFlag: Pick<FeatureFlagBasicType, 'filters'> | nullonChange: (exposureCriteria: ExperimentExposureCriteria) => void+ /** Which data sources the experiment's metrics read. The compact panel blocks an exposure of the other one. */+ metricDataSources?: MetricDataSourcescompact?: boolean}@@ -282,16 +302,26 @@ export function ExposureCriteriaPanel({experiment,featureFlag,onChange,+ metricDataSources,compact,}: ExposureCriteriaPanelProps): JSX.Element {const groupTypeIndex = featureFlag?.filters?.aggregation_group_type_index// getActivationConfig, not a raw field check: a stored default-sentinel exposure_config// still composes with activation, and the editor must classify it the way the backend doesconst isActivation = !!getActivationConfig(experiment.exposure_criteria)- const isCustom = !isActivation && !!experiment.exposure_criteria?.exposure_config+ const warehouseExposure = getWarehouseNativeExposure(experiment.exposure_criteria)+ const isCustom = !isActivation && !warehouseExposure && !!experiment.exposure_criteria?.exposure_configconst activationEventEnabled = useFeatureFlag('EXPERIMENT_ACTIVATION_EVENT')// Keep an existing activation config editable even if the team is no longer flagged inconst showActivationOption = activationEventEnabled || isActivation+ const warehouseNativeEnabled = useFeatureFlag('BIGQUERY_DIRECT_QUERY')+ // Keep an existing warehouse-native exposure editable even if the team is no longer flagged in+ const showWarehouseNativeOption = warehouseNativeEnabled || !!warehouseExposure+ const { warehouseNative: warehouseNativeBlockedReason, posthog: posthogBlockedReason } =+ getExposureSourceBlockedReasons(+ !!warehouseExposure,+ metricDataSources ?? { warehouseNative: false, posthog: false }+ )const experimentExposureEventEnabled = useFeatureFlag('EXPERIMENT_EXPOSURE_EVENT')const defaultExposureEvent = resolvedExposureEvent(experiment,@@ -324,13 +354,30 @@ export function ExposureCriteriaPanel({size="small"dropdownMatchSelectWidth={false}dropdownPlacement="bottom-end"- value={isCustom ? 'custom' : isActivation ? 'activation' : 'default'}+ value={+ warehouseExposure+ ? 'warehouse_native'+ : isCustom+ ? 'custom'+ : isActivation+ ? 'activation'+ : 'default'+ }onChange={(value) => {- // Re-selecting the current mode must not reset a configured event- if ((value === 'custom' && isCustom) || (value === 'activation' && isActivation)) {+ // Re-selecting the current mode must not reset a configured event or query+ if (+ (value === 'custom' && isCustom) ||+ (value === 'activation' && isActivation) ||+ (value === 'warehouse_native' && warehouseExposure)+ ) {return}- if (value === 'custom') {+ if (value === 'warehouse_native') {+ onChange({+ exposure_config: DEFAULT_WAREHOUSE_NATIVE_EXPOSURE_CONFIG,+ activation_config: undefined,+ })+ } else if (value === 'custom') {onChange({exposure_config: DEFAULT_EXPOSURE_CONFIG,activation_config: undefined,@@ -348,6 +395,7 @@ export function ExposureCriteriaPanel({{value: 'default' as const,label: defaultExposureLabel,+ disabledReason: posthogBlockedReason,labelInMenu: (<div><div>{defaultExposureLabel}</div>@@ -362,6 +410,7 @@ export function ExposureCriteriaPanel({{value: 'activation' as const,label: 'Activation event',+ disabledReason: posthogBlockedReason,labelInMenu: (<div><div>Activation event</div>@@ -376,6 +425,7 @@ export function ExposureCriteriaPanel({{value: 'custom' as const,label: 'Custom event',+ disabledReason: posthogBlockedReason,labelInMenu: (<div><div>Custom event</div>@@ -385,9 +435,34 @@ export function ExposureCriteriaPanel({</div>),},+ ...(showWarehouseNativeOption+ ? [+ {+ value: 'warehouse_native' as const,+ label: 'Warehouse-native query',+ disabledReason: warehouseNativeBlockedReason,+ labelInMenu: (+ <div>+ <div>Warehouse-native query</div>+ <div className="text-xs text-muted font-normal">+ Read exposures with a SQL query that runs in your own+ warehouse+ </div>+ </div>+ ),+ 'data-attr': 'experiment-wizard-exposure-warehouse-native',+ },+ ]+ : []),]}/></div>+ {warehouseExposure && (+ <WarehouseNativeExposureFields+ exposure={warehouseExposure}+ onChange={(exposure) => onChange({ exposure_config: exposure })}+ />+ )}{isCustom && (<InclusionActionFilterexperiment={experiment}@@ -457,21 +532,24 @@ export function ExposureCriteriaPanel({/></div>- {/* As tall as the small selects above, so the three labels are evenly spaced */}- <div className="flex min-h-[2.0625rem] items-center">- <TestAccountFilterSwitch- checked={hasFilters && resolveFilterTestAccounts(experiment.exposure_criteria)}- onChange={(checked: boolean) => {- onChange({ filterTestAccounts: checked })- }}- bordered={false}- fullWidth- settingsLinkIcon="new-tab"- // Keep the label to its text, or the whole row becomes a click target for the switch- labelClassName="text-secondary font-normal !flex-none"- className="p-0 justify-between"- />- </div>+ {/* Test account filters read PostHog person and event properties, which warehouse rows do not have */}+ {!warehouseExposure && (+ // As tall as the small selects above, so the three labels are evenly spaced+ <div className="flex min-h-[2.0625rem] items-center">+ <TestAccountFilterSwitch+ checked={hasFilters && resolveFilterTestAccounts(experiment.exposure_criteria)}+ onChange={(checked: boolean) => {+ onChange({ filterTestAccounts: checked })+ }}+ bordered={false}+ fullWidth+ settingsLinkIcon="new-tab"+ // Keep the label to its text, or the whole row becomes a click target for the switch+ labelClassName="text-secondary font-normal !flex-none"+ className="p-0 justify-between"+ />+ </div>+ )}</div>)}diff --git a/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricsPanel.tsx b/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricsPanel.tsxindex 5875993455e2..125d265f8d6c 100644--- a/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricsPanel.tsx+++ b/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricsPanel.tsx@@ -17,6 +17,7 @@ import {import { MetricSourceModal } from 'products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal'import { SharedMetricModal } from 'products/experiments/frontend/modals/SharedMetricModal/SharedMetricModal'import { sharedMetricModalLogic } from 'products/experiments/frontend/modals/SharedMetricModal/sharedMetricModalLogic'+import type { MetricDataSources } from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'import { EmptyMetricsPanel } from './EmptyMetricsPanel'import { MetricList } from './MetricList'@@ -28,6 +29,8 @@ export type MetricsPanelProps = {onDeleteMetric: (metric: ExperimentMetric, context: MetricContext) => voidonSaveSharedMetrics: (metrics: ExperimentMetric[], context: MetricContext) => voidonSaveExposureCriteria: (exposureCriteria: ExperimentExposureCriteria) => void+ /** Which data sources the metrics read, shared metrics included. */+ metricDataSources: MetricDataSourcescompact?: boolean}@@ -46,6 +49,7 @@ export const MetricsPanel = ({onDeleteMetric,onSaveSharedMetrics,onSaveExposureCriteria,+ metricDataSources,compact,}: MetricsPanelProps): JSX.Element => {const { closeExperimentMetricModal } = useActions(experimentMetricModalLogic)@@ -89,7 +93,7 @@ export const MetricsPanel = ({</div>)}- <MetricSourceModal />+ <MetricSourceModal experiment={experiment} /><ExperimentMetricModalexperiment={experiment}exposureCriteria={experiment.exposure_criteria}@@ -111,7 +115,7 @@ export const MetricsPanel = ({closeSharedMetricModal()}}/>- <ExposureCriteriaModal onSave={onSaveExposureCriteria} />+ <ExposureCriteriaModal onSave={onSaveExposureCriteria} metricDataSources={metricDataSources} /></div>)}diff --git a/frontend/src/scenes/experiments/ExperimentForm/createExperimentLogic.ts b/frontend/src/scenes/experiments/ExperimentForm/createExperimentLogic.tsindex 8255ff2d0b94..b046974af687 100644--- a/frontend/src/scenes/experiments/ExperimentForm/createExperimentLogic.ts+++ b/frontend/src/scenes/experiments/ExperimentForm/createExperimentLogic.ts@@ -28,6 +28,10 @@ import type {ExperimentFeatureFlagFiltersApi,ExperimentTypeEnumApi,} from 'products/experiments/frontend/generated/api.schemas'+import {+ alignMetricsWithExposure,+ getWarehouseNativeExposure,+} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'import { visionScannersCreate } from 'products/replay_vision/frontend/generated/api'import { experimentScannerParams } from 'products/replay_vision/frontend/replay_scanners/experimentTargeting'@@ -232,13 +236,22 @@ export const createExperimentLogic = kea<createExperimentLogicType>([{setExperiment: (_, { experiment }) => experiment,setExperimentValue: (state, { name, value }) => ({ ...state, [name]: value }),- setExposureCriteria: (state, { criteria }) => ({- ...state,- exposure_criteria: {- ...state.exposure_criteria,- ...criteria,- },- }),+ setExposureCriteria: (state, { criteria }) => {+ const exposureCriteria = { ...state.exposure_criteria, ...criteria }+ const warehouseExposure = getWarehouseNativeExposure(exposureCriteria)+ return {+ ...state,+ exposure_criteria: exposureCriteria,+ // The API rejects a metric on another connection or unit than the exposure+ ...(warehouseExposure && {+ metrics: alignMetricsWithExposure(state.metrics ?? [], warehouseExposure),+ metrics_secondary: alignMetricsWithExposure(+ state.metrics_secondary ?? [],+ warehouseExposure+ ),+ }),+ }+ },setFeatureFlagConfig: (state, { config }) => {const currentConfig = state.feature_flag_config ?? {}const filters: ExperimentFeatureFlagFiltersApi = { ...currentConfig.filters }diff --git a/frontend/src/scenes/experiments/ExperimentView/ExperimentView.tsx b/frontend/src/scenes/experiments/ExperimentView/ExperimentView.tsxindex 298a7f5e296c..9a0f29f2bae4 100644--- a/frontend/src/scenes/experiments/ExperimentView/ExperimentView.tsx+++ b/frontend/src/scenes/experiments/ExperimentView/ExperimentView.tsx@@ -249,7 +249,7 @@ export function ExperimentView(): JSX.Element {/>{/* Modern experiment modals */}- <MetricSourceModal />+ <MetricSourceModal experiment={experiment} /><ExperimentMetricModalexperiment={experiment}exposureCriteria={exposureCriteria}diff --git a/frontend/src/scenes/experiments/ExperimentView/ExposureCriteria.tsx b/frontend/src/scenes/experiments/ExperimentView/ExposureCriteria.tsxindex a01de9913b84..e8dc98161482 100644--- a/frontend/src/scenes/experiments/ExperimentView/ExposureCriteria.tsx+++ b/frontend/src/scenes/experiments/ExperimentView/ExposureCriteria.tsx@@ -16,13 +16,15 @@ import { ChartDisplayCategory } from '~/types'import { NegatedEventFilterNote } from 'products/experiments/frontend/components/NegatedEventFilterNote'import { SelectableCard } from 'products/experiments/frontend/components/SelectableCard'import { isLaunched } from 'products/experiments/frontend/experimentStatus'-import { WarehouseNativeConnectionFields } from 'products/experiments/frontend/warehouseNative/WarehouseNativeConnectionFields'-import { WarehouseNativeQueryEditor } from 'products/experiments/frontend/warehouseNative/WarehouseNativeQueryEditor'+import { WarehouseNativeExposureFields } from 'products/experiments/frontend/warehouseNative/WarehouseNativeExposureFields'import {EMPTY_WAREHOUSE_NATIVE_BINDING,+ type MetricDataSources,getExperimentMetricDataSources,+ getExposureSourceBlockedReasons,getWarehouseNativeExposure,getWarehouseNativeExposureError,+ getWarehouseNativeMismatchError,} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'import { experimentLogic } from '../experimentLogic'@@ -33,9 +35,11 @@ import { exposureCriteriaModalLogic } from './exposureCriteriaModalLogic'type ExposureCriteriaModalProps = {onSave: (exposureCriteria: ExperimentExposureCriteria) => void+ /** Which data sources the metrics read. The wizard passes those of its draft, which `experimentLogic` does not hold. */+ metricDataSources?: MetricDataSources}-export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): JSX.Element | null {+export function ExposureCriteriaModal({ onSave, metricDataSources }: ExposureCriteriaModalProps): JSX.Element | null {const { isExposureCriteriaModalOpen, exposureCriteria } = useValues(exposureCriteriaModalLogic)const { experiment, resolvedExposureEvent } = useValues(experimentLogic)const launched = isLaunched(experiment)@@ -56,16 +60,20 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): Jconst warehouseNativeEnabled = useFeatureFlag('BIGQUERY_DIRECT_QUERY')// Keep an existing warehouse-native exposure editable even if the team is no longer flagged inconst showWarehouseNativeCard = warehouseNativeEnabled || !!warehouseExposure- // The API rejects an exposure that reads another data source than the metrics, so the metrics must go first- const metricDataSources = getExperimentMetricDataSources(experiment)- const warehouseNativeBlockedReason =- !warehouseExposure && metricDataSources.posthog- ? "Remove this experiment's metrics first. A warehouse-native exposure only works with warehouse-native metrics."- : undefined- const posthogBlockedReason =- warehouseExposure && metricDataSources.warehouseNative- ? "Remove this experiment's metrics first. Warehouse-native metrics only work with a warehouse-native exposure."- : undefined+ const { warehouseNative: warehouseNativeBlockedReason, posthog: posthogBlockedReason } =+ getExposureSourceBlockedReasons(+ !!warehouseExposure,+ metricDataSources ?? getExperimentMetricDataSources(experiment)+ )+ // A shared metric keeps its own connection and unit, so the save cannot move it to an edited exposure+ const saveDisabledReason = warehouseExposure+ ? (getWarehouseNativeExposureError(warehouseExposure) ??+ getWarehouseNativeMismatchError(+ warehouseExposure,+ { warehouseNative: false, posthog: false },+ (experiment.saved_metrics ?? []).map((savedMetric) => savedMetric?.query)+ ))+ : nullreturn (<LemonModal@@ -92,9 +100,7 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): JcloseExposureCriteriaModal()}}type="primary"- disabledReason={- warehouseExposure ? getWarehouseNativeExposureError(warehouseExposure) : undefined- }+ disabledReason={saveDisabledReason}>Save</LemonButton>@@ -215,27 +221,10 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): J)}</div>{warehouseExposure && (- <div className="mb-4 flex flex-col gap-4">- <WarehouseNativeConnectionFields- binding={warehouseExposure}- onChange={(binding) =>- setExposureCriteria({- ...exposureCriteria,- exposure_config: { ...warehouseExposure, ...binding },- })- }- />- <WarehouseNativeQueryEditor- label="Exposure query"- hint="Return one row per exposure with the columns entity_id, variant and timestamp. PostHog keeps the first exposure of each unit."- query={warehouseExposure.query}- onChange={(query) =>- setExposureCriteria({- ...exposureCriteria,- exposure_config: { ...warehouseExposure, query },- })- }- data-attr="experiment-warehouse-native-exposure-query"+ <div className="mb-4">+ <WarehouseNativeExposureFields+ exposure={warehouseExposure}+ onChange={(exposure) => setExposureCriteria({ ...exposureCriteria, exposure_config: exposure })}/></div>)}diff --git a/frontend/src/scenes/experiments/ExperimentWizard/ExperimentWizard.tsx b/frontend/src/scenes/experiments/ExperimentWizard/ExperimentWizard.tsxindex e549aafb2703..6ed9bc384e32 100644--- a/frontend/src/scenes/experiments/ExperimentWizard/ExperimentWizard.tsx+++ b/frontend/src/scenes/experiments/ExperimentWizard/ExperimentWizard.tsx@@ -14,7 +14,7 @@ import { AnalyticsStep } from './steps/AnalyticsStep'import { VariantsStep } from './steps/VariantsStep'export function ExperimentWizard(): JSX.Element {- const { currentStep, isLastStep, isFirstStep, isExperimentSubmitting, stepValidationErrors, hasFormErrors } =+ const { currentStep, isLastStep, isFirstStep, isExperimentSubmitting, stepValidationErrors, saveDisabledReason } =useValues(experimentWizardLogic)const { nextStep, prevStep, setStep, saveExperiment } = useActions(experimentWizardLogic)@@ -68,7 +68,7 @@ export function ExperimentWizard(): JSX.Element {type="primary"onClick={saveExperiment}loading={isExperimentSubmitting}- disabledReason={hasFormErrors ? 'Please fix all errors before saving' : undefined}+ disabledReason={saveDisabledReason}>Save as draft</LemonButton>diff --git a/frontend/src/scenes/experiments/ExperimentWizard/experimentWizardLogic.test.tsx b/frontend/src/scenes/experiments/ExperimentWizard/experimentWizardLogic.test.tsxindex 1362b5de592b..e302eab182e2 100644--- a/frontend/src/scenes/experiments/ExperimentWizard/experimentWizardLogic.test.tsx+++ b/frontend/src/scenes/experiments/ExperimentWizard/experimentWizardLogic.test.tsx@@ -13,6 +13,14 @@ import { featureFlagLogic } from 'lib/logic/featureFlagLogic'import { featureFlagsLogic } from 'scenes/feature-flags/featureFlagsLogic'import { useMocks } from '~/mocks/jest'+import {+ type ExperimentMetric,+ type ExperimentMetricSource,+ ExperimentMetricType,+ type ExperimentWarehouseNativeExposureConfig,+ ExperimentWarehouseNativeMathType,+ NodeKind,+} from '~/queries/schema/schema-general'import { initKeaTests } from '~/test/init'import { AccessControlLevel, AccessControlResourceType, type AppContext, type FeatureFlagType } from '~/types'@@ -107,6 +115,16 @@ const apiMocks = {},}+const warehouseExposure = (+ overrides: Partial<ExperimentWarehouseNativeExposureConfig> = {}+): ExperimentWarehouseNativeExposureConfig => ({+ kind: NodeKind.ExperimentWarehouseNativeExposureConfig,+ connection_id: 'connection-1',+ unit: { unit_type: 'customer', id_namespace: 'billing_customer_id' },+ query: 'SELECT customer_id AS entity_id, variant, assigned_at AS timestamp FROM billing.assignments',+ ...overrides,+})+describe('experimentWizardLogic', () => {describe('feature flag input flow', () => {let logic: ReturnType<typeof experimentWizardLogic.build>@@ -630,6 +648,63 @@ describe('experimentWizardLogic', () => {})})+ it('blocks the save while a warehouse-native exposure is unfinished, and reports the exposure source once', async () => {+ await expectLogic(logic, () => {+ logic.actions.setExposureCriteria({ exposure_config: warehouseExposure({ connection_id: '' }) })+ }).toDispatchActions([+ logic.actionCreators.reportExperimentWizardExposureSourceSelected('warehouse_native'),+ ])+ // The stepper stays clean until the user leaves the step, but the save is blocked right away+ expect(logic.values.stepValidationErrors.analytics).toEqual([])+ expect(logic.values.saveDisabledReason).toBe('Select a BigQuery connection')++ logic.actions.markStepDeparted('analytics')+ expect(logic.values.stepValidationErrors.analytics).toEqual(['Select a BigQuery connection'])++ // Filling in the exposure keeps the same source, so it is not a new selection+ await expectLogic(logic, () => {+ logic.actions.setExposureCriteria({ exposure_config: warehouseExposure() })+ }).toNotHaveDispatchedActions(['reportExperimentWizardExposureSourceSelected'])+ expect(logic.values.stepValidationErrors.analytics).toEqual([])+ expect(logic.values.saveDisabledReason).toBeUndefined()+ })++ it('blocks the save while a shared metric cannot run with the exposure', () => {+ const sharedMetric = (source: ExperimentMetricSource): ExperimentMetric =>+ ({+ kind: NodeKind.ExperimentMetric,+ uuid: 'shared-1',+ metric_type: ExperimentMetricType.MEAN,+ source,+ isSharedMetric: true,+ sharedMetricId: 7,+ }) as ExperimentMetric+ const exposure = warehouseExposure()+ const sharedWarehouseMetric = sharedMetric({+ kind: NodeKind.ExperimentWarehouseNativeNode,+ connection_id: exposure.connection_id,+ unit: exposure.unit,+ query: 'SELECT customer_id AS entity_id, paid_at AS timestamp FROM billing.payments',+ math: ExperimentWarehouseNativeMathType.TotalCount,+ })+ logic.actions.setExposureCriteria({ exposure_config: exposure })++ // The draft keeps shared metrics outside `experiment`, so they are easy to leave out of the check+ createLogic.actions.setSharedMetrics({+ primary: [],+ secondary: [sharedMetric({ kind: NodeKind.EventsNode, event: '$pageview' })],+ })+ expect(logic.values.metricDataSources).toEqual({ warehouseNative: false, posthog: true })+ expect(logic.values.saveDisabledReason).toMatch(/only works with warehouse-native metrics/)++ createLogic.actions.setSharedMetrics({ primary: [sharedWarehouseMetric], secondary: [] })+ expect(logic.values.saveDisabledReason).toBeUndefined()++ // An exposure edit moves the draft's own metrics, but a shared metric keeps its connection+ logic.actions.setExposureCriteria({ exposure_config: warehouseExposure({ connection_id: 'connection-2' }) })+ expect(logic.values.saveDisabledReason).toMatch(/shared metric uses another connection/)+ })+it('saveExperiment marks all steps as departed, revealing all errors', async () => {// Experiment has empty name and key — errors hidden until departureexpect(logic.values.stepValidationErrors.about).toEqual([])@@ -728,6 +803,52 @@ describe('experimentWizardLogic', () => {// Flag config no longer travels through the deprecated parameters keys.expect(capturedPayload.parameters).not.toHaveProperty('feature_flag_variants')})++ it('saves warehouse-native metrics on the connection and unit of the exposure as last edited', async () => {+ createLogic.actions.setExperimentValue('name', 'Ship it')+ createLogic.actions.setExperimentValue('feature_flag_key', 'ship-it')+ logic.actions.validateFeatureFlagKey('ship-it')+ await waitFor(() => {+ expect(createLogic.values.featureFlagKeyValidation).toMatchObject({ valid: true })+ })+ const exposure = warehouseExposure()+ logic.actions.setExposureCriteria({ exposure_config: exposure })+ logic.actions.setExperiment({+ ...createLogic.values.experiment,+ metrics: [+ {+ kind: NodeKind.ExperimentMetric,+ uuid: 'metric-1',+ metric_type: ExperimentMetricType.MEAN,+ source: {+ kind: NodeKind.ExperimentWarehouseNativeNode,+ connection_id: exposure.connection_id,+ unit: exposure.unit,+ query: 'SELECT customer_id AS entity_id, paid_at AS timestamp FROM billing.payments',+ math: ExperimentWarehouseNativeMathType.TotalCount,+ },+ },+ ],+ })+ // The metric was added first. The exposure then moves to another connection and unit.+ const editedExposure = warehouseExposure({+ connection_id: 'connection-2',+ unit: { unit_type: 'account', id_namespace: 'crm_account_id' },+ })+ logic.actions.setExposureCriteria({ exposure_config: editedExposure })++ logic.actions.saveExperiment()+ await waitFor(() => {+ expect(capturedPayload).not.toBeNull()+ })++ expect(capturedPayload.exposure_criteria.exposure_config).toEqual(editedExposure)+ expect(capturedPayload.metrics[0].source).toMatchObject({+ connection_id: 'connection-2',+ unit: { unit_type: 'account', id_namespace: 'crm_account_id' },+ query: 'SELECT customer_id AS entity_id, paid_at AS timestamp FROM billing.payments',+ })+ })})describe('VariantsStep with linked feature flag', () => {diff --git a/frontend/src/scenes/experiments/ExperimentWizard/experimentWizardLogic.ts b/frontend/src/scenes/experiments/ExperimentWizard/experimentWizardLogic.tsindex 34d5af759386..82e2d8923d81 100644--- a/frontend/src/scenes/experiments/ExperimentWizard/experimentWizardLogic.ts+++ b/frontend/src/scenes/experiments/ExperimentWizard/experimentWizardLogic.ts@@ -6,6 +6,13 @@ import type { Experiment, FeatureFlagType } from '~/types'import { NEW_EXPERIMENT } from 'products/experiments/frontend/constants'import { selectExistingFeatureFlagModalLogic } from 'products/experiments/frontend/modals/SelectExistingFeatureFlagModal/selectExistingFeatureFlagModalLogic'+import {+ type MetricDataSources,+ getMetricDataSources,+ getWarehouseNativeExposure,+ getWarehouseNativeExposureError,+ getWarehouseNativeMismatchError,+} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'import type {ExperimentExposureCriteria,@@ -16,6 +23,7 @@ import type { FeatureFlagFilters, MultivariateFlagVariant } from '../../../typesimport { createExperimentLogic } from '../ExperimentForm/createExperimentLogic'import type { FeatureFlagKeyValidation } from '../ExperimentForm/variantsPanelLogic'import { variantsPanelLogic } from '../ExperimentForm/variantsPanelLogic'+import { type ExposureSource, getExposureSource } from '../exposureContract'import { getExperimentVariants, getFlagVariants } from '../utils'export type ExperimentWizardStep = 'about' | 'variants' | 'analytics'@@ -70,8 +78,11 @@ export interface experimentWizardLogicValues {isFirstStep: booleanisLastStep: booleanlinkedFeatureFlag: FeatureFlagType | null+ metricDataSources: MetricDataSources+ saveDisabledReason: string | undefinedstepNumber: numberstepValidationErrors: Record<ExperimentWizardStep, string[]>+ warehouseNativeSourcesError: string | null}// Generated by kea-typegen. Update if you're an agent, ignore if you're human.@@ -117,6 +128,9 @@ export interface experimentWizardLogicActions {secondary: ExperimentMetricUnion[]}} // createExperimentLogic+ reportExperimentWizardExposureSourceSelected: (exposureSource: ExposureSource) => {+ exposureSource: ExposureSource+ } // eventUsageLogicreportExperimentWizardStarted: () => {value: true} // eventUsageLogic@@ -187,19 +201,40 @@ export interface experimentWizardLogicMeta {stepNumber: (currentStep: ExperimentWizardStep) => numberisLastStep: (currentStep: ExperimentWizardStep) => booleanisFirstStep: (currentStep: ExperimentWizardStep) => boolean+ metricDataSources: (+ experiment: Experiment & {+ feature_flag_filters?: FeatureFlagFilters+ },+ sharedMetrics: {+ primary: ExperimentMetric[]+ secondary: ExperimentMetric[]+ }+ ) => MetricDataSources+ warehouseNativeSourcesError: (+ experiment: Experiment & {+ feature_flag_filters?: FeatureFlagFilters+ },+ sharedMetrics: {+ primary: ExperimentMetric[]+ secondary: ExperimentMetric[]+ },+ metricDataSources: MetricDataSources+ ) => string | nullstepValidationErrors: (experiment: Experiment & {feature_flag_filters?: FeatureFlagFilters},featureFlagKeyValidation: FeatureFlagKeyValidation | null,linkedFeatureFlag: FeatureFlagType | null,- departedSteps: Record<string, boolean>+ departedSteps: Record<string, boolean>,+ warehouseNativeSourcesError: string | null) => Record<ExperimentWizardStep, string[]>currentStepHasErrors: (stepValidationErrors: Record<ExperimentWizardStep, string[]>,currentStep: ExperimentWizardStep) => booleanhasFormErrors: (stepValidationErrors: Record<ExperimentWizardStep, string[]>) => boolean+ saveDisabledReason: (warehouseNativeSourcesError: string | null, hasFormErrors: boolean) => string | undefined}}@@ -246,7 +281,11 @@ export const experimentWizardLogic = kea<experimentWizardLogicType>([selectExistingFeatureFlagModalLogic,['loadFeatureFlagsForAutocomplete', 'loadFeatureFlagsSuccess'],eventUsageLogic,- ['reportExperimentWizardStarted', 'reportExperimentWizardStepViewed'],+ [+ 'reportExperimentWizardStarted',+ 'reportExperimentWizardStepViewed',+ 'reportExperimentWizardExposureSourceSelected',+ ],],})),@@ -311,13 +350,52 @@ export const experimentWizardLogic = kea<experimentWizardLogicType>([(s) => [s.currentStep],(currentStep: ExperimentWizardStep): boolean => currentStep === WIZARD_STEPS[0],],+ // The draft keeps shared metrics outside `experiment`, as ids only+ metricDataSources: [+ (s) => [s.experiment, s.sharedMetrics],+ (+ experiment: Experiment,+ sharedMetrics: { primary: ExperimentMetric[]; secondary: ExperimentMetric[] }+ ): MetricDataSources =>+ getMetricDataSources([+ ...(experiment.metrics ?? []),+ ...(experiment.metrics_secondary ?? []),+ ...sharedMetrics.primary,+ ...sharedMetrics.secondary,+ ]),+ ],+ // Why the API would reject the exposure and the metrics of the draft, or null+ warehouseNativeSourcesError: [+ (s) => [s.experiment, s.sharedMetrics, s.metricDataSources],+ (+ experiment: Experiment,+ sharedMetrics: { primary: ExperimentMetric[]; secondary: ExperimentMetric[] },+ metricDataSources: MetricDataSources+ ): string | null => {+ const warehouseExposure = getWarehouseNativeExposure(experiment.exposure_criteria)+ return (+ (warehouseExposure && getWarehouseNativeExposureError(warehouseExposure)) ??+ getWarehouseNativeMismatchError(warehouseExposure, metricDataSources, [+ ...sharedMetrics.primary,+ ...sharedMetrics.secondary,+ ])+ )+ },+ ],stepValidationErrors: [- (s) => [s.experiment, s.featureFlagKeyValidation, s.linkedFeatureFlag, s.departedSteps],+ (s) => [+ s.experiment,+ s.featureFlagKeyValidation,+ s.linkedFeatureFlag,+ s.departedSteps,+ s.warehouseNativeSourcesError,+ ],(experiment: Experiment,featureFlagKeyValidation: FeatureFlagKeyValidation | null,linkedFeatureFlag: FeatureFlagType | null,- departedSteps: Record<string, boolean>+ departedSteps: Record<string, boolean>,+ warehouseNativeSourcesError: string | null): Record<ExperimentWizardStep, string[]> => {const errors: Record<ExperimentWizardStep, string[]> = {about: [],@@ -336,6 +414,10 @@ export const experimentWizardLogic = kea<experimentWizardLogicType>([}}+ if (departedSteps.analytics && warehouseNativeSourcesError) {+ errors.analytics.push(warehouseNativeSourcesError)+ }+if ((experiment.description?.length ?? 0) > 3000) {errors.about.push('Hypothesis must be 3000 characters or fewer')}@@ -379,9 +461,21 @@ export const experimentWizardLogic = kea<experimentWizardLogicType>([(errors: Record<ExperimentWizardStep, string[]>): boolean =>WIZARD_STEPS.some((step) => errors[step]?.length > 0),],+ // An exposure that the API rejects blocks the save before the stepper marks the step+ saveDisabledReason: [+ (s) => [s.warehouseNativeSourcesError, s.hasFormErrors],+ (warehouseNativeSourcesError: string | null, hasFormErrors: boolean): string | undefined =>+ warehouseNativeSourcesError ?? (hasFormErrors ? 'Please fix all errors before saving' : undefined),+ ],}),listeners(({ actions, values, selectors }) => ({+ setExposureCriteria: (_, __, ___, previousState) => {+ const exposureSource = getExposureSource(values.experiment.exposure_criteria)+ if (exposureSource !== getExposureSource(selectors.experiment(previousState).exposure_criteria)) {+ actions.reportExperimentWizardExposureSourceSelected(exposureSource)+ }+ },_applyStep: ({ step, navigation }, _, __, previousState) => {try {sessionStorage.setItem(stepStorageKey(), step)diff --git a/frontend/src/scenes/experiments/ExperimentWizard/steps/AnalyticsStep.tsx b/frontend/src/scenes/experiments/ExperimentWizard/steps/AnalyticsStep.tsxindex b6e997fe4e10..7ddb138e70b3 100644--- a/frontend/src/scenes/experiments/ExperimentWizard/steps/AnalyticsStep.tsx+++ b/frontend/src/scenes/experiments/ExperimentWizard/steps/AnalyticsStep.tsx@@ -11,7 +11,7 @@ import { experimentWizardLogic } from '../experimentWizardLogic'import { ReplayVisionScannerOption } from './ReplayVisionScannerOption'export function AnalyticsStep(): JSX.Element {- const { experiment, linkedFeatureFlag, sharedMetrics } = useValues(experimentWizardLogic)+ const { experiment, linkedFeatureFlag, sharedMetrics, metricDataSources } = useValues(experimentWizardLogic)const { setExperiment, setExposureCriteria, setSharedMetrics } = useActions(experimentWizardLogic)const experimentScanners = useFeatureFlag('VISION_EXPERIMENT_SCANNER')@@ -24,6 +24,7 @@ export function AnalyticsStep(): JSX.Element {experiment={experiment}featureFlag={linkedFeatureFlag}onChange={setExposureCriteria}+ metricDataSources={metricDataSources}compact/></div>@@ -44,6 +45,7 @@ export function AnalyticsStep(): JSX.Element {<MetricsPanelexperiment={experiment}sharedMetrics={sharedMetrics}+ metricDataSources={metricDataSources}compactonSaveMetric={(metric, context) => {const isNew = !experiment[context.field].some((m) => m.uuid === metric.uuid)diff --git a/frontend/src/scenes/experiments/experimentLogic.tsx b/frontend/src/scenes/experiments/experimentLogic.tsxindex e315f9fdb96f..0e40668093cd 100644--- a/frontend/src/scenes/experiments/experimentLogic.tsx+++ b/frontend/src/scenes/experiments/experimentLogic.tsx@@ -113,7 +113,7 @@ import {import type { ExperimentMetricUnion } from '../../queries/schema/schema-general'import type { ExperimentHoldoutType, ExperimentIdType } from '../../types'import { experimentMetricsLogic } from './experimentMetricsLogic'-import { featureFlagVariantProperty, getActivationConfig, resolvedExposureEvent } from './exposureContract'+import { featureFlagVariantProperty, getExposureSource, resolvedExposureEvent } from './exposureContract'import { holdoutsLogic } from './holdoutsLogic'import {addExposureToMetric,@@ -1971,13 +1971,7 @@ export const experimentLogic = kea<experimentLogicType>([// pinned: analytics event name and property values. A rename breaks dashboards.posthog.capture('experiment exposure criteria updated', {experiment_id: values.experimentId,- exposure_source: warehouseExposure- ? 'warehouse_native'- : getActivationConfig(exposureCriteria)- ? 'activation'- : exposureCriteria?.exposure_config- ? 'custom'- : 'default',+ exposure_source: getExposureSource(exposureCriteria),})actions.refreshExperimentResults(true, 'experiment_config_change')},diff --git a/frontend/src/scenes/experiments/exposureContract.ts b/frontend/src/scenes/experiments/exposureContract.tsindex 4cd929e4773e..b9a31f2532ef 100644--- a/frontend/src/scenes/experiments/exposureContract.ts+++ b/frontend/src/scenes/experiments/exposureContract.ts@@ -156,6 +156,19 @@ export function getActivationConfig(return activationConfig}+export type ExposureSource = 'warehouse_native' | 'activation' | 'custom' | 'default'++/** What decides who enters the analysis: a warehouse-native query, an activation event, a custom event, or the default event. */+export function getExposureSource(exposureCriteria: ExperimentExposureCriteria | null | undefined): ExposureSource {+ if (exposureCriteria?.exposure_config?.kind === NodeKind.ExperimentWarehouseNativeExposureConfig) {+ return 'warehouse_native'+ }+ if (getActivationConfig(exposureCriteria ?? undefined)) {+ return 'activation'+ }+ return exposureCriteria?.exposure_config ? 'custom' : 'default'+}+/*** Determines which event and variant property carry the exposure for an experiment.*diff --git a/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.test.tsx b/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.test.tsxnew file mode 100644index 000000000000..c33904cc3569--- /dev/null+++ b/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.test.tsx@@ -0,0 +1,57 @@+import '@testing-library/jest-dom'++import { cleanup, render, screen } from '@testing-library/react'+import userEvent from '@testing-library/user-event'++import { ExperimentMetricType, NodeKind } from '~/queries/schema/schema-general'+import { initKeaTests } from '~/test/init'++import {+ METRIC_CONTEXTS,+ experimentMetricModalLogic,+} from 'products/experiments/frontend/modals/ExperimentMetricModal/experimentMetricModalLogic'++import { MetricSourceModal } from './MetricSourceModal'+import { metricSourceModalLogic } from './metricSourceModalLogic'++describe('MetricSourceModal', () => {+ beforeEach(() => {+ initKeaTests()+ metricSourceModalLogic.mount()+ experimentMetricModalLogic.mount()+ })++ afterEach(() => {+ cleanup()+ })++ it('starts a warehouse-native metric when the experiment it is given has a warehouse-native exposure', async () => {+ const unit = { unit_type: 'customer', id_namespace: 'billing_customer_id' }+ // The wizard draft lives outside experimentLogic, so the modal must read the experiment it is given+ render(+ <MetricSourceModal+ experiment={{+ metrics: [],+ metrics_secondary: [],+ start_date: undefined,+ exposure_criteria: {+ exposure_config: {+ kind: NodeKind.ExperimentWarehouseNativeExposureConfig,+ connection_id: 'connection-1',+ unit,+ query: 'SELECT customer_id AS entity_id, variant, assigned_at AS timestamp FROM billing.assignments',+ },+ },+ }}+ />+ )+ metricSourceModalLogic.actions.openMetricSourceModal(METRIC_CONTEXTS.primary)++ await userEvent.click(await screen.findByText('Single-use'))++ expect(experimentMetricModalLogic.values.metric).toMatchObject({+ metric_type: ExperimentMetricType.MEAN,+ source: { kind: NodeKind.ExperimentWarehouseNativeNode, connection_id: 'connection-1', unit, query: '' },+ })+ })+})diff --git a/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.tsx b/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.tsxindex b2ab34789831..ce3a1a3bf5af 100644--- a/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.tsx+++ b/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.tsx@@ -3,9 +3,10 @@ import { useActions, useValues } from 'kea'import { IconWarning } from '@posthog/icons'import { LemonModal, Link } from '@posthog/lemon-ui'-import { experimentLogic } from 'scenes/experiments/experimentLogic'import { getDefaultFunnelMetric } from 'scenes/experiments/utils'+import type { Experiment } from '~/types'+import { experimentMetricModalLogic } from 'products/experiments/frontend/modals/ExperimentMetricModal/experimentMetricModalLogic'import { sharedMetricModalLogic } from 'products/experiments/frontend/modals/SharedMetricModal/sharedMetricModalLogic'import {@@ -17,15 +18,18 @@ import { metricSourceModalLogic } from './metricSourceModalLogic'const METRIC_COUNT_WARNING_THRESHOLD = 3-export const MetricSourceModal = (): JSX.Element | null => {+export const MetricSourceModal = ({+ experiment,+}: {+ /** The experiment that receives the metric. The wizard passes its draft, which `experimentLogic` does not hold. */+ experiment: Pick<Experiment, 'metrics' | 'metrics_secondary' | 'start_date' | 'exposure_criteria'>+}): JSX.Element | null => {const { isModalOpen, context } = useValues(metricSourceModalLogic)const { closeMetricSourceModal } = useActions(metricSourceModalLogic)const { openExperimentMetricModal, setMetric } = useActions(experimentMetricModalLogic)const { openSharedMetricModal } = useActions(sharedMetricModalLogic)- const { experiment } = useValues(experimentLogic)-const metricCount =context.field === 'metrics' ? (experiment.metrics?.length ?? 0) : (experiment.metrics_secondary?.length ?? 0)diff --git a/products/experiments/frontend/warehouseNative/WarehouseNativeExposureFields.stories.tsx b/products/experiments/frontend/warehouseNative/WarehouseNativeExposureFields.stories.tsxnew file mode 100644index 000000000000..76d4aff49a9c--- /dev/null+++ b/products/experiments/frontend/warehouseNative/WarehouseNativeExposureFields.stories.tsx@@ -0,0 +1,53 @@+import type { Meta, StoryObj } from '@storybook/react'++import { mswDecorator } from '~/mocks/browser'+import { type ExperimentWarehouseNativeExposureConfig, NodeKind } from '~/queries/schema/schema-general'++import { WarehouseNativeExposureFields } from './WarehouseNativeExposureFields'++const EXPOSURE: ExperimentWarehouseNativeExposureConfig = {+ kind: NodeKind.ExperimentWarehouseNativeExposureConfig,+ connection_id: '0190a3c5-0000-0000-0000-000000000001',+ unit: { unit_type: 'customer', id_namespace: 'billing_customer_id' },+ query: 'SELECT customer_id AS entity_id, variant, assigned_at AS timestamp\nFROM billing.experiment_assignments',+}++const meta: Meta<typeof WarehouseNativeExposureFields> = {+ title: 'Experiments/WarehouseNativeExposureFields',+ component: WarehouseNativeExposureFields,+ args: {+ exposure: EXPOSURE,+ onChange: () => {},+ },+ parameters: {+ testOptions: { waitForSelector: '.monaco-editor .view-line' },+ },+ decorators: [+ (Story) => (+ <div className="max-w-prose">+ <Story />+ </div>+ ),+ mswDecorator({+ get: {+ '/api/projects/:team_id/external_data_sources/connections/': [+ {+ id: EXPOSURE.connection_id,+ schema_name: null,+ prefix: 'analytics',+ engine: null,+ source_type: 'BigQuery',+ access_method: 'direct',+ supports_hogql: false,+ is_builtin_managed_warehouse: false,+ description: 'Analytics warehouse',+ },+ ],+ '/api/projects/:team_id/external_data_sources/direct_connection_options/': [],+ },+ }),+ ],+}+export default meta++export const ExposureQuery: StoryObj<typeof WarehouseNativeExposureFields> = {}diff --git a/products/experiments/frontend/warehouseNative/WarehouseNativeExposureFields.tsx b/products/experiments/frontend/warehouseNative/WarehouseNativeExposureFields.tsxnew file mode 100644index 000000000000..952f88971a24--- /dev/null+++ b/products/experiments/frontend/warehouseNative/WarehouseNativeExposureFields.tsx@@ -0,0 +1,27 @@+import type { ExperimentWarehouseNativeExposureConfig } from '~/queries/schema/schema-general'++import { WarehouseNativeConnectionFields } from './WarehouseNativeConnectionFields'+import { WarehouseNativeQueryEditor } from './WarehouseNativeQueryEditor'++export type WarehouseNativeExposureFieldsProps = {+ exposure: ExperimentWarehouseNativeExposureConfig+ onChange: (exposure: ExperimentWarehouseNativeExposureConfig) => void+}++export function WarehouseNativeExposureFields({ exposure, onChange }: WarehouseNativeExposureFieldsProps): JSX.Element {+ return (+ <div className="flex flex-col gap-4">+ <WarehouseNativeConnectionFields+ binding={exposure}+ onChange={(binding) => onChange({ ...exposure, ...binding })}+ />+ <WarehouseNativeQueryEditor+ label="Exposure query"+ hint="Return one row per exposure with the columns entity_id, variant and timestamp. PostHog keeps the first exposure of each unit."+ query={exposure.query}+ onChange={(query) => onChange({ ...exposure, query })}+ data-attr="experiment-warehouse-native-exposure-query"+ />+ </div>+ )+}diff --git a/products/experiments/frontend/warehouseNative/warehouseNativeSources.test.ts b/products/experiments/frontend/warehouseNative/warehouseNativeSources.test.tsindex 1ed7436e64ed..d97ea204f993 100644--- a/products/experiments/frontend/warehouseNative/warehouseNativeSources.test.ts+++ b/products/experiments/frontend/warehouseNative/warehouseNativeSources.test.ts@@ -11,7 +11,9 @@ import {alignMetricsWithExposure,experimentUsesWarehouseNativeSources,getExperimentMetricDataSources,+ getExposureSourceBlockedReasons,getWarehouseNativeMetricError,+ getWarehouseNativeMismatchError,toWarehouseNativeMetric,} from './warehouseNativeSources'@@ -137,4 +139,37 @@ describe('warehouseNativeSources', () => {expect(experimentUsesWarehouseNativeSources(full)).toBe(usesWarehouse)expect(getExperimentMetricDataSources(full)).toEqual(metricDataSources)})++ it.each([+ ['an event exposure with event metrics', false, { warehouseNative: false, posthog: true }, 'warehouseNative'],+ ['a warehouse-native exposure with its metrics', true, { warehouseNative: true, posthog: false }, 'posthog'],+ ['an exposure with no metrics', true, { warehouseNative: false, posthog: false }, null],+ ] as const)('blocks the other data source for %s', (_, exposureIsWarehouseNative, metricDataSources, blocked) => {+ const reasons = getExposureSourceBlockedReasons(exposureIsWarehouseNative, metricDataSources)++ expect(!!reasons.warehouseNative).toBe(blocked === 'warehouseNative')+ expect(!!reasons.posthog).toBe(blocked === 'posthog')+ })++ it.each([+ ['a shared metric on the same connection and unit', warehouseSource(), null],+ ['a shared metric on another connection', warehouseSource({ connection_id: 'connection-2' }), /shared metric/],+ [+ 'a shared metric on another unit',+ warehouseSource({ unit: { unit_type: 'account', id_namespace: 'crm_account_id' } }),+ /shared metric/,+ ],+ ])('checks a warehouse-native exposure against %s', (_, sharedSource, expected) => {+ const exposure = { ...warehouseSource(), kind: NodeKind.ExperimentWarehouseNativeExposureConfig as const }++ const error = getWarehouseNativeMismatchError(exposure, { warehouseNative: true, posthog: false }, [+ meanMetric(sharedSource),+ ])++ if (expected) {+ expect(error).toMatch(expected)+ } else {+ expect(error).toBeNull()+ }+ })})diff --git a/products/experiments/frontend/warehouseNative/warehouseNativeSources.ts b/products/experiments/frontend/warehouseNative/warehouseNativeSources.tsindex 8a270bb802d4..0e77206ce58e 100644--- a/products/experiments/frontend/warehouseNative/warehouseNativeSources.ts+++ b/products/experiments/frontend/warehouseNative/warehouseNativeSources.ts@@ -58,23 +58,50 @@ export function getWarehouseNativeBinding(metric: ExperimentMetric): WarehouseNareturn source ? { connection_id: source.connection_id, unit: source.unit } : EMPTY_WAREHOUSE_NATIVE_BINDING}+export type MetricDataSources = { warehouseNative: boolean; posthog: boolean }++/** Which data sources the metric queries read. A query of another kind than `ExperimentMetric` reads PostHog data. */+export function getMetricDataSources(queries: ({ kind: NodeKind } | null | undefined)[]): MetricDataSources {+ const present = queries.filter((query): query is { kind: NodeKind } => !!query)+ const warehouseNative = present.filter(+ (query) =>+ query.kind === NodeKind.ExperimentMetric && metricUsesWarehouseNativeSource(query as ExperimentMetric)+ )+ return { warehouseNative: warehouseNative.length > 0, posthog: warehouseNative.length < present.length }+}+/*** Which data sources the metrics of the experiment read, shared metrics included. The API accepts* warehouse-native metrics only under a warehouse-native exposure, and no other metric under one.*/export function getExperimentMetricDataSources(experiment: Pick<Experiment, 'metrics' | 'metrics_secondary' | 'saved_metrics'>-): { warehouseNative: boolean; posthog: boolean } {- const queries = [+): MetricDataSources {+ return getMetricDataSources([...(experiment.metrics ?? []),...(experiment.metrics_secondary ?? []),...(experiment.saved_metrics ?? []).map((savedMetric) => savedMetric?.query),- ].filter(Boolean)- const warehouseNative = queries.filter(- (query) =>- query.kind === NodeKind.ExperimentMetric && metricUsesWarehouseNativeSource(query as ExperimentMetric)- )- return { warehouseNative: warehouseNative.length > 0, posthog: warehouseNative.length < queries.length }+ ])+}++/**+ * Why the exposure cannot change to the other data source. The API rejects an exposure that reads+ * another data source than the metrics, so the metrics must go first.+ */+export function getExposureSourceBlockedReasons(+ exposureIsWarehouseNative: boolean,+ metricDataSources: MetricDataSources+): { warehouseNative?: string; posthog?: string } {+ return {+ warehouseNative:+ !exposureIsWarehouseNative && metricDataSources.posthog+ ? "Remove this experiment's metrics first. A warehouse-native exposure only works with warehouse-native metrics."+ : undefined,+ posthog:+ exposureIsWarehouseNative && metricDataSources.warehouseNative+ ? "Remove this experiment's metrics first. Warehouse-native metrics only work with a warehouse-native exposure."+ : undefined,+ }}export function experimentUsesWarehouseNativeSources(@@ -131,6 +158,14 @@ export function toWarehouseNativeMetric(}}+const metricMatchesBinding = (metric: ExperimentMetric, binding: WarehouseNativeBinding): boolean =>+ getWarehouseNativeSources(metric).every(+ (source) =>+ source.connection_id === binding.connection_id &&+ source.unit.unit_type === binding.unit.unit_type &&+ source.unit.id_namespace === binding.unit.id_namespace+ )+/*** Copies the exposure's connection and unit onto the warehouse-native metrics. The API rejects an* exposure that differs from the metrics, so a changed exposure must save together with them.@@ -145,16 +180,38 @@ export function alignMetricsWithExposure<T extends { kind: NodeKind }>(return metric}const experimentMetric = metric as unknown as ExperimentMetric- const aligned = getWarehouseNativeSources(experimentMetric).every(- (source) =>- source.connection_id === exposure.connection_id &&- source.unit.unit_type === exposure.unit.unit_type &&- source.unit.id_namespace === exposure.unit.id_namespace- )- return aligned ? metric : (toWarehouseNativeMetric(experimentMetric, exposure) as unknown as T)+ return metricMatchesBinding(experimentMetric, exposure)+ ? metric+ : (toWarehouseNativeMetric(experimentMetric, exposure) as unknown as T)})}+/**+ * Why the API would reject the exposure together with the metrics, or null. A shared metric keeps+ * its own connection and unit, so `alignMetricsWithExposure` cannot move it to a changed exposure.+ */+export function getWarehouseNativeMismatchError(+ exposure: ExperimentWarehouseNativeExposureConfig | null,+ metricDataSources: MetricDataSources,+ sharedMetricQueries: ({ kind: NodeKind } | null | undefined)[]+): string | null {+ if (!exposure) {+ return metricDataSources.warehouseNative+ ? 'Warehouse-native metrics need a warehouse-native exposure. Remove them, or change the exposure.'+ : null+ }+ if (metricDataSources.posthog) {+ return 'A warehouse-native exposure only works with warehouse-native metrics. Remove the other metrics, or change the exposure.'+ }+ const sharedMetricDiffers = sharedMetricQueries.some(+ (query) =>+ query?.kind === NodeKind.ExperimentMetric && !metricMatchesBinding(query as ExperimentMetric, exposure)+ )+ return sharedMetricDiffers+ ? 'A shared metric uses another connection, unit or ID type than the exposure. Remove it, or match the exposure to it.'+ : null+}+const getBindingError = (binding: WarehouseNativeBinding): string | null => {if (!binding.connection_id) {return 'Select a BigQuery connection'