ChatbotX icon indicating copy to clipboard operation
ChatbotX copied to clipboard

Analytics: "All contacts by channel" donut shows the same data as "New contacts by channel" — both read a query already scoped to `contact_created`

Open rawdaymx opened this issue 2 months ago • 1 comments

Summary

The contacts analytics dashboard renders two donut charts side by side, AllContactsByChannelChart (title: "All contacts by channel") and ContactsByChannelChart (title: "New contacts by channel"). Both components read the exact same Zustand store field, contactsByChannel, with identical mapping logic — the only difference between the two files is the chart title. Tracing the field to its source shows why: getContactsByChannel in contact-stats.repository.ts filters on "eventType" = 'contact_created' inside the selected date range, so the underlying query was already scoped to new contacts before either component ever sees it. There is no second query or store field carrying an unscoped "all contacts by channel" breakdown — the chart titled "All" is showing the same "new in range" numbers as the chart titled "New".

A control that confirms this isn't a rendering coincidence: getContactsCount, a few lines below in the same repository file, computes a genuine "all contacts" total by summing InboxContactStat.totalContacts — a table keyed by inbox, which carries channel via its Inbox relation. That table is never joined or grouped by channel anywhere in the codebase, so no code path currently produces the number the "All" donut's title promises.

Environment

Static defect — read against upstream/main at commit 62e9fe323 (checked out locally, not a deployed instance).

File Line(s)
packages/analytics-nextjs/src/components/charts/all-contacts-by-channel-chart.tsx 8-29
packages/analytics-nextjs/src/components/charts/contacts-by-channel-chart.tsx 8-29
packages/analytics-nextjs/src/provider/analysis-store.ts 538-547
packages/analytics-nextjs/src/routes/contact.ts 217-251
packages/analytics/src/repositories/postgres/contact-stats.repository.ts 579-620

How to see it

  1. Open all-contacts-by-channel-chart.tsx and contacts-by-channel-chart.tsx side by side — both call useAnalysisStore((state) => state.contactsByChannel) and map it the same way.
  2. Follow contactsByChannel to where it's populated: getContactsByChannel in analysis-store.ts:538 calls contactsByDimensionAnalyticsAPI({ dimension: "channel" }).
  3. Follow that route (routes/contact.ts:217) to contactAnalyticsService.getContactsByChannel → contactStatsRepository.getContactsByChannel → the shared private method getContactsByDimension (contact-stats.repository.ts:592), whose SQL filters "eventType" = 'contact_created' AND "occurredAt" BETWEEN from AND to.
  4. There is no other query, route, or store action producing a channel-scoped "all contacts" number. getContactsCount (line 578) computes a genuine unscoped total, but only as a single sum for the whole workspace, not broken down by channel.

Expected: the chart titled "All contacts by channel" shows a channel breakdown of every contact ever created in the workspace (or at least unscoped by the "new in range" filter). Actual: it shows exactly the same numbers as "New contacts by channel", because both read the same query, which is scoped to contact_created events inside the selected date range.

Where it comes from

getContactsByDimension (packages/analytics/src/repositories/postgres/contact-stats.repository.ts:596-620):

SELECT
  "channel" AS dimension,
  COUNT(*)::int AS count,
  COUNT(DISTINCT "contactId")::int AS "uniqueContacts"
FROM "AnalyticsContactEvent"
WHERE "workspaceId" = ${workspaceId}
  AND "occurredAt" >= ${from}
  AND "occurredAt" <= ${to}
  AND "eventType" = 'contact_created'
  AND "channel" IS NOT NULL
  AND "channel" != ''
GROUP BY 1
ORDER BY count DESC

Both getContactsByChannel and getContactsByCountry share this method, and both charts that consume the channel variant share the same store field — there is only one query, used twice under two different titles.

Why we think this is a defect rather than the intended design

We looked for a deliberate reason AllContactsByChannelChart might intentionally show the same "new in range" numbers (e.g. if "all" meant "all channels" rather than "all contacts"), but nothing in the code or its history supports that reading: git log for both chart files shows the same commits touching both, most recently 628eadbe3 (#663), which only adds i18n labels and does not touch the data source. Nothing distinguishes the two components' data — only their titles do. We found no PR or code comment stating this duplication is deliberate.

Suggested fix

We don't have a fix to propose: producing a genuine "all contacts by channel" number requires a new query (e.g., grouping InboxContactStat.totalContacts by Inbox.channel), which is an architecture decision — should "all" mean "all-time contacts" (from InboxContactStat, which doesn't track channel per row directly but could via the Inbox join) or something else. We did not build a patch for this reason: the right shape of the fix depends on a decision only the maintainers can make, not a one-line change to an existing query.

What we did not verify

  • We did not check whether other analytics dashboards elsewhere in the product have the same "all" vs "new" duplication pattern — this report is scoped to the contacts-by-channel pair we found while working on an onboarding feature.
  • We did not measure this against a running instance; the trace above was done reading upstream/main at 62e9fe323.

Related issues — none of these describes it

Searched "contacts by channel", "allContactsByChannel", "donut chart", and "analytics duplicate" in this repository's issues — no results for any of the four. A control search for "getUserData commentAnchor" (expected to return our own issue #1186) did return it, confirming the search mechanism itself works and the empty results above are a true absence, not a query failure.

rawdaymx avatar Sep 19 '26 14:09 rawdaymx

Thanks for the detailed report. This has been fixed in #1501: "All contacts by channel" now shows the contacts that exist at the end of the selected range, separate from "New contacts by channel".

viethung0o0 avatar Oct 16 '26 09:10 viethung0o0