Skip to content

feat(analytics): tag discuss-launched task events with signal_report_id#2369

Merged
andrewm4894 merged 4 commits into
mainfrom
posthog-code/tag-discuss-session-events-with-report-id
May 27, 2026
Merged

feat(analytics): tag discuss-launched task events with signal_report_id#2369
andrewm4894 merged 4 commits into
mainfrom
posthog-code/tag-discuss-session-events-with-report-id

Conversation

@andrewm4894
Copy link
Copy Markdown
Member

@andrewm4894 andrewm4894 commented May 26, 2026

Summary

Tags discuss-launched sessions with signal_report_id so a session's PostHog events can be filtered down to a single inbox report.

  • Renderer: Task created is tagged at both call sites; a super-property (setActiveTaskAnalyticsContext) tags the rest of the in-task renderer events and is cleared on navigation away.
  • Agent → LLM gateway: configureEnvironment forwards x-posthog-property-signal_report_id, which the gateway lifts onto $ai_generation events. No posthog-repo change needed — the gateway already lifts arbitrary x-posthog-property-* headers.

Caveat

Only the Anthropic path forwards the header (ANTHROPIC_CUSTOM_HEADERS); OpenAI/codex models won't be tagged.

Test plan

  • typecheck, lint, agent + renderer unit tests
  • Verified live: renderer events tagged in-task and cleared on navigating to the inbox; gateway already lifts x-posthog-property-task_id onto $ai_generation (the same path this PR adds signal_report_id to)
  • $ai_generation carrying signal_report_id — verify post-deploy; discuss runs a prebuilt sandbox agent, so it isn't locally testable

When a user starts a chat from an inbox report via the Discuss button, the
task and task run already record the report ID on the backend, but the
analytics events did not — so it was not possible to filter PostHog events
down to a single discuss-launched session.

This adds `signal_report_id` to:

- `TASK_CREATED` (both call sites: useDiscussReport and useTaskCreation),
  so the event that opens the session is directly tagged.
- All subsequent events fired while the user is viewing the task, via a
  PostHog super-property registered through `setActiveTaskAnalyticsContext`
  on navigation. The property is set when entering a task-detail view
  whose task has `signal_report` populated, and cleared when navigating
  elsewhere (including via goBack/goForward).

Caveat: background events for non-active tasks (e.g. `AGENT_FILE_ACTIVITY`
firing while the user is on the inbox) won't pick up the super-property.
Threading the report ID through SessionService directly would cover that,
but it's substantially more invasive and can be done later if needed.

Generated-By: PostHog Code
Task-Id: d9940e39-a843-4018-ace7-be597fd964cd
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 26, 2026

Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
apps/code/src/renderer/stores/navigationStore.test.ts:19-22
**New behavior not tested**

`setActiveTaskAnalyticsContext` was added to the mock to prevent import errors, but no test cases were added to verify it is actually called — or called with the right argument — when navigating to/from a task that has `signal_report` set. Without these tests, a future refactor could silently stop tagging events and the suite would still pass. Consider adding a parameterised test that: (1) navigates to a task whose `signal_report` is populated and asserts `setActiveTaskAnalyticsContext` was called with that task, and (2) navigates back/away and asserts it was called with `null`.

### Issue 2 of 2
apps/code/src/renderer/utils/analytics.ts:160-170
**Stale super-property on cold start**

`posthog.register` persists super-properties to local storage, so `signal_report_id` survives an app restart. On the next launch, the store is rehydrated from persisted state before `hydrateTask` calls `navigateToTask` (which eventually re-invokes `setActiveTaskAnalyticsContext`). Events fired in that window — e.g. the initial `TASK_VIEWED` call or any session-level captures — would carry the `signal_report_id` from the previous session. The gap is small in practice, but worth a call to `posthog.unregister("signal_report_id")` during `initializePostHog` so the slate is always clean at startup.

Reviews (1): Last reviewed commit: "feat(analytics): tag discuss-launched ta..." | Re-trigger Greptile

Comment thread apps/code/src/renderer/stores/navigationStore.test.ts
Comment thread apps/code/src/renderer/utils/analytics.ts
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 06453d4c96

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread apps/code/src/renderer/utils/analytics.ts
Comment thread apps/code/src/renderer/utils/analytics.ts
Comment thread apps/code/src/renderer/utils/analytics.ts
PostHog persists super-properties to local storage, so `signal_report_id`
registered by `setActiveTaskAnalyticsContext` during one session would
survive an app restart. Events fired on next launch — before navigation
re-applies the current task's context — could carry a stale ID from the
previous session.

Unregister `signal_report_id` inside `initializePostHog` so every cold
start begins with a clean slate. The helper still re-registers it when
the user lands on a discuss-launched task.

Addresses Greptile P2 review feedback on #2369.

Generated-By: PostHog Code
Task-Id: d9940e39-a843-4018-ace7-be597fd964cd
@andrewm4894 andrewm4894 self-assigned this May 27, 2026
…uss-session-events-with-report-id

# Conflicts:
#	apps/code/src/renderer/utils/analytics.ts
Thread the inbox report UUID into configureEnvironment so it is emitted
as an x-posthog-property-signal_report_id header. The PostHog LLM gateway
lifts it onto the $ai_generation events the agent produces, so a discuss-
launched session's LLM traffic is tagged at source (Anthropic path only).

Renderer events were already tagged via the super-property in #2369; this
covers the agent/gateway-emitted events that the renderer cannot reach.
@andrewm4894 andrewm4894 requested review from a team May 27, 2026 12:17
@andrewm4894 andrewm4894 merged commit 36ab02d into main May 27, 2026
14 checks passed
@andrewm4894 andrewm4894 deleted the posthog-code/tag-discuss-session-events-with-report-id branch May 27, 2026 13:58
Gilbert09 added a commit that referenced this pull request May 28, 2026
Mirror desktop's inbox event names + property shapes on the mobile app so
both clients are comparable in PostHog dashboards.

Events wired:
- `Inbox viewed` — fired once per inbox-tab focus, with priority/actionability
  breakdown counts (per #2295).
- `Inbox report opened` / `closed` / `scrolled` — fired from the report
  detail screen via a new `useInboxEngagementTracker` hook, with priority +
  actionability snapshotted at open time.
- `Inbox report action` — fired for:
  - Dismiss / snooze via the dismiss sheet (with `dismissal_reason` and
    `dismissal_note`, truncated to 1000 chars per #2287).
  - "Start task" on the detail screen → `create_pr`, surface `detail_pane`.
  - Tinder swipe-right (accept) → `create_pr`, surface `list_row`.
  - Tinder swipe-left (dismiss) → `dismiss`, surface `list_row`.
  - Expanding the Signals list on the detail screen → `expand_signal`.

For #2369 (tagging discuss-launched task events with `signal_report_id`):
the task detail screen registers a `signal_report_id` PostHog super-property
for the duration of the screen when the task carries one, matching the
desktop super-property behaviour.

#2380 (`Signal source connected`) is intentionally skipped: there is no
signal-source connect / data-source-setup flow in the mobile app. We do not
fire any "Task created" event on mobile yet, so the desktop-side `Task
created` tagging in #2369 also doesn't apply here — only the super-property
half does, and that's what we wired.

Architecture: analytics types and the `useAnalytics()` / `useActiveTaskAnalyticsContext()`
hooks live in `apps/mobile/src/lib/analytics.ts`. Event names and property
shapes are literal mirrors of `apps/code/src/shared/types/analytics.ts` —
no shared package extraction since the desktop types live inside `apps/code`,
not in a shared workspace.

Tests:
- `apps/mobile/src/lib/analytics.test.ts` — `computeReportAgeHours` and
  the `useActiveTaskAnalyticsContext` super-property lifecycle.
- `apps/mobile/src/features/inbox/utils.test.ts` —
  `buildInboxViewedProperties` (priority/actionability counts,
  has_active_filters detection).
- `apps/mobile/src/features/inbox/hooks/useInboxEngagementTracker.test.ts`
  — OPENED/CLOSED/SCROLLED lifecycle, scroll-once semantics, and
  signalAction inheritance vs. overrides.

`pnpm --filter @posthog/mobile test` → 119 passed (14 new). `pnpm lint`
clean. No new TypeScript errors.

Generated-By: PostHog Code
Task-Id: 2e652fec-af01-4476-b6cf-ab9b4de015db
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