Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

fix(analytics): same hotkey events firing too often (#2186)

dev-rbcee69bc01fb6
diff --git a/js/app/packages/app/component/GlobalHotkeys.tsx b/js/app/packages/app/component/GlobalHotkeys.tsx
index be2b7b6733..d42d2fb273 100644
--- a/js/app/packages/app/component/GlobalHotkeys.tsx
+++ b/js/app/packages/app/component/GlobalHotkeys.tsx
@@ -4,7 +4,7 @@ import { TOKENS } from '@core/hotkey/tokens';
import type { ValidHotkey } from '@core/hotkey/types';
import { AiInstructionsIcon } from '@queries/storage/instructions-md';
import { registerHotkey } from 'core/hotkey/hotkeys';
-import { createMemo } from 'solid-js';
+import { createMemo, onCleanup } from 'solid-js';
import { useLogout } from '@core/auth/logout';
import LogoutIcon from '@icon/regular/sign-out.svg';
import UserIcon from '@icon/regular/user.svg';
@@ -35,10 +35,26 @@ import { useHandleFileUpload } from '@app/util/handleFileUpload';
import Upload from '@icon/regular/upload.svg';
import { useAnalytics } from '@app/component/analytics-context';
import { useSubscribeToKeypress } from '@app/signal/hotkeyRoot';
+import { debounce } from '@solid-primitives/scheduled';
function useHotkeyAnalytics(): void {
const analytics = useAnalytics();
+ const track = (
+ description: string,
+ token: string | undefined,
+ key: string
+ ) => {
+ analytics.track('hotkey_use', {
+ action: description,
+ token,
+ key,
+ });
+ };
+
+ const debouncedTrack = debounce(track, 250);
+
+ let lastFired: string | undefined;
useSubscribeToKeypress((context) => {
// Only track when a command was actually executed
if (!context.commandCaptured) return;
@@ -49,10 +65,24 @@ function useHotkeyAnalytics(): void {
? command.description()
: command.description;
- analytics.track('hotkey_use', {
- action: description,
- token: command.hotkeyToken,
- });
+ const pressedKeysString = context.pressedKeysString;
+
+ // If we keep firing the same key, we can debounce the track call to avoid
+ // sending many of the same event (like for tab, j, k, etc.). Otherwise, we
+ // can just track normally for unique events
+ let trackFn = lastFired === pressedKeysString ? debouncedTrack : track;
+
+ if (lastFired !== pressedKeysString) {
+ debouncedTrack.clear();
+ }
+
+ trackFn(description, command.hotkeyToken, pressedKeysString);
+
+ lastFired = pressedKeysString;
+ });
+
+ onCleanup(() => {
+ debouncedTrack.clear();
});
}