feat(automations): enhance tracking for automation lifecycle events

- Added tracking for automation creation, updates, deletions, and trigger modifications, including success and failure events.
- Implemented event tracking in the automation creation process, including chat approval and rejection scenarios.
- Updated the instrumentation client to ensure correct typing for PostHog integration.
- Refactored existing mutation atoms to include tracking calls for automation-related actions, improving analytics capabilities.
This commit is contained in:
DESKTOP-RTLN3BA\$punk 2026-05-30 01:13:21 -07:00
parent b1b51ada89
commit 92b1d7a9f7
4 changed files with 316 additions and 14 deletions

View file

@ -7,6 +7,21 @@ import type {
TriggerUpdateRequest,
} from "@/contracts/types/automation.types";
import { automationsApiService } from "@/lib/apis/automations-api.service";
import {
trackAutomationCreated,
trackAutomationCreateFailed,
trackAutomationDeleted,
trackAutomationDeleteFailed,
trackAutomationStatusChanged,
trackAutomationTriggerAdded,
trackAutomationTriggerAddFailed,
trackAutomationTriggerRemoved,
trackAutomationTriggerRemoveFailed,
trackAutomationTriggerUpdated,
trackAutomationTriggerUpdateFailed,
trackAutomationUpdated,
trackAutomationUpdateFailed,
} from "@/lib/posthog/events";
import { cacheKeys } from "@/lib/query-client/cache-keys";
import { queryClient } from "@/lib/query-client/client";
@ -33,13 +48,28 @@ export const createAutomationMutationAtom = atomWithMutation(() => ({
mutationFn: async (request: AutomationCreateRequest) => {
return automationsApiService.createAutomation(request);
},
onSuccess: (_, variables) => {
onSuccess: (automation, variables) => {
invalidateList(variables.search_space_id);
toast.success("Automation created");
trackAutomationCreated({
search_space_id: variables.search_space_id,
automation_id: automation.id,
task_count: variables.definition.plan.length,
trigger_type: variables.triggers?.[0]?.type ?? "none",
has_schedule: (variables.triggers?.length ?? 0) > 0,
agent_llm_id: variables.definition.models?.agent_llm_id,
image_generation_config_id: variables.definition.models?.image_generation_config_id,
vision_llm_config_id: variables.definition.models?.vision_llm_config_id,
tags_count: variables.definition.metadata?.tags?.length,
});
},
onError: (error: Error) => {
onError: (error: Error, variables) => {
console.error("Error creating automation:", error);
toast.error("Failed to create automation");
trackAutomationCreateFailed({
search_space_id: variables.search_space_id,
error: error.message,
});
},
}));
@ -52,10 +82,32 @@ export const updateAutomationMutationAtom = atomWithMutation(() => ({
invalidateDetail(vars.automationId);
invalidateList(automation.search_space_id);
toast.success("Automation updated");
// A status-only patch (pause/resume/archive) is a distinct action from a
// definition/name edit, so split it into its own event.
if (vars.patch.status && !vars.patch.definition) {
trackAutomationStatusChanged({
automation_id: vars.automationId,
search_space_id: automation.search_space_id,
next_status: vars.patch.status,
});
} else {
trackAutomationUpdated({
automation_id: vars.automationId,
search_space_id: automation.search_space_id,
has_definition_change: !!vars.patch.definition,
has_name_change: vars.patch.name != null,
has_description_change: vars.patch.description !== undefined,
task_count: vars.patch.definition?.plan?.length,
});
}
},
onError: (error: Error) => {
onError: (error: Error, vars) => {
console.error("Error updating automation:", error);
toast.error("Failed to update automation");
trackAutomationUpdateFailed({
automation_id: vars.automationId,
error: error.message,
});
},
}));
@ -69,10 +121,18 @@ export const deleteAutomationMutationAtom = atomWithMutation(() => ({
invalidateList(vars.searchSpaceId);
invalidateDetail(vars.automationId);
toast.success("Automation deleted");
trackAutomationDeleted({
automation_id: vars.automationId,
search_space_id: vars.searchSpaceId,
});
},
onError: (error: Error) => {
onError: (error: Error, vars) => {
console.error("Error deleting automation:", error);
toast.error("Failed to delete automation");
trackAutomationDeleteFailed({
automation_id: vars.automationId,
error: error.message,
});
},
}));
@ -81,13 +141,24 @@ export const addTriggerMutationAtom = atomWithMutation(() => ({
mutationFn: async (vars: { automationId: number; payload: TriggerCreateRequest }) => {
return automationsApiService.addTrigger(vars.automationId, vars.payload);
},
onSuccess: (_, vars) => {
onSuccess: (trigger, vars) => {
invalidateDetail(vars.automationId);
toast.success("Trigger added");
trackAutomationTriggerAdded({
automation_id: vars.automationId,
trigger_id: trigger.id,
trigger_type: trigger.type,
enabled: trigger.enabled,
has_cron: !!trigger.params?.cron,
});
},
onError: (error: Error) => {
onError: (error: Error, vars) => {
console.error("Error adding trigger:", error);
toast.error("Failed to add trigger");
trackAutomationTriggerAddFailed({
automation_id: vars.automationId,
error: error.message,
});
},
}));
@ -103,10 +174,26 @@ export const updateTriggerMutationAtom = atomWithMutation(() => ({
onSuccess: (_, vars) => {
invalidateDetail(vars.automationId);
toast.success("Trigger updated");
const change: "enabled" | "params" | "other" = vars.patch.params
? "params"
: vars.patch.enabled !== undefined && vars.patch.enabled !== null
? "enabled"
: "other";
trackAutomationTriggerUpdated({
automation_id: vars.automationId,
trigger_id: vars.triggerId,
change,
enabled: vars.patch.enabled ?? undefined,
});
},
onError: (error: Error) => {
onError: (error: Error, vars) => {
console.error("Error updating trigger:", error);
toast.error("Failed to update trigger");
trackAutomationTriggerUpdateFailed({
automation_id: vars.automationId,
trigger_id: vars.triggerId,
error: error.message,
});
},
}));
@ -119,9 +206,18 @@ export const removeTriggerMutationAtom = atomWithMutation(() => ({
onSuccess: (vars) => {
invalidateDetail(vars.automationId);
toast.success("Trigger removed");
trackAutomationTriggerRemoved({
automation_id: vars.automationId,
trigger_id: vars.triggerId,
});
},
onError: (error: Error) => {
onError: (error: Error, vars) => {
console.error("Error removing trigger:", error);
toast.error("Failed to remove trigger");
trackAutomationTriggerRemoveFailed({
automation_id: vars.automationId,
trigger_id: vars.triggerId,
error: error.message,
});
},
}));