: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
feat(logs): record the route of missing-link ask ai clicks (#117701)
Jon McCallum092e35a49e9a
diff --git a/products/logs/frontend/components/LogsViewer/LogContextLinks/MissingLinkInvestigate.tsx b/products/logs/frontend/components/LogsViewer/LogContextLinks/MissingLinkInvestigate.tsxindex 652a39206d71..0177fafa2e53 100644--- a/products/logs/frontend/components/LogsViewer/LogContextLinks/MissingLinkInvestigate.tsx+++ b/products/logs/frontend/components/LogsViewer/LogContextLinks/MissingLinkInvestigate.tsx@@ -1,5 +1,4 @@import { useActions, useValues } from 'kea'-import posthog from 'posthog-js'import { IconSearch, IconSparkles } from '@posthog/icons'import { LemonButton } from '@posthog/lemon-ui'@@ -9,30 +8,12 @@ import { useOnMountEffect } from 'lib/hooks/useOnMountEffect'import { humanList } from 'lib/utils/strings'import { maxGlobalLogic } from 'scenes/max/maxGlobalLogic'-import { getServiceName } from 'products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils'import { ParsedLogMessage } from 'products/logs/frontend/types'import type { LogContextLinksSurface } from './LogContextLinks'import { missingLinkInvestigateLogic } from './missingLinkInvestigateLogic'import { MissingLink, buildMissingLinkPrompt } from './missingLinkPrompt'--type InvestigateOption = AgentPromptDestination | 'posthog-ai-task'--// pinned: analytics event names and properties, renaming breaks dashboards-function captureInvestigate(- event: 'shown' | 'clicked',- log: ParsedLogMessage,- missing: MissingLink[],- surface: LogContextLinksSurface,- option?: InvestigateOption-): void {- posthog.capture(`logs missing link investigate ${event}`, {- option,- missing,- source: `log-${surface}`,- service_name: getServiceName(log),- })-}+import { captureInvestigate, missingLinkEventContext } from './missingLinkTelemetry'function missingLabel(missing: MissingLink[]): string {const links = humanList(missing)@@ -54,7 +35,9 @@ export function MissingLinkInvestigate({const { launching, startsSidePanelTask } = useValues(missingLinkInvestigateLogic)const { startTask } = useActions(missingLinkInvestigateLogic)- useOnMountEffect(() => captureInvestigate('shown', log, missing, surface))+ const eventContext = missingLinkEventContext(log, missing, surface)++ useOnMountEffect(() => captureInvestigate('shown', eventContext))return (<div className="flex flex-wrap items-center gap-1">@@ -72,7 +55,7 @@ export function MissingLinkInvestigate({},]}onRun={({ agentKey }) =>- captureInvestigate('clicked', log, missing, surface, agentKey as AgentPromptDestination)+ captureInvestigate('clicked', eventContext, { option: agentKey as AgentPromptDestination })}data-attr={`logs-${surface}-missing-link-agent-prompt`}/>@@ -82,10 +65,7 @@ export function MissingLinkInvestigate({type="tertiary"icon={<IconSparkles />}loading={startsSidePanelTask && launching}- onClick={() => {- captureInvestigate('clicked', log, missing, surface, 'posthog-ai-task')- startTask(buildMissingLinkPrompt(log, missing), onLeave ?? (() => {}))- }}+ onClick={() => startTask(buildMissingLinkPrompt(log, missing), onLeave ?? (() => {}), eventContext)}data-attr={`logs-${surface}-missing-link-posthog-ai`}>Ask PostHog AIdiff --git a/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkInvestigateLogic.test.ts b/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkInvestigateLogic.test.tsindex 7f54e558243c..24f4e92dd368 100644--- a/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkInvestigateLogic.test.ts+++ b/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkInvestigateLogic.test.ts@@ -1,5 +1,6 @@import { router } from 'kea-router'import { expectLogic } from 'kea-test-utils'+import posthog from 'posthog-js'import { FEATURE_FLAGS } from 'lib/constants'import { featureFlagLogic } from 'lib/logic/featureFlagLogic'@@ -17,9 +18,12 @@ import { SidePanelTab } from '~/types'import { composerSeedLogic } from 'products/posthog_ai/frontend/api/logics'import { missingLinkInvestigateLogic } from './missingLinkInvestigateLogic'+import { MissingLinkEventContext } from './missingLinkTelemetry'jest.mock('lib/utils/newInternalTab')+const eventContext: MissingLinkEventContext = { missing: ['trace'], source: 'log-drawer', service_name: 'checkout-api' }+describe('missingLinkInvestigateLogic', () => {let logic: ReturnType<typeof missingLinkInvestigateLogic.build>@@ -51,12 +55,17 @@ describe('missingLinkInvestigateLogic', () => {featureFlagLogic.actions.setFeatureFlags(flags, Object.fromEntries(flags.map((flag) => [flag, true])))router.actions.push('/logs');(newInternalTab as jest.Mock).mockClear()+ const capture = jest.spyOn(posthog, 'capture').mockImplementation(() => undefined)await expectLogic(logic, () => {- logic.actions.startTask('Find the missing session ID', () => {})+ logic.actions.startTask('Find the missing session ID', () => {}, eventContext)}).toFinishAllListeners()const seed = composerSeedLogic({ panelId: MAX_SIDE_PANEL_ID }).values.seed+ expect(capture.mock.calls.filter(([event]) => event.startsWith('logs missing link'))).toEqual([+ ['logs missing link investigate clicked', { ...eventContext, option: 'posthog-ai-task', route }],+ ])+ capture.mockRestore()expect({seeded: seed?.prompt ?? null,sidePanelTab: sidePanelStateLogic.values.sidePanelOpen ? sidePanelStateLogic.values.selectedTab : null,diff --git a/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkInvestigateLogic.ts b/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkInvestigateLogic.tsindex c68dba49811e..0e52f38c9712 100644--- a/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkInvestigateLogic.ts+++ b/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkInvestigateLogic.ts@@ -17,6 +17,8 @@ import {runnerPanelLogic,} from 'products/posthog_ai/frontend/api/logics'+import { InvestigateRoute, MissingLinkEventContext, captureInvestigate } from './missingLinkTelemetry'+// Generated by kea-typegen. Update if you're an agent, ignore if you're human.export interface missingLinkInvestigateLogicValues {seed: ComposerSeed | null // composerSeedLogic@@ -54,9 +56,11 @@ export interface missingLinkInvestigateLogicActions {} // sidePanelStateLogicstartTask: (prompt: string,- closeHost: () => void+ closeHost: () => void,+ eventContext: MissingLinkEventContext) => {closeHost: () => void+ eventContext: MissingLinkEventContextprompt: string}}@@ -113,7 +117,11 @@ export const missingLinkInvestigateLogic = kea<missingLinkInvestigateLogicType>(})),actions({- startTask: (prompt: string, closeHost: () => void) => ({ prompt, closeHost }),+ startTask: (prompt: string, closeHost: () => void, eventContext: MissingLinkEventContext) => ({+ prompt,+ closeHost,+ eventContext,+ }),}),selectors({@@ -134,21 +142,29 @@ export const missingLinkInvestigateLogic = kea<missingLinkInvestigateLogicType>(}),listeners(({ actions, values }) => ({- startTask: ({ prompt, closeHost }) => {+ startTask: ({ prompt, closeHost, eventContext }) => {+ const route: InvestigateRoute = !values.startsSidePanelTask+ ? values.todayRailEnabled+ ? 'ai-scene'+ : 'legacy-ask'+ : values.launching+ ? 'side-panel-already-launching'+ : 'side-panel-task'+ captureInvestigate('clicked', eventContext, { option: 'posthog-ai-task', route })// The drawer and the logs modal sit above the side panel, so close them before it opens.closeHost()actions.closeLogsViewerModal()- if (!values.startsSidePanelTask) {- if (values.todayRailEnabled) {- // The rail keeps legacy PostHog AI in the /ai scene, in the same tab.- router.actions.push(urls.ai(undefined, prompt))- } else {- // The shared ask path opens the question in a new PostHog AI tab.- actions.askSidePanelMax(prompt)- }+ if (route === 'ai-scene') {+ // The rail keeps legacy PostHog AI in the /ai scene, in the same tab.+ router.actions.push(urls.ai(undefined, prompt))+ return+ }+ if (route === 'legacy-ask') {+ // The shared ask path opens the question in a new PostHog AI tab.+ actions.askSidePanelMax(prompt)return}- if (values.launching) {+ if (route === 'side-panel-already-launching') {// Show the investigation already starting rather than queueing a second one.actions.openSidePanel(SidePanelTab.Max)returndiff --git a/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkTelemetry.ts b/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkTelemetry.tsnew file mode 100644index 000000000000..15a1253fe421--- /dev/null+++ b/products/logs/frontend/components/LogsViewer/LogContextLinks/missingLinkTelemetry.ts@@ -0,0 +1,36 @@+import posthog from 'posthog-js'++import { AgentPromptDestination } from 'lib/components/AgentPromptButton/AgentPromptButton'++import { getServiceName } from 'products/logs/frontend/components/LogsViewer/LogContextSelector/logContextUtils'+import { ParsedLogMessage } from 'products/logs/frontend/types'++import type { LogContextLinksSurface } from './LogContextLinks'+import { MissingLink } from './missingLinkPrompt'++export type InvestigateOption = AgentPromptDestination | 'posthog-ai-task'++export type InvestigateRoute = 'side-panel-task' | 'side-panel-already-launching' | 'legacy-ask' | 'ai-scene'++export interface MissingLinkEventContext {+ missing: MissingLink[]+ source: `log-${LogContextLinksSurface}`+ service_name: string | null+}++export function missingLinkEventContext(+ log: ParsedLogMessage,+ missing: MissingLink[],+ surface: LogContextLinksSurface+): MissingLinkEventContext {+ return { missing, source: `log-${surface}`, service_name: getServiceName(log) }+}++// pinned: analytics event names and properties, renaming breaks dashboards+export function captureInvestigate(+ event: 'shown' | 'clicked',+ context: MissingLinkEventContext,+ extra: { option?: InvestigateOption; route?: InvestigateRoute } = {}+): void {+ posthog.capture(`logs missing link investigate ${event}`, { ...context, ...extra })+}