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.

1.6 KB
Raw Blame
import { PathsV2StepSource } from '~/queries/schema/schema-general'

export interface StepSourcePreset {
    key: 'pageViews' | 'screenViews'
    label: string
    stepSources: PathsV2StepSource[]
}

/** One-click step source configurations, replacing v1's checkbox event-type buckets. */
export const STEP_SOURCE_PRESETS: Record<StepSourcePreset['key'], StepSourcePreset> = {
    pageViews: {
        key: 'pageViews',
        label: 'Page views',
        stepSources: [{ event: '$pageview', namingProperty: '$pathname' }],
    },
    screenViews: {
        key: 'screenViews',
        label: 'Screen views',
        stepSources: [{ event: '$screen', namingProperty: '$screen_name' }],
    },
}

/** What the runner defaults absent step sources to: the page views preset. */
export const DEFAULT_STEP_SOURCES = STEP_SOURCE_PRESETS.pageViews.stepSources

/** The preset matching the given step sources, or null when they were customized. */
export function presetForStepSources(stepSources: PathsV2StepSource[] | undefined): StepSourcePreset | null {
    if (!stepSources) {
        // The runner defaults absent step sources to the page views preset.
        return STEP_SOURCE_PRESETS.pageViews
    }
    for (const preset of Object.values(STEP_SOURCE_PRESETS)) {
        if (
            stepSources.length === preset.stepSources.length &&
            preset.stepSources.every(
                (source, index) =>
                    stepSources[index].event === source.event &&
                    (stepSources[index].namingProperty ?? null) === (source.namingProperty ?? null)
            )
        ) {
            return preset
        }
    }
    return null
}