: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, listeners, path, reducers } from 'kea'
import { router } from 'kea-router'
import type { LocationChangedPayload } from 'kea-router/lib/types'
import posthog from 'posthog-js'
import { toast } from '@posthog/quill'
import { removeProjectIdIfPresent } from 'lib/utils/kea-router'
import { newDashboardLogic } from 'scenes/dashboard/newDashboardLogic'
import { urls } from 'scenes/urls'
import { canvasSceneLogic } from 'products/canvas/frontend/scene/canvasSceneLogic'
import { withBackLink } from './analyticsBackLink'
import { AnalyticsType, newCanvasSpaceIdForPath } from './analyticsUtils'
// pinned: analytics property values. Renaming them breaks dashboards.
export type NewAnalyticsSource = 'menu' | 'sidebar' | 'home' | 'list'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface newAnalyticsLogicValues {
newInsightDialogOpen: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface newAnalyticsLogicActions {
locationChanged: ({
method,
pathname,
search,
searchParams,
hash,
hashParams,
initial,
url,
routerState,
}: LocationChangedPayload) => {
hash: string
hashParams: Record<string, any>
initial: boolean
method: 'POP' | 'PUSH' | 'REPLACE'
pathname: string
routerState: Record<string, any>
search: string
searchParams: Record<string, any>
url: string
} // router
pickNewAnalyticsType: (
type: AnalyticsType,
source: NewAnalyticsSource
) => {
source: NewAnalyticsSource
type: AnalyticsType
}
setNewInsightDialogOpen: (open: boolean) => {
open: boolean
}
}
export type newAnalyticsLogicType = MakeLogicType<newAnalyticsLogicValues, newAnalyticsLogicActions>
export const newAnalyticsLogic = kea<newAnalyticsLogicType>([
path(['scenes', 'analytics', 'newAnalyticsLogic']),
connect(() => ({ actions: [router, ['locationChanged']] })),
actions({
pickNewAnalyticsType: (type: AnalyticsType, source: NewAnalyticsSource) => ({ type, source }),
setNewInsightDialogOpen: (open: boolean) => ({ open }),
}),
reducers({
// Picking a type in the dialog navigates to the editor, which closes it.
newInsightDialogOpen: [
false,
{
setNewInsightDialogOpen: (_, { open }) => open,
locationChanged: () => false,
},
],
}),
listeners(({ actions }) => ({
pickNewAnalyticsType: ({ type, source }) => {
// pinned: analytics event name and properties. Renaming them breaks dashboards.
posthog.capture('views new picked', { type, source })
const pathname = removeProjectIdIfPresent(router.values.location.pathname)
// The Analytics pages host the pickers themselves, so creating there keeps the page in place.
const onAnalyticsPage = pathname === urls.analytics() || pathname.startsWith(`${urls.analytics()}/`)
if (type === 'canvas') {
const canvasId = /^\/canvases\/([^/]+)$/.exec(pathname)?.[1]
const canvas = canvasId ? canvasSceneLogic.findMounted({ id: canvasId })?.values.canvas : null
if (canvasId && canvasId !== 'new' && !canvas) {
toast.error({ title: 'Wait for the canvas to load, then try again.' })
return
}
const spaceId = newCanvasSpaceIdForPath(pathname, canvas ?? null)
router.actions.push(withBackLink(urls.canvasNew(spaceId)))
} else if (type === 'notebook') {
router.actions.push(withBackLink(urls.notebook('new')))
} else if (type === 'insight') {
if (onAnalyticsPage) {
actions.setNewInsightDialogOpen(true)
} else {
router.actions.push(urls.insightNew())
}
} else {
const dashboardModal = onAnalyticsPage ? newDashboardLogic.findMounted() : null
if (dashboardModal) {
dashboardModal.actions.showNewDashboardModal()
} else {
// The dashboards page opens its new-dashboard dialog from this hash. Some scenes already host
// that dialog, so a second host in the shell would open it twice on those pages.
router.actions.push(`${urls.dashboards()}#newDashboard=modal`)
}
}
},
})),
])