: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
import { useEffect, useRef, useState } from 'react'
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'
import { Link } from 'lib/lemon-ui/Link'
import { Spinner } from 'lib/lemon-ui/Spinner'
import { Tooltip } from 'lib/lemon-ui/Tooltip'
import { SeriesActionFilter } from 'scenes/insights/filters/ActionFilter/ActionFilter'
import { SeriesNode } from 'scenes/insights/filters/ActionFilter/seriesNode'
import { urls } from 'scenes/urls'
import { SceneContent } from '~/layout/scenes/components/SceneContent'
import { SceneDivider } from '~/layout/scenes/components/SceneDivider'
import { SceneSection } from '~/layout/scenes/components/SceneSection'
import { performQuery } from '~/queries/query'
import {
ExperimentExposureCriteria,
ExperimentFunnelMetricStep,
ExperimentMetric,
ExperimentMetricSource,
ExperimentMetricType,
ExperimentRetentionStart,
NodeKind,
isExperimentExposureNode,
isExperimentFunnelMetric,
isExperimentMeanMetric,
isExperimentRatioMetric,
isExperimentRetentionMetric,
} from '~/queries/schema/schema-general'
import {
ChartDisplayCategory,
ExperimentMetricGoal,
ExperimentMetricMathType,
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 {
ExperimentMetricOutlierHandling,
ExperimentRatioMetricOutlierHandling,
} from './ExperimentMetricOutlierHandling'
import { ExperimentMetricThreshold, isThresholdAvailableForMath } from './ExperimentMetricThreshold'
import { EXPOSURE_DEFAULT_EVENT, getActivationConfig, isDefaultExposureConfig } from './exposureContract'
import { metricSourcesToSeries, seriesToMetricConfig, seriesToMetricSource } from './metricQueryUtils'
import { commonActionFilterProps } from './Metrics/Selectors'
import {
getAllowedMathTypes,
getDefaultExperimentMetric,
getEventCountQuery,
getExposureConfigDisplayName,
getMathAvailability,
} from './utils'
export function getExposureCriteriaLabel(
exposureCriteria: ExperimentExposureCriteria | undefined,
defaultEvent: string = EXPOSURE_DEFAULT_EVENT
): string {
const exposureConfig = exposureCriteria?.exposure_config
if (!exposureConfig || isDefaultExposureConfig(exposureConfig)) {
return defaultEvent
}
return getExposureConfigDisplayName(exposureConfig)
}
const loadEventCount = async (
metric: ExperimentMetric,
filterTestAccounts: boolean,
setEventCount: (count: number | null) => void,
setIsLoading: (loading: boolean) => void
): Promise<void> => {
setIsLoading(true)
try {
const query = getEventCountQuery(metric, filterTestAccounts)
if (!query) {
setEventCount(0)
return
}
const response = await performQuery(query)
let count = 0
if (response.results.length > 0) {
const firstResult = response.results[0]
if (firstResult && typeof firstResult.aggregated_value === 'number') {
count = firstResult.aggregated_value
}
}
setEventCount(count)
} catch (error) {
lemonToast.error(JSON.stringify(error))
setEventCount(0)
} finally {
setIsLoading(false)
}
}
const dataWarehousePopoverFields: DataWarehousePopoverField[] = [
{
key: 'timestamp_field',
label: 'Timestamp Field',
description: 'The column in your data warehouse table that contains the timestamp of each row',
},
{
key: 'data_warehouse_join_key',
label: 'Data Warehouse Join Key',
description:
'The column in your data warehouse table that identifies which user each row belongs to (e.g. user_id, email)',
allowHogQL: true,
},
{
key: 'events_join_key',
label: 'Events Join Key',
description: 'The field on PostHog events to match against the data warehouse join key (usually distinct_id)',
allowHogQL: true,
hogQLOnly: true,
tableName: 'events',
},
]
/** The sources every metric type accepts: those the series picker can produce. */
const isPickerSource = (source: ExperimentMetricSource | undefined): source is ExperimentFunnelMetricStep =>
!!source &&
(source.kind === NodeKind.EventsNode ||
source.kind === NodeKind.ActionsNode ||
source.kind === NodeKind.ExperimentDataWarehouseNode)
export function ExperimentMetricForm({
metric,
isSharedMetric = false,
handleSetMetric,
filterTestAccounts,
exposureCriteria,
openExposureCriteriaModal,
resolvedExposureEvent = EXPOSURE_DEFAULT_EVENT,
}: {
metric: ExperimentMetric
isSharedMetric?: boolean
handleSetMetric: (newMetric: ExperimentMetric) => void
filterTestAccounts: boolean
exposureCriteria?: ExperimentExposureCriteria | undefined
openExposureCriteriaModal?: (exposureCriteria?: ExperimentExposureCriteria) => void
/**
* The experiment's server-resolved default exposure event. Omitted for shared metrics, which
* aren't tied to one experiment and so have no resolvable event.
*/
resolvedExposureEvent?: string
}): 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
const previousRetentionStart = useRef<{
start_event: ExperimentRetentionStart
start_handling: 'first_seen' | 'last_seen'
conversion_window?: number
conversion_window_unit?: FunnelConversionWindowTimeUnit
} | null>(null)
const getEventTypeLabel = (): string => {
if (isExperimentMeanMetric(metric)) {
return metric.source.kind === NodeKind.ActionsNode ? 'actions' : 'events'
} else if (isExperimentFunnelMetric(metric)) {
const lastStep = metric.series[metric.series.length - 1]
return lastStep?.kind === NodeKind.ActionsNode ? 'actions' : 'events'
} else if (isExperimentRatioMetric(metric)) {
return 'events'
}
return 'events'
}
const handleSetSeries = (series: SeriesNode[]): void => {
const metricConfig = seriesToMetricConfig(metric.metric_type, series)
if (metricConfig) {
const updatedMetric = { ...metric, ...metricConfig }
/**
* Switching to a math type that doesn't support thresholds must clear any stale
* threshold, otherwise the disabled threshold input traps the value and outlier
* handling stays disabled with no way to recover.
*/
if (isExperimentMeanMetric(updatedMetric) && !isThresholdAvailableForMath(updatedMetric.source.math)) {
updatedMetric.threshold = undefined
}
handleSetMetric(updatedMetric)
}
}
const handleMetricTypeChange = (newMetricType: ExperimentMetricType): void => {
// Extract current sources from the existing metric to preserve selections
let sources: ExperimentMetricSource[] = []
if (isExperimentMeanMetric(metric)) {
sources = [metric.source]
} else if (isExperimentFunnelMetric(metric)) {
sources = metric.series
} else if (isExperimentRatioMetric(metric)) {
sources = [metric.numerator]
if (metric.denominator) {
sources.push(metric.denominator)
}
} else if (isExperimentRetentionMetric(metric)) {
// An exposure-anchored start has no literal event to carry over
if (!isExperimentExposureNode(metric.start_event)) {
sources = [metric.start_event]
}
if (metric.completion_event) {
sources.push(metric.completion_event)
}
}
const newMetric = getDefaultExperimentMetric(newMetricType)
// Apply the existing sources to the new metric type to preserve selections
if (sources.length > 0 && sources[0]) {
if (newMetricType === ExperimentMetricType.MEAN && isExperimentMeanMetric(newMetric)) {
newMetric.source = sources[0]
} else if (newMetricType === ExperimentMetricType.FUNNEL && isExperimentFunnelMetric(newMetric)) {
newMetric.series = sources.filter(isPickerSource)
} else if (newMetricType === ExperimentMetricType.RATIO && isExperimentRatioMetric(newMetric)) {
newMetric.numerator = sources[0]
} else if (newMetricType === ExperimentMetricType.RETENTION && isExperimentRetentionMetric(newMetric)) {
if (isPickerSource(sources[0])) {
newMetric.start_event = sources[0]
}
if (isPickerSource(sources[1])) {
newMetric.completion_event = sources[1]
}
}
}
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.',
},
]
// dependencies for the loadEventCount useEffect call
const meanSource = isExperimentMeanMetric(metric) ? metric.source : null
const funnelSeries = isExperimentFunnelMetric(metric) ? metric.series : null
const ratioNumerator = isExperimentRatioMetric(metric) ? metric.numerator : null
const ratioDenominator = isExperimentRatioMetric(metric) ? metric.denominator : null
const retentionStartEvent = isExperimentRetentionMetric(metric) ? metric.start_event : null
const retentionCompletionEvent = isExperimentRetentionMetric(metric) ? metric.completion_event : null
useEffect(() => {
loadEventCount(metric, filterTestAccounts, setEventCount, setIsLoading)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
metric.metric_type,
meanSource,
funnelSeries,
ratioNumerator,
ratioDenominator,
retentionStartEvent,
retentionCompletionEvent,
filterTestAccounts,
])
const hideDeleteBtn = (_: any, index: number): boolean => index === 0
const activationConfig = getActivationConfig(exposureCriteria)
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
data-attr="metrics-selector"
value={metric.metric_type}
onChange={handleMetricTypeChange}
options={radioOptions}
/>
</div>
</SceneSection>
<SceneDivider />
<SceneSection title="Metric" className="max-w-prose">
<LemonBanner
type="info"
action={
exposureCriteria && openExposureCriteriaModal
? {
size: 'xsmall',
onClick: () => openExposureCriteriaModal(exposureCriteria),
icon: <IconPencil />,
tooltip: 'Edit exposure criteria',
}
: undefined
}
>
<span className="inline-flex items-center gap-2 flex-wrap">
{exposureCriteria ? (
<>
Counts only after exposure event{' '}
<LemonTag>{getExposureCriteriaLabel(exposureCriteria, resolvedExposureEvent)}</LemonTag>
{activationConfig && (
<>
and activation event{' '}
<LemonTag>{getExposureConfigDisplayName(activationConfig)}</LemonTag>
</>
)}
</>
) : usesWarehouseNative ? (
<>Counts only after exposure. Each experiment sets its own exposure query.</>
) : (
<>
Counts only after exposure event (<LemonTag>{resolvedExposureEvent}</LemonTag> by
default)
</>
)}
<Tooltip
docLink="https://posthog.com/docs/experiments/exposures"
title={
<div className="space-y-2">
<p>
Metrics are only counted for users who have been exposed to the experiment. This
ensures fair comparison between control and test groups.
</p>
<p>
{exposureCriteria
? 'The exposure event is shared across all metrics in this experiment.'
: 'The exposure event will be configured at the experiment level and shared across all metrics.'}
</p>
</div>
}
>
<IconInfo className="text-muted-alt text-base" />
</Tooltip>
</span>
</LemonBanner>
{isExperimentMeanMetric(metric) && (
<>
{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
should return a numeric value and will be evaluated for each user in the experiment.{' '}
<Link to="https://posthog.com/docs/hogql/expressions" target="_blank">
Learn more about HogQL expressions
</Link>
</div>
)}
<ExperimentMetricThreshold
math={metric.source.math}
value={metric.threshold}
onChange={(value) =>
handleSetMetric({
...metric,
threshold: value,
/**
* Setting a threshold disables outlier handling, so clear any stale
* bounds to keep the metric consistent with the UI and pass validation.
*/
...(value !== undefined && {
lower_bound_percentile: undefined,
upper_bound_percentile: undefined,
ignore_zeros: undefined,
}),
})
}
/>
</>
)}
{isExperimentFunnelMetric(metric) && (
<SeriesActionFilter
bordered
series={metricSourcesToSeries(metric.series)}
trendsDisplayCategory={ChartDisplayCategory.TimeSeries}
dataWarehouseNodeKind={NodeKind.ExperimentDataWarehouseNode}
onChange={handleSetSeries}
typeKey="experiment-metric"
buttonCopy="Add step"
showSeriesIndicator={false}
hideRename={false}
hideDeleteBtn={hideDeleteBtn}
sortable={true}
showNestedArrow={true}
// showNumericalPropsOnly={true}
mathAvailability={mathAvailability}
allowedMathTypes={allowedMathTypes}
{...commonActionFilterProps}
dataWarehousePopoverFields={dataWarehousePopoverFields}
/>
)}
{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>
<SeriesActionFilter
bordered
series={metricSourcesToSeries([metric.numerator])}
trendsDisplayCategory={ChartDisplayCategory.TimeSeries}
dataWarehouseNodeKind={NodeKind.ExperimentDataWarehouseNode}
onChange={(series) => {
const source = seriesToMetricSource(series)
if (source) {
handleSetMetric({
...metric,
numerator: source,
})
}
}}
typeKey="experiment-metric-numerator"
buttonCopy="Add numerator event"
showSeriesIndicator={false}
hideRename={true}
entitiesLimit={1}
showNumericalPropsOnly={true}
mathAvailability={mathAvailability}
allowedMathTypes={allowedMathTypes}
dataWarehousePopoverFields={dataWarehousePopoverFields}
{...commonActionFilterProps}
/>
</div>
<div>
<LemonLabel className="mb-1">Denominator (what you're dividing by)</LemonLabel>
<SeriesActionFilter
bordered
series={metricSourcesToSeries([metric.denominator])}
trendsDisplayCategory={ChartDisplayCategory.TimeSeries}
dataWarehouseNodeKind={NodeKind.ExperimentDataWarehouseNode}
onChange={(series) => {
const source = seriesToMetricSource(series)
if (source) {
handleSetMetric({
...metric,
denominator: source,
})
}
}}
typeKey="experiment-metric-denominator"
buttonCopy="Add denominator event"
showSeriesIndicator={false}
hideRename={true}
entitiesLimit={1}
mathAvailability={mathAvailability}
allowedMathTypes={allowedMathTypes}
dataWarehousePopoverFields={dataWarehousePopoverFields}
{...commonActionFilterProps}
/>
</div>
</div>
)}
{isExperimentRetentionMetric(metric) && (
<div className="space-y-4">
<div>
<LemonLabel className="mb-1">
Start event
<Tooltip title="The event that triggers retention tracking. Retention is measured from when users perform this event.">
<IconInfo className="ml-1 text-muted" />
</Tooltip>
</LemonLabel>
<LemonRadio
className="my-1.5"
data-attr="experiment-retention-start-source"
orientation="horizontal"
value={isExperimentExposureNode(metric.start_event) ? 'exposure' : 'custom'}
onChange={(value) => {
if (value === 'exposure') {
if (!isExperimentExposureNode(metric.start_event)) {
previousRetentionStart.current = {
start_event: metric.start_event,
start_handling: metric.start_handling,
conversion_window: metric.conversion_window,
conversion_window_unit: metric.conversion_window_unit,
}
}
// the backend rejects a conversion window or 'last_seen' start handling on an exposure start
handleSetMetric({
...metric,
start_event: { kind: NodeKind.ExperimentExposureNode },
start_handling: 'first_seen',
conversion_window: undefined,
conversion_window_unit: undefined,
})
} else {
const previous = previousRetentionStart.current
handleSetMetric({
...metric,
start_event: previous?.start_event ?? {
kind: NodeKind.EventsNode,
event: '$pageview',
name: '$pageview',
math: ExperimentMetricMathType.TotalCount,
},
start_handling: previous?.start_handling ?? 'first_seen',
conversion_window: previous?.conversion_window,
conversion_window_unit: previous?.conversion_window_unit,
})
}
}}
options={[
{ value: 'custom', label: 'Custom event' },
{ value: 'exposure', label: 'Experiment exposure' },
]}
/>
{isExperimentExposureNode(metric.start_event) ? (
<div className="text-muted text-xs">
{isSharedMetric
? "Retention starts from each user's first exposure. Each experiment using this metric anchors on its own exposure event."
: "Retention starts from each user's first exposure to the experiment."}
</div>
) : (
<SeriesActionFilter
bordered
series={metricSourcesToSeries([metric.start_event])}
trendsDisplayCategory={ChartDisplayCategory.TimeSeries}
dataWarehouseNodeKind={NodeKind.ExperimentDataWarehouseNode}
onChange={(series) => {
const source = seriesToMetricSource(series)
if (source) {
handleSetMetric({ ...metric, start_event: source })
}
}}
typeKey="experiment-metric-start-event"
buttonCopy="Add start event"
showSeriesIndicator={false}
hideRename={true}
entitiesLimit={1}
showNumericalPropsOnly={true}
mathAvailability={mathAvailability}
allowedMathTypes={allowedMathTypes}
dataWarehousePopoverFields={dataWarehousePopoverFields}
{...commonActionFilterProps}
/>
)}
</div>
<div>
<LemonLabel className="mb-1">
Completion event
<Tooltip title="The event that indicates a user was 'retained'. We check if users who performed the start event also perform this event within the retention window.">
<IconInfo className="ml-1 text-muted" />
</Tooltip>
</LemonLabel>
<SeriesActionFilter
bordered
series={metricSourcesToSeries([metric.completion_event])}
trendsDisplayCategory={ChartDisplayCategory.TimeSeries}
dataWarehouseNodeKind={NodeKind.ExperimentDataWarehouseNode}
onChange={(series) => {
const source = seriesToMetricSource(series)
if (source) {
handleSetMetric({ ...metric, completion_event: source })
}
}}
typeKey="experiment-metric-completion-event"
buttonCopy="Add completion event"
showSeriesIndicator={false}
hideRename={true}
entitiesLimit={1}
showNumericalPropsOnly={true}
mathAvailability={mathAvailability}
allowedMathTypes={allowedMathTypes}
dataWarehousePopoverFields={dataWarehousePopoverFields}
{...commonActionFilterProps}
/>
</div>
<div>
<LemonLabel className="mb-1">
Retention window
<Tooltip
title={
<div className="space-y-2">
<p>
The time period after the start event during which we check for the
completion event.
</p>
<p>
"From 7 to 14 days" captures completions from day 7 through day 14
(inclusive).
</p>
</div>
}
>
<IconInfo className="ml-1 text-muted" />
</Tooltip>
</LemonLabel>
<div className="flex items-center gap-2">
<span className="text-muted">From</span>
<LemonInput
type="number"
min={0}
value={metric.retention_window_start}
onChange={(value) => {
handleSetMetric({
...metric,
retention_window_start: Number(value),
})
}}
className="w-20"
/>
<span className="text-muted">to</span>
<LemonInput
type="number"
min={metric.retention_window_start || 0}
value={metric.retention_window_end}
onChange={(value) => {
handleSetMetric({
...metric,
retention_window_end: Number(value),
})
}}
className="w-20"
/>
<LemonSelect
value={metric.retention_window_unit || 'day'}
onChange={(value) => {
handleSetMetric({
...metric,
retention_window_unit: value as FunnelConversionWindowTimeUnit,
})
}}
options={[
{ label: 'days', value: FunnelConversionWindowTimeUnit.Day },
{ label: 'hours', value: FunnelConversionWindowTimeUnit.Hour },
]}
/>
</div>
<span className="text-muted text-xs">
{isExperimentExposureNode(metric.start_event)
? "After the user's first exposure"
: 'After the start event'}
</span>
</div>
{!isExperimentExposureNode(metric.start_event) && (
<div>
<LemonLabel className="mb-1">
When users have multiple start events
<Tooltip title="Choose how to handle users who perform the start event multiple times during the experiment.">
<IconInfo className="ml-1 text-muted" />
</Tooltip>
</LemonLabel>
<LemonSelect
value={metric.start_handling || 'first_seen'}
onChange={(value) => {
handleSetMetric({
...metric,
start_handling: value as 'first_seen' | 'last_seen',
})
}}
options={[
{
label: 'Use first start event',
value: 'first_seen',
},
{
label: 'Use last start event',
value: 'last_seen',
},
]}
/>
</div>
)}
</div>
)}
</SceneSection>
<SceneDivider />
<SceneSection title="Goal" className="max-w-prose">
<div className="flex flex-col gap-1">
<LemonSelect<ExperimentMetricGoal>
value={metric.goal || ExperimentMetricGoal.Increase}
onChange={(value) => handleSetMetric({ ...metric, goal: value })}
options={[
{ value: ExperimentMetricGoal.Increase, label: 'Increase' },
{ value: ExperimentMetricGoal.Decrease, label: 'Decrease' },
]}
/>
<div className="text-muted text-xs mt-1">
For example, conversion rates should increase, while bounce rates should decrease.
</div>
</div>
</SceneSection>
<SceneDivider />
{/* The backend ignores the conversion window for exposure-anchored retention: the retention window already anchors on exposure */}
{!(isExperimentRetentionMetric(metric) && isExperimentExposureNode(metric.start_event)) && (
<>
<ExperimentMetricConversionWindowFilter metric={metric} handleSetMetric={handleSetMetric} />
<SceneDivider />
</>
)}
{isExperimentFunnelMetric(metric) && (
<>
<ExperimentMetricFunnelOrderSelector metric={metric} handleSetMetric={handleSetMetric} />
<SceneDivider />
</>
)}
{isExperimentMeanMetric(metric) && (
<>
<ExperimentMetricOutlierHandling metric={metric} handleSetMetric={handleSetMetric} />
{!usesWarehouseNative && <SceneDivider />}
</>
)}
{isExperimentRatioMetric(metric) && (
<>
<ExperimentRatioMetricOutlierHandling metric={metric} handleSetMetric={handleSetMetric} />
{!usesWarehouseNative && <SceneDivider />}
</>
)}
{!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>
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'}
</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>
)
}