AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend): fire the run_agent and schedule_agent DataFast goals from every human path (#14289)
Nicholas Tindled523f898ac5a
diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/CronSchedulerDialog/useCronSchedulerDialog.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/CronSchedulerDialog/useCronSchedulerDialog.tsindex f238badc34..ec4f09437c 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/CronSchedulerDialog/useCronSchedulerDialog.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/CronSchedulerDialog/useCronSchedulerDialog.ts@@ -1,5 +1,6 @@import { usePostV1CreateExecutionSchedule } from "@/app/api/__generated__/endpoints/schedules/schedules";import { useToast } from "@/components/molecules/Toast/use-toast";+import { trackScheduleCreatedGoal } from "@/services/analytics/activation-goals";import { useUserTimezone } from "@/lib/hooks/useUserTimezone";import { getTimezoneDisplayName } from "@/lib/timezone-utils";import { invalidateAllScheduleQueries } from "@/services/schedules/invalidate-schedules";@@ -44,6 +45,7 @@ export const useCronSchedulerDialog = ({title: "Schedule created",description: "Schedule created successfully",});+ if (flowID) trackScheduleCreatedGoal({ id: flowID }, "builder");invalidateAllScheduleQueries(queryClient, flowID ?? undefined);}},diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunGraph/useRunGraph.test.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunGraph/useRunGraph.test.tsxindex f5cefeb2af..3f9d688f6d 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunGraph/useRunGraph.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunGraph/useRunGraph.test.tsx@@ -28,9 +28,31 @@ vi.mock("@/app/(platform)/build/hooks/useSaveGraph", () => ({const mockExecuteGraph = vi.fn();const mockStopGraph = vi.fn();+const mockSendDatafastEvent = vi.hoisted(() => vi.fn());+vi.mock("@/services/analytics", () => ({+ analytics: { sendDatafastEvent: mockSendDatafastEvent },+}));++interface ExecutionVariables {+ data: { dry_run?: boolean };+}++interface ExecutionOptions {+ mutation: {+ onSuccess: (+ response: { status: number; data: Record<string, unknown> },+ variables: ExecutionVariables,+ ) => void;+ };+}+vi.mock("@/app/api/__generated__/endpoints/graphs/graphs", () => ({- usePostV1ExecuteGraphAgent: () => ({- mutateAsync: mockExecuteGraph,+ usePostV1ExecuteGraphAgent: (options: ExecutionOptions) => ({+ async mutateAsync(variables: ExecutionVariables) {+ const response = await mockExecuteGraph(variables);+ options.mutation.onSuccess(response, variables);+ return response;+ },isPending: false,}),usePostV1StopGraphExecution: () => ({@@ -78,6 +100,10 @@ import { useRunGraph } from "./useRunGraph";describe("useRunGraph", () => {beforeEach(() => {vi.clearAllMocks();+ mockExecuteGraph.mockResolvedValue({+ status: 200,+ data: { id: "run-1", graph_id: "graph-1" },+ });});afterEach(() => {@@ -115,4 +141,35 @@ describe("useRunGraph", () => {expect.objectContaining({ graphId: "graph-1", graphVersion: 1 }),);});++ it("records a successful real run with its builder surface", async () => {+ mockSaveGraph.mockResolvedValue({ id: "graph-1", version: 1 });+ const { result } = renderHook(() => useRunGraph());++ await act(async () => {+ await result.current.handleRunGraph();+ });++ expect(mockSendDatafastEvent).toHaveBeenCalledExactlyOnceWith("run_agent", {+ id: "graph-1",+ name: "",+ surface: "builder",+ });+ });++ it("does not count a simulation as an activation", async () => {+ mockSaveGraph.mockResolvedValue({ id: "graph-1", version: 1 });+ const { result } = renderHook(() => useRunGraph());++ await act(async () => {+ await result.current.handleRunGraph({ dryRun: true });+ });++ expect(mockExecuteGraph).toHaveBeenCalledWith(+ expect.objectContaining({+ data: expect.objectContaining({ dry_run: true }),+ }),+ );+ expect(mockSendDatafastEvent).not.toHaveBeenCalled();+ });});diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunGraph/useRunGraph.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunGraph/useRunGraph.tsindex eee441bd4d..f721a435a4 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunGraph/useRunGraph.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunGraph/useRunGraph.ts@@ -5,6 +5,7 @@ import {import { useToast } from "@/components/molecules/Toast/use-toast";import { parseAsInteger, parseAsString, useQueryStates } from "nuqs";import { GraphExecutionMeta } from "@/app/api/__generated__/models/graphExecutionMeta";+import { trackAgentRunGoal } from "@/services/analytics/activation-goals";import { useGraphStore } from "@/app/(platform)/build/stores/graphStore";import { useShallow } from "zustand/react/shallow";import { useEffect, useState } from "react";@@ -77,12 +78,17 @@ export const useRunGraph = () => {const { mutateAsync: executeGraph, isPending: isExecutingGraph } =usePostV1ExecuteGraphAgent({mutation: {- onSuccess: (response: any) => {+ onSuccess: (response, variables) => {clearAllNodeErrors();- const { id } = response.data as GraphExecutionMeta;+ const { id, graph_id } = response.data as GraphExecutionMeta;setQueryStates({flowExecutionID: id,});+ // Simulate goes through the same mutation as Run; only a real run+ // is an activation.+ if (!variables.data.dry_run) {+ trackAgentRunGoal({ id: graph_id }, "builder");+ }},onError: (error: any) => {setIsGraphRunning(false);diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunInputDialog/useRunInputDialog.test.tsx b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunInputDialog/useRunInputDialog.test.tsxindex ae3fdf0a67..a302cd54af 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunInputDialog/useRunInputDialog.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunInputDialog/useRunInputDialog.test.tsx@@ -20,9 +20,27 @@ vi.mock("@/components/molecules/Toast/use-toast", () => ({}));const mockExecuteGraph = vi.fn();+const mockSendDatafastEvent = vi.hoisted(() => vi.fn());+vi.mock("@/services/analytics", () => ({+ analytics: { sendDatafastEvent: mockSendDatafastEvent },+}));++interface ExecutionOptions {+ mutation: {+ onSuccess: (response: {+ status: number;+ data: Record<string, unknown>;+ }) => void;+ };+}+vi.mock("@/app/api/__generated__/endpoints/graphs/graphs", () => ({- usePostV1ExecuteGraphAgent: () => ({- mutateAsync: mockExecuteGraph,+ usePostV1ExecuteGraphAgent: (options: ExecutionOptions) => ({+ async mutateAsync(variables: unknown) {+ const response = await mockExecuteGraph(variables);+ options.mutation.onSuccess(response);+ return response;+ },isPending: false,}),}));@@ -71,6 +89,10 @@ import { useRunInputDialog } from "./useRunInputDialog";describe("useRunInputDialog", () => {beforeEach(() => {vi.clearAllMocks();+ mockExecuteGraph.mockResolvedValue({+ status: 200,+ data: { id: "run-1", graph_id: "graph-1" },+ });});afterEach(() => {@@ -108,4 +130,20 @@ describe("useRunInputDialog", () => {expect.objectContaining({ graphId: "graph-1", graphVersion: 1 }),);});++ it("records a successful run from the input dialog", async () => {+ const { result } = renderHook(() =>+ useRunInputDialog({ setIsOpen: vi.fn() }),+ );++ await act(async () => {+ await result.current.handleManualRun();+ });++ expect(mockSendDatafastEvent).toHaveBeenCalledExactlyOnceWith("run_agent", {+ id: "graph-1",+ name: "",+ surface: "builder",+ });+ });});diff --git a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunInputDialog/useRunInputDialog.ts b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunInputDialog/useRunInputDialog.tsindex e0a14710a8..20d2535eea 100644--- a/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunInputDialog/useRunInputDialog.ts+++ b/autogpt_platform/frontend/src/app/(platform)/build/components/BuilderActions/components/RunInputDialog/useRunInputDialog.ts@@ -1,5 +1,6 @@import { useGraphStore } from "@/app/(platform)/build/stores/graphStore";import { usePostV1ExecuteGraphAgent } from "@/app/api/__generated__/endpoints/graphs/graphs";+import { trackAgentRunGoal } from "@/services/analytics/activation-goals";import {ApiError,@@ -44,10 +45,11 @@ export const useRunInputDialog = ({usePostV1ExecuteGraphAgent({mutation: {onSuccess: (response) => {- const { id } = response.data as GraphExecutionMeta;+ const { id, graph_id } = response.data as GraphExecutionMeta;setQueryStates({flowExecutionID: id,});+ trackAgentRunGoal({ id: graph_id }, "builder");},onError: (error) => {if (error instanceof ApiError && error.isGraphValidationError()) {diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/RunAgentModal/useAgentRunModal.test.tsx b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/RunAgentModal/useAgentRunModal.test.tsxnew file mode 100644index 0000000000..15282c8f8b--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/RunAgentModal/useAgentRunModal.test.tsx@@ -0,0 +1,100 @@+import { getPostV1ExecuteGraphAgentMockHandler } from "@/app/api/__generated__/endpoints/graphs/graphs.msw";+import type { GraphExecutionMeta } from "@/app/api/__generated__/models/graphExecutionMeta";+import type { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";+import { server } from "@/mocks/mock-server";+import { act, renderHook, waitFor } from "@/tests/integrations/test-utils";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import type { ReactNode } from "react";+import { afterEach, describe, expect, test, vi } from "vitest";+import { useAgentRunModal } from "./useAgentRunModal";++function wrapper({ children }: { children: ReactNode }) {+ const client = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ return <QueryClientProvider client={client}>{children}</QueryClientProvider>;+}++const sendDatafastEvent = vi.hoisted(() => vi.fn());+vi.mock("@/services/analytics", () => ({+ analytics: { sendDatafastEvent },+}));++const toastMock = vi.fn();+vi.mock("@/components/molecules/Toast/use-toast", async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@/components/molecules/Toast/use-toast")+ >();+ return {+ ...actual,+ useToast: () => ({ toast: toastMock }),+ };+});++const agent = {+ id: "lib-1",+ graph_id: "graph-xyz",+ graph_version: 1,+ name: "My agent",+ input_schema: { properties: {}, required: [] },+ credentials_input_schema: { properties: {}, required: [] },+ trigger_setup_info: null,+} as unknown as LibraryAgent;++const execution = {+ id: "run-1",+ graph_id: "graph-xyz",+} as GraphExecutionMeta;++afterEach(() => {+ toastMock.mockClear();+ sendDatafastEvent.mockClear();+ server.resetHandlers();+});++describe("useAgentRunModal", () => {+ test("a real run records the run_agent goal from the library surface", async () => {+ server.use(getPostV1ExecuteGraphAgentMockHandler(execution));+ const onRun = vi.fn();++ const { result } = renderHook(() => useAgentRunModal(agent, { onRun }), {+ wrapper,+ });++ act(() => {+ result.current.handleRun();+ });++ await waitFor(() => {+ expect(onRun).toHaveBeenCalledWith(+ expect.objectContaining({ id: "run-1" }),+ );+ });+ expect(sendDatafastEvent).toHaveBeenCalledExactlyOnceWith("run_agent", {+ id: "graph-xyz",+ name: "My agent",+ surface: "library",+ });+ });++ test("a simulation goes through the same mutation but is not an activation", async () => {+ server.use(getPostV1ExecuteGraphAgentMockHandler(execution));+ const onRun = vi.fn();++ const { result } = renderHook(() => useAgentRunModal(agent, { onRun }), {+ wrapper,+ });++ act(() => {+ result.current.handleSimulate();+ });++ await waitFor(() => {+ expect(onRun).toHaveBeenCalledWith(+ expect.objectContaining({ id: "run-1" }),+ );+ });+ expect(sendDatafastEvent).not.toHaveBeenCalled();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/RunAgentModal/useAgentRunModal.tsx b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/RunAgentModal/useAgentRunModal.tsxindex 9ce0cb1d33..994462638e 100644--- a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/RunAgentModal/useAgentRunModal.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/RunAgentModal/useAgentRunModal.tsx@@ -12,7 +12,7 @@ import { LibraryAgentPreset } from "@/app/api/__generated__/models/libraryAgentPimport { useToast } from "@/components/molecules/Toast/use-toast";import { isEmpty } from "@/lib/utils";import { CredentialsProvidersContext } from "@/providers/agent-credentials/credentials-provider";-import { analytics } from "@/services/analytics";+import { trackAgentRunGoal } from "@/services/analytics/activation-goals";import { useQueryClient } from "@tanstack/react-query";import {useCallback,@@ -157,7 +157,7 @@ export function useAgentRunModal(// API mutationsconst executeGraphMutation = usePostV1ExecuteGraphAgent({mutation: {- onSuccess: (response) => {+ onSuccess: (response, variables) => {if (response.status === 200) {toast({title: "Agent execution started",@@ -166,10 +166,14 @@ export function useAgentRunModal(queryKey: getGetV1ListGraphExecutionsQueryKey(agent.graph_id),});callbacks?.onRun?.(response.data);- analytics.sendDatafastEvent("run_agent", {- name: agent.name,- id: agent.graph_id,- });+ // Simulate goes through the same mutation as Run; only a real run+ // is an activation.+ if (!variables.data.dry_run) {+ trackAgentRunGoal(+ { id: agent.graph_id, name: agent.name },+ "library",+ );+ }setIsOpen(false);}},diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/ScheduleAgentModal/useScheduleAgentModal.ts b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/ScheduleAgentModal/useScheduleAgentModal.tsindex 85f02baa58..97e7aae1a2 100644--- a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/ScheduleAgentModal/useScheduleAgentModal.ts+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/modals/ScheduleAgentModal/useScheduleAgentModal.ts@@ -2,6 +2,7 @@ import { usePostV1CreateExecutionSchedule as useCreateSchedule } from "@/app/apiimport { GraphExecutionJobInfo } from "@/app/api/__generated__/models/graphExecutionJobInfo";import { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";import { useToast } from "@/components/molecules/Toast/use-toast";+import { trackScheduleCreatedGoal } from "@/services/analytics/activation-goals";import { invalidateAllScheduleQueries } from "@/services/schedules/invalidate-schedules";import { useQueryClient } from "@tanstack/react-query";import { useCallback, useMemo, useState } from "react";@@ -33,6 +34,10 @@ export function useScheduleAgentModal(toast({title: "Schedule created",});+ trackScheduleCreatedGoal(+ { id: agent.graph_id, name: agent.name },+ "library",+ );callbacks?.onCreateSchedule?.(response.data);invalidateAllScheduleQueries(queryClient, agent.graph_id);// Reset formdiff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedRunView/components/SelectedRunActions/useSelectedRunActions.test.tsx b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedRunView/components/SelectedRunActions/useSelectedRunActions.test.tsxnew file mode 100644index 0000000000..035f789c2d--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedRunView/components/SelectedRunActions/useSelectedRunActions.test.tsx@@ -0,0 +1,111 @@+import { getPostV1ExecuteGraphAgentMockHandler } from "@/app/api/__generated__/endpoints/graphs/graphs.msw";+import type { GraphExecution } from "@/app/api/__generated__/models/graphExecution";+import type { GraphExecutionMeta } from "@/app/api/__generated__/models/graphExecutionMeta";+import type { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";+import { server } from "@/mocks/mock-server";+import { act, renderHook, waitFor } from "@/tests/integrations/test-utils";+import { QueryClient, QueryClientProvider } from "@tanstack/react-query";+import type { ReactNode } from "react";+import { afterEach, describe, expect, test, vi } from "vitest";+import { useSelectedRunActions } from "./useSelectedRunActions";++function wrapper({ children }: { children: ReactNode }) {+ const client = new QueryClient({+ defaultOptions: { queries: { retry: false } },+ });+ return <QueryClientProvider client={client}>{children}</QueryClientProvider>;+}++const sendDatafastEvent = vi.hoisted(() => vi.fn());+vi.mock("@/services/analytics", () => ({+ analytics: { sendDatafastEvent },+}));++const toastMock = vi.fn();+vi.mock("@/components/molecules/Toast/use-toast", async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@/components/molecules/Toast/use-toast")+ >();+ return {+ ...actual,+ useToast: () => ({ toast: toastMock }),+ };+});++const agent = {+ id: "lib-1",+ graph_id: "graph-xyz",+ graph_version: 1,+ name: "My agent",+} as unknown as LibraryAgent;++const run = {+ id: "run-0",+ graph_id: "graph-xyz",+ graph_version: 1,+ status: "COMPLETED",+ inputs: { topic: "news" },+ credential_inputs: {},+} as unknown as GraphExecution;++afterEach(() => {+ toastMock.mockClear();+ sendDatafastEvent.mockClear();+ server.resetHandlers();+});++describe("useSelectedRunActions", () => {+ test("handleRunAgain records a run_agent goal from the rerun surface", async () => {+ server.use(+ getPostV1ExecuteGraphAgentMockHandler({+ id: "run-1",+ graph_id: "graph-xyz",+ } as GraphExecutionMeta),+ );+ const onSelectRun = vi.fn();++ const { result } = renderHook(+ () =>+ useSelectedRunActions({+ agentGraphId: "graph-xyz",+ run,+ agent,+ onSelectRun,+ }),+ { wrapper },+ );++ await act(async () => {+ await result.current.handleRunAgain();+ });++ await waitFor(() => {+ expect(onSelectRun).toHaveBeenCalledWith("run-1");+ });+ expect(sendDatafastEvent).toHaveBeenCalledExactlyOnceWith("run_agent", {+ id: "graph-xyz",+ name: "My agent",+ surface: "rerun",+ });+ });++ test("handleRunAgain without a run surfaces a destructive toast and no goal", async () => {+ const { result } = renderHook(+ () => useSelectedRunActions({ agentGraphId: "graph-xyz", agent }),+ { wrapper },+ );++ await act(async () => {+ await result.current.handleRunAgain();+ });++ expect(toastMock).toHaveBeenCalledWith(+ expect.objectContaining({+ title: "Run not found",+ variant: "destructive",+ }),+ );+ expect(sendDatafastEvent).not.toHaveBeenCalled();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedRunView/components/SelectedRunActions/useSelectedRunActions.ts b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedRunView/components/SelectedRunActions/useSelectedRunActions.tsindex 9bcfd9d964..8a8d8a37a9 100644--- a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedRunView/components/SelectedRunActions/useSelectedRunActions.ts+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedRunView/components/SelectedRunActions/useSelectedRunActions.ts@@ -12,6 +12,7 @@ import {import type { GraphExecution } from "@/app/api/__generated__/models/graphExecution";import type { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";import { okData } from "@/app/api/helpers";+import { trackAgentRunGoal } from "@/services/analytics/activation-goals";import { useToast } from "@/components/molecules/Toast/use-toast";import { useQueryClient } from "@tanstack/react-query";import { useState } from "react";@@ -97,6 +98,9 @@ export function useSelectedRunActions({});const newRunId = okData(res)?.id;+ if (newRunId) {+ trackAgentRunGoal({ id: run.graph_id, name: agent?.name }, "rerun");+ }await queryClient.invalidateQueries({queryKey: getGetV1ListGraphExecutionsQueryKey(agentGraphId),diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedScheduleView/components/SelectedScheduleActions/useSelectedScheduleActions.test.tsx b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedScheduleView/components/SelectedScheduleActions/useSelectedScheduleActions.test.tsxindex 03e7ea389c..97ffae1a35 100644--- a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedScheduleView/components/SelectedScheduleActions/useSelectedScheduleActions.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedScheduleView/components/SelectedScheduleActions/useSelectedScheduleActions.test.tsx@@ -1,4 +1,7 @@+import { getPostV1ExecuteGraphAgentMockHandler } from "@/app/api/__generated__/endpoints/graphs/graphs.msw";import { getDeleteV1DeleteExecutionScheduleMockHandler } from "@/app/api/__generated__/endpoints/schedules/schedules.msw";+import type { GraphExecutionJobInfo } from "@/app/api/__generated__/models/graphExecutionJobInfo";+import type { GraphExecutionMeta } from "@/app/api/__generated__/models/graphExecutionMeta";import type { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";import { server } from "@/mocks/mock-server";import * as invalidateSchedules from "@/services/schedules/invalidate-schedules";@@ -15,6 +18,11 @@ function wrapper({ children }: { children: ReactNode }) {return <QueryClientProvider client={client}>{children}</QueryClientProvider>;}+const sendDatafastEvent = vi.hoisted(() => vi.fn());+vi.mock("@/services/analytics", () => ({+ analytics: { sendDatafastEvent },+}));+const toastMock = vi.fn();vi.mock("@/components/molecules/Toast/use-toast", async (importOriginal) => {const actual =@@ -31,10 +39,20 @@ const agent = {id: "lib-1",graph_id: "graph-xyz",graph_version: 1,+ name: "My agent",} as unknown as LibraryAgent;+const schedule = {+ id: "sched-1",+ graph_id: "graph-xyz",+ graph_version: 1,+ input_data: { topic: "news" },+ input_credentials: {},+} as unknown as GraphExecutionJobInfo;+afterEach(() => {toastMock.mockClear();+ sendDatafastEvent.mockClear();server.resetHandlers();});@@ -104,4 +122,38 @@ describe("useSelectedScheduleActions", () => {}),);});++ test("handleRunNow records a run_agent goal from the library surface", async () => {+ server.use(+ getPostV1ExecuteGraphAgentMockHandler({+ id: "run-1",+ graph_id: "graph-xyz",+ } as GraphExecutionMeta),+ );+ const onSelectRun = vi.fn();++ const { result } = renderHook(+ () =>+ useSelectedScheduleActions({+ agent,+ scheduleId: "sched-1",+ schedule,+ onSelectRun,+ }),+ { wrapper },+ );++ await act(async () => {+ await result.current.handleRunNow();+ });++ await waitFor(() => {+ expect(onSelectRun).toHaveBeenCalledWith("run-1");+ });+ expect(sendDatafastEvent).toHaveBeenCalledExactlyOnceWith("run_agent", {+ id: "graph-xyz",+ name: "My agent",+ surface: "library",+ });+ });});diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedScheduleView/components/SelectedScheduleActions/useSelectedScheduleActions.ts b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedScheduleView/components/SelectedScheduleActions/useSelectedScheduleActions.tsindex ce4efa4a5b..def829fa21 100644--- a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedScheduleView/components/SelectedScheduleActions/useSelectedScheduleActions.ts+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/selected-views/SelectedScheduleView/components/SelectedScheduleActions/useSelectedScheduleActions.ts@@ -8,6 +8,7 @@ import { useDeleteV1DeleteExecutionSchedule } from "@/app/api/__generated__/endpimport type { GraphExecutionJobInfo } from "@/app/api/__generated__/models/graphExecutionJobInfo";import type { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";import { okData } from "@/app/api/helpers";+import { trackAgentRunGoal } from "@/services/analytics/activation-goals";import { useToast } from "@/components/molecules/Toast/use-toast";import { invalidateAllScheduleQueries } from "@/services/schedules/invalidate-schedules";import { useQueryClient } from "@tanstack/react-query";@@ -86,6 +87,12 @@ export function useSelectedScheduleActions({});const newRunID = okData(res)?.id;+ if (newRunID) {+ trackAgentRunGoal(+ { id: schedule.graph_id, name: agent.name },+ "library",+ );+ }await queryClient.invalidateQueries({queryKey: getGetV1ListGraphExecutionsQueryKey(agent.graph_id),diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/components/ScheduleActionsDropdown.test.tsx b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/components/ScheduleActionsDropdown.test.tsxnew file mode 100644index 0000000000..4cd22a22ca--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/components/ScheduleActionsDropdown.test.tsx@@ -0,0 +1,86 @@+import { getPostV1ExecuteGraphAgentMockHandler } from "@/app/api/__generated__/endpoints/graphs/graphs.msw";+import type { GraphExecutionJobInfo } from "@/app/api/__generated__/models/graphExecutionJobInfo";+import type { GraphExecutionMeta } from "@/app/api/__generated__/models/graphExecutionMeta";+import type { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";+import { server } from "@/mocks/mock-server";+import {+ fireEvent,+ render,+ screen,+ waitFor,+} from "@/tests/integrations/test-utils";+import { afterEach, describe, expect, test, vi } from "vitest";+import { ScheduleActionsDropdown } from "./ScheduleActionsDropdown";++const sendDatafastEvent = vi.hoisted(() => vi.fn());+vi.mock("@/services/analytics", () => ({+ analytics: { sendDatafastEvent },+}));++const toastMock = vi.fn();+vi.mock("@/components/molecules/Toast/use-toast", async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@/components/molecules/Toast/use-toast")+ >();+ return {+ ...actual,+ useToast: () => ({ toast: toastMock }),+ };+});++const agent = {+ id: "lib-1",+ graph_id: "graph-xyz",+ graph_version: 1,+ name: "My agent",+} as unknown as LibraryAgent;++const schedule = {+ id: "sched-1",+ graph_id: "graph-xyz",+ graph_version: 1,+ input_data: {},+ input_credentials: {},+} as unknown as GraphExecutionJobInfo;++afterEach(() => {+ toastMock.mockClear();+ sendDatafastEvent.mockClear();+ server.resetHandlers();+});++describe("ScheduleActionsDropdown", () => {+ test("Run now records a run_agent goal from the library surface", async () => {+ server.use(+ getPostV1ExecuteGraphAgentMockHandler({+ id: "run-1",+ graph_id: "graph-xyz",+ } as GraphExecutionMeta),+ );+ const onRunCreated = vi.fn();++ render(+ <ScheduleActionsDropdown+ agent={agent}+ schedule={schedule}+ onRunCreated={onRunCreated}+ />,+ );++ // Radix DropdownMenu opens on pointerdown, not click, under happy-dom.+ fireEvent.pointerDown(screen.getByLabelText("More actions"), {+ button: 0,+ });+ fireEvent.click(await screen.findByText("Run now"));++ await waitFor(() => {+ expect(onRunCreated).toHaveBeenCalledWith("run-1");+ });+ expect(sendDatafastEvent).toHaveBeenCalledExactlyOnceWith("run_agent", {+ id: "graph-xyz",+ name: "My agent",+ surface: "library",+ });+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/components/ScheduleActionsDropdown.tsx b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/components/ScheduleActionsDropdown.tsxindex 14915fa782..1ad5d0dcc8 100644--- a/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/components/ScheduleActionsDropdown.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/library/agents/[id]/components/NewAgentLibraryView/components/sidebar/SidebarRunsList/components/ScheduleActionsDropdown.tsx@@ -8,6 +8,7 @@ import { useDeleteV1DeleteExecutionSchedule } from "@/app/api/__generated__/endpimport type { GraphExecutionJobInfo } from "@/app/api/__generated__/models/graphExecutionJobInfo";import type { LibraryAgent } from "@/app/api/__generated__/models/libraryAgent";import { okData } from "@/app/api/helpers";+import { trackAgentRunGoal } from "@/services/analytics/activation-goals";import { Button } from "@/components/atoms/Button/Button";import { Text } from "@/components/atoms/Text/Text";import { Dialog } from "@/components/molecules/Dialog/Dialog";@@ -87,6 +88,12 @@ export function ScheduleActionsDropdown({});const newRunID = okData(res)?.id;+ if (newRunID) {+ trackAgentRunGoal(+ { id: schedule.graph_id, name: agent.name },+ "library",+ );+ }await queryClient.invalidateQueries({queryKey: getGetV1ListGraphExecutionsQueryKey(agent.graph_id),diff --git a/autogpt_platform/frontend/src/services/analytics/__tests__/activation-goals.test.ts b/autogpt_platform/frontend/src/services/analytics/__tests__/activation-goals.test.tsnew file mode 100644index 0000000000..279da42347--- /dev/null+++ b/autogpt_platform/frontend/src/services/analytics/__tests__/activation-goals.test.ts@@ -0,0 +1,45 @@+import { beforeEach, describe, expect, it, vi } from "vitest";+import {+ trackAgentRunGoal,+ trackScheduleCreatedGoal,+} from "../activation-goals";++const sendDatafastEvent = vi.hoisted(() => vi.fn());++vi.mock("@/services/analytics", () => ({+ analytics: { sendDatafastEvent },+}));++beforeEach(() => {+ sendDatafastEvent.mockReset();+});++describe("activation goals", () => {+ it("reports a human agent run with its surface", () => {+ trackAgentRunGoal({ id: "graph-1", name: "Daily digest" }, "builder");++ expect(sendDatafastEvent).toHaveBeenCalledWith("run_agent", {+ id: "graph-1",+ name: "Daily digest",+ surface: "builder",+ });+ });++ it("reports a new schedule and tolerates a missing agent name", () => {+ trackScheduleCreatedGoal({ id: "graph-2", name: null }, "library");++ expect(sendDatafastEvent).toHaveBeenCalledWith("schedule_agent", {+ id: "graph-2",+ name: "",+ surface: "library",+ });+ });++ it("never lets a failing tag break the action it describes", () => {+ sendDatafastEvent.mockImplementation(() => {+ throw new Error("tag exploded");+ });++ expect(() => trackAgentRunGoal({ id: "graph-3" }, "rerun")).not.toThrow();+ });+});diff --git a/autogpt_platform/frontend/src/services/analytics/activation-goals.ts b/autogpt_platform/frontend/src/services/analytics/activation-goals.tsnew file mode 100644index 0000000000..c2058879cb--- /dev/null+++ b/autogpt_platform/frontend/src/services/analytics/activation-goals.ts@@ -0,0 +1,42 @@+import { analytics } from "@/services/analytics";++/**+ * DataFast goals for the activation actions a person takes in the browser.+ *+ * These exist for marketing attribution (which channel brings users who+ * actually run things), so they fire only on human clicks, never for+ * server-triggered work. The server-side record of every run lives in+ * PostHog (`run_agent`, `run_autopilot`, ...) and the `analytics.*` views;+ * this file is only the DataFast mirror of the two goals it can see.+ *+ * `run_agent` used to fire from a single library modal, so builder runs,+ * re-runs and the builder's run dialog never counted. Every human run path+ * goes through `trackAgentRunGoal` now.+ */++export type AgentRunSurface = "library" | "builder" | "rerun";+export type ScheduleSurface = "library" | "builder";++interface AgentRef {+ id: string;+ name?: string | null;+}++export function trackAgentRunGoal(agent: AgentRef, surface: AgentRunSurface) {+ sendGoal("run_agent", { id: agent.id, name: agent.name ?? "", surface });+}++export function trackScheduleCreatedGoal(+ agent: AgentRef,+ surface: ScheduleSurface,+) {+ sendGoal("schedule_agent", { id: agent.id, name: agent.name ?? "", surface });+}++function sendGoal(name: string, metadata: Record<string, string>) {+ try {+ analytics.sendDatafastEvent(name, metadata);+ } catch {+ // Attribution must never break the action it describes.+ }+}