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.

fix(posthog-ai): restore /ticket handover event on conversations path (#73525)

Christiaan Hendriksen6bd0f7eaa644
diff --git a/frontend/src/lib/components/Support/supportLogic.test.ts b/frontend/src/lib/components/Support/supportLogic.test.ts
index f5aebff84e80..7c68a9d4151c 100644
--- a/frontend/src/lib/components/Support/supportLogic.test.ts
+++ b/frontend/src/lib/components/Support/supportLogic.test.ts
@@ -116,10 +116,19 @@ describe('supportLogic', () => {
const zendeskCalls = (): unknown[][] => fetchMock.mock.calls.filter(([url]) => url === ZENDESK_URL)
+ const aiTicketCaptures = (): unknown[][] =>
+ (posthog.capture as jest.Mock).mock.calls.filter(([event]) => event === 'posthog_ai_support_ticket_created')
+
const enableConversationsFlag = (): void => {
featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.PRODUCT_SUPPORT_SIDE_PANEL]: true })
}
+ // Flag state leaks between tests through the window app context, so flag-off tests must
+ // disable explicitly rather than rely on the default
+ const disableConversationsFlag = (): void => {
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.PRODUCT_SUPPORT_SIDE_PANEL]: false })
+ }
+
beforeEach(() => {
fetchMock = jest.fn(() =>
Promise.resolve({
@@ -129,6 +138,7 @@ describe('supportLogic', () => {
} as unknown as Response)
)
global.fetch = fetchMock
+ ;(posthog.capture as jest.Mock).mockClear()
initKeaTests()
logic = supportLogic.build()
logic.mount()
@@ -250,5 +260,66 @@ describe('supportLogic', () => {
expect(zendeskCalls()).toHaveLength(0)
expect(logic.values.lastSubmittedTicketId).toBeNull()
})
+
+ // Regression guard: the AI /ticket handover event must fire from supportLogic once the ticket id
+ // resolves. It previously lived in a component effect that raced the async conversations
+ // round-trip and silently dropped the event once submissions were routed to conversations.
+ it('captures posthog_ai_support_ticket_created once on the conversations path when AI context is present', async () => {
+ const sendMessage = jest.fn().mockResolvedValue({ ticket_id: 't1' })
+ ;(posthog as any).conversations = { isAvailable: () => true, sendMessage }
+ enableConversationsFlag()
+
+ await logic.asyncActions.submitSupportTicket({
+ ...FORM_FIELDS,
+ ai_conversation_id: 'conv-1',
+ ai_trace_id: 'trace-1',
+ ai_feedback_rating: 'bad',
+ })
+
+ expect(aiTicketCaptures()).toEqual([
+ [
+ 'posthog_ai_support_ticket_created',
+ {
+ $ai_conversation_id: 'conv-1',
+ $ai_session_id: 'conv-1',
+ $ai_trace_id: 'trace-1',
+ $ai_support_ticket_id: 't1',
+ $ai_feedback_rating: 'bad',
+ },
+ ],
+ ])
+ })
+
+ it('captures posthog_ai_support_ticket_created on the Zendesk fallback path with the zendesk ticket id', async () => {
+ // Flag off and no conversations extension → Zendesk path; fetchMock returns request id 123
+ disableConversationsFlag()
+ await logic.asyncActions.submitSupportTicket({
+ ...FORM_FIELDS,
+ ai_conversation_id: 'conv-2',
+ ai_trace_id: null,
+ })
+
+ expect(aiTicketCaptures()).toEqual([
+ [
+ 'posthog_ai_support_ticket_created',
+ {
+ $ai_conversation_id: 'conv-2',
+ $ai_session_id: 'conv-2',
+ $ai_trace_id: null,
+ $ai_support_ticket_id: '123',
+ },
+ ],
+ ])
+ })
+
+ it('does not capture the AI ticket event for a regular (non-AI) submission', async () => {
+ const sendMessage = jest.fn().mockResolvedValue({ ticket_id: 't1' })
+ ;(posthog as any).conversations = { isAvailable: () => true, sendMessage }
+ enableConversationsFlag()
+
+ await logic.asyncActions.submitSupportTicket(FORM_FIELDS)
+
+ expect(aiTicketCaptures()).toHaveLength(0)
+ })
})
})
diff --git a/frontend/src/lib/components/Support/supportLogic.ts b/frontend/src/lib/components/Support/supportLogic.ts
index 7546ef5468ba..0aafa1e415f2 100644
--- a/frontend/src/lib/components/Support/supportLogic.ts
+++ b/frontend/src/lib/components/Support/supportLogic.ts
@@ -485,6 +485,11 @@ export type SupportFormFields = {
exception_event?: SupportTicketExceptionEvent
isEmailFormOpen?: boolean | 'true' | 'false'
tags?: string[]
+ // Set when the ticket originates from a PostHog AI (/ticket, feedback) handover, so the created
+ // ticket can be attributed back to the conversation regardless of which submit path files it.
+ ai_conversation_id?: string | null
+ ai_trace_id?: string | null
+ ai_feedback_rating?: string | null
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
@@ -796,6 +801,24 @@ export const supportLogic = kea<supportLogicType>([
},
submitSupportTicket: async (formValues: SupportFormFields) => {
const { name, email, kind, target_area, severity_level, message, exception_event, tags } = formValues
+ const { ai_conversation_id, ai_trace_id, ai_feedback_rating } = formValues
+
+ // Attribute PostHog AI (/ticket, feedback) handovers to the conversation. The ticket id
+ // only exists once a submit path resolves — capturing from here means it fires no matter
+ // which path files the ticket, instead of relying on a component effect that races the
+ // async conversations round-trip and silently drops the event.
+ const captureAiSupportTicket = (ticketId: string | number): void => {
+ if (!ai_conversation_id) {
+ return
+ }
+ posthog.capture('posthog_ai_support_ticket_created', {
+ $ai_conversation_id: ai_conversation_id,
+ $ai_session_id: ai_conversation_id,
+ $ai_trace_id: ai_trace_id ?? null,
+ $ai_support_ticket_id: String(ticketId),
+ ...(ai_feedback_rating ? { $ai_feedback_rating: ai_feedback_rating } : {}),
+ })
+ }
// Conversations is where support is headed, so wait for the extension rather than racing
// it to the (temporary) Zendesk fallback
@@ -816,6 +839,7 @@ export const supportLogic = kea<supportLogicType>([
// No support_ticket capture here: the backend fires $conversation_ticket_created
// for every new ticket, so a client-side event would double-count
actions.setLastSubmittedTicketId(response.ticket_id)
+ captureAiSupportTicket(response.ticket_id)
lemonToast.success(
values.sidePanelAvailable
? 'Got the message! You can view replies from our support engineers in the support panel.'
@@ -1100,6 +1124,7 @@ export const supportLogic = kea<supportLogicType>([
actions.ensureZendeskOrganization()
actions.setLastSubmittedTicketId(zendesk_ticket_id)
+ captureAiSupportTicket(zendesk_ticket_id)
// Only close and reset the form on success
actions.closeSupportForm()
diff --git a/frontend/src/scenes/max/FeedbackPrompt.tsx b/frontend/src/scenes/max/FeedbackPrompt.tsx
index ab6489e286a2..77dcf3e6cf34 100644
--- a/frontend/src/scenes/max/FeedbackPrompt.tsx
+++ b/frontend/src/scenes/max/FeedbackPrompt.tsx
@@ -1,5 +1,4 @@
import { useActions, useValues } from 'kea'
-import posthog from 'posthog-js'
import { useEffect, useState } from 'react'
import { IconDocument } from '@posthog/icons'
@@ -46,13 +45,8 @@ export function FeedbackPrompt({ conversationId, traceId }: FeedbackPromptProps)
if (pendingTicketSubmission && lastSubmittedTicketId && lastSubmittedTicketId !== ticketIdBeforeSubmission) {
captureFeedback(conversationId, traceId, 'bad', currentTriggerType, ticketMessageText || undefined)
- posthog.capture('posthog_ai_support_ticket_created', {
- $ai_conversation_id: conversationId,
- $ai_session_id: conversationId,
- $ai_trace_id: traceId,
- $ai_support_ticket_id: lastSubmittedTicketId,
- $ai_feedback_rating: 'bad',
- })
+ // The posthog_ai_support_ticket_created event (with $ai_feedback_rating) is captured in
+ // supportLogic once the ticket id resolves, so it fires on every submit path.
setIsSupportModalOpen(false)
setPendingTicketSubmission(false)
completeDetailedFeedback()
@@ -100,6 +94,9 @@ export function FeedbackPrompt({ conversationId, traceId }: FeedbackPromptProps)
target_area: 'posthog-ai',
severity_level: 'low',
message: feedbackText,
+ ai_conversation_id: conversationId,
+ ai_trace_id: traceId,
+ ai_feedback_rating: 'bad',
})
setIsSupportModalOpen(true)
diff --git a/frontend/src/scenes/max/TicketPrompt.tsx b/frontend/src/scenes/max/TicketPrompt.tsx
index 063deec94447..fc1fa150d865 100644
--- a/frontend/src/scenes/max/TicketPrompt.tsx
+++ b/frontend/src/scenes/max/TicketPrompt.tsx
@@ -1,5 +1,4 @@
import { useActions, useValues } from 'kea'
-import posthog from 'posthog-js'
import { useCallback, useEffect, useState } from 'react'
import { LemonButton, LemonInput, LemonModal } from '@posthog/lemon-ui'
@@ -51,12 +50,8 @@ export function TicketPrompt({
const handleTicketCreated = useCallback(
(ticketId: string): void => {
- posthog.capture('posthog_ai_support_ticket_created', {
- $ai_conversation_id: conversationId,
- $ai_session_id: conversationId,
- $ai_trace_id: traceId,
- $ai_support_ticket_id: ticketId,
- })
+ // The posthog_ai_support_ticket_created event is captured in supportLogic once the ticket
+ // id resolves, so it fires on every submit path (see ai_conversation_id below).
// Persist the confirmation message and add it to the thread
const confirmationMessage = formatConfirmationMessage(ticketId)
@@ -66,7 +61,7 @@ export function TicketPrompt({
setIsSubmitting(false)
closeSupportForm()
},
- [conversationId, traceId, appendMessageToConversation, closeSupportForm]
+ [appendMessageToConversation, closeSupportForm]
)
useEffect(() => {
@@ -92,6 +87,8 @@ export function TicketPrompt({
severity_level: 'low',
message: messageContent,
tags: ['raised_from_posthog_ai_chat'],
+ ai_conversation_id: conversationId,
+ ai_trace_id: traceId,
})
setIsSupportModalOpen(true)
}