Skip to content

Commit 6f5e964

Browse files
committed
refactor: replace track* functions with generic typed track function
1 parent 8d2e9d1 commit 6f5e964

File tree

8 files changed

+236
-69
lines changed

8 files changed

+236
-69
lines changed

pnpm-lock.yaml

Lines changed: 197 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/renderer/components/MainLayout.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,15 @@ import { TaskDetail } from "@features/tasks/components/TaskDetail";
99
import { TaskList } from "@features/tasks/components/TaskList";
1010
import { useIntegrations } from "@hooks/useIntegrations";
1111
import { Box, Flex } from "@radix-ui/themes";
12-
import { trackTaskView } from "@renderer/lib/analytics";
12+
import { track } from "@renderer/lib/analytics";
1313
import type { Task } from "@shared/types";
1414
import { useLayoutStore } from "@stores/layoutStore";
1515
import { useTabStore } from "@stores/tabStore";
1616
import { useCallback, useEffect, useState } from "react";
1717
import { useHotkeys } from "react-hotkeys-hook";
1818
import { Toaster } from "sonner";
1919
import { NotetakerView } from "@/renderer/features/notetaker/components/NotetakerView";
20+
import { ANALYTICS_EVENTS } from "@/types/analytics";
2021

2122
export function MainLayout() {
2223
const { activeTabId, tabs, createTab, setActiveTab, closeTab } =
@@ -98,7 +99,7 @@ export function MainLayout() {
9899
});
99100

100101
// Track task view
101-
trackTaskView({
102+
track(ANALYTICS_EVENTS.TASK_VIEWED, {
102103
task_id: task.id,
103104
has_repository: !!task.repository_config,
104105
});

src/renderer/features/auth/stores/authStore.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { PostHogAPIClient } from "@api/posthogClient";
2-
import {
3-
identifyUser,
4-
resetUser,
5-
trackUserLoggedIn,
6-
trackUserLoggedOut,
7-
} from "@renderer/lib/analytics";
2+
import { identifyUser, resetUser, track } from "@renderer/lib/analytics";
83
import { queryClient } from "@renderer/lib/queryClient";
94
import { useTabStore } from "@renderer/stores/tabStore";
105
import type { CloudRegion } from "@shared/types/oauth";
@@ -14,6 +9,7 @@ import {
149
getCloudUrlFromRegion,
1510
TOKEN_REFRESH_BUFFER_MS,
1611
} from "@/constants/oauth";
12+
import { ANALYTICS_EVENTS } from "@/types/analytics";
1713

1814
const RECALL_API_URL = "https://us-west-2.recall.ai";
1915

@@ -137,7 +133,7 @@ export const useAuthStore = create<AuthState>()(
137133
project_id: projectId.toString(),
138134
region,
139135
});
140-
trackUserLoggedIn({
136+
track(ANALYTICS_EVENTS.USER_LOGGED_IN, {
141137
project_id: projectId.toString(),
142138
region,
143139
});
@@ -389,7 +385,7 @@ export const useAuthStore = create<AuthState>()(
389385
},
390386
logout: () => {
391387
// Track logout before clearing state
392-
trackUserLoggedOut();
388+
track(ANALYTICS_EVENTS.USER_LOGGED_OUT);
393389
resetUser();
394390

395391
if (refreshTimeoutId) {

src/renderer/features/repository-picker/components/RepositoryPicker.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { GitBranch as GitBranchIcon } from "@phosphor-icons/react";
33
import { ChevronDownIcon } from "@radix-ui/react-icons";
44
import { Box, Button, Flex, Popover, Text, TextField } from "@radix-ui/themes";
55
import { useRepositoryIntegration } from "@renderer/hooks/useIntegrations";
6-
import { trackRepositorySelect } from "@renderer/lib/analytics";
6+
import { track } from "@renderer/lib/analytics";
77
import type { RepositoryConfig } from "@shared/types";
88
import { cloneStore } from "@stores/cloneStore";
99
import { useMemo, useRef, useState } from "react";
1010
import { useHotkeys } from "react-hotkeys-hook";
11+
import { ANALYTICS_EVENTS } from "@/types/analytics";
1112

1213
interface RepositoryPickerProps {
1314
value: RepositoryConfig | null;
@@ -93,7 +94,7 @@ export function RepositoryPicker({
9394
setOpen(false);
9495

9596
// Track repository selection
96-
trackRepositorySelect({
97+
track(ANALYTICS_EVENTS.REPOSITORY_SELECTED, {
9798
repository_provider: "github", // Currently only GitHub is supported
9899
source: "task-creation",
99100
});

src/renderer/features/tasks/hooks/useTasks.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { useTaskExecutionStore } from "@features/tasks/stores/taskExecutionStore";
22
import { useAuthenticatedMutation } from "@hooks/useAuthenticatedMutation";
33
import { useAuthenticatedQuery } from "@hooks/useAuthenticatedQuery";
4-
import { trackTaskCreate } from "@renderer/lib/analytics";
4+
import { track } from "@renderer/lib/analytics";
55
import type { Task } from "@shared/types";
66
import { useQueryClient } from "@tanstack/react-query";
7+
import { ANALYTICS_EVENTS } from "@/types/analytics";
78

89
const taskKeys = {
910
all: ["tasks"] as const,
@@ -53,7 +54,7 @@ export function useCreateTask() {
5354
queryClient.invalidateQueries({ queryKey: taskKeys.lists() });
5455

5556
// Track task creation
56-
trackTaskCreate({
57+
track(ANALYTICS_EVENTS.TASK_CREATED, {
5758
has_repository: !!variables.repositoryConfig,
5859
auto_run: variables.autoRun || false,
5960
created_from: variables.createdFrom || "cli",

src/renderer/features/tasks/stores/taskExecutionStore.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useAuthStore } from "@features/auth/stores/authStore";
22
import { useSettingsStore } from "@features/settings/stores/settingsStore";
33
import type { AgentEvent } from "@posthog/agent";
4-
import { trackTaskRun } from "@renderer/lib/analytics";
4+
import { track } from "@renderer/lib/analytics";
55
import type {
66
ClarifyingQuestion,
77
ExecutionMode,
@@ -19,6 +19,7 @@ import type {
1919
ExecutionMode as AnalyticsExecutionMode,
2020
ExecutionType,
2121
} from "@/types/analytics";
22+
import { ANALYTICS_EVENTS } from "@/types/analytics";
2223

2324
interface ArtifactEvent {
2425
type: string;
@@ -379,7 +380,7 @@ export const useTaskExecutionStore = create<TaskExecutionStore>()(
379380
currentTaskState.executionMode;
380381
const hasRepository = !!task.repository_config;
381382

382-
trackTaskRun({
383+
track(ANALYTICS_EVENTS.TASK_RUN, {
383384
task_id: taskId,
384385
execution_type: executionType,
385386
execution_mode: executionMode,

src/renderer/lib/analytics.ts

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
import posthog from "posthog-js/dist/module.full.no-external";
2-
import {
3-
ANALYTICS_EVENTS,
4-
type RepositorySelectProperties,
5-
type TaskCreateProperties,
6-
type TaskListViewProperties,
7-
type TaskRunProperties,
8-
type TaskViewProperties,
9-
type UserIdentifyProperties,
2+
import type {
3+
EventPropertyMap,
4+
UserIdentifyProperties,
105
} from "../../types/analytics";
116

127
let isInitialized = false;
@@ -47,44 +42,14 @@ export function resetUser() {
4742
posthog.reset();
4843
}
4944

50-
export function trackTaskListView(properties?: TaskListViewProperties) {
51-
if (!isInitialized) return;
52-
53-
posthog.capture(ANALYTICS_EVENTS.TASK_LIST_VIEWED, properties);
54-
}
55-
56-
export function trackTaskCreate(properties: TaskCreateProperties) {
57-
if (!isInitialized) return;
58-
59-
posthog.capture(ANALYTICS_EVENTS.TASK_CREATED, properties);
60-
}
61-
62-
export function trackTaskView(properties: TaskViewProperties) {
63-
if (!isInitialized) return;
64-
65-
posthog.capture(ANALYTICS_EVENTS.TASK_VIEWED, properties);
66-
}
67-
68-
export function trackTaskRun(properties: TaskRunProperties) {
69-
if (!isInitialized) return;
70-
71-
posthog.capture(ANALYTICS_EVENTS.TASK_RUN, properties);
72-
}
73-
74-
export function trackRepositorySelect(properties: RepositorySelectProperties) {
75-
if (!isInitialized) return;
76-
77-
posthog.capture(ANALYTICS_EVENTS.REPOSITORY_SELECTED, properties);
78-
}
79-
80-
export function trackUserLoggedIn(properties?: UserIdentifyProperties) {
81-
if (!isInitialized) return;
82-
83-
posthog.capture(ANALYTICS_EVENTS.USER_LOGGED_IN, properties);
84-
}
85-
86-
export function trackUserLoggedOut() {
45+
export function track<K extends keyof EventPropertyMap>(
46+
eventName: K,
47+
...args: EventPropertyMap[K] extends never
48+
? []
49+
: EventPropertyMap[K] extends undefined
50+
? [properties?: EventPropertyMap[K]]
51+
: [properties: EventPropertyMap[K]]
52+
) {
8753
if (!isInitialized) return;
88-
89-
posthog.capture(ANALYTICS_EVENTS.USER_LOGGED_OUT);
54+
posthog.capture(eventName, args[0]);
9055
}

src/types/analytics.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,14 @@ export const ANALYTICS_EVENTS = {
6565

6666
export type AnalyticsEvent =
6767
(typeof ANALYTICS_EVENTS)[keyof typeof ANALYTICS_EVENTS];
68+
69+
// Event property mapping
70+
export type EventPropertyMap = {
71+
[ANALYTICS_EVENTS.TASK_LIST_VIEWED]: TaskListViewProperties | undefined;
72+
[ANALYTICS_EVENTS.TASK_CREATED]: TaskCreateProperties;
73+
[ANALYTICS_EVENTS.TASK_VIEWED]: TaskViewProperties;
74+
[ANALYTICS_EVENTS.TASK_RUN]: TaskRunProperties;
75+
[ANALYTICS_EVENTS.REPOSITORY_SELECTED]: RepositorySelectProperties;
76+
[ANALYTICS_EVENTS.USER_LOGGED_IN]: UserIdentifyProperties | undefined;
77+
[ANALYTICS_EVENTS.USER_LOGGED_OUT]: never;
78+
};

0 commit comments

Comments
 (0)