Skip to content

Agents Manager: add host-injectable tracking handler#109909

Draft
budzanowski wants to merge 1 commit intotrunkfrom
add/agents-manager-tracking-handler
Draft

Agents Manager: add host-injectable tracking handler#109909
budzanowski wants to merge 1 commit intotrunkfrom
add/agents-manager-tracking-handler

Conversation

@budzanowski
Copy link
Copy Markdown
Contributor

Summary

Adds a tracking module to @automattic/agents-manager that wraps @automattic/calypso-analytics's recordTracksEvent so hosts can inject a tracking handler without changing the default Calypso tracks behavior.

Also adds six new chat-shell events (panel_view, panel_close, panel_mode_change, button_click × 3) that fire exclusively through the host handler — invisible to hosts that don't opt in.

This is the first of two companion PRs. The CIAB side is in add/ciab-assistant-tracking-handler on ciab-admin, which pre-sets the trackingHandler to route chat events into CIAB's tracks pipeline as assistant_* with page_surface_area context. That PR is blocked on this one merging and deploying.

Why

Today agents-manager fires four calypso_agents_manager_* tracks events (feedback ×2, link click ×2) via direct calls to @automattic/calypso-analytics. These work fine in Calypso but there's no way for hosts like CIAB to observe them in their own telemetry pipelines — and there's no chat-shell telemetry at all (no panel opens, closes, dock/undock, button clicks).

This PR introduces a single gateway for all agents-manager tracks events with a host extension point, without changing the default behavior. Hosts that don't care see zero new events. Hosts that opt in receive every event (existing and new) through a single handler they control.

Design

tracking.ts module

Two entry points for call sites:

  • recordTracksEvent( name, props ) — drop-in replacement for the Calypso analytics import. Prepends calypso_ when firing through the default path, and also delivers the unprefixed name to any registered handler.
  • trackEvent( name, props ) — handler-only. No default Calypso path. Use for new events that have never existed in the Calypso namespace.

Two host-controlled switches:

  • setTrackingHandler( fn ) — register or clear the handler.
  • setDefaultTracksEnabled( enabled ) — toggle the default Calypso path. When disabled, only the handler receives events.

Host extension surface

Extends the existing window.__agentsManagerActions pre-set pattern (same as isCompactMode, isChatEnabled, desktopMediaQuery):

Property Type Default Description
trackingHandler function undefined Host-injected tracking handler
disableDefaultTracks boolean false Suppresses the default Calypso path

useSetupCustomActions reads both on first mount and calls setTrackingHandler / setDefaultTracksEnabled accordingly.

Call site changes

4 existing calypso_agents_manager_* events — dropped calypso_ prefix + swapped import

File Old event name New event name
components/custom-a-link/index.tsx calypso_agents_manager_link_click agents_manager_link_click
components/sources-display/index.tsx calypso_agents_manager_link_click agents_manager_link_click
hooks/use-feedback-action.ts calypso_agents_manager_response_feedback_action agents_manager_response_feedback_action
hooks/use-feedback-action.ts calypso_agents_manager_response_feedback_submitted agents_manager_response_feedback_submitted

What fires at the transport layer is unchanged: the wrapper prepends calypso_ before calling the real recordTracksEvent, so the tracks pipeline still sees calypso_agents_manager_link_click et al. Existing tests that mock @automattic/calypso-analytics pass unmodified.

6 new chat-shell events — handler-only via trackEvent

Event File Trigger
panel_view hooks/use-agent-layout-manager/index.tsx Sidebar FAB click (docked)
panel_view components/agent-dock/index.tsx Pop-out expand
panel_close hooks/use-agent-layout-manager/index.tsx Sidebar close (docked)
panel_close components/agent-dock/index.tsx Pop-out close
panel_mode_change components/agent-dock/index.tsx Dock/undock dropdown (both directions)
button_click (new_chat) components/agent-dock/index.tsx Header dropdown
button_click (view_history) components/chat-header/index.tsx History button
button_click (select_conversation) components/agent-dock/index.tsx handleSelectConversation

All carry chat_state and section_name. panel_mode_change adds previous_state / new_state. button_click adds button_label (and conversation_type for select_conversation).

Default behavior

Unchanged for hosts that don't register a handler:

  • The 4 legacy events still fire as calypso_agents_manager_* via the Calypso tracks pipeline (identical bytes on the wire)
  • The 6 new events do NOT fire at all — they're pure handler-only events

Test plan

  • New unit tests in src/__tests__/tracking.test.ts cover all 4 quadrants (default on/off × handler set/unset) plus the switches
  • Extended use-setup-custom-actions.test.ts to cover the trackingHandler and disableDefaultTracks pre-set wiring
  • All 112 existing agents-manager tests still pass unmodified
  • Lint clean (yarn lint:js packages/agents-manager)
  • Sandbox: build + sync to widgets.wp.com/agents-manager/ on a sandbox, visit a CIAB site, verify:
    • Legacy events (calypso_agents_manager_link_click etc.) still fire when no handler is registered
    • After pre-setting window.__agentsManagerActions = { trackingHandler: (event, props) => console.log('EVENT', event, props) } in the browser, reload, and verify each panel lifecycle / button click produces a console entry with the unprefixed name
    • After setting disableDefaultTracks: true in the same pre-set, verify no calypso_agents_manager_* events fire in the Network tab (only the handler receives them)

Related

  • Companion PR (CIAB): add/ciab-assistant-tracking-handler — pre-sets the handler to route chat events into CIAB's assistant_* namespace with page_surface_area context
  • Replaces the now-closed Automattic/big-sky-plugin#5932 and Automattic/ciab-admin#4280, which pointed at the wrong code path (Big Sky's OrchestratorDock is disabled in CIAB; the actual chat is agents-manager)

🤖 Generated with Claude Code

Adds a `tracking` module that wraps `@automattic/calypso-analytics`'s
`recordTracksEvent` so hosts can inject a tracking handler without
changing the default Calypso tracks behavior.

## tracking module

Two entry points for call sites:
- `recordTracksEvent( name, props )` — drop-in replacement for the
  Calypso analytics import. Prepends `calypso_` when firing through
  the default path, and also delivers to any registered handler with
  the unprefixed name.
- `trackEvent( name, props )` — handler-only. No default path, so new
  events are invisible to hosts that haven't opted in.

Two host-controlled switches:
- `setTrackingHandler( fn )` — register/clear the handler.
- `setDefaultTracksEnabled( enabled )` — toggle the default Calypso
  tracks path. When disabled, only the handler receives events.

Both are wired into the existing `window.__agentsManagerActions`
pre-set pattern via `trackingHandler` and `disableDefaultTracks`
properties, read by `useSetupCustomActions` on first mount.

## Call site migration

The 4 existing `calypso_agents_manager_*` tracks events flow through
the new `recordTracksEvent` wrapper. Their event names drop the
`calypso_` prefix at the call site (the wrapper prepends it):

- components/custom-a-link — `agents_manager_link_click`
- components/sources-display — `agents_manager_link_click`
- hooks/use-feedback-action — `agents_manager_response_feedback_action`
- hooks/use-feedback-action — `agents_manager_response_feedback_submitted`

## New chat-shell events (handler-only, via trackEvent)

- `panel_view`        — sidebar open (use-agent-layout-manager)
                        + pop-out expand (agent-dock)
- `panel_close`       — sidebar close (use-agent-layout-manager)
                        + pop-out close (agent-dock)
- `panel_mode_change` — dock / undock dropdown (agent-dock)
- `button_click`      — new_chat, view_history, select_conversation

All carry `chat_state` and `section_name`; `panel_mode_change` adds
`previous_state`/`new_state`; `button_click` adds `button_label`.

## Default behavior

Unchanged for hosts that don't register a handler. The 4 legacy
events still fire as `calypso_agents_manager_*` via the Calypso
tracks pipeline. The 6 new events do not fire at all unless a host
opts in — they're pure handler-only events.

Companion PR (CIAB side): `add/ciab-assistant-tracking-handler`
in ciab-admin, which pre-sets the \`trackingHandler\` to route chat
events into CIAB's tracks pipeline as \`assistant_*\`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@matticbot
Copy link
Copy Markdown
Contributor

This PR modifies the release build for the following Calypso Apps:

For info about this notification, see here: PCYsg-OT6-p2

  • agents-manager
  • notifications
  • wpcom-block-editor

To test WordPress.com changes, run install-plugin.sh $pluginSlug add/agents-manager-tracking-handler on your sandbox.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants