:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(customer-analytics): keep account usage/spend insight results across tab switches (#66162)
Arthur Moreira de Deus68033029c592
diff --git a/products/customer_analytics/frontend/components/Accounts/AGENTS.md b/products/customer_analytics/frontend/components/Accounts/AGENTS.mdindex 06ce3c7d8a8e..b9410892893e 100644--- a/products/customer_analytics/frontend/components/Accounts/AGENTS.md+++ b/products/customer_analytics/frontend/components/Accounts/AGENTS.md@@ -58,7 +58,9 @@ Sort safety: removing the sorted column drops the sort (`clearSortIfColumnRemove`AccountsHogQLTable.useExpandable()` makes expansion **controlled** by `accountsExpansionLogic`: `isRowExpanded` reads `expandedAccountIds`, `onRowExpand`/`onRowCollapse` dispatch `toggleAccountExpanded`. The body is `AccountNotebooksExpansion`, a `LemonTabs` over `notes` / `users` / `usage` (`AccountExpansionTab`) plus the Useful links sidebar. Active tab comes from `activeTabFor(accountId)` (defaults to `notes`).-**Tab data is cached for the row's expanded lifetime, not refetched per tab switch.** `LemonTabs` only renders the active tab's content (keyed by `activeKey`), so a tab's logic would normally unmount the moment you switch away and refetch on return. To avoid that, `AccountNotebooksExpansion` holds a mount reference to each per-tab logic via `useMountedLogic` — `accountRelatedUsersLogic({ externalId })` and `accountBillingLogic` for both `kind: 'usage'` and `kind: 'spend'`. Since `AccountNotebooksExpansion` stays mounted for as long as the row is expanded (it's the `expandedRowRender` body), those keyed instances — and their loaded data — survive tab switches and only tear down when the row collapses. The `useMountedLogic` props must stay identical to the `useValues` props in the tab components (`AccountRelatedUsersExpansion`, `AccountBillingExpansion`) so both resolve to the same keyed instance. `accountNotebooksLogic` and `accountLinksLogic` already mount at this level (Notes content and the always-rendered sidebar), so they need no explicit mount reference. Data is loaded once on mount and not auto-refreshed while the row stays open — acceptable since this data needn't be live.+**Tab data is cached for the row's expanded lifetime, not refetched per tab switch.** `LemonTabs` only renders the active tab's content (keyed by `activeKey`), so a tab's logic would normally unmount the moment you switch away and refetch on return. To avoid that, `AccountNotebooksExpansion` holds a mount reference to each per-tab logic via `useMountedLogic` — `accountRelatedUsersLogic({ externalId })` and `accountBillingLogic` for both `kind: 'usage'` and `kind: 'spend'`. Since `AccountNotebooksExpansion` stays mounted for as long as the row is expanded (it's the `expandedRowRender` body), those keyed instances survive tab switches and only tear down when the row collapses. The `useMountedLogic` props must stay identical to the `useValues` props in the tab components (`AccountRelatedUsersExpansion`, `AccountBillingExpansion`) so both resolve to the same keyed instance. `accountNotebooksLogic` and `accountLinksLogic` already mount at this level (Notes content and the always-rendered sidebar), so they need no explicit mount reference. Data is loaded once on mount and not auto-refreshed while the row stays open — acceptable since this data needn't be live.++The Users tab reads its loaded data straight off `accountRelatedUsersLogic`, so the root mount above keeps it cached. The **Usage/Spend** tabs are different: `accountBillingLogic` only holds the saved-insight _metadata_ — the actual insight **results** live in the `dataNodeLogic`/`dataVisualizationLogic` that the embedded `<Query>` mounts inside the tab body, which the root mount does **not** keep alive. So `AccountBillingExpansion` passes `attachTo={accountBillingLogic-instance}` to each `<Query>`: `useAttachedLogic` mounts the query's data logics and attaches them to the (root-mounted) `accountBillingLogic`, so they outlive the `<Query>` unmounting on tab switch and only detach when the row collapses. Without `attachTo`, usage/spend refetch on every return to the tab even with the metadata logic mounted.The Usage tab renders an existing saved billing-usage insight — **point users to it, don't rebuild usage as a new insight.**diff --git a/products/customer_analytics/frontend/components/Accounts/AccountBillingExpansion.tsx b/products/customer_analytics/frontend/components/Accounts/AccountBillingExpansion.tsxindex 862890bc7681..42e13d1bd057 100644--- a/products/customer_analytics/frontend/components/Accounts/AccountBillingExpansion.tsx+++ b/products/customer_analytics/frontend/components/Accounts/AccountBillingExpansion.tsx@@ -70,6 +70,9 @@ export function AccountBillingExpansion({variablesOverride={variableOverridesByShortId[insight.short_id] ?? null}readOnlyembedded+ // Attach the insight's data logic to accountBillingLogic (mounted at the expanded-row+ // root) so the loaded results survive tab switches instead of refetching on return.+ attachTo={logic}/></div></div>