Skip to content

Commit 531d81f

Browse files
committed
fix(analytics): fix posthog in useCallback deps and fire block events for bulk operations
1 parent e01511c commit 531d81f

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

  • apps/sim

apps/sim/app/workspace/[workspaceId]/home/home.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export function Home({ chatId }: HomeProps = {}) {
3030
const router = useRouter()
3131
const { data: session } = useSession()
3232
const posthog = usePostHog()
33+
const posthogRef = useRef(posthog)
3334
const [initialPrompt, setInitialPrompt] = useState('')
3435
const hasCheckedLandingStorageRef = useRef(false)
3536
const initialViewInputRef = useRef<HTMLDivElement>(null)
@@ -202,12 +203,16 @@ export function Home({ chatId }: HomeProps = {}) {
202203
return () => cancelAnimationFrame(id)
203204
}, [resources])
204205

206+
useEffect(() => {
207+
posthogRef.current = posthog
208+
}, [posthog])
209+
205210
const handleSubmit = useCallback(
206211
(text: string, fileAttachments?: FileAttachmentForApi[], contexts?: ChatContext[]) => {
207212
const trimmed = text.trim()
208213
if (!trimmed && !(fileAttachments && fileAttachments.length > 0)) return
209214

210-
captureEvent(posthog, 'task_message_sent', {
215+
captureEvent(posthogRef.current, 'task_message_sent', {
211216
has_attachments: !!(fileAttachments && fileAttachments.length > 0),
212217
has_contexts: !!(contexts && contexts.length > 0),
213218
is_new_task: !chatId,
@@ -219,7 +224,7 @@ export function Home({ chatId }: HomeProps = {}) {
219224

220225
sendMessage(trimmed || 'Analyze the attached file(s).', fileAttachments, contexts)
221226
},
222-
[sendMessage, posthog]
227+
[sendMessage]
223228
)
224229

225230
useEffect(() => {

apps/sim/stores/workflows/workflow/store.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,15 @@ export const useWorkflowStore = create<WorkflowStore>()(
316316
}
317317
}
318318

319-
if (blocks.length === 1) {
320-
const workflowId = get().currentWorkflowId ?? 'unknown'
321-
import('@/lib/posthog/client')
322-
.then(({ captureClientEvent }) => {
323-
captureClientEvent('block_added', {
324-
block_type: blocks[0].type,
325-
workflow_id: workflowId,
326-
})
327-
})
328-
.catch(() => {})
329-
}
319+
const workflowId = get().currentWorkflowId ?? 'unknown'
320+
const uniqueBlockTypes = [...new Set(blocks.map((b) => b.type))]
321+
import('@/lib/posthog/client')
322+
.then(({ captureClientEvent }) => {
323+
for (const blockType of uniqueBlockTypes) {
324+
captureClientEvent('block_added', { block_type: blockType, workflow_id: workflowId })
325+
}
326+
})
327+
.catch(() => {})
330328

331329
get().updateLastSaved()
332330
},
@@ -387,19 +385,21 @@ export const useWorkflowStore = create<WorkflowStore>()(
387385
parallels: generateParallelBlocks(newBlocks),
388386
})
389387

390-
if (ids.length === 1) {
391-
const blockType = currentBlocks[ids[0]]?.type
392-
if (blockType) {
393-
const workflowId = get().currentWorkflowId ?? 'unknown'
394-
import('@/lib/posthog/client')
395-
.then(({ captureClientEvent }) => {
388+
const workflowId = get().currentWorkflowId ?? 'unknown'
389+
const uniqueRemovedTypes = [
390+
...new Set(ids.map((id) => currentBlocks[id]?.type).filter((t): t is string => !!t)),
391+
]
392+
if (uniqueRemovedTypes.length > 0) {
393+
import('@/lib/posthog/client')
394+
.then(({ captureClientEvent }) => {
395+
for (const blockType of uniqueRemovedTypes) {
396396
captureClientEvent('block_removed', {
397397
block_type: blockType,
398398
workflow_id: workflowId,
399399
})
400-
})
401-
.catch(() => {})
402-
}
400+
}
401+
})
402+
.catch(() => {})
403403
}
404404

405405
get().updateLastSaved()

0 commit comments

Comments
 (0)