Skip to content

feat(comment-automation): delivery stats, miss tracking, tag counters, and multi-text replies - #1171

Merged
sung17 merged 3 commits into
mainfrom
feat/comment-automation-delivery-stats
Sep 13, 2026
Merged

sung17 merged 3 commits into
mainfrom
feat/comment-automation-delivery-stats

Conversation

@sung17

@sung17 sung17 commented Sep 13, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Adds the delivery funnel to comment automation — Sent / Delivered / Seen / Clicked / Failed — plus a Misses column backed by its own FBCommentAutomationMiss table, so a filter decline is recorded with a reason instead of vanishing.
  • Adds comment tag counters ({{total_tagged}} / {{total_new_tagged}}) and multi-text public replies (several comment replies that still count as one reply).
  • Fixes a set of defects found while verifying the funnel end to end against a real Page. Every one of them surfaced as a silent zero, never an error — the analytics layer swallows its own failures by design.

Changes

New capability

  • packages/database — FBCommentAutomationMiss table + migration; misses live apart from FBCommentAutomationEvent because that table purges failed rows nightly, which would walk the denominator down every night.
  • apps/worker/src/integration/handlers/comment-automation/ — miss collection on every filter decline, comment tag counters, multi-text public reply dispatch.
  • apps/builder + packages/analytics-nextjs — stat columns, drill-down dialog, replies chart, error logs, day/month bucketing. New ReplyTextsField; all strings via useTranslations() across all 20 locales.

Fixes

  • Delivered/Seen were stuck at zero for a text private reply: markDelivered ran before recordReplyEvent inserted the row, so the UPDATE matched nothing — and Seen requires deliveredAt IS NOT NULL. The row is now born delivered.
  • Clicked could never increment: the private flow reply passed only commentAnchor, while every button encoder reads metadata.commentAutomationId.
  • A flow/AIAgent reply was enqueued before its analytics row existed; with no replyAfter the job could settle a row that was not there yet. Dispatch is deferred until after the row is written.
  • Messenger image/video steps with buttons dropped metadata entirely — losing comment-automation, broadcast and sequence attribution alike.
  • markClicked could double-count concurrent clicks; markSeen accepted a read receipt older than the reply; settleEvent never cleared failedAt on the way back to sent; getContacts paged without a tiebreaker and counted contacts the list did not show.

Observability

  • The click path was silent end to end. Two warns (click with no attribution, click matching no row) plus three LOG_DEBUG-gated diagnostics now cover encode → tap → lookup → count.
  • logDiagnostic calls isLevelEnabled through an optional call, so the repo's hand-rolled logger doubles in tests opt out instead of throwing.

Test plan

  • pnpm lint (includes i18n-check and the drizzle drift guard)
  • pnpm --filter worker check-types, --filter builder, --filter @chatbotx.io/analytics, --filter @chatbotx.io/integration-messenger
  • pnpm --filter worker test — 2295 passed
  • pnpm --filter builder test — 3591 passed
  • pnpm --filter @chatbotx.io/analytics test — 215 passed
  • pnpm --filter @chatbotx.io/integration-messenger test — 281 passed
  • pnpm --filter @chatbotx.io/database test — 706 passed
  • Media-attribution regression test verified red without the fix, green with it
  • db:migrate on staging — not run here, needs explicit approval per repo rules
  • Manual against a real Page: text private reply → Sent + Delivered rise, opening the DM raises Seen
  • Manual: flow private reply with a button and no replyAfter (delay 0, the race condition) → Delivered rises, tapping raises Clicked
  • Manual: public-only automation reads ---- across the funnel, while its own analytics page still shows data
  • Manual: image/video step with a button → tap is attributed

🤖 Generated with Claude Code

@github-actions github-actions Bot added the feature New feature or request label Sep 13, 2026
Deathgiver and others added 3 commits September 13, 2026 22:12
…livery stats

Record delivery lifecycle events for comment automation replies and surface
them as Sent/Delivered/Seen/Clicked/Failed columns on the fb-comments and
ig-comments tables, reusing the broadcast/sequence stats vocabulary so the
contacts dialog and bulk-tag pipeline stay a single implementation.

- add lifetime counters on FBCommentAutomation plus the event columns and
  indexes behind them (migration 20260911081114)
- increment counters from the message/flow listeners via the anchor resolver
- add the shared stats cell, stat columns, and contacts dialog components
- extend the analytics repository/service with the contacts listing query

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
…lti-text public replies

Builds on the delivery-stat columns with the three things they could not
answer on their own: which comments the automation declined, how many tags a
commenter has accumulated, and replies made of more than one message.

- FBCommentAutomationMiss: every filter decline now records a reason, so the
  Misses column reports a real denominator instead of silence. Misses go to
  their own table because FBCommentAutomationEvent purges failed rows nightly.
- Comment tag counters behind {{total_tagged}} / {{total_new_tagged}}.
- Multi-text public reply (ReplyTextsField), posted as separate comment
  replies but counted as one reply.
- Per-automation analytics: replies chart, error logs, day/month bucketing.

Fixes found while verifying the stats end to end, each of which showed up as a
silent zero rather than an error:

- Delivered/Seen stayed at zero for a text private reply: markDelivered ran
  before recordReplyEvent inserted the row, so the UPDATE matched nothing. The
  row is now born delivered.
- Clicked could never increment: the private flow reply passed only
  commentAnchor, while every button encoder reads metadata.commentAutomationId.
- A flow/AIAgent reply was enqueued before its analytics row existed, so with
  no replyAfter the job could settle a row that was not there yet. Dispatch is
  now deferred until after the row is written.
- Messenger image/video steps with buttons dropped metadata entirely, losing
  comment-automation, broadcast and sequence attribution alike.
- markClicked could double-count concurrent clicks; markSeen accepted a read
  receipt older than the reply; settleEvent never cleared failedAt on the way
  back to sent; getContacts paged without a tiebreaker and counted contacts
  the list did not show.

The click path was silent end to end, so two warns and three opt-in
diagnostics now cover it. logDiagnostic calls isLevelEnabled through an
optional call so the repo's hand-rolled logger doubles in tests opt out
instead of throwing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Both snapshots were generated before main dropped AIAssistant/AITrigger and
added the ErrorLog columns, so after the rebase they forked the chain at an
older tip and still described tables that no longer exist. drizzle-kit
reported a non-commutative chain: two branches each adding sentCount.

Neither migration.sql changed — the SQL was always correct. What changed is
the snapshots' lineage and content:

- 20260911081114_add_comment_automation_delivery_stats is renamed to
  20260913042025_... so folder order matches prevIds order; it used to sort
  between two of main's own migrations while claiming to follow the older tip.
- Both snapshots are rebuilt as "main's tip + this branch's own entities",
  dropping the AIAssistant/AITrigger entries they should never have carried.

normalizeReplyTexts moves into fbCommentAutomationService, where main's #1098
refactor put every write to this table. It was applied per call site before,
which left the template installer out — and a row whose `value` disagrees with
its `values` sends the wrong text with no error.

Verified with drizzle-kit check ("Everything's fine") and db:check-drift.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@sung17
sung17 force-pushed the feat/comment-automation-delivery-stats branch from f3f23b5 to 46074e8 Compare September 13, 2026 15:35
@sung17
sung17 merged commit 39be94e into main Sep 13, 2026
4 checks passed
hello-jackytruong referenced this pull request in hello-jackytruong/ChatWorkAI Nov 22, 2026
…, and multi-text replies (#1171)

* feat(comment-automation): track sent/delivered/seen/clicked/failed delivery stats

Record delivery lifecycle events for comment automation replies and surface
them as Sent/Delivered/Seen/Clicked/Failed columns on the fb-comments and
ig-comments tables, reusing the broadcast/sequence stats vocabulary so the
contacts dialog and bulk-tag pipeline stay a single implementation.

- add lifetime counters on FBCommentAutomation plus the event columns and
  indexes behind them (migration 20260911081114)
- increment counters from the message/flow listeners via the anchor resolver
- add the shared stats cell, stat columns, and contacts dialog components
- extend the analytics repository/service with the contacts listing query

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* feat(comment-automation): track missed comments, tag counters, and multi-text public replies

Builds on the delivery-stat columns with the three things they could not
answer on their own: which comments the automation declined, how many tags a
commenter has accumulated, and replies made of more than one message.

- FBCommentAutomationMiss: every filter decline now records a reason, so the
  Misses column reports a real denominator instead of silence. Misses go to
  their own table because FBCommentAutomationEvent purges failed rows nightly.
- Comment tag counters behind {{total_tagged}} / {{total_new_tagged}}.
- Multi-text public reply (ReplyTextsField), posted as separate comment
  replies but counted as one reply.
- Per-automation analytics: replies chart, error logs, day/month bucketing.

Fixes found while verifying the stats end to end, each of which showed up as a
silent zero rather than an error:

- Delivered/Seen stayed at zero for a text private reply: markDelivered ran
  before recordReplyEvent inserted the row, so the UPDATE matched nothing. The
  row is now born delivered.
- Clicked could never increment: the private flow reply passed only
  commentAnchor, while every button encoder reads metadata.commentAutomationId.
- A flow/AIAgent reply was enqueued before its analytics row existed, so with
  no replyAfter the job could settle a row that was not there yet. Dispatch is
  now deferred until after the row is written.
- Messenger image/video steps with buttons dropped metadata entirely, losing
  comment-automation, broadcast and sequence attribution alike.
- markClicked could double-count concurrent clicks; markSeen accepted a read
  receipt older than the reply; settleEvent never cleared failedAt on the way
  back to sent; getContacts paged without a tiebreaker and counted contacts
  the list did not show.

The click path was silent end to end, so two warns and three opt-in
diagnostics now cover it. logDiagnostic calls isLevelEnabled through an
optional call so the repo's hand-rolled logger doubles in tests opt out
instead of throwing.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

* fix(database): relink comment automation migrations onto main's chain

Both snapshots were generated before main dropped AIAssistant/AITrigger and
added the ErrorLog columns, so after the rebase they forked the chain at an
older tip and still described tables that no longer exist. drizzle-kit
reported a non-commutative chain: two branches each adding sentCount.

Neither migration.sql changed — the SQL was always correct. What changed is
the snapshots' lineage and content:

- 20260911081114_add_comment_automation_delivery_stats is renamed to
  20260913042025_... so folder order matches prevIds order; it used to sort
  between two of main's own migrations while claiming to follow the older tip.
- Both snapshots are rebuilt as "main's tip + this branch's own entities",
  dropping the AIAssistant/AITrigger entries they should never have carried.

normalizeReplyTexts moves into fbCommentAutomationService, where main's #1098
refactor put every write to this table. It was applied per call site before,
which left the template installer out — and a row whose `value` disagrees with
its `values` sends the wrong text with no error.

Verified with drizzle-kit check ("Everything's fine") and db:check-drift.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>

---------

Co-authored-by: Deathgiver <[email protected]>
Co-authored-by: Claude Opus 5 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant