: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 { BindLogic, useActions, useValues } from 'kea'
import { router } from 'kea-router'
import { useEffect } from 'react'
import { IconPencil } from '@posthog/icons'
import { Spinner } from '@posthog/lemon-ui'
import { useFeatureFlag } from 'lib/hooks/useFeatureFlag'
import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'
import { useAttachedLogic } from 'lib/logic/scenes/useAttachedLogic'
import { urls } from 'scenes/urls'
import { Query } from '~/queries/Query/Query'
import { CustomerJourneySelect } from 'products/customer_analytics/frontend/components/CustomerJourneys/CustomerJourneySelect'
import { CustomerJourneysEmptyState } from 'products/customer_analytics/frontend/components/CustomerJourneys/CustomerJourneysEmptyState'
import { customerJourneysLogic } from 'products/customer_analytics/frontend/components/CustomerJourneys/customerJourneysLogic'
import { NotebookNodeAttributeProperties, NotebookNodeProps, NotebookNodeType } from '../../types'
import { getCustomerProfileRemoveMenuItem } from '../customerProfileNotebookNodeMenu'
import { createPostHogWidgetNode } from '../NodeWrapper'
import { notebookNodeLogic } from '../notebookNodeLogic'
import { getLogicKey } from '../utils'
type NotebookNodeCustomerJourneyAttributes = {
personId?: string
groupKey?: string
groupTypeIndex?: number
tabId: string
}
const Component = ({ attributes }: NotebookNodeProps<NotebookNodeCustomerJourneyAttributes>): JSX.Element | null => {
const isJourneysEnabled = useFeatureFlag('CUSTOMER_ANALYTICS_JOURNEYS')
const { expanded, notebookLogic } = useValues(notebookNodeLogic)
const { setMenuItems, setTitlePlaceholder, setSettingsDisabledReason } = useActions(notebookNodeLogic)
const { personId, groupKey, groupTypeIndex, tabId } = attributes
const logicKey = getLogicKey({ personId, groupKey, tabId })
const logic = customerJourneysLogic({ key: logicKey, personId, groupKey, groupTypeIndex })
useAttachedLogic(logic, notebookLogic)
const { journeyOptions, journeysLoading, activeInsightLoading, filteredQuery, activeJourney } = useValues(logic)
useEffect(() => {
if (activeJourney) {
setTitlePlaceholder(`Customer journey - ${activeJourney.name}`)
}
}, [setTitlePlaceholder, activeJourney])
// The settings panel only selects between journeys, so it has nothing to offer until one exists
useEffect(() => {
setSettingsDisabledReason(
!journeysLoading && journeyOptions.length === 0 ? 'Create a journey to configure this panel' : null
)
}, [setSettingsDisabledReason, journeysLoading, journeyOptions.length])
useOnMountEffect(() => {
const removeMenuItem = getCustomerProfileRemoveMenuItem(NotebookNodeType.CustomerJourney)
const editJourneysMenuItem = {
label: 'Edit journeys',
onClick: () => router.actions.push(urls.customerAnalyticsJourneys()),
sideIcon: <IconPencil />,
}
if (removeMenuItem) {
setMenuItems([editJourneysMenuItem, removeMenuItem])
return
}
setMenuItems([editJourneysMenuItem])
})
if (journeysLoading || activeInsightLoading) {
return (
<div className="flex items-center justify-center p-8">
<Spinner />
</div>
)
}
if (journeyOptions.length === 0) {
return <CustomerJourneysEmptyState embedded />
}
if (!isJourneysEnabled || !expanded || !filteredQuery) {
return null
}
return (
<Query
query={filteredQuery}
attachTo={notebookLogic}
readOnly
context={{
insightProps: {
dashboardItemId: `new-AdHoc.${logicKey}`,
query: filteredQuery,
},
}}
/>
)
}
const Settings = ({
attributes,
}: NotebookNodeAttributeProperties<NotebookNodeCustomerJourneyAttributes>): JSX.Element => {
const { personId, groupKey, groupTypeIndex, tabId } = attributes
const logicKey = getLogicKey({ personId, groupKey, tabId })
return (
<BindLogic logic={customerJourneysLogic} props={{ key: logicKey, personId, groupKey, groupTypeIndex }}>
<div className="flex items-center gap-2 p-2">
<CustomerJourneySelect type="secondary" />
</div>
</BindLogic>
)
}
export const NotebookNodeCustomerJourney = createPostHogWidgetNode<NotebookNodeCustomerJourneyAttributes>({
nodeType: NotebookNodeType.CustomerJourney,
titlePlaceholder: 'Customer journey',
Component,
Settings,
resizeable: false,
expandable: true,
startExpanded: true,
attributes: {
personId: {},
groupKey: {},
groupTypeIndex: {},
tabId: {},
},
})