Skip to content

The micro-VM for AI agents — light enough to embed on your laptop, elastic enough to power an agentic cloud.

5.7 KB
Raw Blame
import { useLayoutEffect, useRef } from 'react'
import { matchPath, useLocation } from 'react-router-dom'
import {
  consoleActions,
  consoleControls,
  consolePages,
  validOnboardingInteraction,
  type ConsoleInteraction,
} from '../../../shared/console-analytics'
import { billingTab } from '@/lib/billing-tab'
import type { FirstRunUseCase } from '@/lib/first-run'
import { RoutePath } from '@/enums/RoutePath'
import { useConfig } from './useConfig'
import { useApi } from './useApi'

const explicitInteractions = new EventTarget()
type ExplicitInteraction =
  | { action: string; selection?: string; control: 'tab' }
  | { action: 'org_create_started' | 'use_case_selected'; organizationId: string; selection?: FirstRunUseCase }
const routes = [
  ...consolePages.filter((route) => !route.includes(':')),
  ...consolePages.filter((route) => route.includes(':')),
]
const consolePage = (path: string) => routes.find((route) => matchPath(route, path))

function recordExplicitInteraction(detail: ExplicitInteraction) {
  explicitInteractions.dispatchEvent(new CustomEvent('interaction', { detail }))
}

export function recordConsoleTabSelection(action: string, selection?: string) {
  recordExplicitInteraction({ action, selection, control: 'tab' })
}

export function recordConsoleOnboardingStep(
  action: 'org_create_started' | 'use_case_selected',
  organizationId: string,
  selection?: FirstRunUseCase,
) {
  recordExplicitInteraction({ action, organizationId, selection })
}

/** Record only transitions and interactions that occur during an active grant. */
export function useConsoleAnalytics(organizationId: string, enabledFrom: string | null | undefined) {
  const api = useApi()
  const { billingApiUrl } = useConfig()
  const { pathname, search, key } = useLocation()
  const pending = useRef(0)
  const waiting = useRef<Array<() => void>>([])
  const lastView = useRef<{ key: string; at: Date; sent: boolean } | undefined>(undefined)

  // Navigation shares the backlog, but queued events belong to one consent grant.
  useLayoutEffect(() => {
    const queue = waiting.current
    return () => {
      queue.length = 0
    }
  }, [organizationId, enabledFrom])

  useLayoutEffect(() => {
    const viewKey = JSON.stringify([organizationId, key, pathname])
    if (lastView.current?.key !== viewKey) lastView.current = { key: viewKey, at: new Date(), sent: false }
    const view = lastView.current
    if (enabledFrom === null) view.sent = true
    const page = consolePage(pathname)
    if (!page || !enabledFrom || !organizationId) return
    const send = (event: ConsoleInteraction['event'], at: Date, properties: Partial<ConsoleInteraction> = {}) => {
      const body: ConsoleInteraction = {
        event,
        page,
        occurredAt: at.toISOString(),
        ...(page === RoutePath.BILLING && billingApiUrl && { billingTab: billingTab(new URLSearchParams(search)) }),
        ...properties,
      }
      const post = () => {
        pending.current++
        void api.axiosInstance
          .post('/analytics/organizations/' + encodeURIComponent(organizationId) + '/console-events', body, {
            timeout: 2000,
          })
          .catch(() => {})
          .finally(() => {
            pending.current--
            waiting.current.shift()?.()
          })
      }
      if (pending.current < 2) post()
      else if (waiting.current.length < 16) waiting.current.push(post)
    }
    if (!view.sent && view.at.getTime() >= Date.parse(enabledFrom)) {
      view.sent = true
      send('$pageview', view.at)
    }
    const sendOnboarding = (action: string | undefined, control?: string, selection?: string) => {
      if (!validOnboardingInteraction({ page, action, control, selection })) return false
      send('console_onboarding_step', new Date(), { action, control, selection })
      return true
    }
    const click = (event: MouseEvent) => {
      if (
        !(event.target instanceof Element) ||
        event.target.closest('[data-analytics-preferences],.xterm,[contenteditable="true"]')
      )
        return
      const target = event.target.closest('button,a[href],[role="button"],[role="menuitem"],[role="tab"]')
      if (!target) return
      const control = target.getAttribute('role') ?? target.tagName.toLowerCase()
      if (!consoleControls.includes(control) || (control === 'tab' && target.closest('[data-console-tab-selections]')))
        return
      const action = target.getAttribute('data-console-action') ?? undefined
      const selection = target.getAttribute('data-console-selection') ?? undefined
      if (sendOnboarding(action, control, selection)) return
      const destination =
        target instanceof HTMLAnchorElement && target.origin === window.location.origin
          ? consolePage(target.pathname)
          : undefined
      send('console_control_click', new Date(), {
        control,
        ...(action && consoleActions.includes(action) && { action }),
        destination,
      })
    }
    const record = (event: Event) => {
      const { detail } = event as CustomEvent<ExplicitInteraction>
      if ('organizationId' in detail) {
        if (detail.organizationId === organizationId) sendOnboarding(detail.action, undefined, detail.selection)
        return
      }
      const { action, control, selection } = detail
      if (!sendOnboarding(action, control, selection) && consoleActions.includes(action)) {
        send('console_control_click', new Date(), { control, action })
      }
    }
    explicitInteractions.addEventListener('interaction', record)
    document.addEventListener('click', click, true)
    return () => {
      explicitInteractions.removeEventListener('interaction', record)
      document.removeEventListener('click', click, true)
    }
  }, [api, enabledFrom, billingApiUrl, key, organizationId, pathname, search])
}