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.

45.1 KB
Raw Blame
import {
    LogicWrapper,
    key,
    props,
    MakeLogicType,
    actions,
    afterMount,
    connect,
    isBreakpoint,
    kea,
    listeners,
    path,
    reducers,
    selectors,
} from 'kea'
import { subscriptions } from 'kea-subscriptions'

import { dayjs } from 'lib/dayjs'
import { escapeHogQLString } from 'lib/utils/hogql'
import { isAbortedRequest } from 'lib/utils/requests'
import { pluralize } from 'lib/utils/strings'
import { teamLogic } from 'scenes/teamLogic'

import { actionsModel } from '~/models/actionsModel'
import { dataNodeCollectionLogic } from '~/queries/nodes/DataNode/dataNodeCollectionLogic'
import { performQuery } from '~/queries/query'
import {
    ActionConversionGoal,
    CompareFilter,
    CustomEventConversionGoal,
    DataTableNode,
    HogQLQuery,
    InsightVizNode,
    NodeKind,
    TrendsFilter,
    WebAnalyticsConversionGoal,
    WebAnalyticsOrderByFields,
    WebAnalyticsPropertyFilters,
    WebBotsBreakdown,
    WebStatsBreakdown,
    WebStatsTableQuery,
} from '~/queries/schema/schema-general'
import { ActionType, BaseMathType, ChartDisplayType, InsightLogicProps, TeamType } from '~/types'

import {
    AI_VIEW,
    CRAWLER,
    GOOGLE_VIEW,
    HUMAN_EVENTS,
    HUMAN_VIEW,
    PAGE_PERFORMANCE_EVENTS,
    PagePerformanceMetric,
    PagePerformanceWindow,
    RAW_PATH,
    AI_CHANNEL_FILTER,
    AI_CRAWLER_FILTERS,
    aiCrawlerRequestSeries,
    buildBreakdownQuery,
    buildPagePerformancePathExpr,
    pageCandidatesPredicate,
    pageKeyExpr,
    resolvePagePerformanceWindow,
    windowPredicates,
} from 'products/web_analytics/frontend/searchAndAi/searchAndAiQueries'

import type { TeamPublicType } from '../../types'
import {
    TileId,
    WEB_ANALYTICS_DATA_COLLECTION_NODE_ID,
    WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
    getWebAnalyticsBreakdownFilter,
    loadPriorityMap,
} from './common'
import { getDashboardItemId } from './insightsUtils'
import { webAnalyticsLogic } from './webAnalyticsLogic'
import type { DateFilterState } from './webAnalyticsLogic'
import { WebAnalyticsLogicProps } from './webAnalyticsLogicProps'

const PAGE_TABLE_LIMIT = 20

const PAGE_CANDIDATE_LIMIT = 200

export interface PagePerformanceBreakdownState {
    page: string
    metric: PagePerformanceMetric
}

export interface PagePerformanceOrderBy {
    column: string
    direction: 'ASC' | 'DESC'
}

export interface OverviewTotals {
    visitors: number
    visitorsPrevious: number
    google: number
    googlePrevious: number
    llm: number
    llmPrevious: number
    crawls: number
    crawlsPrevious: number
    pages: number
    /** Forwarded access-log events. Without them the crawler numbers can only ever be zero. */
    serverLogs: number
}

export type PagePerformanceBucket = 'hour' | 'day' | 'week'

export interface OverviewSeriesPoint {
    label: string
    visitors: number
    google: number
    llm: number
    crawls: number
}

export type OverviewMetricKey = 'visitors' | 'google_search' | 'llm_referrals' | 'agent_crawls'

export interface OverviewMetric {
    key: OverviewMetricKey
    label: string
    value: number
    previous: number | null
    changeFromPreviousPct: number | null
    sparkline: number[]
    sparklineLabels: string[]
}

export interface AiSectionQueries {
    referralTrend: InsightVizNode
    byEngine: DataTableNode
    landingPages: DataTableNode
    crawlerTrend: InsightVizNode
    byCrawler: DataTableNode
    crawledPages: DataTableNode
}

const OVERVIEW_METRICS: {
    key: OverviewMetricKey
    label: string
    value: (totals: OverviewTotals) => number
    previous: (totals: OverviewTotals) => number
    series: (point: OverviewSeriesPoint) => number
}[] = [
    {
        key: 'visitors',
        label: 'Visitors',
        value: (t) => t.visitors,
        previous: (t) => t.visitorsPrevious,
        series: (p) => p.visitors,
    },
    {
        key: 'google_search',
        label: 'Google search',
        value: (t) => t.google,
        previous: (t) => t.googlePrevious,
        series: (p) => p.google,
    },
    {
        key: 'llm_referrals',
        label: 'AI referrals',
        value: (t) => t.llm,
        previous: (t) => t.llmPrevious,
        series: (p) => p.llm,
    },
    {
        key: 'agent_crawls',
        label: 'AI crawls',
        value: (t) => t.crawls,
        previous: (t) => t.crawlsPrevious,
        series: (p) => p.crawls,
    },
]

const EMPTY_OVERVIEW_TOTALS: OverviewTotals = {
    visitors: 0,
    visitorsPrevious: 0,
    google: 0,
    googlePrevious: 0,
    llm: 0,
    llmPrevious: 0,
    crawls: 0,
    crawlsPrevious: 0,
    pages: 0,
    serverLogs: 0,
}

export type PagePerformanceTabState = 'loading' | 'ready' | 'no-events' | 'no-traffic-in-range'
export type PagePerformanceAiTrafficState = 'loading' | 'ready' | 'empty'
export type PagePerformanceCrawlerState = 'loading' | 'ready' | 'empty' | 'needs-server-logs'

export interface PagePerformanceDataState {
    tab: PagePerformanceTabState
    aiTraffic: PagePerformanceAiTrafficState
    crawlers: PagePerformanceCrawlerState
}

const READY_DATA_STATE: PagePerformanceDataState = { tab: 'ready', aiTraffic: 'ready', crawlers: 'ready' }

/**
 * Which of the tab's three stories can actually be told from the data at hand. A zero here is
 * ambiguous: crawlers never run JavaScript, so no forwarded access logs means no crawler rows are
 * possible, whatever the site's real crawler traffic is. Separating that from a genuine zero keeps
 * the tab from reporting an instrumentation gap as a fact about the world.
 */
export const resolvePagePerformanceDataState = (
    totals: OverviewTotals | null,
    hasIngestedEvents: boolean
): PagePerformanceDataState => {
    if (!totals) {
        return { tab: 'loading', aiTraffic: 'loading', crawlers: 'loading' }
    }
    if (totals.visitors === 0 && totals.pages === 0 && totals.crawls === 0 && totals.serverLogs === 0) {
        return {
            tab: hasIngestedEvents ? 'no-traffic-in-range' : 'no-events',
            aiTraffic: 'empty',
            crawlers: hasIngestedEvents ? 'needs-server-logs' : 'empty',
        }
    }
    return {
        tab: 'ready',
        aiTraffic: totals.llm > 0 ? 'ready' : 'empty',
        crawlers: totals.crawls > 0 ? 'ready' : totals.serverLogs > 0 ? 'empty' : 'needs-server-logs',
    }
}

const conversionMatch = (conversionGoal: WebAnalyticsConversionGoal | null): string | null => {
    if (conversionGoal && 'actionId' in conversionGoal) {
        return `matchesAction(${(conversionGoal as ActionConversionGoal).actionId})`
    }
    if (conversionGoal && 'customEventName' in conversionGoal) {
        return `event = ${escapeHogQLString((conversionGoal as CustomEventConversionGoal).customEventName)}`
    }
    return null
}

export interface ParsedOverviewResponse {
    totals: Record<string, number>
    buckets: { bucket: dayjs.Dayjs; values: Record<string, number> }[]
}

/**
 * Splits an overview response into its grand total and its per-bucket series. The queries group by
 * `GROUPING SETS ((bucket), ())`, so one row carries the exact total over the whole window — summing
 * the buckets would instead sum per-bucket uniques and overcount visitors.
 */
export const parsePagePerformanceOverviewResponse = (
    columns: string[] | undefined,
    results: unknown[][] | undefined,
    window: PagePerformanceWindow
): ParsedOverviewResponse => {
    const cols = columns ?? []
    const bucketIdx = cols.indexOf('bucket')
    const totals: Record<string, number> = {}
    const buckets: ParsedOverviewResponse['buckets'] = []

    const readRow = (row: unknown[]): Record<string, number> => {
        const values: Record<string, number> = {}
        cols.forEach((name, idx) => {
            if (name !== 'bucket') {
                values[name] = Number(row[idx] ?? 0)
            }
        })
        return values
    }

    for (const row of results ?? []) {
        if (!Array.isArray(row)) {
            continue
        }
        const rawBucket = bucketIdx >= 0 ? row[bucketIdx] : null
        const bucket = rawBucket ? dayjs.tz(String(rawBucket), window.timezone) : null
        // The grand-total row comes back with the DateTime default rather than a real bucket.
        if (!bucket || !bucket.isValid() || bucket.year() <= 1970) {
            Object.assign(totals, readRow(row))
            continue
        }
        if (window.currentFrom && bucket.isBefore(window.currentFrom)) {
            continue
        }
        buckets.push({ bucket, values: readRow(row) })
    }

    buckets.sort((a, b) => a.bucket.valueOf() - b.bucket.valueOf())
    return { totals, buckets }
}

/** Aligns the human and crawler series onto one bucket axis — either query can miss a bucket entirely. */
export const mergePagePerformanceSeries = (
    human: ParsedOverviewResponse,
    crawler: ParsedOverviewResponse
): OverviewSeriesPoint[] => {
    const byBucket = new Map<number, OverviewSeriesPoint>()

    const upsert = (bucket: dayjs.Dayjs): OverviewSeriesPoint => {
        const key = bucket.valueOf()
        const existing = byBucket.get(key)
        if (existing) {
            return existing
        }
        const point: OverviewSeriesPoint = {
            label: bucket.toISOString(),
            visitors: 0,
            google: 0,
            llm: 0,
            crawls: 0,
        }
        byBucket.set(key, point)
        return point
    }

    for (const { bucket, values } of human.buckets) {
        const point = upsert(bucket)
        point.visitors = values.visitors ?? 0
        point.google = values.google ?? 0
        point.llm = values.llm ?? 0
    }
    for (const { bucket, values } of crawler.buckets) {
        upsert(bucket).crawls = values.crawls ?? 0
    }

    return [...byBucket.entries()].sort(([a], [b]) => a - b).map(([, point]) => point)
}

export const resolvePagePerformanceBucket = (window: PagePerformanceWindow): PagePerformanceBucket => {
    if (!window.currentFrom) {
        return 'week'
    }
    const days = window.currentTo.diff(window.currentFrom, 'day', true)
    if (days <= 2) {
        return 'hour'
    }
    if (days <= 120) {
        return 'day'
    }
    return 'week'
}

const BUCKET_HOGQL_FN: Record<PagePerformanceBucket, string> = {
    hour: 'toStartOfHour',
    day: 'toStartOfDay',
    week: 'toStartOfWeek',
}

export interface MetricCellValue {
    current: number
    previous: number
}

/** Every leaderboard metric column is `tuple(current, previous)`; agent crawls is `tuple(crawls, agents)`. */
export const parseMetricCell = (value: unknown): MetricCellValue | null =>
    Array.isArray(value) && value.length >= 2
        ? { current: Number(value[0] ?? 0), previous: Number(value[1] ?? 0) }
        : null

const SORTABLE_COLUMNS = new Set([
    'breakdown_value',
    'visitors',
    'google_search',
    'llm_referrals',
    'agent_crawls',
    'conversions',
    'avg_time',
])
const orderByExpr = (column: string): string =>
    `"context.columns.${SORTABLE_COLUMNS.has(column) ? column : 'visitors'}"`

/**
 * In the page_durations subquery, `{filters}` matches the carrier event (the NEXT pageview, or the
 * pageleave), not the measured page, so an event property that varies per pageview (e.g. $pathname)
 * can skew avg_time. Session- and person-scoped filters are stable across the pair and stay coherent.
 */
export const buildPagePerformanceTableQuery = (
    window: PagePerformanceWindow,
    orderBy: PagePerformanceOrderBy,
    conversionGoal: WebAnalyticsConversionGoal | null,
    pathExpr: string,
    previousPathExpr: string,
    candidates: string[]
): string => {
    const { cur, prev, full } = windowPredicates(window)
    const pageKey = pageKeyExpr(pathExpr)
    const previousPageKey = pageKeyExpr(previousPathExpr)
    const candidatePredicate = pageCandidatesPredicate(pageKeyExpr(RAW_PATH), candidates)
    const previousCandidatePredicate = pageCandidatesPredicate(
        pageKeyExpr('properties.$prev_pageview_pathname'),
        candidates
    )

    const match = conversionMatch(conversionGoal)
    const conversions = match ? `tuple(countIf((${match}) AND ${cur}), countIf((${match}) AND ${prev}))` : 'tuple(0, 0)'

    const orderExpr = orderByExpr(orderBy.column)

    return `
SELECT
    ${pageKey} AS "context.columns.breakdown_value",
    tuple(
        uniqIf(person_id, (${HUMAN_VIEW}) AND ${cur}),
        uniqIf(person_id, (${HUMAN_VIEW}) AND ${prev})
    ) AS "context.columns.visitors",
    tuple(
        uniqIf(person_id, (${GOOGLE_VIEW}) AND ${cur}),
        uniqIf(person_id, (${GOOGLE_VIEW}) AND ${prev})
    ) AS "context.columns.google_search",
    tuple(
        uniqIf(person_id, (${AI_VIEW}) AND ${cur}),
        uniqIf(person_id, (${AI_VIEW}) AND ${prev})
    ) AS "context.columns.llm_referrals",
    tuple(
        countIf((${CRAWLER}) AND ${cur}),
        uniqIf(\`$virt_bot_name\`, (${CRAWLER}) AND ${cur})
    ) AS "context.columns.agent_crawls",
    ${conversions} AS "context.columns.conversions",
    any(page_durations.avg_time) AS "context.columns.avg_time"
FROM events
LEFT JOIN (
    SELECT
        ${previousPageKey} AS page_key,
        quantile(0.90)(least(toFloat(properties.$prev_pageview_duration), 86400)) AS avg_time
    FROM events
    WHERE and(
        event IN ('$pageview', '$screen', '$pageleave'),
        properties.$prev_pageview_pathname IS NOT NULL,
        properties.$prev_pageview_duration IS NOT NULL,
        (${previousCandidatePredicate}),
        (${cur}),
        {filters}
    )
    GROUP BY page_key
) AS page_durations ON page_durations.page_key = ${pageKey}
WHERE and(
    (event IN ${PAGE_PERFORMANCE_EVENTS}${match ? ` OR (${match})` : ''}),
    properties.$pathname IS NOT NULL,
    properties.$pathname != '',
    (${candidatePredicate}),
    (${full}),
    {filters}
)
GROUP BY "context.columns.breakdown_value"
HAVING uniqIf(person_id, (${HUMAN_VIEW}) AND ${cur}) > 0
ORDER BY ${orderExpr} ${orderBy.direction}
LIMIT ${PAGE_TABLE_LIMIT}
`
}

const buildOverviewHumanQuery = (
    window: PagePerformanceWindow,
    pathExpr: string,
    bucketSize: PagePerformanceBucket
): string => {
    const { cur, prev, full } = windowPredicates(window)
    const pageKey = pageKeyExpr(pathExpr)

    return `
SELECT
    ${BUCKET_HOGQL_FN[bucketSize]}(timestamp) AS bucket,
    uniqIf(person_id, (${HUMAN_VIEW}) AND ${cur}) AS visitors,
    uniqIf(person_id, (${HUMAN_VIEW}) AND ${prev}) AS visitors_previous,
    uniqIf(person_id, (${GOOGLE_VIEW}) AND ${cur}) AS google,
    uniqIf(person_id, (${GOOGLE_VIEW}) AND ${prev}) AS google_previous,
    uniqIf(person_id, (${AI_VIEW}) AND ${cur}) AS llm,
    uniqIf(person_id, (${AI_VIEW}) AND ${prev}) AS llm_previous,
    uniqIf(${pageKey}, (${HUMAN_VIEW}) AND ${cur}) AS pages
FROM events
WHERE and(
    event IN ${HUMAN_EVENTS},
    properties.$pathname IS NOT NULL,
    properties.$pathname != '',
    (${full}),
    {filters}
)
GROUP BY GROUPING SETS ((bucket), ())
`
}

const buildOverviewCrawlerQuery = (window: PagePerformanceWindow, bucketSize: PagePerformanceBucket): string => {
    const { cur, prev, full } = windowPredicates(window)

    return `
SELECT
    ${BUCKET_HOGQL_FN[bucketSize]}(timestamp) AS bucket,
    countIf((${CRAWLER}) AND ${cur}) AS crawls,
    countIf((${CRAWLER}) AND ${prev}) AS crawls_previous,
    countIf(event = '$http_log' AND ${cur}) AS server_logs
FROM events
WHERE and(
    event IN ${PAGE_PERFORMANCE_EVENTS},
    properties.$pathname IS NOT NULL,
    properties.$pathname != '',
    (${full}),
    {filters}
)
GROUP BY GROUPING SETS ((bucket), ())
`
}

export interface pagePerformanceLogicValues {
    allActions: ActionType[]
    currentTeam: TeamPublicType | TeamType | null
    compareFilter: CompareFilter
    conversionGoal: WebAnalyticsConversionGoal | null
    dateFilter: DateFilterState
    filterTestAccounts: boolean
    isPathCleaningEnabled: boolean
    webAnalyticsFilters: WebAnalyticsPropertyFilters
    aiSectionQueries: AiSectionQueries
    breakdownModal: PagePerformanceBreakdownState | null
    breakdownQuery: DataTableNode | null
    candidatesError: string | null
    candidatesInput: string
    candidatesLoading: boolean
    dataState: PagePerformanceDataState
    footerText: string
    goalLabel: string | null
    orderBy: PagePerformanceOrderBy
    bucketSize: PagePerformanceBucket
    comparePeriods: boolean
    siteVisitors: number
    overviewCrawlerQuery: string
    overviewError: string | null
    overviewHumanQuery: string
    overviewInput: string
    overviewLoading: boolean
    overviewMetrics: OverviewMetric[]
    overviewSeries: OverviewSeriesPoint[]
    overviewTotals: OverviewTotals | null
    pageCandidateQuery: WebStatsTableQuery
    pageCandidates: string[] | null
    pageTableQuery: DataTableNode
    pageTableInput: string
    pathExpr: string
    previousPathExpr: string
    window: PagePerformanceWindow
}

export interface pagePerformanceLogicActions {
    reloadAll: () => {}
    closeBreakdown: () => {
        value: true
    }
    loadOverview: (reset?: boolean) => { reset: boolean }
    loadOverviewFailure: (error: string) => {
        error: string
    }
    loadOverviewSuccess: (
        overviewTotals: OverviewTotals,
        overviewSeries: OverviewSeriesPoint[]
    ) => {
        overviewTotals: OverviewTotals
        overviewSeries: OverviewSeriesPoint[]
    }
    loadCandidates: (reset?: boolean) => { reset: boolean }
    loadCandidatesFailure: (error: string) => {
        error: string
    }
    loadCandidatesSuccess: (pageCandidates: string[]) => {
        pageCandidates: string[]
    }
    openBreakdown: (breakdown: PagePerformanceBreakdownState) => {
        breakdown: PagePerformanceBreakdownState
    }
    setOrderBy: (
        column: string,
        direction: 'ASC' | 'DESC'
    ) => {
        column: string
        direction: 'ASC' | 'DESC'
    }
}

export interface pagePerformanceLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        window: (
            dateFilter: DateFilterState,
            compareFilter: CompareFilter,
            currentTeam: TeamPublicType | TeamType | null
        ) => PagePerformanceWindow
        pathExpr: (isPathCleaningEnabled: boolean, currentTeam: TeamPublicType | TeamType | null) => string
        previousPathExpr: (isPathCleaningEnabled: boolean, currentTeam: TeamPublicType | TeamType | null) => string
        pageCandidateQuery: (
            dateFilter: DateFilterState,
            filterTestAccounts: boolean,
            webAnalyticsFilters: WebAnalyticsPropertyFilters
        ) => WebStatsTableQuery
        pageTableQuery: (
            window: PagePerformanceWindow,
            orderBy: PagePerformanceOrderBy,
            conversionGoal: WebAnalyticsConversionGoal | null,
            filterTestAccounts: boolean,
            webAnalyticsFilters: WebAnalyticsPropertyFilters,
            pathExpr: string,
            previousPathExpr: string,
            pageCandidates: string[] | null
        ) => DataTableNode
        aiSectionQueries: (
            dateFilter: DateFilterState,
            filterTestAccounts: boolean,
            conversionGoal: WebAnalyticsConversionGoal | null,
            compareFilter: CompareFilter,
            webAnalyticsFilters: WebAnalyticsPropertyFilters
        ) => AiSectionQueries
        bucketSize: (window: PagePerformanceWindow) => PagePerformanceBucket
        overviewHumanQuery: (
            window: PagePerformanceWindow,
            pathExpr: string,
            bucketSize: PagePerformanceBucket
        ) => string
        overviewCrawlerQuery: (window: PagePerformanceWindow, bucketSize: PagePerformanceBucket) => string
        overviewInput: (
            overviewHumanQuery: string,
            overviewCrawlerQuery: string,
            filterTestAccounts: boolean,
            webAnalyticsFilters: WebAnalyticsPropertyFilters
        ) => string
        candidatesInput: (pageCandidateQuery: WebStatsTableQuery) => string
        breakdownQuery: (
            breakdownModal: PagePerformanceBreakdownState | null,
            window: PagePerformanceWindow,
            filterTestAccounts: boolean,
            webAnalyticsFilters: WebAnalyticsPropertyFilters,
            pathExpr: string
        ) => DataTableNode | null
        comparePeriods: (compareFilter: CompareFilter) => boolean
        siteVisitors: (overviewTotals: OverviewTotals | null) => number
        dataState: (
            overviewTotals: OverviewTotals | null,
            overviewError: string | null,
            currentTeam: TeamPublicType | TeamType | null
        ) => PagePerformanceDataState
        overviewMetrics: (
            overviewTotals: OverviewTotals | null,
            overviewSeries: OverviewSeriesPoint[],
            comparePeriods: boolean
        ) => OverviewMetric[]
        goalLabel: (conversionGoal: WebAnalyticsConversionGoal | null, allActions: ActionType[]) => string | null
        footerText: (
            overviewTotals: OverviewTotals | null,
            goalLabel: string | null,
            isPathCleaningEnabled: boolean
        ) => string
    }
}

export type pagePerformanceLogicType = MakeLogicType<
    pagePerformanceLogicValues,
    pagePerformanceLogicActions,
    WebAnalyticsLogicProps,
    pagePerformanceLogicMeta
>

export const pagePerformanceLogic: LogicWrapper<pagePerformanceLogicType> = kea<pagePerformanceLogicType>([
    props({} as WebAnalyticsLogicProps),
    key((props) => props.context ?? 'web-analytics'),
    path(['scenes', 'webAnalytics', 'pagePerformanceLogic']),
    connect((props: WebAnalyticsLogicProps) => ({
        actions: [dataNodeCollectionLogic({ key: WEB_ANALYTICS_DATA_COLLECTION_NODE_ID }), ['reloadAll']],
        values: [
            webAnalyticsLogic(props),
            [
                'dateFilter',
                'shouldFilterTestAccounts as filterTestAccounts',
                'compareFilter',
                'conversionGoal',
                'isPathCleaningEnabled',
                'webAnalyticsFilters',
            ],
            teamLogic,
            ['currentTeam'],
            actionsModel,
            ['actions as allActions'],
        ],
    })),
    actions({
        setOrderBy: (column: string, direction: 'ASC' | 'DESC') => ({ column, direction }),
        openBreakdown: (breakdown: PagePerformanceBreakdownState) => ({ breakdown }),
        closeBreakdown: true,
        loadOverview: (reset: boolean = false) => ({ reset }),
        loadOverviewFailure: (error: string) => ({ error }),
        loadOverviewSuccess: (overviewTotals: OverviewTotals, overviewSeries: OverviewSeriesPoint[]) => ({
            overviewTotals,
            overviewSeries,
        }),
        loadCandidates: (reset: boolean = false) => ({ reset }),
        loadCandidatesFailure: (error: string) => ({ error }),
        loadCandidatesSuccess: (pageCandidates: string[]) => ({ pageCandidates }),
    }),
    reducers({
        orderBy: [
            { column: 'visitors', direction: 'DESC' } as PagePerformanceOrderBy,
            {
                setOrderBy: (_, { column, direction }) => ({ column, direction }),
            },
        ],
        breakdownModal: [
            null as PagePerformanceBreakdownState | null,
            {
                openBreakdown: (_, { breakdown }) => breakdown,
                closeBreakdown: () => null,
            },
        ],
        overviewTotals: [
            null as OverviewTotals | null,
            {
                loadOverview: (state, { reset }) => (reset ? null : state),
                loadOverviewSuccess: (_, { overviewTotals }) => overviewTotals,
            },
        ],
        overviewSeries: [
            [] as OverviewSeriesPoint[],
            {
                loadOverview: (state, { reset }) => (reset ? [] : state),
                loadOverviewSuccess: (_, { overviewSeries }) => overviewSeries,
            },
        ],
        overviewError: [
            null as string | null,
            {
                loadOverview: () => null,
                loadOverviewFailure: (_, { error }) => error,
            },
        ],
        overviewLoading: [
            false,
            {
                loadOverview: () => true,
                loadOverviewSuccess: () => false,
                loadOverviewFailure: () => false,
            },
        ],
        pageCandidates: [
            null as string[] | null,
            {
                loadCandidates: (state, { reset }) => (reset ? null : state),
                loadCandidatesSuccess: (state, { pageCandidates }) =>
                    state &&
                    state.length === pageCandidates.length &&
                    state.every((page, index) => page === pageCandidates[index])
                        ? state
                        : pageCandidates,
            },
        ],
        candidatesError: [
            null as string | null,
            {
                loadCandidates: () => null,
                loadCandidatesFailure: (_, { error }) => error,
            },
        ],
        candidatesLoading: [
            false,
            {
                loadCandidates: () => true,
                loadCandidatesSuccess: () => false,
                loadCandidatesFailure: () => false,
            },
        ],
    }),
    selectors(() => ({
        window: [
            (s) => [s.dateFilter, s.compareFilter, s.currentTeam],
            (
                dateFilter: DateFilterState,
                compareFilter: CompareFilter,
                currentTeam: TeamPublicType | TeamType | null
            ): PagePerformanceWindow =>
                resolvePagePerformanceWindow(dateFilter, compareFilter, currentTeam?.timezone ?? 'UTC'),
        ],
        pathExpr: [
            (s) => [s.isPathCleaningEnabled, s.currentTeam],
            (isPathCleaningEnabled: boolean, currentTeam: TeamType | null): string =>
                buildPagePerformancePathExpr(RAW_PATH, isPathCleaningEnabled, currentTeam?.path_cleaning_filters),
        ],
        previousPathExpr: [
            (s) => [s.isPathCleaningEnabled, s.currentTeam],
            (isPathCleaningEnabled: boolean, currentTeam: TeamType | null): string =>
                buildPagePerformancePathExpr(
                    'properties.$prev_pageview_pathname',
                    isPathCleaningEnabled,
                    currentTeam?.path_cleaning_filters
                ),
        ],
        pageCandidateQuery: [
            (s) => [s.dateFilter, s.filterTestAccounts, s.webAnalyticsFilters],
            (
                dateFilter: DateFilterState,
                filterTestAccounts: boolean,
                webAnalyticsFilters: WebAnalyticsPropertyFilters
            ): WebStatsTableQuery => ({
                kind: NodeKind.WebStatsTableQuery,
                breakdownBy: WebStatsBreakdown.Page,
                dateRange: { date_from: dateFilter.dateFrom, date_to: dateFilter.dateTo },
                properties: webAnalyticsFilters,
                compareFilter: { compare: false },
                doPathCleaning: false,
                filterTestAccounts,
                includeHost: true,
                limit: PAGE_CANDIDATE_LIMIT,
                orderBy: [WebAnalyticsOrderByFields.Visitors, 'DESC'],
                useWebAnalyticsPrecompute: true,
                tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
            }),
        ],
        pageTableQuery: [
            (s) => [
                s.window,
                s.orderBy,
                s.conversionGoal,
                s.filterTestAccounts,
                s.webAnalyticsFilters,
                s.pathExpr,
                s.previousPathExpr,
                s.pageCandidates,
            ],
            (
                window: PagePerformanceWindow,
                orderBy: PagePerformanceOrderBy,
                conversionGoal: WebAnalyticsConversionGoal | null,
                filterTestAccounts: boolean,
                webAnalyticsFilters: WebAnalyticsPropertyFilters,
                pathExpr: string,
                previousPathExpr: string,
                pageCandidates: string[] | null
            ): DataTableNode => ({
                kind: NodeKind.DataTableNode,
                source: {
                    kind: NodeKind.HogQLQuery,
                    query: buildPagePerformanceTableQuery(
                        window,
                        orderBy,
                        conversionGoal,
                        pathExpr,
                        previousPathExpr,
                        pageCandidates ?? []
                    ),
                    filters: { filterTestAccounts, properties: webAnalyticsFilters },
                    tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                },
                embedded: true,
                showActions: false,
                columns: [
                    'context.columns.breakdown_value',
                    'context.columns.visitors',
                    'context.columns.google_search',
                    'context.columns.llm_referrals',
                    'context.columns.agent_crawls',
                    'context.columns.conversions',
                    'context.columns.avg_time',
                ],
            }),
        ],
        aiSectionQueries: [
            (s) => [s.dateFilter, s.filterTestAccounts, s.conversionGoal, s.compareFilter, s.webAnalyticsFilters],
            (
                dateFilter: DateFilterState,
                filterTestAccounts: boolean,
                conversionGoal: WebAnalyticsConversionGoal | null,
                compareFilter: CompareFilter,
                webAnalyticsFilters: WebAnalyticsPropertyFilters
            ): AiSectionQueries => {
                const dateRange = { date_from: dateFilter.dateFrom, date_to: dateFilter.dateTo }
                const interval = dateFilter.interval

                const referralFilters: WebAnalyticsPropertyFilters = [AI_CHANNEL_FILTER, ...webAnalyticsFilters]
                const crawlerFilters: WebAnalyticsPropertyFilters = [...AI_CRAWLER_FILTERS, ...webAnalyticsFilters]

                const referralColumns = [
                    'breakdown_value',
                    'visitors',
                    'views',
                    ...(conversionGoal ? ['total_conversions', 'conversion_rate'] : []),
                    'cross_sell',
                ]

                const referralTable = (breakdownBy: WebStatsBreakdown): DataTableNode => ({
                    full: true,
                    kind: NodeKind.DataTableNode,
                    source: {
                        kind: NodeKind.WebStatsTableQuery,
                        properties: referralFilters,
                        breakdownBy,
                        dateRange,
                        compareFilter,
                        conversionGoal,
                        limit: 10,
                        filterTestAccounts,
                        tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                    },
                    embedded: false,
                    showActions: true,
                    columns: referralColumns,
                })

                const crawlerTable = (breakdownBy: WebBotsBreakdown): DataTableNode => ({
                    full: true,
                    kind: NodeKind.DataTableNode,
                    source: {
                        kind: NodeKind.WebBotsTableQuery,
                        breakdownBy,
                        dateRange,
                        properties: crawlerFilters,
                        limit: 10,
                        filterTestAccounts,
                        tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                    },
                    showActions: false,
                    embedded: false,
                })

                return {
                    referralTrend: {
                        kind: NodeKind.InsightVizNode,
                        source: {
                            kind: NodeKind.TrendsQuery,
                            dateRange,
                            interval,
                            series: [
                                {
                                    kind: NodeKind.EventsNode,
                                    event: '$pageview',
                                    name: '$pageview',
                                    math: BaseMathType.UniqueSessions,
                                },
                            ],
                            trendsFilter: {
                                display: ChartDisplayType.ActionsLineGraph,
                                showLegend: true,
                            } as TrendsFilter,
                            breakdownFilter: getWebAnalyticsBreakdownFilter(WebStatsBreakdown.InitialReferringDomain),
                            properties: referralFilters,
                            filterTestAccounts,
                            tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                        },
                        hidePersonsModal: true,
                        embedded: true,
                    },
                    byEngine: referralTable(WebStatsBreakdown.InitialReferringDomain),
                    landingPages: referralTable(WebStatsBreakdown.InitialPage),
                    crawlerTrend: {
                        kind: NodeKind.InsightVizNode,
                        source: {
                            kind: NodeKind.TrendsQuery,
                            dateRange,
                            interval,
                            series: [aiCrawlerRequestSeries('Requests')],
                            trendsFilter: { display: ChartDisplayType.ActionsLineGraph } as TrendsFilter,
                            properties: crawlerFilters,
                            filterTestAccounts,
                            tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                        },
                        hidePersonsModal: true,
                        embedded: true,
                    },
                    byCrawler: crawlerTable(WebBotsBreakdown.Crawler),
                    crawledPages: crawlerTable(WebBotsBreakdown.Path),
                }
            },
        ],
        bucketSize: [
            (s) => [s.window],
            (window: PagePerformanceWindow): PagePerformanceBucket => resolvePagePerformanceBucket(window),
        ],
        comparePeriods: [
            (s) => [s.compareFilter],
            (compareFilter: CompareFilter): boolean => compareFilter.compare !== false,
        ],
        dataState: [
            (s) => [s.overviewTotals, s.overviewError, s.currentTeam],
            (
                overviewTotals: OverviewTotals | null,
                overviewError: string | null,
                currentTeam: TeamPublicType | TeamType | null
            ): PagePerformanceDataState =>
                // A failed overview says nothing about the sections, so let their own queries speak instead.
                overviewError
                    ? READY_DATA_STATE
                    : resolvePagePerformanceDataState(overviewTotals, !!currentTeam?.ingested_event),
        ],
        // A number rather than the totals object, so a reload doesn't re-render every row on identity alone.
        siteVisitors: [
            (s) => [s.overviewTotals],
            (overviewTotals: OverviewTotals | null): number => overviewTotals?.visitors ?? 0,
        ],
        overviewHumanQuery: [
            (s) => [s.window, s.pathExpr, s.bucketSize],
            (window: PagePerformanceWindow, pathExpr: string, bucketSize: PagePerformanceBucket): string =>
                buildOverviewHumanQuery(window, pathExpr, bucketSize),
        ],
        overviewCrawlerQuery: [
            (s) => [s.window, s.bucketSize],
            (window: PagePerformanceWindow, bucketSize: PagePerformanceBucket): string =>
                buildOverviewCrawlerQuery(window, bucketSize),
        ],
        overviewInput: [
            (s) => [s.overviewHumanQuery, s.overviewCrawlerQuery, s.filterTestAccounts, s.webAnalyticsFilters],
            (
                overviewHumanQuery: string,
                overviewCrawlerQuery: string,
                filterTestAccounts: boolean,
                webAnalyticsFilters: WebAnalyticsPropertyFilters
            ): string =>
                JSON.stringify([overviewHumanQuery, overviewCrawlerQuery, filterTestAccounts, webAnalyticsFilters]),
        ],
        pageTableInput: [
            (s) => [s.overviewInput, s.orderBy, s.conversionGoal],
            (
                overviewInput: string,
                orderBy: PagePerformanceOrderBy,
                conversionGoal: WebAnalyticsConversionGoal | null
            ): string => JSON.stringify([overviewInput, orderBy, conversionGoal]),
        ],
        candidatesInput: [
            (s) => [s.pageCandidateQuery],
            (pageCandidateQuery: WebStatsTableQuery): string => JSON.stringify(pageCandidateQuery),
        ],
        breakdownQuery: [
            (s) => [s.breakdownModal, s.window, s.filterTestAccounts, s.webAnalyticsFilters, s.pathExpr],
            (
                breakdownModal: PagePerformanceBreakdownState | null,
                window: PagePerformanceWindow,
                filterTestAccounts: boolean,
                webAnalyticsFilters: WebAnalyticsPropertyFilters,
                pathExpr: string
            ): DataTableNode | null => {
                if (!breakdownModal) {
                    return null
                }
                const query = buildBreakdownQuery(breakdownModal.metric, breakdownModal.page, window, pathExpr)
                if (!query) {
                    return null
                }
                return {
                    kind: NodeKind.DataTableNode,
                    source: {
                        kind: NodeKind.HogQLQuery,
                        query,
                        filters: { filterTestAccounts, properties: webAnalyticsFilters },
                        tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                    },
                    embedded: true,
                    showActions: false,
                }
            },
        ],
        overviewMetrics: [
            (s) => [s.overviewTotals, s.overviewSeries, s.comparePeriods],
            (
                overviewTotals: OverviewTotals | null,
                overviewSeries: OverviewSeriesPoint[],
                comparePeriods: boolean
            ): OverviewMetric[] => {
                const totals = overviewTotals ?? EMPTY_OVERVIEW_TOTALS
                const sparklineLabels = overviewSeries.map((point) => point.label)
                return OVERVIEW_METRICS.map(({ key, label, value, previous, series }) => {
                    const current = value(totals)
                    const before = previous(totals)
                    return {
                        key,
                        label,
                        value: current,
                        previous: comparePeriods ? before : null,
                        changeFromPreviousPct:
                            comparePeriods && before > 0 ? Math.round(((current - before) / before) * 100) : null,
                        sparkline: overviewSeries.map(series),
                        sparklineLabels,
                    }
                })
            },
        ],
        goalLabel: [
            (s) => [s.conversionGoal, s.allActions],
            (conversionGoal: WebAnalyticsConversionGoal | null, allActions: ActionType[]): string | null => {
                if (!conversionGoal) {
                    return null
                }
                if ('actionId' in conversionGoal) {
                    return allActions.find((a) => a.id === conversionGoal.actionId)?.name ?? 'Conversion goal'
                }
                return conversionGoal.customEventName
            },
        ],
        footerText: [
            (s) => [s.overviewTotals, s.goalLabel, s.isPathCleaningEnabled],
            (
                overviewTotals: OverviewTotals | null,
                goalLabel: string | null,
                isPathCleaningEnabled: boolean
            ): string => {
                return [
                    overviewTotals ? pluralize(overviewTotals.pages, 'page') : null,
                    `conversion goal: ${goalLabel ?? 'not set'}`,
                    `path cleaning ${isPathCleaningEnabled ? 'on' : 'off'}`,
                ]
                    .filter(Boolean)
                    .join(' · ')
            },
        ],
    })),
    listeners(({ values, actions, cache }) => {
        // Re-adding under the same key disposes the previous controller, aborting the request it owns.
        const signalFor = (key: string): AbortSignal => {
            const abortController = new AbortController()
            cache.disposables.add(() => () => abortController.abort(), key, { pauseOnPageHidden: false })
            return abortController.signal
        }
        const isCancellation = (error: unknown): boolean =>
            (error instanceof Error && isBreakpoint(error)) || isAbortedRequest(error)
        const failureMessage = (error: unknown): string =>
            error instanceof Error ? error.message : 'Could not load search and AI data'

        return {
            loadOverview: async ({ reset }, breakpoint) => {
                await breakpoint(300)
                const signal = signalFor('overviewRequest')
                const overviewNode = (query: string): HogQLQuery => ({
                    kind: NodeKind.HogQLQuery,
                    query,
                    filters: {
                        filterTestAccounts: values.filterTestAccounts,
                        properties: values.webAnalyticsFilters,
                    },
                    tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                })
                try {
                    const [humanResponse, crawlerResponse] = await Promise.all([
                        performQuery(
                            overviewNode(values.overviewHumanQuery),
                            { signal },
                            reset ? 'blocking' : 'force_blocking'
                        ),
                        performQuery(
                            overviewNode(values.overviewCrawlerQuery),
                            { signal },
                            reset ? 'blocking' : 'force_blocking'
                        ),
                    ])
                    breakpoint()
                    const { window: dateWindow } = values
                    const human = parsePagePerformanceOverviewResponse(
                        humanResponse.columns,
                        humanResponse.results,
                        dateWindow
                    )
                    const crawler = parsePagePerformanceOverviewResponse(
                        crawlerResponse.columns,
                        crawlerResponse.results,
                        dateWindow
                    )
                    actions.loadOverviewSuccess(
                        {
                            visitors: human.totals.visitors ?? 0,
                            visitorsPrevious: human.totals.visitors_previous ?? 0,
                            google: human.totals.google ?? 0,
                            googlePrevious: human.totals.google_previous ?? 0,
                            llm: human.totals.llm ?? 0,
                            llmPrevious: human.totals.llm_previous ?? 0,
                            crawls: crawler.totals.crawls ?? 0,
                            crawlsPrevious: crawler.totals.crawls_previous ?? 0,
                            pages: human.totals.pages ?? 0,
                            serverLogs: crawler.totals.server_logs ?? 0,
                        },
                        mergePagePerformanceSeries(human, crawler)
                    )
                } catch (error) {
                    if (isCancellation(error) || cache.disposables.isDisposed) {
                        return
                    }
                    breakpoint()
                    actions.loadOverviewFailure(failureMessage(error))
                }
            },
            loadCandidates: async ({ reset }, breakpoint) => {
                await breakpoint(300)
                const signal = signalFor('candidatesRequest')
                try {
                    const candidatesResponse = await performQuery(
                        values.pageCandidateQuery,
                        { signal },
                        reset ? 'blocking' : 'force_blocking'
                    )
                    breakpoint()
                    const pageCandidates = (candidatesResponse.results ?? []).flatMap((candidate) => {
                        if (!Array.isArray(candidate) || typeof candidate[0] !== 'string' || candidate[0] === '') {
                            return []
                        }
                        return [candidate[0]]
                    })
                    actions.loadCandidatesSuccess(pageCandidates)
                } catch (error) {
                    if (isCancellation(error) || cache.disposables.isDisposed) {
                        return
                    }
                    breakpoint()
                    actions.loadCandidatesFailure(failureMessage(error))
                }
            },
            reloadAll: () => {
                actions.loadOverview()
                actions.loadCandidates()
            },
        }
    }),
    subscriptions(({ actions }) => ({
        overviewInput: () => {
            actions.loadOverview(true)
        },
        candidatesInput: () => {
            actions.loadCandidates(true)
        },
    })),
    afterMount(({ actions }) => {
        actions.loadOverview(true)
        actions.loadCandidates(true)
    }),
])

export const createPagePerformanceInsightProps = (tile: TileId, tab?: string): InsightLogicProps => ({
    dashboardItemId: getDashboardItemId(tile, tab, false),
    loadPriority: loadPriorityMap[tile],
    dataNodeCollectionId: WEB_ANALYTICS_DATA_COLLECTION_NODE_ID,
})