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.

17.4 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { router } from 'kea-router'

import { isNotNil } from 'lib/utils/guards'
import { removeProjectIdIfPresent, stripTrailingSlash } from 'lib/utils/kea-router'
import { objectsEqual } from 'lib/utils/objects'

import {
    EventsNode,
    GroupNode,
    NodeKind,
    TrendsFilter,
    WebAnalyticsPropertyFilter,
    WebBotsBreakdown,
    WebAnalyticsPropertyFilters,
} from '~/queries/schema/schema-general'
import { isWebAnalyticsPropertyFilters } from '~/queries/web-analytics-schema-guards'
import {
    BaseMathType,
    BreakdownType,
    ChartDisplayType,
    FilterLogicalOperator,
    InsightLogicProps,
    PropertyFilterBaseValue,
    PropertyFilterType,
    PropertyOperator,
} from '~/types'

import {
    BOT_ANALYTICS_EVENTS,
    INITIAL_WEB_ANALYTICS_FILTER,
    TabsTileTab,
    TileId,
    WEB_ANALYTICS_DATA_COLLECTION_NODE_ID,
    WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
    WebAnalyticsTile,
    loadPriorityMap,
} from './common'
import { getDashboardItemId } from './insightsUtils'
import { webAnalyticsLogic } from './webAnalyticsLogic'
import type { DateFilterState } from './webAnalyticsLogic'

const teamId = window.POSTHOG_APP_CONTEXT?.current_team?.id
// Distinct prefix from webAnalyticsFilterLogic so bot-tab filters don't collide with the regular tab.
const persistConfig = { persist: true, prefix: `${teamId}__bot_` }

// The window pathname carries the /project/:id prefix and may keep a trailing slash (route
// matching strips it, `location.pathname` does not), so normalize before comparing.
const isOnBotsRoute = (): boolean =>
    stripTrailingSlash(removeProjectIdIfPresent(router.values.location.pathname)) === '/web/bots'

// Push the bot tab's filter state into the /web/bots URL. Only writes while the bots route is
// active; never touches the regular Analytics tab URL because `rawBotAnalyticsFilters` lives in
// a separate logic.
const syncBotFiltersToUrl = (
    rawBotAnalyticsFilters: WebAnalyticsPropertyFilters,
    dateFilter: DateFilterState
): void => {
    if (!isOnBotsRoute()) {
        return
    }
    const urlParams = new URLSearchParams(router.values.location.search)
    if (rawBotAnalyticsFilters.length > 0) {
        urlParams.set('filters', JSON.stringify(rawBotAnalyticsFilters))
    } else {
        urlParams.delete('filters')
    }
    // Always write the dates: a stale date param seeded from the current search would otherwise
    // survive and be re-applied by the urlToAction pass this replace triggers.
    const { dateFrom, dateTo, interval } = dateFilter
    urlParams.set('date_from', dateFrom ?? '')
    urlParams.set('date_to', dateTo ?? '')
    urlParams.set('interval', interval ?? '')
    router.actions.replace(`/web/bots${urlParams.toString() ? '?' + urlParams.toString() : ''}`)
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface botAnalyticsLogicValues {
    dateFilter: DateFilterState // webAnalyticsLogic
    filterTestAccounts: boolean // webAnalyticsLogic
    _botTrendsTab: string | null
    botFilters: WebAnalyticsPropertyFilters
    botTrendsTab: string
    rawBotAnalyticsFilters: WebAnalyticsPropertyFilters
    tiles: WebAnalyticsTile[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface botAnalyticsLogicActions {
    clearBotAnalyticsFilters: () => {
        value: true
    }
    setBotAnalyticsFilters: (filters: WebAnalyticsPropertyFilters) => {
        filters: WebAnalyticsPropertyFilters
    }
    setBotTrendsTab: (tab: string) => {
        tab: string
    }
    toggleBotAnalyticsFilter: (
        type: PropertyFilterType.Event | PropertyFilterType.Person | PropertyFilterType.Session,
        key: string,
        value: number | string | null
    ) => {
        key: string
        type: PropertyFilterType.Event | PropertyFilterType.Person | PropertyFilterType.Session
        value: number | string | null
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface botAnalyticsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        botTrendsTab: (_botTrendsTab: string | null) => string
        botFilters: (rawBotAnalyticsFilters: WebAnalyticsPropertyFilters) => WebAnalyticsPropertyFilters
        tiles: (
            dateFilter: DateFilterState,
            filterTestAccounts: boolean,
            botTrendsTab: string,
            botFilters: WebAnalyticsPropertyFilters
        ) => WebAnalyticsTile[]
    }
}

export type botAnalyticsLogicType = MakeLogicType<
    botAnalyticsLogicValues,
    botAnalyticsLogicActions,
    Record<string, any>,
    botAnalyticsLogicMeta
>

export const botAnalyticsLogic = kea<botAnalyticsLogicType>([
    path(['scenes', 'webAnalytics', 'botAnalyticsLogic']),
    connect(() => ({
        values: [webAnalyticsLogic, ['dateFilter', 'shouldFilterTestAccounts as filterTestAccounts']],
    })),
    actions({
        setBotAnalyticsFilters: (filters: WebAnalyticsPropertyFilters) => ({ filters }),
        toggleBotAnalyticsFilter: (
            type: PropertyFilterType.Event | PropertyFilterType.Person | PropertyFilterType.Session,
            key: string,
            value: string | number | null
        ) => ({ type, key, value }),
        clearBotAnalyticsFilters: true,
        setBotTrendsTab: (tab: string) => ({ tab }),
    }),
    reducers({
        rawBotAnalyticsFilters: [
            INITIAL_WEB_ANALYTICS_FILTER,
            persistConfig,
            {
                setBotAnalyticsFilters: (_, { filters }) => filters,
                clearBotAnalyticsFilters: () => INITIAL_WEB_ANALYTICS_FILTER,
                // Mirror webAnalyticsFilterLogic.togglePropertyFilter add/merge/IsNotSet semantics.
                toggleBotAnalyticsFilter: (oldFilters, { key, value, type }): WebAnalyticsPropertyFilters => {
                    if (value === null) {
                        const isNotSetFilterExists = oldFilters.some(
                            (f) => f.type === type && f.key === key && f.operator === PropertyOperator.IsNotSet
                        )
                        if (isNotSetFilterExists) {
                            return oldFilters.filter(
                                (f) => !(f.type === type && f.key === key && f.operator === PropertyOperator.IsNotSet)
                            )
                        }
                        return [
                            ...oldFilters,
                            {
                                type,
                                key,
                                operator: PropertyOperator.IsNotSet,
                            },
                        ]
                    }

                    const similarFilterExists = oldFilters.some(
                        (f) => f.type === type && f.key === key && f.operator === PropertyOperator.Exact
                    )

                    if (similarFilterExists) {
                        return oldFilters
                            .map((f: WebAnalyticsPropertyFilter) => {
                                if (
                                    f.key !== key ||
                                    f.type !== type ||
                                    ![PropertyOperator.Exact, PropertyOperator.IsNotSet].includes(f.operator)
                                ) {
                                    return f
                                }
                                const oldValue = (Array.isArray(f.value) ? f.value : [f.value]).filter(isNotNil)
                                let newValue: PropertyFilterBaseValue[]
                                if (oldValue.includes(value)) {
                                    if (oldValue.length > 1) {
                                        newValue = [value]
                                    } else {
                                        return null
                                    }
                                } else {
                                    newValue = [...oldValue, value]
                                }
                                return {
                                    type: PropertyFilterType.Event,
                                    key,
                                    operator: PropertyOperator.Exact,
                                    value: newValue,
                                } as const
                            })
                            .filter(isNotNil)
                    }

                    return [
                        ...oldFilters,
                        {
                            type,
                            key,
                            value,
                            operator: PropertyOperator.Exact,
                        },
                    ]
                },
            },
        ],
        _botTrendsTab: [
            null as string | null,
            persistConfig,
            {
                setBotTrendsTab: (_, { tab }) => tab,
            },
        ],
    }),
    selectors(({ actions }) => ({
        botTrendsTab: [(s) => [s._botTrendsTab], (tab: string | null) => tab || 'crawler'],
        botFilters: [
            (s) => [s.rawBotAnalyticsFilters],
            (rawBotAnalyticsFilters: WebAnalyticsPropertyFilters): WebAnalyticsPropertyFilters => [
                // User-provided filters first, then the forced bot scope.
                ...rawBotAnalyticsFilters.filter((f) => 'key' in f && f.key !== '$virt_is_bot'),
                {
                    key: '$virt_is_bot',
                    value: ['true'],
                    operator: PropertyOperator.Exact,
                    type: PropertyFilterType.Event,
                },
                {
                    key: '$virt_bot_name',
                    value: [''],
                    operator: PropertyOperator.IsNot,
                    type: PropertyFilterType.Event,
                },
            ],
        ],
        tiles: [
            (s) => [s.dateFilter, s.filterTestAccounts, s.botTrendsTab, s.botFilters],
            (
                dateFilter: import('./webAnalyticsLogic').DateFilterState,
                filterTestAccounts: boolean,
                botTrendsTab: string,
                botFilters: WebAnalyticsPropertyFilters
            ): WebAnalyticsTile[] => {
                const dateRange = { date_from: dateFilter.dateFrom, date_to: dateFilter.dateTo }
                const interval = dateFilter.interval

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

                const botTrendsNodes: EventsNode[] = BOT_ANALYTICS_EVENTS.map((event) => ({
                    event,
                    kind: NodeKind.EventsNode as const,
                    math: BaseMathType.TotalCount,
                    name: event,
                }))
                // Combine bot events into a single "Requests" series so the tooltip shows one
                // unified value per breakdown bucket instead of one column per underlying event.
                const botTrendsSeries: GroupNode[] = [
                    {
                        kind: NodeKind.GroupNode,
                        name: BOT_ANALYTICS_EVENTS.join(', '),
                        custom_name: 'Requests',
                        operator: FilterLogicalOperator.Or,
                        nodes: botTrendsNodes,
                        math: BaseMathType.TotalCount,
                    },
                ]

                const createBotTrendsTab = (
                    id: string,
                    title: string,
                    breakdown: string,
                    breakdownType: BreakdownType
                ): TabsTileTab => ({
                    id,
                    title,
                    linkText: title,
                    query: {
                        kind: NodeKind.InsightVizNode,
                        source: {
                            kind: NodeKind.TrendsQuery,
                            dateRange,
                            interval: interval ?? 'hour',
                            series: botTrendsSeries,
                            trendsFilter: {
                                display: ChartDisplayType.ActionsLineGraph,
                            } as TrendsFilter,
                            breakdownFilter: {
                                breakdown,
                                breakdown_type: breakdownType,
                            },
                            properties: botFilters,
                            filterTestAccounts,
                            tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                        },
                        hidePersonsModal: true,
                        embedded: true,
                    },
                    insightProps: createInsightProps(TileId.BOT_TRENDS, id),
                    canOpenInsight: true,
                    showIntervalSelect: true,
                })

                const tiles: (WebAnalyticsTile | null)[] = [
                    {
                        kind: 'tabs',
                        tileId: TileId.BOT_TRENDS,
                        layout: {
                            colSpanClassName: 'md:col-span-full',
                        },
                        activeTabId: botTrendsTab,
                        setTabId: actions.setBotTrendsTab,
                        tabs: [
                            createBotTrendsTab('crawler', 'Crawler', '$virt_bot_name', 'event'),
                            createBotTrendsTab('category', 'Category', '$virt_traffic_category', 'event'),
                            createBotTrendsTab('host', 'Host', '$host', 'event'),
                            createBotTrendsTab('path', 'Path', '$pathname', 'event'),
                        ],
                    },
                    {
                        kind: 'query',
                        tileId: TileId.BOT_CRAWLERS,
                        title: 'Crawlers',
                        layout: {
                            colSpanClassName: 'md:col-span-full',
                        },
                        query: {
                            full: true,
                            kind: NodeKind.DataTableNode,
                            source: {
                                kind: NodeKind.WebBotsTableQuery,
                                breakdownBy: WebBotsBreakdown.Crawler,
                                dateRange,
                                properties: botFilters,
                                filterTestAccounts,
                                tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                            },
                            showActions: false,
                            embedded: true,
                        },
                        insightProps: createInsightProps(TileId.BOT_CRAWLERS, 'table'),
                        canOpenModal: false,
                        canOpenInsight: true,
                    },
                    {
                        kind: 'query',
                        tileId: TileId.BOT_PATHS,
                        title: 'Most crawled paths',
                        layout: {
                            colSpanClassName: 'md:col-span-full',
                        },
                        query: {
                            full: true,
                            kind: NodeKind.DataTableNode,
                            source: {
                                kind: NodeKind.WebBotsTableQuery,
                                breakdownBy: WebBotsBreakdown.Path,
                                dateRange,
                                properties: botFilters,
                                filterTestAccounts,
                                tags: WEB_ANALYTICS_DEFAULT_QUERY_TAGS,
                            },
                            showActions: false,
                            embedded: true,
                        },
                        insightProps: createInsightProps(TileId.BOT_PATHS, 'table'),
                        canOpenModal: false,
                        canOpenInsight: true,
                    },
                ]

                return tiles.filter(isNotNil)
            },
        ],
    })),
    listeners(({ values }) => {
        const syncUrl = (): void => syncBotFiltersToUrl(values.rawBotAnalyticsFilters, values.dateFilter)
        return {
            setBotAnalyticsFilters: syncUrl,
            toggleBotAnalyticsFilter: syncUrl,
            clearBotAnalyticsFilters: syncUrl,
        }
    }),
    afterMount(({ actions, values }) => {
        if (!isOnBotsRoute()) {
            return
        }
        // Take ownership of the shared `filters` URL param. A param present at mount is a deep
        // link (tab switches scrub it in `buildStateUrl`), so adopt it; otherwise write the
        // persisted bot filters back so the URL matches the displayed results.
        const urlFilters = router.values.searchParams['filters']
        if (isWebAnalyticsPropertyFilters(urlFilters)) {
            if (!objectsEqual(urlFilters, values.rawBotAnalyticsFilters)) {
                actions.setBotAnalyticsFilters(urlFilters)
            }
        } else if (values.rawBotAnalyticsFilters.length > 0 || urlFilters !== undefined) {
            syncBotFiltersToUrl(values.rawBotAnalyticsFilters, values.dateFilter)
        }
    }),
])