π OpenHands: AI-Driven Development
fix: enrich conversation_created PostHog event (#1614)
Hiep Le3e40f458aa4a
diff --git a/__tests__/hooks/mutation/use-new-conversation-command.test.tsx b/__tests__/hooks/mutation/use-new-conversation-command.test.tsxindex 5e26f959c1..ed71f25e71 100644--- a/__tests__/hooks/mutation/use-new-conversation-command.test.tsx+++ b/__tests__/hooks/mutation/use-new-conversation-command.test.tsx@@ -39,6 +39,21 @@ vi.mock("#/utils/custom-toast-handlers", () => ({TOAST_OPTIONS: { position: "top-right" },}));+// /new fires conversation_created through the real useTracking hook. Mock the+// lower-level deps (posthog + settings) rather than useTracking itself so the+// emitted payload can be asserted.+const { captureMock } = vi.hoisted(() => ({ captureMock: vi.fn() }));++vi.mock("posthog-js/react", () => ({+ usePostHog: () => ({ capture: captureMock }),+}));++vi.mock("#/hooks/query/use-settings", () => ({+ useSettings: () => ({+ data: { email: "[email protected]", user_consents_to_analytics: true },+ }),+}));+const mockConversation = {id: "conv-123",title: "Test Conversation",@@ -233,4 +248,34 @@ describe("useNewConversationCommand", () => {expect(mockToast.dismiss).toHaveBeenCalledWith("clear-conversation");});});++ it("emits conversation_created on success with the /new no-context payload", async () => {+ const readyTask = makeStartTask();+ vi.spyOn(+ AgentServerConversationService,+ "createConversation",+ ).mockResolvedValue(readyTask as never);++ const { result } = renderHook(() => useNewConversationCommand(), {+ wrapper,+ });++ await result.current.mutateAsync();++ await waitFor(() => {+ expect(captureMock).toHaveBeenCalledWith(+ "conversation_created",+ expect.objectContaining({+ conversation_id: "new-conv-999",+ task_id: "task-789",+ is_start_task: false,+ has_repository: false,+ has_workspace: false,+ has_initial_query: false,+ has_parent_conversation: false,+ entry_point: "new_command",+ }),+ );+ });+ });});diff --git a/__tests__/hooks/use-handle-plan-click.test.tsx b/__tests__/hooks/use-handle-plan-click.test.tsxindex f96fb3fe19..67d2415981 100644--- a/__tests__/hooks/use-handle-plan-click.test.tsx+++ b/__tests__/hooks/use-handle-plan-click.test.tsx@@ -289,6 +289,7 @@ describe("useHandlePlanClick", () => {{parentConversationId: conversationId,agentType: "plan",+ entryPoint: "plan_sub_conversation",},expect.objectContaining({onSuccess: expect.any(Function),diff --git a/__tests__/hooks/use-tracking.test.ts b/__tests__/hooks/use-tracking.test.tsindex 7984dc0c06..5c134a5b7d 100644--- a/__tests__/hooks/use-tracking.test.ts+++ b/__tests__/hooks/use-tracking.test.ts@@ -49,14 +49,84 @@ describe("useTracking", () => {});describe("trackConversationCreated", () => {- it("captures conversation_created with has_repository flag", () => {- getTracking().trackConversationCreated({ hasRepository: true });+ it("captures conversation_created with full metadata for a repository, task-start conversation", () => {+ getTracking().trackConversationCreated({+ conversationId: "task-abc123",+ taskId: "abc123",+ hasRepository: true,+ gitProvider: "github",+ hasWorkspace: false,+ hasInitialQuery: true,+ hasParentConversation: false,+ entryPoint: "sidebar_cloud_menu",+ });expect(captureMock).toHaveBeenCalledWith("conversation_created", {+ conversation_id: "task-abc123",+ task_id: "abc123",+ // task-prefixed conversation_id => cloud sandbox still provisioning+ is_start_task: true,has_repository: true,+ git_provider: "github",+ has_workspace: false,+ workspace_mode: undefined,+ has_initial_query: true,+ agent_type: undefined,+ has_parent_conversation: false,+ entry_point: "sidebar_cloud_menu",+ ...COMMON,+ });+ });++ it("captures conversation_created for a local workspace start with no repository", () => {+ getTracking().trackConversationCreated({+ conversationId: "conv-1",+ taskId: "conv-1",+ hasRepository: false,+ hasWorkspace: true,+ workspaceMode: "local_repo",+ hasInitialQuery: false,+ hasParentConversation: false,+ entryPoint: "sidebar_local_menu",+ });++ expect(captureMock).toHaveBeenCalledWith("conversation_created", {+ conversation_id: "conv-1",+ task_id: "conv-1",+ // real conversation_id => ready immediately (local)+ is_start_task: false,+ has_repository: false,+ git_provider: undefined,+ has_workspace: true,+ workspace_mode: "local_repo",+ has_initial_query: false,+ agent_type: undefined,+ has_parent_conversation: false,+ entry_point: "sidebar_local_menu",...COMMON,});});++ it("captures agent_type and has_parent_conversation for a plan sub-conversation", () => {+ getTracking().trackConversationCreated({+ conversationId: "task-plan-1",+ taskId: "plan-1",+ hasRepository: false,+ hasWorkspace: false,+ hasInitialQuery: false,+ agentType: "plan",+ hasParentConversation: true,+ entryPoint: "plan_sub_conversation",+ });++ expect(captureMock).toHaveBeenCalledWith(+ "conversation_created",+ expect.objectContaining({+ agent_type: "plan",+ has_parent_conversation: true,+ }),+ );+ });});describe("trackPushButtonClick", () => {diff --git a/__tests__/routes/launch.test.tsx b/__tests__/routes/launch.test.tsxindex 23ded2c635..150f90b966 100644--- a/__tests__/routes/launch.test.tsx+++ b/__tests__/routes/launch.test.tsx@@ -452,6 +452,7 @@ describe("LaunchRoute", () => {},],query: undefined,+ entryPoint: "launch_deeplink",});});});@@ -479,6 +480,7 @@ describe("LaunchRoute", () => {},],query: "/city-weather:now Tokyo",+ entryPoint: "launch_deeplink",});});});diff --git a/src/components/features/conversation-panel/cloud-new-conversation-menu.tsx b/src/components/features/conversation-panel/cloud-new-conversation-menu.tsxindex 5c47b85899..1bc84eaa7d 100644--- a/src/components/features/conversation-panel/cloud-new-conversation-menu.tsx+++ b/src/components/features/conversation-panel/cloud-new-conversation-menu.tsx@@ -171,6 +171,7 @@ export function CloudNewConversationMenu({gitProvider: repo.git_provider,branch: repo.main_branch ?? "main",},+ entryPoint: "sidebar_cloud_menu",},{onSuccess: (data) => {diff --git a/src/components/features/conversation-panel/conversation-panel.tsx b/src/components/features/conversation-panel/conversation-panel.tsxindex a2b407f1e3..638d8e3868 100644--- a/src/components/features/conversation-panel/conversation-panel.tsx+++ b/src/components/features/conversation-panel/conversation-panel.tsx@@ -476,6 +476,7 @@ export function ConversationPanel({{workingDir: launch.workingDir,repository: launch.repository,+ entryPoint: "sidebar_relaunch_project",},{onSuccess: (data) => {diff --git a/src/components/features/conversation-panel/local-new-conversation-menu.tsx b/src/components/features/conversation-panel/local-new-conversation-menu.tsxindex 811cc50d01..349bd0b829 100644--- a/src/components/features/conversation-panel/local-new-conversation-menu.tsx+++ b/src/components/features/conversation-panel/local-new-conversation-menu.tsx@@ -121,7 +121,7 @@ export function LocalNewConversationMenu({const launch = (workingDir?: string) => {if (isCreating) return;createConversation(- { workingDir },+ { workingDir, entryPoint: "sidebar_local_menu" },{onSuccess: (data) => {setOpen(false);diff --git a/src/components/features/home/home-chat-launcher.tsx b/src/components/features/home/home-chat-launcher.tsxindex eb7d5a44ee..f2a72c16bd 100644--- a/src/components/features/home/home-chat-launcher.tsx+++ b/src/components/features/home/home-chat-launcher.tsx@@ -96,6 +96,7 @@ export function HomeChatLauncher() {// query here would create a duplicate text-only initial_message.let variables: Parameters<typeof createConversation>[0] = {query: hasAttachments ? undefined : trimmed || undefined,+ entryPoint: "home_chat_launcher",};if (isLocal && pendingWorkspace) {variables = {diff --git a/src/components/features/home/new-conversation.tsx b/src/components/features/home/new-conversation.tsxindex 55a4a43175..4927ce6c15 100644--- a/src/components/features/home/new-conversation.tsx+++ b/src/components/features/home/new-conversation.tsx@@ -43,7 +43,7 @@ export function NewConversation() {type="button"onClick={() =>createConversation(- {},+ { entryPoint: "home_start_from_scratch" },{onSuccess: (data) =>navigate(`/conversations/${data.conversation_id}`),diff --git a/src/components/features/home/new-conversation/create-conversation-button.tsx b/src/components/features/home/new-conversation/create-conversation-button.tsxindex ef654dea59..c198b7e726 100644--- a/src/components/features/home/new-conversation/create-conversation-button.tsx+++ b/src/components/features/home/new-conversation/create-conversation-button.tsx@@ -23,7 +23,7 @@ export function CreateConversationButton() {const handleCreateConversation = () => {createConversation(- {},+ { entryPoint: "home_new_conversation_button" },{onSuccess: (data) => navigate(`/conversations/${data.conversation_id}`),},diff --git a/src/components/features/home/repo-selection-form.tsx b/src/components/features/home/repo-selection-form.tsxindex 98196a9846..a019358cbb 100644--- a/src/components/features/home/repo-selection-form.tsx+++ b/src/components/features/home/repo-selection-form.tsx@@ -232,6 +232,7 @@ export function RepositorySelectionForm({gitProvider: selectedRepository.git_provider || "github",branch: selectedBranch.name || "main",},+ entryPoint: "home_repo_form",},{onSuccess: (data) =>diff --git a/src/components/features/home/tasks/task-card.tsx b/src/components/features/home/tasks/task-card.tsxindex 59a7687bf3..cc199ff5e3 100644--- a/src/components/features/home/tasks/task-card.tsx+++ b/src/components/features/home/tasks/task-card.tsx@@ -38,6 +38,7 @@ export function TaskCard({ task }: TaskCardProps) {gitProvider: task.git_provider,},suggestedTask: task,+ entryPoint: "home_task_card",},{onSuccess: (data) => {diff --git a/src/components/features/home/workspace-selection-form.tsx b/src/components/features/home/workspace-selection-form.tsxindex 75c417aefd..5dab287849 100644--- a/src/components/features/home/workspace-selection-form.tsx+++ b/src/components/features/home/workspace-selection-form.tsx@@ -160,7 +160,7 @@ export function WorkspaceSelectionForm({return;}createConversation(- { workingDir: selectedWorkspace.path },+ { workingDir: selectedWorkspace.path, entryPoint: "home_workspace_form" },{onSuccess: (data) => navigate(`/conversations/${data.conversation_id}`),},diff --git a/src/components/features/onboarding/steps/say-hello-step.tsx b/src/components/features/onboarding/steps/say-hello-step.tsxindex 4fa45f2e0e..e4a3435217 100644--- a/src/components/features/onboarding/steps/say-hello-step.tsx+++ b/src/components/features/onboarding/steps/say-hello-step.tsx@@ -54,7 +54,7 @@ export function SayHelloStep({// Explicitly omit `repository` and `workingDir` so the// conversation starts with no workspace, per the spec.createConversation(- { query: message.trim() },+ { query: message.trim(), entryPoint: "onboarding_say_hello" },{onSuccess: (data) => {navigate(`/conversations/${data.conversation_id}`);diff --git a/src/hooks/mutation/use-create-conversation.ts b/src/hooks/mutation/use-create-conversation.tsindex fa4d260fe5..5467e04618 100644--- a/src/hooks/mutation/use-create-conversation.ts+++ b/src/hooks/mutation/use-create-conversation.ts@@ -30,6 +30,7 @@ interface CreateConversationVariables {plugins?: PluginSpec[];workingDir?: string;workspaceMode?: WorkspaceMode;+ entryPoint?: string; // analytics only; not forwarded to the service}interface CreateConversationResponse {@@ -158,9 +159,18 @@ export const useCreateConversation = () => {task_id: conversation.id,};},- onSuccess: async (_, { repository }) => {+ onSuccess: async (data, variables) => {trackConversationCreated({- hasRepository: !!repository,+ conversationId: data.conversation_id,+ taskId: data.task_id,+ hasRepository: !!variables.repository,+ gitProvider: variables.repository?.gitProvider,+ hasWorkspace: !!variables.workingDir,+ workspaceMode: variables.workspaceMode,+ hasInitialQuery: !!variables.query,+ agentType: variables.agentType,+ hasParentConversation: !!variables.parentConversationId,+ entryPoint: variables.entryPoint,});// Invalidate (rather than remove) so the existing paginated list staysdiff --git a/src/hooks/mutation/use-new-conversation-command.ts b/src/hooks/mutation/use-new-conversation-command.tsindex 897447de0c..b70f524c4b 100644--- a/src/hooks/mutation/use-new-conversation-command.ts+++ b/src/hooks/mutation/use-new-conversation-command.ts@@ -10,12 +10,14 @@ import {} from "#/utils/custom-toast-handlers";import { useNavigation } from "#/context/navigation-context";import { useActiveConversation } from "#/hooks/query/use-active-conversation";+import { useTracking } from "#/hooks/use-tracking";export const useNewConversationCommand = () => {const queryClient = useQueryClient();const { navigate } = useNavigation();const { t } = useTranslation("openhands");const { data: conversation } = useActiveConversation();+ const { trackConversationCreated } = useTracking();const mutation = useMutation({mutationFn: async () => {@@ -55,6 +57,7 @@ export const useNewConversationCommand = () => {return {newConversationId,oldConversationId: conversation.id,+ taskId: startTask.id,};},onMutate: () => {@@ -64,6 +67,16 @@ export const useNewConversationCommand = () => {});},onSuccess: (data) => {+ trackConversationCreated({+ conversationId: data.newConversationId,+ taskId: data.taskId,+ hasRepository: false,+ hasWorkspace: false,+ hasInitialQuery: false,+ hasParentConversation: false,+ entryPoint: "new_command",+ });+toast.dismiss("clear-conversation");displaySuccessToast(t(I18nKey.CONVERSATION$CLEAR_SUCCESS));navigate(`/conversations/${data.newConversationId}`);diff --git a/src/hooks/use-handle-plan-click.ts b/src/hooks/use-handle-plan-click.tsindex dd68fcfd02..243cef56cf 100644--- a/src/hooks/use-handle-plan-click.ts+++ b/src/hooks/use-handle-plan-click.ts@@ -63,6 +63,7 @@ export const useHandlePlanClick = () => {{parentConversationId: conversation.id,agentType: "plan",+ entryPoint: "plan_sub_conversation",},{onSuccess: (data) => {diff --git a/src/hooks/use-tracking.ts b/src/hooks/use-tracking.tsindex a5eeef3ccf..7043c64706 100644--- a/src/hooks/use-tracking.ts+++ b/src/hooks/use-tracking.ts@@ -2,6 +2,7 @@ import { usePostHog } from "posthog-js/react";import { useSettings } from "./query/use-settings";import { Provider } from "#/types/settings";import type { BackendKind } from "#/api/backend-registry/types";+import type { WorkspaceMode } from "#/api/conversation-metadata-store";/*** Hook that provides tracking functions with automatic data collection@@ -36,11 +37,41 @@ export const useTracking = () => {};const trackConversationCreated = ({+ conversationId,+ taskId,hasRepository,+ gitProvider,+ hasWorkspace,+ workspaceMode,+ hasInitialQuery,+ agentType,+ hasParentConversation,+ entryPoint,}: {+ conversationId: string;+ taskId?: string;hasRepository: boolean;+ gitProvider?: Provider;+ hasWorkspace: boolean;+ workspaceMode?: WorkspaceMode;+ hasInitialQuery: boolean;+ agentType?: "default" | "plan";+ hasParentConversation: boolean;+ entryPoint?: string;}) => {- track("conversation_created", { has_repository: hasRepository });+ track("conversation_created", {+ conversation_id: conversationId,+ task_id: taskId,+ is_start_task: conversationId.startsWith("task-"),+ has_repository: hasRepository,+ git_provider: gitProvider,+ has_workspace: hasWorkspace,+ workspace_mode: workspaceMode,+ has_initial_query: hasInitialQuery,+ agent_type: agentType,+ has_parent_conversation: hasParentConversation,+ entry_point: entryPoint,+ });};const trackPushButtonClick = () => {diff --git a/src/routes/launch.tsx b/src/routes/launch.tsxindex 4caca63fd2..418186d2d9 100644--- a/src/routes/launch.tsx+++ b/src/routes/launch.tsx@@ -158,6 +158,7 @@ export default function LaunchRoute() {const result = await createConversation.mutateAsync({plugins,query: initialMessage,+ entryPoint: "launch_deeplink",});navigate(`/conversations/${result.conversation_id}`);} catch {