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.

9.2 KB
Raw Blame
import { useActions, useValues } from 'kea'

import { IconPencil } from '@posthog/icons'
import { LemonButton, LemonCheckbox, LemonSelect, LemonTag, Link } from '@posthog/lemon-ui'

import { ObjectTags } from 'lib/components/ObjectTags/ObjectTags'
import { userHasAccess } from 'lib/utils/accessControlUtils'
import { LinkedHogFunctions } from 'scenes/hog-functions/list/LinkedHogFunctions'
import { experimentsConfigLogic } from 'scenes/settings/environment/experimentsConfigLogic'
import { urls } from 'scenes/urls'

import { tagsModel } from '~/models/tagsModel'
import {
    AccessControlLevel,
    AccessControlResourceType,
    ExperimentStatsMethod,
    PropertyFilterType,
    PropertyOperator,
} from '~/types'

import { DEFAULT_LOOKBACK_DAYS } from 'products/experiments/frontend/constants'
import { CupedModal } from 'products/experiments/frontend/modals/CupedModal/CupedModal'
import { StatsMethodModal } from 'products/experiments/frontend/modals/StatsMethodModal/StatsMethodModal'

import { experimentLogic } from '../experimentLogic'
import { modalsLogic } from '../modalsLogic'
import { getBaselineVariantKey } from '../utils'
import { getCupedSelection, resolveCupedEnabled, resolveCupedLookbackDays } from './cuped'
import { resolveSequentialEnabled } from './sequential'

export function SettingsTab(): JSX.Element {
    const { experiment, statsMethod, variants, experimentUpdateLoading } = useValues(experimentLogic)
    const { updateExperiment, updateExperimentSettings } = useActions(experimentLogic)
    const { openStatsEngineModal, openCupedModal } = useActions(modalsLogic)
    const { experimentsConfig } = useValues(experimentsConfigLogic)
    const { tags: allExistingTags } = useValues(tagsModel)

    const canEditExperiment = userHasAccess(
        AccessControlResourceType.Experiment,
        AccessControlLevel.Editor,
        experiment.user_access_level
    )

    const isBayesian = statsMethod === ExperimentStatsMethod.Bayesian

    const confidenceDisplay = isBayesian
        ? `${((experiment.stats_config?.bayesian?.ci_level ?? 0.95) * 100).toFixed(0)}%`
        : `${((1 - (experiment.stats_config?.frequentist?.alpha ?? 0.05)) * 100).toFixed(0)}%`

    const teamDefaultCupedEnabled = experimentsConfig?.default_cuped_enabled ?? false
    const teamDefaultCupedLookbackDays = experimentsConfig?.default_cuped_lookback_days ?? null
    const cupedExplicitlySet = getCupedSelection(experiment.stats_config?.cuped) !== 'default'
    const cupedEnabled = resolveCupedEnabled(experiment.stats_config?.cuped, teamDefaultCupedEnabled)
    const cupedLookbackDays = resolveCupedLookbackDays(
        experiment.stats_config?.cuped,
        teamDefaultCupedLookbackDays,
        DEFAULT_LOOKBACK_DAYS
    )

    const teamDefaultSequentialEnabled = experimentsConfig?.default_sequential_testing_enabled ?? false
    const sequentialEnabled = resolveSequentialEnabled(
        experiment.stats_config?.frequentist,
        teamDefaultSequentialEnabled
    )

    const returnTo = urls.experiment(experiment.id)

    // Only show alerts section for saved experiments, as the alert relies on experiment.id for filtering
    const shouldShowSignificanceAlerts = typeof experiment.id === 'number'

    return (
        <div className="flex flex-col gap-8">
            <div>
                <h2 className="font-semibold text-lg">Tags</h2>
                {canEditExperiment ? (
                    <ObjectTags
                        tags={experiment.tags ?? []}
                        // Not updateExperimentSettings: tags don't affect metric
                        // computation, so don't trigger its results refresh.
                        onChange={(tags) => updateExperiment({ tags })}
                        saving={experimentUpdateLoading}
                        tagsAvailable={allExistingTags.filter((tag: string) => !experiment.tags?.includes(tag))}
                        data-attr="experiment-tags"
                    />
                ) : (
                    <ObjectTags tags={experiment.tags ?? []} staticOnly data-attr="experiment-tags" />
                )}
            </div>
            <div>
                <h2 className="font-semibold text-lg">Statistics</h2>
                <div className="flex items-center gap-2">
                    <span>
                        {isBayesian ? 'Bayesian' : 'Frequentist'} / {confidenceDisplay}
                        {!isBayesian && sequentialEnabled && ' · Sequential testing'}
                    </span>
                    <LemonButton
                        type="secondary"
                        size="xsmall"
                        icon={<IconPencil />}
                        onClick={openStatsEngineModal}
                        tooltip="Edit statistics settings"
                        aria-label="Edit statistics settings"
                    />
                </div>
                <StatsMethodModal />
            </div>
            <div>
                <h2 className="font-semibold text-lg">CUPED</h2>
                <div className="flex items-center gap-2">
                    <LemonTag type={cupedEnabled ? 'success' : 'default'}>
                        {cupedEnabled ? 'Enabled' : 'Disabled'}
                    </LemonTag>
                    {cupedEnabled && <span>{cupedLookbackDays}-day lookback</span>}
                    <LemonButton
                        type="secondary"
                        size="xsmall"
                        icon={<IconPencil />}
                        onClick={openCupedModal}
                        tooltip="Edit CUPED settings"
                        aria-label="Edit CUPED settings"
                    />
                </div>
                <p className="text-muted text-xs mt-1">
                    Use pre-experiment data to detect significant effects faster. Currently supported for mean and
                    funnel metrics.{' '}
                    {!cupedExplicitlySet && (
                        <>
                            Default is set in{' '}
                            <Link to={urls.settings('environment-experiments', 'environment-experiment-cuped-enabled')}>
                                environment settings
                            </Link>
                            .
                        </>
                    )}
                </p>
                <CupedModal />
            </div>
            <div>
                <h2 className="font-semibold text-lg">Baseline variant</h2>
                <LemonSelect
                    value={getBaselineVariantKey(experiment)}
                    loading={experimentUpdateLoading}
                    options={variants.map((v) => ({
                        value: v.key,
                        label: v.key,
                    }))}
                    onChange={(value) => {
                        updateExperimentSettings({
                            stats_config: { ...experiment.stats_config, baseline_variant_key: value },
                        })
                    }}
                />
                <p className="text-muted text-xs mt-1">The variant all others are compared against.</p>
            </div>
            <div>
                <h2 className="font-semibold text-lg">Conversion windows</h2>
                <div className="flex items-center gap-2">
                    <LemonCheckbox
                        label="Require completed conversion or retention window"
                        checked={experiment.only_count_matured_users ?? false}
                        disabled={experimentUpdateLoading}
                        onChange={(checked) => {
                            updateExperimentSettings({ only_count_matured_users: checked })
                        }}
                    />
                </div>
                <p className="text-muted text-xs mt-1">
                    Exclude participants whose conversion or retention window hasn't elapsed yet. Default is set in{' '}
                    <Link to={urls.settings('environment-experiments', 'environment-experiment-matured-users')}>
                        environment settings
                    </Link>
                    .
                </p>
            </div>
            {shouldShowSignificanceAlerts && (
                <div>
                    <h2 className="font-semibold text-lg">Notifications</h2>
                    <p className="text-muted text-xs mt-1">Get notified when a metric reaches significance.</p>
                    <LinkedHogFunctions
                        type="internal_destination"
                        subTemplateIds={['experiment-significant']}
                        forceFilterGroups={[
                            {
                                events: [{ id: '$experiment_metric_significant', type: 'events' }],
                                properties: [
                                    {
                                        key: 'experiment_id',
                                        type: PropertyFilterType.Event,
                                        value: experiment.id,
                                        operator: PropertyOperator.Exact,
                                    },
                                ],
                            },
                        ]}
                        queryParams={{ returnTo }}
                    />
                </div>
            )}
        </div>
    )
}