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(experiments): Add warehouse-native queries to the metric and exposure forms (#130010)

Juraj Majerikec1691dfb24b
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 78335087f8e6..0e737c603d3d 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -3472,6 +3472,10 @@ snapshots:
hash: v1.k794b7964.47d21cdf33f368b06633d317125603fc69b5675d9389c038b40c824e73f1a405.0ufZrtGmQpexxX1aaLjAVmf3-YVGmHd7p8-HB9cbQjk
experiments-varianttimeseriestooltip--significant-day--light:
hash: v1.k794b7964.6e850075cec396cda57b4d44acd24b1fb4deb1e00c640db0049d172e8d99f2bb.Im0tdmWO-QcJ_sukw7HEC5eSF0ZBsTJgYvKKf-KxnLA
+ experiments-warehousenativemetricfields--shared-ratio-metric--dark:
+ hash: v1.k794b7964.725376bdb2387961defbc6e14cb786936236c46383110b60684b0d34bb8810a5.ecVYEPkV-p8nvLY497gnhBIaCyiN8b8QiYtDswjYsr4
+ experiments-warehousenativemetricfields--shared-ratio-metric--light:
+ hash: v1.k794b7964.1dad4255c61b319bf1b34dbf62dcd94d8b56aaee7c7f402d0ead4cf8aa975c39.N-G_KGAYFb2uWzCK3TB-QLepx-lHc3N_x3ghDRvwm2c
exporter-funnels--funnel-time-to-convert-insight--dark:
hash: v1.k794b7964.32e0fdd763e52aa74a859cc74be91c34aa9bc415f698cd276a1cbf5165e07e96.83HRBJWS8NFTguzPf-6KQ58qPcfF8byzAZ6DrhaMS4M
exporter-funnels--funnel-time-to-convert-insight--light:
diff --git a/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricCard.tsx b/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricCard.tsx
index 9a56e4f67a1d..5a692923d481 100644
--- a/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricCard.tsx
+++ b/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricCard.tsx
@@ -11,6 +11,7 @@ import { getDefaultMetricTitle, getMetricTag } from '~/scenes/experiments/Metric
import type { MetricContext } from 'products/experiments/frontend/modals/ExperimentMetricModal/experimentMetricModalLogic'
import { experimentMetricModalLogic } from 'products/experiments/frontend/modals/ExperimentMetricModal/experimentMetricModalLogic'
+import { metricUsesWarehouseNativeSource } from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
import { MetricConversionWindow } from './MetricConversionWindow'
import { MetricEventDetails } from './MetricEventDetails'
@@ -98,7 +99,9 @@ export const MetricCard = ({ metric, metricContext, onDelete, filterTestAccounts
<MetricOutlierHandling metric={metric} />
</div>
- <MetricRecentActivity metric={metric} filterTestAccounts={filterTestAccounts} />
+ {!metricUsesWarehouseNativeSource(metric) && (
+ <MetricRecentActivity metric={metric} filterTestAccounts={filterTestAccounts} />
+ )}
</div>
</div>
</div>
diff --git a/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricEventDetails.tsx b/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricEventDetails.tsx
index ce0f60059516..722e1e61f700 100644
--- a/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricEventDetails.tsx
+++ b/frontend/src/scenes/experiments/ExperimentForm/MetricsPanel/MetricEventDetails.tsx
@@ -18,11 +18,14 @@ import {
isExperimentRatioMetric,
} from '~/queries/schema/schema-general'
+import { isWarehouseNativeSource } from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
+
const getSourceName = (source: ExperimentMetricSource): string =>
match(source)
.when(isEventsNode, (node) => node.name || node.event || 'Unknown event')
.when(isActionsNode, (node) => node.name || `Action ${node.id}`)
.when(isDataWarehouseNode, (node) => node.name || node.table_name)
+ .when(isWarehouseNativeSource, (node) => node.custom_name || 'Warehouse-native query')
.otherwise(() => 'Unknown')
const getMathLabel = (math: string | undefined): string => {
diff --git a/frontend/src/scenes/experiments/ExperimentMetricForm.test.tsx b/frontend/src/scenes/experiments/ExperimentMetricForm.test.tsx
index 02283250c567..c02c5f89030c 100644
--- a/frontend/src/scenes/experiments/ExperimentMetricForm.test.tsx
+++ b/frontend/src/scenes/experiments/ExperimentMetricForm.test.tsx
@@ -4,7 +4,13 @@ import { render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import type { ReactNode } from 'react'
-import { ExperimentMetric, ExperimentMetricType, NodeKind } from '~/queries/schema/schema-general'
+import {
+ ExperimentMetric,
+ ExperimentMetricType,
+ ExperimentWarehouseNativeExposureConfig,
+ ExperimentWarehouseNativeMathType,
+ NodeKind,
+} from '~/queries/schema/schema-general'
import { ExperimentMetricMathType, FunnelConversionWindowTimeUnit } from '~/types'
import { ExperimentMetricForm } from './ExperimentMetricForm'
@@ -40,6 +46,10 @@ jest.mock('scenes/insights/filters/ActionFilter/ActionFilter', () => ({
),
}))
+jest.mock('lib/monaco/CodeEditorResizable', () => ({
+ CodeEditorResizeable: ({ value }: { value: string }) => <pre>{value}</pre>,
+}))
+
describe('ExperimentMetricForm', () => {
it('updates a funnel metric from the data warehouse popover fields', async () => {
const metric: ExperimentMetric = {
@@ -138,4 +148,56 @@ describe('ExperimentMetricForm', () => {
conversion_window_unit: FunnelConversionWindowTimeUnit.Day,
})
})
+
+ it('edits a warehouse-native metric and keeps every source warehouse-native when its type changes', async () => {
+ const exposure: 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, exposed_at AS timestamp FROM billing.exposures',
+ }
+ const source = {
+ kind: NodeKind.ExperimentWarehouseNativeNode as const,
+ 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,
+ }
+ const metric: ExperimentMetric = {
+ kind: NodeKind.ExperimentMetric,
+ uuid: 'metric-1',
+ metric_type: ExperimentMetricType.MEAN,
+ source,
+ }
+ const handleSetMetric = jest.fn()
+
+ const { container } = render(
+ <ExperimentMetricForm
+ metric={metric}
+ handleSetMetric={handleSetMetric}
+ filterTestAccounts={false}
+ exposureCriteria={{ exposure_config: exposure }}
+ />
+ )
+
+ expect(screen.getByText(source.query)).toBeInTheDocument()
+ expect(container.querySelector('[data-attr="experiment-metric-type-funnel"]')).toBeDisabled()
+
+ // a click on the selected data source must not reset the metric
+ await userEvent.click(
+ container.querySelector('[data-attr="experiment-metric-data-source-warehouse-native"]') as HTMLElement
+ )
+ expect(handleSetMetric).not.toHaveBeenCalled()
+
+ await userEvent.click(container.querySelector('[data-attr="experiment-metric-type-ratio"]') as HTMLElement)
+
+ expect(handleSetMetric).toHaveBeenCalledWith(
+ expect.objectContaining({
+ uuid: 'metric-1',
+ metric_type: ExperimentMetricType.RATIO,
+ numerator: source,
+ denominator: { ...source, query: '' },
+ })
+ )
+ })
})
diff --git a/frontend/src/scenes/experiments/ExperimentMetricForm.tsx b/frontend/src/scenes/experiments/ExperimentMetricForm.tsx
index b501a56e9cf1..2c89ebd74ee1 100644
--- a/frontend/src/scenes/experiments/ExperimentMetricForm.tsx
+++ b/frontend/src/scenes/experiments/ExperimentMetricForm.tsx
@@ -4,9 +4,11 @@ import { IconInfo, IconPencil } from '@posthog/icons'
import { LemonBanner, LemonInput } from '@posthog/lemon-ui'
import { DataWarehousePopoverField } from 'lib/components/TaxonomicFilter/types'
+import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { IconOpenInNew } from 'lib/lemon-ui/icons'
import { LemonLabel } from 'lib/lemon-ui/LemonLabel'
import { LemonRadio } from 'lib/lemon-ui/LemonRadio'
+import { LemonSegmentedButton } from 'lib/lemon-ui/LemonSegmentedButton'
import { LemonSelect } from 'lib/lemon-ui/LemonSelect'
import { LemonTag } from 'lib/lemon-ui/LemonTag'
import { lemonToast } from 'lib/lemon-ui/LemonToast'
@@ -42,6 +44,15 @@ import {
FunnelConversionWindowTimeUnit,
} from '~/types'
+import { WarehouseNativeMetricFields } from 'products/experiments/frontend/warehouseNative/WarehouseNativeMetricFields'
+import {
+ EMPTY_WAREHOUSE_NATIVE_BINDING,
+ getWarehouseNativeBinding,
+ getWarehouseNativeExposure,
+ metricUsesWarehouseNativeSource,
+ toWarehouseNativeMetric,
+} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
+
import { ExperimentMetricConversionWindowFilter } from './ExperimentMetricConversionWindowFilter'
import { ExperimentMetricFunnelOrderSelector } from './ExperimentMetricFunnelOrderSelector'
import {
@@ -159,6 +170,11 @@ export function ExperimentMetricForm({
}): JSX.Element {
const mathAvailability = getMathAvailability(metric.metric_type)
const allowedMathTypes = getAllowedMathTypes(metric.metric_type)
+ const warehouseNativeEnabled = useFeatureFlag('BIGQUERY_DIRECT_QUERY')
+ const warehouseExposure = getWarehouseNativeExposure(exposureCriteria)
+ const usesWarehouseNative = metricUsesWarehouseNativeSource(metric)
+ // Keep an existing warehouse-native metric editable even if the team is no longer flagged in
+ const showDataSource = warehouseNativeEnabled || usesWarehouseNative || !!warehouseExposure
const [eventCount, setEventCount] = useState<number | null>(null)
const [isLoading, setIsLoading] = useState(false)
// holds the custom start configuration while the exposure sentinel is selected, so switching back restores it
@@ -240,36 +256,69 @@ export function ExperimentMetricForm({
}
}
- handleSetMetric({
+ const typedMetric = {
...newMetric,
// Keep the current uuid and name
uuid: metric.uuid,
name: metric.name,
- })
+ }
+ // A new metric type starts with event sources, which a warehouse-native metric cannot mix in
+ handleSetMetric(
+ usesWarehouseNative
+ ? toWarehouseNativeMetric(typedMetric, warehouseExposure ?? getWarehouseNativeBinding(metric))
+ : typedMetric
+ )
}
+ const handleDataSourceChange = (dataSource: 'posthog' | 'warehouse_native'): void => {
+ // The segmented button also reports a click on the selected option, which must not reset the metric
+ if ((dataSource === 'warehouse_native') === usesWarehouseNative) {
+ return
+ }
+ if (dataSource === 'warehouse_native') {
+ handleSetMetric(toWarehouseNativeMetric(metric, warehouseExposure ?? EMPTY_WAREHOUSE_NATIVE_BINDING))
+ } else {
+ handleSetMetric({
+ ...getDefaultExperimentMetric(metric.metric_type),
+ uuid: metric.uuid,
+ name: metric.name,
+ goal: metric.goal,
+ })
+ }
+ }
+
+ const warehouseNativeTypeReason = usesWarehouseNative
+ ? 'A warehouse-native metric can be a mean or a ratio.'
+ : undefined
+
const radioOptions = [
{
value: ExperimentMetricType.FUNNEL,
label: 'Funnel',
+ 'data-attr': 'experiment-metric-type-funnel',
+ disabledReason: warehouseNativeTypeReason,
description:
'Calculates the percentage of users exposed to the experiment who completed the funnel. Useful for measuring conversion rates.',
},
{
value: ExperimentMetricType.MEAN,
label: 'Mean',
+ 'data-attr': 'experiment-metric-type-mean',
description:
'Calculates the value per user exposed to the experiment. Useful for measuring count of clicks, revenue or other numeric values.',
},
{
value: ExperimentMetricType.RATIO,
label: 'Ratio',
+ 'data-attr': 'experiment-metric-type-ratio',
description:
'Calculates the ratio between two metrics. Useful when you want to use a different denominator than users exposed to the experiment.',
},
{
value: ExperimentMetricType.RETENTION,
label: 'Retention',
+ 'data-attr': 'experiment-metric-type-retention',
+ disabledReason: warehouseNativeTypeReason,
description:
'Calculates the retention rate of users exposed to the experiment. Useful for measuring the percentage of users who return to the app after a certain period of time.',
},
@@ -303,6 +352,45 @@ export function ExperimentMetricForm({
return (
<SceneContent>
+ {showDataSource && (
+ <>
+ <SceneSection title="Data source" className="max-w-prose">
+ <div className="flex flex-col gap-2">
+ <div>
+ <LemonSegmentedButton
+ size="small"
+ value={usesWarehouseNative ? 'warehouse_native' : 'posthog'}
+ onChange={handleDataSourceChange}
+ options={[
+ {
+ value: 'posthog',
+ label: 'PostHog data',
+ disabledReason: warehouseExposure
+ ? "This experiment's exposure is a warehouse-native query, so its metrics must be warehouse-native too."
+ : undefined,
+ 'data-attr': 'experiment-metric-data-source-posthog',
+ },
+ {
+ value: 'warehouse_native',
+ label: 'Warehouse-native',
+ disabledReason:
+ !isSharedMetric && !warehouseExposure
+ ? "Set this experiment's exposure to a warehouse-native query first."
+ : undefined,
+ 'data-attr': 'experiment-metric-data-source-warehouse-native',
+ },
+ ]}
+ />
+ </div>
+ <div className="text-secondary text-xs">
+ A warehouse-native metric runs a SQL query in your own warehouse through a direct
+ connection. Only the aggregated results leave the warehouse.
+ </div>
+ </div>
+ </SceneSection>
+ <SceneDivider />
+ </>
+ )}
<SceneSection title={isSharedMetric ? 'Shared metric type' : 'Metric type'} className="max-w-prose">
<div>
<LemonRadio
@@ -340,6 +428,8 @@ export function ExperimentMetricForm({
</>
)}
</>
+ ) : usesWarehouseNative ? (
+ <>Counts only after exposure. Each experiment sets its own exposure query.</>
) : (
<>
Counts only after exposure event (<LemonTag>{resolvedExposureEvent}</LemonTag> by
@@ -369,23 +459,31 @@ export function ExperimentMetricForm({
{isExperimentMeanMetric(metric) && (
<>
- <SeriesActionFilter
- bordered
- series={metricSourcesToSeries([metric.source])}
- trendsDisplayCategory={ChartDisplayCategory.TimeSeries}
- dataWarehouseNodeKind={NodeKind.ExperimentDataWarehouseNode}
- onChange={handleSetSeries}
- typeKey="experiment-metric"
- buttonCopy="Add graph series"
- showSeriesIndicator={false}
- hideRename={true}
- entitiesLimit={1}
- showNumericalPropsOnly={true}
- mathAvailability={mathAvailability}
- allowedMathTypes={allowedMathTypes}
- dataWarehousePopoverFields={dataWarehousePopoverFields}
- {...commonActionFilterProps}
- />
+ {usesWarehouseNative ? (
+ <WarehouseNativeMetricFields
+ metric={metric}
+ handleSetMetric={handleSetMetric}
+ exposureBinding={warehouseExposure}
+ />
+ ) : (
+ <SeriesActionFilter
+ bordered
+ series={metricSourcesToSeries([metric.source])}
+ trendsDisplayCategory={ChartDisplayCategory.TimeSeries}
+ dataWarehouseNodeKind={NodeKind.ExperimentDataWarehouseNode}
+ onChange={handleSetSeries}
+ typeKey="experiment-metric"
+ buttonCopy="Add graph series"
+ showSeriesIndicator={false}
+ hideRename={true}
+ entitiesLimit={1}
+ showNumericalPropsOnly={true}
+ mathAvailability={mathAvailability}
+ allowedMathTypes={allowedMathTypes}
+ dataWarehousePopoverFields={dataWarehousePopoverFields}
+ {...commonActionFilterProps}
+ />
+ )}
{metric.source.math === ExperimentMetricMathType.HogQL && (
<div className="text-muted text-sm mt-2">
SQL expressions allow you to write custom computations and aggregations. The expression
@@ -439,7 +537,15 @@ export function ExperimentMetricForm({
/>
)}
- {isExperimentRatioMetric(metric) && (
+ {isExperimentRatioMetric(metric) && usesWarehouseNative && (
+ <WarehouseNativeMetricFields
+ metric={metric}
+ handleSetMetric={handleSetMetric}
+ exposureBinding={warehouseExposure}
+ />
+ )}
+
+ {isExperimentRatioMetric(metric) && !usesWarehouseNative && (
<div className="space-y-4">
<div>
<LemonLabel className="mb-1">Numerator (what you're measuring)</LemonLabel>
@@ -748,57 +854,59 @@ export function ExperimentMetricForm({
{isExperimentMeanMetric(metric) && (
<>
<ExperimentMetricOutlierHandling metric={metric} handleSetMetric={handleSetMetric} />
- <SceneDivider />
+ {!usesWarehouseNative && <SceneDivider />}
</>
)}
{isExperimentRatioMetric(metric) && (
<>
<ExperimentRatioMetricOutlierHandling metric={metric} handleSetMetric={handleSetMetric} />
- <SceneDivider />
+ {!usesWarehouseNative && <SceneDivider />}
</>
)}
- <SceneSection
- title="Recent activity"
- className="max-w-prose"
- titleHelper={
- <div className="flex flex-col gap-2">
- <div>This shows recent activity for your selected metric over the past 2 weeks.</div>
- <div>
- It's a quick health check to ensure your tracking is working properly, so that you'll
- receive accurate results when your experiment starts.
- </div>
- <div>
- If you see zero activity, double-check that this metric is being tracked properly in your
- application. Head to{' '}
- <Link target="_blank" className="font-semibold" to={urls.insightNew()}>
- Product analytics
- <IconOpenInNew fontSize="18" />
- </Link>{' '}
- to do a detailed analysis of the events received so far.
- </div>
- </div>
- }
- >
- <div className="border rounded p-4 bg-bg-light">
- {isLoading ? (
- <div className="flex items-center gap-2">
- <Spinner />
- <span className="text-muted">Loading recent activity...</span>
- </div>
- ) : (
- <div className="flex flex-col gap-1">
- <div className="text-2xl font-semibold">
- {eventCount !== null ? eventCount.toLocaleString() : '0'}
+ {!usesWarehouseNative && (
+ <SceneSection
+ title="Recent activity"
+ className="max-w-prose"
+ titleHelper={
+ <div className="flex flex-col gap-2">
+ <div>This shows recent activity for your selected metric over the past 2 weeks.</div>
+ <div>
+ It's a quick health check to ensure your tracking is working properly, so that you'll
+ receive accurate results when your experiment starts.
</div>
- <div className="text-sm text-muted">
- {eventCount !== null && eventCount > 0
- ? `${getEventTypeLabel()} in the past 2 weeks`
- : 'No recent activity'}
+ <div>
+ If you see zero activity, double-check that this metric is being tracked properly in
+ your application. Head to{' '}
+ <Link target="_blank" className="font-semibold" to={urls.insightNew()}>
+ Product analytics
+ <IconOpenInNew fontSize="18" />
+ </Link>{' '}
+ to do a detailed analysis of the events received so far.
</div>
</div>
- )}
- </div>
- </SceneSection>
+ }
+ >
+ <div className="border rounded p-4 bg-bg-light">
+ {isLoading ? (
+ <div className="flex items-center gap-2">
+ <Spinner />
+ <span className="text-muted">Loading recent activity...</span>
+ </div>
+ ) : (
+ <div className="flex flex-col gap-1">
+ <div className="text-2xl font-semibold">
+ {eventCount !== null ? eventCount.toLocaleString() : '0'}
+ </div>
+ <div className="text-sm text-muted">
+ {eventCount !== null && eventCount > 0
+ ? `${getEventTypeLabel()} in the past 2 weeks`
+ : 'No recent activity'}
+ </div>
+ </div>
+ )}
+ </div>
+ </SceneSection>
+ )}
</SceneContent>
)
}
diff --git a/frontend/src/scenes/experiments/ExperimentView/ExposureCriteria.tsx b/frontend/src/scenes/experiments/ExperimentView/ExposureCriteria.tsx
index f1d33e47ee51..a01de9913b84 100644
--- a/frontend/src/scenes/experiments/ExperimentView/ExposureCriteria.tsx
+++ b/frontend/src/scenes/experiments/ExperimentView/ExposureCriteria.tsx
@@ -16,6 +16,14 @@ 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 {
+ EMPTY_WAREHOUSE_NATIVE_BINDING,
+ getExperimentMetricDataSources,
+ getWarehouseNativeExposure,
+ getWarehouseNativeExposureError,
+} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
import { experimentLogic } from '../experimentLogic'
import { EXPOSURE_DEFAULT_EVENT, getActivationConfig, resolveFilterTestAccounts } from '../exposureContract'
@@ -41,9 +49,23 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): J
// 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 does
const isActivation = !!getActivationConfig(exposureCriteria)
- const isCustom = !isActivation && !!exposureCriteria?.exposure_config
+ const warehouseExposure = getWarehouseNativeExposure(exposureCriteria)
+ const isCustom = !isActivation && !warehouseExposure && !!exposureCriteria?.exposure_config
// Keep an existing activation config editable even if the team is no longer flagged in
const showActivationCard = 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 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
return (
<LemonModal
@@ -70,6 +92,9 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): J
closeExposureCriteriaModal()
}}
type="primary"
+ disabledReason={
+ warehouseExposure ? getWarehouseNativeExposureError(warehouseExposure) : undefined
+ }
>
Save
</LemonButton>
@@ -80,7 +105,7 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): J
Exposure determines when a user enters your experiment. Only events that occur after exposure are
counted in your metrics.
</div>
- <div className="flex gap-4 mb-4">
+ <div className="flex flex-wrap gap-4 mb-4">
<SelectableCard
title="Default"
description={
@@ -89,7 +114,9 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): J
<strong>exposed</strong> to the experiment and included in the analysis.
</>
}
- selected={!isCustom && !isActivation}
+ selected={!isCustom && !isActivation && !warehouseExposure}
+ disabled={!!posthogBlockedReason}
+ disabledReason={posthogBlockedReason ?? ''}
onClick={() => {
setExposureCriteria({
...exposureCriteria,
@@ -109,6 +136,8 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): J
</>
}
selected={isActivation}
+ disabled={!!posthogBlockedReason}
+ disabledReason={posthogBlockedReason ?? ''}
onClick={() => {
// Re-clicking the selected card must not reset a configured event
if (isActivation) {
@@ -136,6 +165,8 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): J
</>
}
selected={isCustom}
+ disabled={!!posthogBlockedReason}
+ disabledReason={posthogBlockedReason ?? ''}
onClick={() => {
// Re-clicking the selected card must not reset a configured event
if (isCustom) {
@@ -152,7 +183,62 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): J
})
}}
/>
+ {showWarehouseNativeCard && (
+ <SelectableCard
+ title="Warehouse-native"
+ description={
+ <>
+ Read exposures with a SQL query that runs in your own warehouse. The metrics of the
+ experiment then run there too, and only aggregated results leave the warehouse.
+ </>
+ }
+ selected={!!warehouseExposure}
+ disabled={!!warehouseNativeBlockedReason}
+ disabledReason={warehouseNativeBlockedReason ?? ''}
+ data-attr="experiment-exposure-warehouse-native"
+ onClick={() => {
+ // Re-clicking the selected card must not reset a configured query
+ if (warehouseExposure) {
+ return
+ }
+ setExposureCriteria({
+ ...exposureCriteria,
+ exposure_config: {
+ kind: NodeKind.ExperimentWarehouseNativeExposureConfig,
+ ...EMPTY_WAREHOUSE_NATIVE_BINDING,
+ query: '',
+ },
+ activation_config: undefined,
+ })
+ }}
+ />
+ )}
</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>
+ )}
{isCustom && exposureCriteria?.exposure_config && (
<div className="mb-4">
<SeriesActionFilter
@@ -251,16 +337,19 @@ export function ExposureCriteriaModal({ onSave }: ExposureCriteriaModalProps): J
'Users exposed to multiple variants will be excluded from the analysis (recommended).'}
</div>
</div>
- <TestAccountFilterSwitch
- checked={hasFilters && resolveFilterTestAccounts(exposureCriteria)}
- onChange={(checked: boolean) => {
- setExposureCriteria({
- ...exposureCriteria,
- filterTestAccounts: checked,
- })
- }}
- fullWidth
- />
+ {/* Test account filters read PostHog person and event properties, which warehouse rows do not have */}
+ {!warehouseExposure && (
+ <TestAccountFilterSwitch
+ checked={hasFilters && resolveFilterTestAccounts(exposureCriteria)}
+ onChange={(checked: boolean) => {
+ setExposureCriteria({
+ ...exposureCriteria,
+ filterTestAccounts: checked,
+ })
+ }}
+ fullWidth
+ />
+ )}
</div>
</LemonModal>
)
diff --git a/frontend/src/scenes/experiments/ExperimentView/PageHeader.tsx b/frontend/src/scenes/experiments/ExperimentView/PageHeader.tsx
index de9a7d666573..4cba213feacb 100644
--- a/frontend/src/scenes/experiments/ExperimentView/PageHeader.tsx
+++ b/frontend/src/scenes/experiments/ExperimentView/PageHeader.tsx
@@ -20,6 +20,7 @@ import { AccessControlLevel, AccessControlResourceType } from '~/types'
import { CopyExperimentToProjectModal } from 'products/experiments/frontend/components/CopyExperimentToProjectModal'
import { DuplicateExperimentModal } from 'products/experiments/frontend/components/DuplicateExperimentModal'
+import { experimentUsesWarehouseNativeSources } from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
import { experimentLogic } from '../experimentLogic'
import { modalsLogic } from '../modalsLogic'
@@ -49,6 +50,12 @@ export function PageHeaderCustom(): JSX.Element {
setHogfettiTrigger(trigger)
})
+ // The API refuses to launch an experiment that reads warehouse-native queries, because nothing runs them yet
+ const launchDisabledReason =
+ experiment && experimentUsesWarehouseNativeSources(experiment)
+ ? 'Warehouse-native experiments cannot run yet. You can keep this one as a draft.'
+ : undefined
+
const canEdit = userHasAccess(
AccessControlResourceType.Experiment,
AccessControlLevel.Editor,
@@ -79,6 +86,7 @@ export function PageHeaderCustom(): JSX.Element {
data-attr="launch-experiment"
onClick={() => launchExperiment()}
loading={launchExperimentLoading}
+ disabledReason={launchDisabledReason}
>
Launch
</Button>
@@ -107,6 +115,7 @@ export function PageHeaderCustom(): JSX.Element {
data-attr="launch-experiment"
onClick={() => launchExperiment()}
loading={launchExperimentLoading}
+ disabledReason={launchDisabledReason}
size="small"
>
Launch
diff --git a/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx b/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx
index 3477d6f88cf0..79832a720b06 100644
--- a/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx
+++ b/frontend/src/scenes/experiments/SharedMetrics/SharedMetric.tsx
@@ -34,6 +34,7 @@ import { AccessControlLevel, AccessControlResourceType, ExperimentsTabs } from '
import { QuillSharedMetricActions } from 'products/experiments/frontend/components/QuillSharedMetricActions'
import type { ExperimentSavedMetricLinkedExperimentApi } from 'products/experiments/frontend/generated/api.schemas'
+import { getWarehouseNativeMetricError } from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
import { ExperimentMetricForm } from '../ExperimentMetricForm'
import { isLegacyExperimentQuery } from '../utils'
@@ -104,9 +105,9 @@ export function SharedMetric(): JSX.Element {
const isLegacy = isLegacyExperimentQuery(sharedMetric?.query)
const saveDisabledReason = isLegacy
? 'Legacy shared metrics can no longer be edited'
- : sharedMetric?.name
- ? undefined
- : 'You must give your metric a name'
+ : !sharedMetric?.name
+ ? 'You must give your metric a name'
+ : (getWarehouseNativeMetricError(sharedMetric.query as ExperimentMetric) ?? undefined)
const handleDelete = async (): Promise<void> => {
if (!sharedMetric.id || deleteCheckLoading) {
diff --git a/frontend/src/scenes/experiments/experimentLogic.tsx b/frontend/src/scenes/experiments/experimentLogic.tsx
index 5368b1a8d23d..e315f9fdb96f 100644
--- a/frontend/src/scenes/experiments/experimentLogic.tsx
+++ b/frontend/src/scenes/experiments/experimentLogic.tsx
@@ -105,11 +105,15 @@ import {
getShippedVariantKey,
isSingleVariantShipped,
} from 'products/experiments/frontend/scenes/experimentsLogic'
+import {
+ alignMetricsWithExposure,
+ getWarehouseNativeExposure,
+} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
import type { ExperimentMetricUnion } from '../../queries/schema/schema-general'
import type { ExperimentHoldoutType, ExperimentIdType } from '../../types'
import { experimentMetricsLogic } from './experimentMetricsLogic'
-import { featureFlagVariantProperty, resolvedExposureEvent } from './exposureContract'
+import { featureFlagVariantProperty, getActivationConfig, resolvedExposureEvent } from './exposureContract'
import { holdoutsLogic } from './holdoutsLogic'
import {
addExposureToMetric,
@@ -1939,10 +1943,22 @@ export const experimentLogic = kea<experimentLogicType>([
actions.refreshExperimentResults(outcome === 'saved', 'metric_config_change')
},
updateExposureCriteria: async () => {
+ const exposureCriteria = values.experiment.exposure_criteria
+ const warehouseExposure = getWarehouseNativeExposure(exposureCriteria)
+ const metrics = values.experiment.metrics ?? []
+ const secondaryMetrics = values.experiment.metrics_secondary ?? []
+ const alignedMetrics = warehouseExposure ? alignMetricsWithExposure(metrics, warehouseExposure) : metrics
+ const alignedSecondaryMetrics = warehouseExposure
+ ? alignMetricsWithExposure(secondaryMetrics, warehouseExposure)
+ : secondaryMetrics
actions.updateExperiment({
exposure_criteria: {
- ...values.experiment.exposure_criteria,
+ ...exposureCriteria,
},
+ ...(alignedMetrics.some((metric, index) => metric !== metrics[index]) && { metrics: alignedMetrics }),
+ ...(alignedSecondaryMetrics.some((metric, index) => metric !== secondaryMetrics[index]) && {
+ metrics_secondary: alignedSecondaryMetrics,
+ }),
update_feature_flag_params: false,
})
const outcome = await inflightUpdateOutcome(cache)
@@ -1952,6 +1968,17 @@ export const experimentLogic = kea<experimentLogicType>([
}
return
}
+ // 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',
+ })
actions.refreshExperimentResults(true, 'experiment_config_change')
},
updateExperimentSettings: async ({ update, fromModal }) => {
diff --git a/frontend/src/scenes/experiments/metricQueryUtils.ts b/frontend/src/scenes/experiments/metricQueryUtils.ts
index f365b0c87158..aaa78d0e9dc0 100644
--- a/frontend/src/scenes/experiments/metricQueryUtils.ts
+++ b/frontend/src/scenes/experiments/metricQueryUtils.ts
@@ -185,6 +185,10 @@ export const getQuery =
if (!isEventMetricSource(source) && !source.math) {
return undefined
}
+ // An insight cannot read a warehouse-native query
+ if (source.kind === NodeKind.ExperimentWarehouseNativeNode) {
+ return undefined
+ }
/**
* return a TrendsQuery
@@ -263,12 +267,12 @@ const getFunnelSeries = (
/**
* The series editor has rows for events, actions and synced warehouse tables only. A
- * warehouse-native source has no row yet, so the form refuses it like the insight converters do.
+ * warehouse-native source has its own fields in the metric form, so it must never reach this editor.
*/
export function metricSourcesToSeries(sources: ExperimentMetricSource[]): SeriesNode[] {
return sources.map((source) => {
if (source.kind === NodeKind.ExperimentWarehouseNativeNode) {
- throw new Error('Warehouse-native sources cannot be edited in the metric form yet')
+ throw new Error('Warehouse-native sources cannot be edited in the series editor')
}
return source
})
diff --git a/frontend/src/scenes/experiments/utils.ts b/frontend/src/scenes/experiments/utils.ts
index 92787fe86dff..261630f83428 100644
--- a/frontend/src/scenes/experiments/utils.ts
+++ b/frontend/src/scenes/experiments/utils.ts
@@ -83,7 +83,7 @@ export function getExposureConfigDisplayName(config: ExperimentExposureConfig):
return config.event || 'Unknown Event'
}
if (config.kind === NodeKind.ExperimentWarehouseNativeExposureConfig) {
- return 'Warehouse exposures'
+ return 'Warehouse-native query'
}
return config.name || `Action ${config.id}`
}
@@ -743,9 +743,8 @@ const getEventCountSeries = (metric: ExperimentMetric): AnyEntityNode[] => {
dataWarehouseNode.properties.length > 0 && { properties: dataWarehouseNode.properties }),
},
])
- .with({ kind: NodeKind.ExperimentWarehouseNativeNode }, () => {
- throw new Error('Warehouse-native sources cannot be shown as an insight yet')
- })
+ // PostHog holds no rows of a warehouse-native source, so it has no recent activity to count.
+ .with({ kind: NodeKind.ExperimentWarehouseNativeNode }, () => [])
.exhaustive()
return series
diff --git a/products/experiments/frontend/modals/ExperimentMetricModal/ExperimentMetricModal.tsx b/products/experiments/frontend/modals/ExperimentMetricModal/ExperimentMetricModal.tsx
index efb0d1216b13..20e0b5beda1b 100644
--- a/products/experiments/frontend/modals/ExperimentMetricModal/ExperimentMetricModal.tsx
+++ b/products/experiments/frontend/modals/ExperimentMetricModal/ExperimentMetricModal.tsx
@@ -9,6 +9,12 @@ import { resolveFilterTestAccounts, resolvedExposureEvent } from 'scenes/experim
import type { ExperimentExposureCriteria, ExperimentMetric } from '~/queries/schema/schema-general'
import type { Experiment } from '~/types'
+import {
+ alignMetricsWithExposure,
+ getWarehouseNativeExposure,
+ getWarehouseNativeMetricError,
+} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
+
import { type MetricContext, experimentMetricModalLogic } from './experimentMetricModalLogic'
export function ExperimentMetricModal({
@@ -30,6 +36,10 @@ export function ExperimentMetricModal({
return null
}
+ // The exposure can change while this modal is open, and the API rejects a metric on another connection or unit
+ const warehouseExposure = getWarehouseNativeExposure(exposureCriteria)
+ const metricToSave = warehouseExposure ? alignMetricsWithExposure([metric], warehouseExposure)[0] : metric
+
return (
<LemonModal
isOpen={isModalOpen}
@@ -72,9 +82,10 @@ export function ExperimentMetricModal({
</LemonButton>
<LemonButton
form="edit-experiment-metric-form"
- onClick={() => onSave(metric, context)}
+ onClick={() => onSave(metricToSave, context)}
type="primary"
data-attr="save-experiment-metric"
+ disabledReason={getWarehouseNativeMetricError(metricToSave) ?? undefined}
>
{isCreateMode ? 'Create' : 'Save'}
</LemonButton>
diff --git a/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.tsx b/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.tsx
index da9aa9905f4a..b2ab34789831 100644
--- a/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.tsx
+++ b/products/experiments/frontend/modals/MetricSourceModal/MetricSourceModal.tsx
@@ -4,9 +4,14 @@ 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 { experimentMetricModalLogic } from 'products/experiments/frontend/modals/ExperimentMetricModal/experimentMetricModalLogic'
import { sharedMetricModalLogic } from 'products/experiments/frontend/modals/SharedMetricModal/sharedMetricModalLogic'
+import {
+ getWarehouseNativeExposure,
+ toWarehouseNativeMetric,
+} from 'products/experiments/frontend/warehouseNative/warehouseNativeSources'
import { metricSourceModalLogic } from './metricSourceModalLogic'
@@ -16,7 +21,7 @@ export const MetricSourceModal = (): JSX.Element | null => {
const { isModalOpen, context } = useValues(metricSourceModalLogic)
const { closeMetricSourceModal } = useActions(metricSourceModalLogic)
- const { openExperimentMetricModal } = useActions(experimentMetricModalLogic)
+ const { openExperimentMetricModal, setMetric } = useActions(experimentMetricModalLogic)
const { openSharedMetricModal } = useActions(sharedMetricModalLogic)
const { experiment } = useValues(experimentLogic)
@@ -35,6 +40,11 @@ export const MetricSourceModal = (): JSX.Element | null => {
onClick={() => {
closeMetricSourceModal()
openExperimentMetricModal(context)
+ // An experiment with a warehouse-native exposure accepts warehouse-native metrics only
+ const warehouseExposure = getWarehouseNativeExposure(experiment.exposure_criteria)
+ if (warehouseExposure) {
+ setMetric(toWarehouseNativeMetric(getDefaultFunnelMetric(), warehouseExposure))
+ }
}}
>
<div className="font-semibold">
diff --git a/products/experiments/frontend/warehouseNative/WarehouseNativeConnectionFields.tsx b/products/experiments/frontend/warehouseNative/WarehouseNativeConnectionFields.tsx
new file mode 100644
index 000000000000..f7f7f66044de
--- /dev/null
+++ b/products/experiments/frontend/warehouseNative/WarehouseNativeConnectionFields.tsx
@@ -0,0 +1,115 @@
+import { useActions, useValues } from 'kea'
+
+import { IconRefresh } from '@posthog/icons'
+import { LemonButton, LemonInput, LemonSelect, Link } from '@posthog/lemon-ui'
+
+import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
+import { LemonLabel } from 'lib/lemon-ui/LemonLabel'
+import { urls } from 'scenes/urls'
+
+import { connectionSelectorLogic } from 'products/data_warehouse/frontend/shared/logics/connectionSelectorLogic'
+
+import type { WarehouseNativeBinding } from './warehouseNativeSources'
+
+export type WarehouseNativeConnectionFieldsProps = {
+ binding: WarehouseNativeBinding
+ onChange: (binding: WarehouseNativeBinding) => void
+}
+
+export function WarehouseNativeConnectionFields({
+ binding,
+ onChange,
+}: WarehouseNativeConnectionFieldsProps): JSX.Element {
+ const { connectionOptions, connectionOptionsLoading } = useValues(connectionSelectorLogic)
+ const { maybeLoadConnectionOptions, loadConnectionOptions } = useActions(connectionSelectorLogic)
+
+ useOnMountEffect(() => {
+ maybeLoadConnectionOptions()
+ })
+
+ // A reload keeps the loaded options, so the select only waits for the first load
+ const loading = connectionOptions === null
+ // A warehouse-native query needs a direct BigQuery connection. A synced source cannot run it.
+ const options = (connectionOptions ?? [])
+ .filter((source) => source.source_type === 'BigQuery' && source.access_method === 'direct')
+ .map((source) => ({ value: source.id, label: source.description || source.prefix || 'BigQuery' }))
+ if (!loading && binding.connection_id && !options.some((option) => option.value === binding.connection_id)) {
+ options.push({ value: binding.connection_id, label: 'Unavailable connection' })
+ }
+
+ return (
+ <div className="flex flex-col gap-3">
+ <div className="flex flex-col gap-1">
+ <LemonLabel>Connection</LemonLabel>
+ <div className="flex items-center gap-2">
+ <div className="min-w-0 flex-1">
+ {!loading && options.length === 0 ? (
+ <div className="text-secondary text-sm">
+ This project has no direct BigQuery connection yet.{' '}
+ <Link
+ to={urls.dataWarehouseSourceNew('BigQuery', undefined, undefined, 'direct')}
+ target="_blank"
+ data-attr="experiment-warehouse-native-add-connection"
+ >
+ Add a BigQuery connection
+ </Link>
+ </div>
+ ) : (
+ <LemonSelect
+ fullWidth
+ loading={loading}
+ value={loading ? null : binding.connection_id || null}
+ onChange={(connectionId) => onChange({ ...binding, connection_id: connectionId ?? '' })}
+ options={options}
+ placeholder="Select a BigQuery connection"
+ data-attr="experiment-warehouse-native-connection"
+ />
+ )}
+ </div>
+ {/* A connection added in another tab only shows after a reload */}
+ <LemonButton
+ icon={<IconRefresh />}
+ type="secondary"
+ onClick={() => loadConnectionOptions()}
+ loading={connectionOptionsLoading}
+ tooltip="Reload connections"
+ aria-label="Reload connections"
+ data-attr="experiment-warehouse-native-reload-connections"
+ />
+ </div>
+ </div>
+ <div className="flex flex-wrap gap-3">
+ <div className="flex min-w-48 flex-1 flex-col gap-1">
+ <LemonLabel info="What the experiment assigns to a variant, such as customer or account.">
+ Randomization unit
+ </LemonLabel>
+ <LemonInput
+ value={binding.unit.unit_type}
+ onChange={(unitType) =>
+ onChange({ ...binding, unit: { ...binding.unit, unit_type: unitType } })
+ }
+ placeholder="customer"
+ data-attr="experiment-warehouse-native-unit-type"
+ />
+ </div>
+ <div className="flex min-w-48 flex-1 flex-col gap-1">
+ <LemonLabel info="Which IDs the entity_id column holds, such as billing_customer_id.">
+ ID type
+ </LemonLabel>
+ <LemonInput
+ value={binding.unit.id_namespace}
+ onChange={(idNamespace) =>
+ onChange({ ...binding, unit: { ...binding.unit, id_namespace: idNamespace } })
+ }
+ placeholder="billing_customer_id"
+ data-attr="experiment-warehouse-native-id-namespace"
+ />
+ </div>
+ </div>
+ <div className="text-secondary text-xs">
+ The exposure query and every metric query must use the same connection, unit and ID type, so that their
+ entity_id values match.
+ </div>
+ </div>
+ )
+}
diff --git a/products/experiments/frontend/warehouseNative/WarehouseNativeMetricFields.stories.tsx b/products/experiments/frontend/warehouseNative/WarehouseNativeMetricFields.stories.tsx
new file mode 100644
index 000000000000..1cfd432c3098
--- /dev/null
+++ b/products/experiments/frontend/warehouseNative/WarehouseNativeMetricFields.stories.tsx
@@ -0,0 +1,77 @@
+import type { Meta, StoryObj } from '@storybook/react'
+
+import { mswDecorator } from '~/mocks/browser'
+import {
+ type ExperimentMetric,
+ ExperimentMetricType,
+ ExperimentWarehouseNativeMathType,
+ NodeKind,
+} from '~/queries/schema/schema-general'
+
+import { WarehouseNativeMetricFields } from './WarehouseNativeMetricFields'
+import type { WarehouseNativeBinding } from './warehouseNativeSources'
+
+const BINDING: WarehouseNativeBinding = {
+ connection_id: '0190a3c5-0000-0000-0000-000000000001',
+ unit: { unit_type: 'customer', id_namespace: 'billing_customer_id' },
+}
+
+const RATIO_METRIC: ExperimentMetric = {
+ kind: NodeKind.ExperimentMetric,
+ uuid: 'revenue-per-order',
+ metric_type: ExperimentMetricType.RATIO,
+ numerator: {
+ kind: NodeKind.ExperimentWarehouseNativeNode,
+ ...BINDING,
+ query: 'SELECT customer_id AS entity_id, paid_at AS timestamp, amount\nFROM billing.payments',
+ math: ExperimentWarehouseNativeMathType.Sum,
+ math_property: 'amount',
+ },
+ denominator: {
+ kind: NodeKind.ExperimentWarehouseNativeNode,
+ ...BINDING,
+ query: 'SELECT customer_id AS entity_id, created_at AS timestamp\nFROM billing.orders',
+ math: ExperimentWarehouseNativeMathType.TotalCount,
+ },
+}
+
+const meta: Meta<typeof WarehouseNativeMetricFields> = {
+ title: 'Experiments/WarehouseNativeMetricFields',
+ component: WarehouseNativeMetricFields,
+ args: {
+ metric: RATIO_METRIC,
+ handleSetMetric: () => {},
+ exposureBinding: null,
+ },
+ 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: BINDING.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 SharedRatioMetric: StoryObj<typeof WarehouseNativeMetricFields> = {}
diff --git a/products/experiments/frontend/warehouseNative/WarehouseNativeMetricFields.tsx b/products/experiments/frontend/warehouseNative/WarehouseNativeMetricFields.tsx
new file mode 100644
index 000000000000..6138e412c710
--- /dev/null
+++ b/products/experiments/frontend/warehouseNative/WarehouseNativeMetricFields.tsx
@@ -0,0 +1,71 @@
+import { type ExperimentMetric, ExperimentMetricType } from '~/queries/schema/schema-general'
+
+import { WarehouseNativeConnectionFields } from './WarehouseNativeConnectionFields'
+import { WarehouseNativeSourceFields } from './WarehouseNativeSourceFields'
+import {
+ type WarehouseNativeBinding,
+ getWarehouseNativeBinding,
+ toWarehouseNativeMetric,
+} from './warehouseNativeSources'
+
+export type WarehouseNativeMetricFieldsProps = {
+ metric: ExperimentMetric
+ handleSetMetric: (metric: ExperimentMetric) => void
+ /**
+ * The connection and the unit of the experiment's exposure. The metric must use the same, so the
+ * form does not ask for them again. A shared metric has no experiment and asks for its own.
+ */
+ exposureBinding: WarehouseNativeBinding | null
+}
+
+export function WarehouseNativeMetricFields({
+ metric,
+ handleSetMetric,
+ exposureBinding,
+}: WarehouseNativeMetricFieldsProps): JSX.Element {
+ const binding = exposureBinding ?? getWarehouseNativeBinding(metric)
+ // Every edit passes through the binding, so no source can keep a connection the others do not have.
+ const warehouseMetric = toWarehouseNativeMetric(metric, binding)
+
+ return (
+ <div className="flex flex-col gap-4">
+ {exposureBinding ? (
+ <div className="text-secondary text-sm">
+ The queries run on the connection of the experiment's exposure and return the same unit (
+ <span className="font-mono text-xs">
+ {exposureBinding.unit.unit_type} · {exposureBinding.unit.id_namespace}
+ </span>
+ ).
+ </div>
+ ) : (
+ <WarehouseNativeConnectionFields
+ binding={binding}
+ onChange={(newBinding) => handleSetMetric(toWarehouseNativeMetric(metric, newBinding))}
+ />
+ )}
+ {warehouseMetric.metric_type === ExperimentMetricType.RATIO ? (
+ <>
+ <WarehouseNativeSourceFields
+ label="Numerator query"
+ source={warehouseMetric.numerator}
+ onChange={(numerator) => handleSetMetric({ ...warehouseMetric, numerator })}
+ data-attr="experiment-warehouse-native-numerator-query"
+ />
+ <WarehouseNativeSourceFields
+ label="Denominator query"
+ source={warehouseMetric.denominator}
+ onChange={(denominator) => handleSetMetric({ ...warehouseMetric, denominator })}
+ data-attr="experiment-warehouse-native-denominator-query"
+ />
+ </>
+ ) : (
+ <WarehouseNativeSourceFields
+ label="Query"
+ source={warehouseMetric.source}
+ onChange={(source) => handleSetMetric({ ...warehouseMetric, source })}
+ data-attr="experiment-warehouse-native-metric-query"
+ />
+ )}
+ </div>
+ )
+}
diff --git a/products/experiments/frontend/warehouseNative/WarehouseNativeQueryEditor.tsx b/products/experiments/frontend/warehouseNative/WarehouseNativeQueryEditor.tsx
new file mode 100644
index 000000000000..47d739b37682
--- /dev/null
+++ b/products/experiments/frontend/warehouseNative/WarehouseNativeQueryEditor.tsx
@@ -0,0 +1,42 @@
+import { LemonLabel } from 'lib/lemon-ui/LemonLabel'
+import { CodeEditorResizeable } from 'lib/monaco/CodeEditorResizable'
+
+export type WarehouseNativeQueryEditorProps = {
+ label: string
+ /** Which columns the query must return. */
+ hint: string
+ query: string
+ onChange: (query: string) => void
+ 'data-attr'?: string
+}
+
+export function WarehouseNativeQueryEditor({
+ label,
+ hint,
+ query,
+ onChange,
+ 'data-attr': dataAttr,
+}: WarehouseNativeQueryEditorProps): JSX.Element {
+ return (
+ <div className="flex flex-col gap-1" data-attr={dataAttr}>
+ <LemonLabel>{label}</LemonLabel>
+ <CodeEditorResizeable
+ language="sql"
+ value={query}
+ onChange={(value) => onChange(value ?? '')}
+ minHeight="8rem"
+ maxHeight="24rem"
+ options={{
+ minimap: { enabled: false },
+ scrollbar: { alwaysConsumeMouseWheel: false },
+ scrollBeyondLastLine: false,
+ lineNumbers: 'off',
+ folding: false,
+ wordWrap: 'on',
+ renderLineHighlight: 'none',
+ }}
+ />
+ <div className="text-secondary text-xs">{hint}</div>
+ </div>
+ )
+}
diff --git a/products/experiments/frontend/warehouseNative/WarehouseNativeSourceFields.tsx b/products/experiments/frontend/warehouseNative/WarehouseNativeSourceFields.tsx
new file mode 100644
index 000000000000..eb5a91f6a3f6
--- /dev/null
+++ b/products/experiments/frontend/warehouseNative/WarehouseNativeSourceFields.tsx
@@ -0,0 +1,68 @@
+import { LemonInput, LemonSelect } from '@posthog/lemon-ui'
+
+import { LemonLabel } from 'lib/lemon-ui/LemonLabel'
+
+import { ExperimentWarehouseNativeMathType, type ExperimentWarehouseNativeNode } from '~/queries/schema/schema-general'
+
+import { WarehouseNativeQueryEditor } from './WarehouseNativeQueryEditor'
+
+export type WarehouseNativeSourceFieldsProps = {
+ label: string
+ source: ExperimentWarehouseNativeNode
+ onChange: (source: ExperimentWarehouseNativeNode) => void
+ 'data-attr'?: string
+}
+
+/** The query and the aggregation of one metric source. The metric owns the connection and the unit. */
+export function WarehouseNativeSourceFields({
+ label,
+ source,
+ onChange,
+ 'data-attr': dataAttr,
+}: WarehouseNativeSourceFieldsProps): JSX.Element {
+ const math = source.math ?? ExperimentWarehouseNativeMathType.TotalCount
+
+ return (
+ <div className="flex flex-col gap-3">
+ <WarehouseNativeQueryEditor
+ label={label}
+ hint="Return one row per outcome with the columns entity_id and timestamp, plus any numeric column you want to sum."
+ query={source.query}
+ onChange={(query) => onChange({ ...source, query })}
+ data-attr={dataAttr}
+ />
+ <div className="flex flex-wrap items-end gap-3">
+ <div className="flex flex-col gap-1">
+ <LemonLabel>Value per unit</LemonLabel>
+ <LemonSelect<ExperimentWarehouseNativeMathType>
+ value={math}
+ onChange={(newMath) =>
+ onChange({
+ ...source,
+ math: newMath,
+ math_property:
+ newMath === ExperimentWarehouseNativeMathType.Sum
+ ? source.math_property
+ : undefined,
+ })
+ }
+ options={[
+ { value: ExperimentWarehouseNativeMathType.TotalCount, label: 'Count of rows' },
+ { value: ExperimentWarehouseNativeMathType.Sum, label: 'Sum of a column' },
+ ]}
+ />
+ </div>
+ {math === ExperimentWarehouseNativeMathType.Sum && (
+ <div className="flex min-w-40 flex-1 flex-col gap-1">
+ <LemonLabel>Column</LemonLabel>
+ <LemonInput
+ value={source.math_property ?? ''}
+ onChange={(column) => onChange({ ...source, math_property: column })}
+ placeholder="amount"
+ />
+ </div>
+ )}
+ </div>
+ </div>
+ )
+}
diff --git a/products/experiments/frontend/warehouseNative/warehouseNativeSources.test.ts b/products/experiments/frontend/warehouseNative/warehouseNativeSources.test.ts
new file mode 100644
index 000000000000..1ed7436e64ed
--- /dev/null
+++ b/products/experiments/frontend/warehouseNative/warehouseNativeSources.test.ts
@@ -0,0 +1,140 @@
+import {
+ type ExperimentMetric,
+ ExperimentMetricType,
+ ExperimentWarehouseNativeMathType,
+ type ExperimentWarehouseNativeNode,
+ NodeKind,
+} from '~/queries/schema/schema-general'
+
+import {
+ type WarehouseNativeBinding,
+ alignMetricsWithExposure,
+ experimentUsesWarehouseNativeSources,
+ getExperimentMetricDataSources,
+ getWarehouseNativeMetricError,
+ toWarehouseNativeMetric,
+} from './warehouseNativeSources'
+
+const BINDING: WarehouseNativeBinding = {
+ connection_id: 'connection-1',
+ unit: { unit_type: 'customer', id_namespace: 'billing_customer_id' },
+}
+
+const warehouseSource = (overrides: Partial<ExperimentWarehouseNativeNode> = {}): ExperimentWarehouseNativeNode => ({
+ kind: NodeKind.ExperimentWarehouseNativeNode,
+ ...BINDING,
+ query: 'SELECT customer_id AS entity_id, paid_at AS timestamp, amount FROM billing.payments',
+ math: ExperimentWarehouseNativeMathType.TotalCount,
+ ...overrides,
+})
+
+const meanMetric = (source: ExperimentWarehouseNativeNode): ExperimentMetric => ({
+ kind: NodeKind.ExperimentMetric,
+ uuid: 'metric-1',
+ metric_type: ExperimentMetricType.MEAN,
+ source,
+})
+
+const EVENTS_METRIC: ExperimentMetric = {
+ kind: NodeKind.ExperimentMetric,
+ uuid: 'metric-2',
+ metric_type: ExperimentMetricType.MEAN,
+ source: { kind: NodeKind.EventsNode, event: '$pageview' },
+}
+
+describe('warehouseNativeSources', () => {
+ it('turns a funnel metric into a warehouse-native mean metric that keeps its identity', () => {
+ const funnel: ExperimentMetric = {
+ kind: NodeKind.ExperimentMetric,
+ uuid: 'metric-3',
+ name: 'Revenue',
+ metric_type: ExperimentMetricType.FUNNEL,
+ series: [{ kind: NodeKind.EventsNode, event: '$pageview' }],
+ }
+
+ expect(toWarehouseNativeMetric(funnel, BINDING)).toEqual({
+ kind: NodeKind.ExperimentMetric,
+ uuid: 'metric-3',
+ name: 'Revenue',
+ goal: undefined,
+ metric_type: ExperimentMetricType.MEAN,
+ source: warehouseSource({ query: '' }),
+ })
+ })
+
+ it('keeps the SQL of a ratio source and replaces an event source, so the ratio never mixes sources', () => {
+ const numerator = warehouseSource({ connection_id: 'old-connection', math_property: 'amount' })
+ const ratio: ExperimentMetric = {
+ kind: NodeKind.ExperimentMetric,
+ metric_type: ExperimentMetricType.RATIO,
+ numerator,
+ denominator: { kind: NodeKind.EventsNode, event: '$pageview' },
+ }
+
+ expect(toWarehouseNativeMetric(ratio, BINDING)).toMatchObject({
+ numerator: { ...numerator, connection_id: 'connection-1' },
+ denominator: warehouseSource({ query: '' }),
+ })
+ })
+
+ it('moves a metric to the connection and unit of a changed exposure, and leaves the other metrics alone', () => {
+ const aligned = meanMetric(warehouseSource())
+ const stale = meanMetric(warehouseSource({ connection_id: 'old-connection' }))
+
+ const result = alignMetricsWithExposure([aligned, stale, EVENTS_METRIC], BINDING)
+
+ expect(result[0]).toBe(aligned)
+ expect(result[1]).toEqual(aligned)
+ expect(result[2]).toBe(EVENTS_METRIC)
+ })
+
+ it.each([
+ ['no connection', warehouseSource({ connection_id: '' }), 'Select a BigQuery connection'],
+ [
+ 'no unit',
+ warehouseSource({ unit: { unit_type: ' ', id_namespace: 'billing_customer_id' } }),
+ 'Enter the randomization unit',
+ ],
+ ['no query', warehouseSource({ query: ' ' }), 'Enter a SQL query for every source'],
+ [
+ 'a sum without a column',
+ warehouseSource({ math: ExperimentWarehouseNativeMathType.Sum }),
+ 'Enter the column to sum',
+ ],
+ [
+ 'a complete sum',
+ warehouseSource({ math: ExperimentWarehouseNativeMathType.Sum, math_property: 'amount' }),
+ null,
+ ],
+ ])('reports why a metric with %s cannot be saved', (_, source, expected) => {
+ expect(getWarehouseNativeMetricError(meanMetric(source))).toBe(expected)
+ })
+
+ it.each([
+ [
+ 'a warehouse-native exposure and no metrics',
+ {
+ exposure_criteria: {
+ exposure_config: {
+ ...warehouseSource(),
+ kind: NodeKind.ExperimentWarehouseNativeExposureConfig as const,
+ },
+ },
+ },
+ true,
+ { warehouseNative: false, posthog: false },
+ ],
+ [
+ 'a warehouse-native shared metric',
+ { saved_metrics: [{ query: meanMetric(warehouseSource()) }] },
+ true,
+ { warehouseNative: true, posthog: false },
+ ],
+ ['event metrics only', { metrics: [EVENTS_METRIC] }, false, { warehouseNative: false, posthog: true }],
+ ])('knows which data sources an experiment with %s reads', (_, experiment, usesWarehouse, metricDataSources) => {
+ const full = { metrics: [], metrics_secondary: [], saved_metrics: [], ...experiment }
+
+ expect(experimentUsesWarehouseNativeSources(full)).toBe(usesWarehouse)
+ expect(getExperimentMetricDataSources(full)).toEqual(metricDataSources)
+ })
+})
diff --git a/products/experiments/frontend/warehouseNative/warehouseNativeSources.ts b/products/experiments/frontend/warehouseNative/warehouseNativeSources.ts
new file mode 100644
index 000000000000..8a270bb802d4
--- /dev/null
+++ b/products/experiments/frontend/warehouseNative/warehouseNativeSources.ts
@@ -0,0 +1,190 @@
+import {
+ type ExperimentExposureCriteria,
+ type ExperimentMeanMetric,
+ type ExperimentMetric,
+ type ExperimentMetricSource,
+ ExperimentMetricType,
+ type ExperimentRatioMetric,
+ type ExperimentWarehouseNativeExposureConfig,
+ ExperimentWarehouseNativeMathType,
+ type ExperimentWarehouseNativeNode,
+ NodeKind,
+ isExperimentMeanMetric,
+ isExperimentRatioMetric,
+} from '~/queries/schema/schema-general'
+import type { Experiment } from '~/types'
+
+/** The connection and the unit. Every warehouse-native query of one experiment must share them. */
+export type WarehouseNativeBinding = Pick<ExperimentWarehouseNativeNode, 'connection_id' | 'unit'>
+
+/** A metric whose every source is a warehouse-native query. Only mean and ratio metrics accept one. */
+export type WarehouseNativeMetric =
+ | (ExperimentMeanMetric & { source: ExperimentWarehouseNativeNode })
+ | (ExperimentRatioMetric & {
+ numerator: ExperimentWarehouseNativeNode
+ denominator: ExperimentWarehouseNativeNode
+ })
+
+export const EMPTY_WAREHOUSE_NATIVE_BINDING: WarehouseNativeBinding = {
+ connection_id: '',
+ unit: { unit_type: '', id_namespace: '' },
+}
+
+export function getWarehouseNativeExposure(
+ exposureCriteria: ExperimentExposureCriteria | null | undefined
+): ExperimentWarehouseNativeExposureConfig | null {
+ const config = exposureCriteria?.exposure_config
+ return config?.kind === NodeKind.ExperimentWarehouseNativeExposureConfig ? config : null
+}
+
+export const isWarehouseNativeSource = (
+ source: { kind: NodeKind } | null | undefined
+): source is ExperimentWarehouseNativeNode => source?.kind === NodeKind.ExperimentWarehouseNativeNode
+
+export function getWarehouseNativeSources(metric: ExperimentMetric): ExperimentWarehouseNativeNode[] {
+ const sources: ExperimentMetricSource[] = isExperimentMeanMetric(metric)
+ ? [metric.source]
+ : isExperimentRatioMetric(metric)
+ ? [metric.numerator, metric.denominator]
+ : []
+ return sources.filter(isWarehouseNativeSource)
+}
+
+export const metricUsesWarehouseNativeSource = (metric: ExperimentMetric): boolean =>
+ getWarehouseNativeSources(metric).length > 0
+
+export function getWarehouseNativeBinding(metric: ExperimentMetric): WarehouseNativeBinding {
+ const [source] = getWarehouseNativeSources(metric)
+ return source ? { connection_id: source.connection_id, unit: source.unit } : EMPTY_WAREHOUSE_NATIVE_BINDING
+}
+
+/**
+ * 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 = [
+ ...(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 }
+}
+
+export function experimentUsesWarehouseNativeSources(
+ experiment: Pick<Experiment, 'exposure_criteria' | 'metrics' | 'metrics_secondary' | 'saved_metrics'>
+): boolean {
+ return (
+ !!getWarehouseNativeExposure(experiment.exposure_criteria) ||
+ getExperimentMetricDataSources(experiment).warehouseNative
+ )
+}
+
+function toWarehouseNativeSource(
+ source: ExperimentMetricSource | undefined,
+ binding: WarehouseNativeBinding
+): ExperimentWarehouseNativeNode {
+ const base: ExperimentWarehouseNativeNode = isWarehouseNativeSource(source)
+ ? source
+ : {
+ kind: NodeKind.ExperimentWarehouseNativeNode,
+ connection_id: '',
+ unit: EMPTY_WAREHOUSE_NATIVE_BINDING.unit,
+ query: '',
+ math: ExperimentWarehouseNativeMathType.TotalCount,
+ }
+ return { ...base, connection_id: binding.connection_id, unit: { ...binding.unit } }
+}
+
+/**
+ * Makes every source of the metric a warehouse-native query on `binding`. A source that is already a
+ * warehouse-native query keeps its SQL. A funnel or retention metric becomes a mean metric, because
+ * those types cannot read a warehouse-native query.
+ */
+export function toWarehouseNativeMetric(
+ metric: ExperimentMetric,
+ binding: WarehouseNativeBinding
+): WarehouseNativeMetric {
+ if (isExperimentRatioMetric(metric)) {
+ return {
+ ...metric,
+ numerator: toWarehouseNativeSource(metric.numerator, binding),
+ denominator: toWarehouseNativeSource(metric.denominator, binding),
+ }
+ }
+ if (isExperimentMeanMetric(metric)) {
+ return { ...metric, source: toWarehouseNativeSource(metric.source, binding) }
+ }
+ return {
+ kind: NodeKind.ExperimentMetric,
+ uuid: metric.uuid,
+ name: metric.name,
+ goal: metric.goal,
+ metric_type: ExperimentMetricType.MEAN,
+ source: toWarehouseNativeSource(undefined, binding),
+ }
+}
+
+/**
+ * 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.
+ * A metric that needs no change keeps its identity.
+ */
+export function alignMetricsWithExposure<T extends { kind: NodeKind }>(
+ metrics: T[],
+ exposure: WarehouseNativeBinding
+): T[] {
+ return metrics.map((metric) => {
+ if (metric.kind !== NodeKind.ExperimentMetric) {
+ 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)
+ })
+}
+
+const getBindingError = (binding: WarehouseNativeBinding): string | null => {
+ if (!binding.connection_id) {
+ return 'Select a BigQuery connection'
+ }
+ if (!binding.unit.unit_type.trim()) {
+ return 'Enter the randomization unit'
+ }
+ if (!binding.unit.id_namespace.trim()) {
+ return 'Enter the ID type'
+ }
+ return null
+}
+
+export function getWarehouseNativeExposureError(config: ExperimentWarehouseNativeExposureConfig): string | null {
+ return getBindingError(config) ?? (config.query.trim() ? null : 'Enter the exposure query')
+}
+
+/** Why the metric cannot be saved yet, or null when its warehouse-native queries are complete. */
+export function getWarehouseNativeMetricError(metric: ExperimentMetric): string | null {
+ for (const source of getWarehouseNativeSources(metric)) {
+ const bindingError = getBindingError(source)
+ if (bindingError) {
+ return bindingError
+ }
+ if (!source.query.trim()) {
+ return 'Enter a SQL query for every source'
+ }
+ if (source.math === ExperimentWarehouseNativeMathType.Sum && !source.math_property?.trim()) {
+ return 'Enter the column to sum'
+ }
+ }
+ return null
+}