: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 { MakeLogicType, actions, connect, kea, path, reducers, selectors } from 'kea'
import { NodeKind, QuerySchema } from '~/queries/schema/schema-general'
import { InsightLogicProps } from '~/types'
import { botAnalyticsLogic } from './botAnalyticsLogic'
import { TileId, WEB_ANALYTICS_DATA_COLLECTION_NODE_ID, WebAnalyticsTile } from './common'
import type { SectionTile } from './common'
import { getDashboardItemId, getNewInsightUrlFactory } from './insightsUtils'
import { pageReportsLogic } from './pageReportsLogic'
import { webAnalyticsLogic } from './webAnalyticsLogic'
export interface WebAnalyticsModalQuery {
tileId: TileId
tabId?: string
title?: string | JSX.Element
query: QuerySchema
insightProps: InsightLogicProps
showIntervalSelect?: boolean
control?: JSX.Element
canOpenInsight?: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface webAnalyticsModalLogicValues {
botAnalyticsTiles: WebAnalyticsTile[] // botAnalyticsLogic
pageReportsTiles: SectionTile[] // pageReportsLogic
webAnalyticsTiles: WebAnalyticsTile[] // webAnalyticsLogic
combinedTiles: WebAnalyticsTile[]
getNewInsightUrl: (tileId: TileId, tabId?: string | undefined) => string | undefined
modal: WebAnalyticsModalQuery | null
modalTileAndTab: {
tabId?: string
tileId: TileId
} | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface webAnalyticsModalLogicActions {
closeModal: () => boolean
openModal: (
tileId: TileId,
tabId?: string
) => {
tabId: string | undefined
tileId: TileId
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface webAnalyticsModalLogicMeta {
__keaTypeGenInternalSelectorTypes: {
combinedTiles: (
webAnalyticsTiles: WebAnalyticsTile[],
pageReportsTiles: SectionTile[],
botAnalyticsTiles: WebAnalyticsTile[]
) => WebAnalyticsTile[]
modal: (
combinedTiles: WebAnalyticsTile[],
modalTileAndTab: {
tabId?: string
tileId: TileId
} | null
) => WebAnalyticsModalQuery | null
getNewInsightUrl: (
combinedTiles: WebAnalyticsTile[]
) => (tileId: TileId, tabId?: string | undefined) => string | undefined
}
}
export type webAnalyticsModalLogicType = MakeLogicType<
webAnalyticsModalLogicValues,
webAnalyticsModalLogicActions,
Record<string, any>,
webAnalyticsModalLogicMeta
>
/**
* This logic serves as a bridge between webAnalyticsLogic and pageReportsLogic
* It combines tiles from both logics and provides a unified API for the modal
*/
export const webAnalyticsModalLogic = kea<webAnalyticsModalLogicType>([
path(['scenes', 'webAnalytics', 'webAnalyticsModalLogic']),
connect(() => ({
values: [
webAnalyticsLogic,
['tiles as webAnalyticsTiles'],
pageReportsLogic,
['tiles as pageReportsTiles'],
botAnalyticsLogic,
['tiles as botAnalyticsTiles'],
],
})),
actions({
openModal: (tileId: TileId, tabId?: string) => ({ tileId, tabId }),
closeModal: () => true,
}),
reducers({
modalTileAndTab: [
null as { tileId: TileId; tabId?: string } | null,
{
openModal: (_, { tileId, tabId }) => ({
tileId,
tabId,
}),
closeModal: () => null,
},
],
}),
selectors({
// Combine tiles from webAnalyticsLogic, flattened pageReportsLogic section tiles, and botAnalyticsLogic.
combinedTiles: [
(s) => [s.webAnalyticsTiles, s.pageReportsTiles, s.botAnalyticsTiles],
(
webAnalyticsTiles: WebAnalyticsTile[],
pageReportsTiles: WebAnalyticsTile[],
botAnalyticsTiles: WebAnalyticsTile[]
): WebAnalyticsTile[] => {
const flattenedPageReportsTiles = pageReportsTiles.flatMap((tile) => {
if (tile.kind === 'section' && tile.tiles) {
return [tile, ...tile.tiles]
}
return tile
})
return [...webAnalyticsTiles, ...flattenedPageReportsTiles, ...botAnalyticsTiles]
},
],
modal: [
(s) => [s.combinedTiles, s.modalTileAndTab],
(
tiles: WebAnalyticsTile[],
modalTileAndTab: { tileId: TileId; tabId?: string } | null
): WebAnalyticsModalQuery | null => {
if (!modalTileAndTab) {
return null
}
const { tileId, tabId } = modalTileAndTab
const tile = tiles.find((t: WebAnalyticsTile) => t.tileId === tileId)
if (!tile) {
return null
}
const extendQuery = (query: QuerySchema): QuerySchema => {
if (
query.kind === NodeKind.DataTableNode &&
(query.source.kind === NodeKind.WebStatsTableQuery ||
query.source.kind === NodeKind.WebExternalClicksTableQuery ||
query.source.kind === NodeKind.WebGoalsQuery)
) {
return {
...query,
source: {
...query.source,
limit: 50,
},
}
}
return query
}
if (tile.kind === 'tabs') {
const tab = tile.tabs.find((t) => t.id === tabId)
if (!tab) {
return null
}
return {
tileId,
tabId,
title: tab.title,
showIntervalSelect: tab.showIntervalSelect,
control: tab.control,
insightProps: {
dashboardItemId: getDashboardItemId(tileId, tabId, true),
loadPriority: 0,
doNotLoad: false,
dataNodeCollectionId: WEB_ANALYTICS_DATA_COLLECTION_NODE_ID,
},
query: extendQuery(tab.query),
canOpenInsight: tab.canOpenInsight,
}
} else if (tile.kind === 'query') {
return {
tileId,
title: tile.title,
showIntervalSelect: tile.showIntervalSelect,
control: tile.control,
insightProps: {
dashboardItemId: getDashboardItemId(tileId, undefined, true),
loadPriority: 0,
dataNodeCollectionId: WEB_ANALYTICS_DATA_COLLECTION_NODE_ID,
},
query: extendQuery(tile.query),
canOpenInsight: !!tile.canOpenInsight,
}
}
return null
},
],
getNewInsightUrl: [(s) => [s.combinedTiles], (tiles: WebAnalyticsTile[]) => getNewInsightUrlFactory(tiles)],
}),
])