mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-03 20:09:33 +01:00
<!-- claude-projects-attribution: {"github_login":"nguyenm7"} -->
## Thinking Path
> - Paperclip is the open source app people use to manage AI agents for
work
> - Paperclip Cloud measures activation with a funnel: account created,
org named, agent named, model connected, chat launched, first agent run
succeeded, first task created
> - `model.connected`, `agent.task_run` and `task.created` come from the
server, but "chat launched" comes only from the browser
(`onboarding_step_completed` with `step=launch`)
> - `task.created` skips chats on purpose, so a person who only chats
with their agent has no server-side step after "first agent run"
> - #23403 added a Cloud-only direct PostHog path
(`trackCloudPostHogEvent`) for onboarding events that must not depend on
the browser
> - This pull request uses that path to emit `chat.started` when the
chat owner's first message or first question-card answer commits
> - The benefit is a server-side "chat started" step that counts a real
action by the person, once per chat, whichever way they reached the chat
## Linked Issues or Issue Description
Refs #23403
**What existing behavior does this improve?**
Server-side activation funnels on Paperclip Cloud.
**Current behavior**
The only "chat launched" signal is the browser `launch` step. Browser
events can be lost (wizard skipped or resumed, navigation, ad blockers
on older builds). On the server, a chat creates an issue but
`emitTaskCreatedAfterCommit` returns early for it, so no event records
that the chat started.
**Proposed behavior**
When the chat owner's first turn commits (their first message, or their
first answer to a question card such as the onboarding welcome card),
the server emits the Cloud-only event `chat.started` with `surface`
(`onboarding_chat` | `agent_chat`) and `agent_id`. Creating or preparing
a chat emits nothing, because onboarding prepares the chat before the
person clicks Launch. The Launch click itself is already relayed
server-side as `onboarding_step_completed` (`step=launch`).
**Reason and benefit**
Funnel steps should come from real product actions that the server
records. With `chat.started`, a funnel can count "chat launched" from
the server and see people who chat but never create a task.
**Breaking changes**
None. The event is new and is Cloud-only.
## What Changed
- `server/src/services/product-telemetry.ts`: add `CHAT_STARTED_EVENT`
and `emitChatStartedIfFirstOwnerTurn`. It runs inside the transaction
that wrote the person's turn and locks the chat's issue row. It checks
that the actor is the chat owner (not an agent, run, or system actor),
and that the owner has no other message and no other answered question
card in the chat. Cancelled or skipped cards do not count. Then it emits
`chat.started` through `trackCloudPostHogEvent` after commit.
- Callers: `addComment` (`issues.ts`), `answerQuestions`
(`issue-thread-interactions.ts`), both headless comment paths
(`headless-comment-group-commit.ts` and the serialized insert in
`routes/headless-channels-schemas.ts`), and the headless one-line
resolve (`headless-events-schedules-interactions.ts`).
- `server/src/__tests__/chat-started-telemetry.test.ts`: unit tests for
the first turn, both surfaces, and every case that must not emit.
- `server/src/__tests__/product-telemetry.test.ts`: embedded-Postgres
tests. Chat creation and system or other-person messages emit nothing.
The owner's first message emits once. A first message after a cancelled
card still emits. A rolled-back first message and a task comment emit
nothing.
- `docs/api/onboarding-telemetry.md`: document the server-emitted
`chat.started` event.
## Verification
- `npx vitest run server/src/__tests__/chat-started-telemetry.test.ts`:
10 passed (helper unit tests).
- `pnpm --filter @paperclipai/server typecheck`: passed.
- The cases in `product-telemetry.test.ts` need embedded Postgres, which
could not start in my environment (it runs as root). CI runs them.
## Risks
- Low. The event is fire-and-forget after commit, inside a `try`. It
does nothing on self-hosted installs, when `PAPERCLIP_POSTHOG_DIRECT=0`,
or when the PostHog environment is not set.
- One event per chat: the check runs under the chat's issue row lock,
and a rolled-back write emits nothing. `/new` in an existing chat does
not emit again.
- Two extra indexed reads run on a person's chat message or answer, and
only on chats.
### Open questions for reviewers
1. **Consent.** Like `model.connected` and the #23403 relay, this server
event is not gated by the browser cookie banner. This PR does not add a
consent check. The only switch is the existing
`PAPERCLIP_POSTHOG_DIRECT=0`. If people who decline cookies must be
excluded from server-side product events, that should be one rule for
all Cloud direct events, not a rule in this PR.
2. **New name or existing name.** This PR adds a new name,
`chat.started`, instead of emitting `onboarding_step_completed`
(`step=launch`) from the server. A new name keeps the browser and server
signals separate, so the two funnels can be compared and the existing
insights do not double count. The other choice is to emit the existing
name with a `source: server` property. Please say which one you prefer.
Not in this PR: "Agent named" has no clean server moment (the setup
agent is saved with a default name before the person names it, and
`agent.created` fires at launch), and `agent.task_run` still does not
say whether a run was a chat reply or a task. That needs a new
registered dimension in the telemetry contract, so it is a follow-up.
## Model Used
Claude Code (Anthropic Claude, agentic coding with tool use and code
execution).
## Checklist
- [x] I have included a thinking path that traces from project context
to this change
- [x] I have specified the model used (with version and capability
details)
- [x] I have checked ROADMAP.md and confirmed this PR does not duplicate
planned core work
- [x] I have searched GitHub for duplicate or related PRs and linked
them above
- [x] I have either (a) linked existing issues with `Fixes: #` / `Closes
#` / `Refs #` OR (b) described the issue in-PR following the relevant
issue template
- [x] I have not referenced internal/instance-local Paperclip issues or
links (only public GitHub `#NNN` / `github.com/paperclipai/paperclip`
URLs)
- [ ] My branch name describes the change (e.g. `docs/...`, `fix/...`)
and contains no internal Paperclip ticket id or instance-derived details
- [x] I have run tests locally and they pass
- [x] I have added or updated tests where applicable
- [x] I have updated relevant documentation to reflect my changes
- [x] I have considered and documented any risks above
- [ ] All Paperclip CI gates are green
- [ ] Greptile is 5/5 with no open P2s, recommendations, or follow-ups
- [x] I will address all Greptile and reviewer comments before
requesting merge
https://claude.ai/code/session_01KmUZthscGtSrd1KaWnwzWm
---
_Generated by [Claude Code](https://claude.ai/code)_
---------
Co-authored-by: Claude <noreply@anthropic.com>
88 lines
5.6 KiB
Markdown
88 lines
5.6 KiB
Markdown
---
|
|
title: Onboarding Telemetry
|
|
summary: First-party server-side onboarding funnel telemetry endpoint
|
|
---
|
|
|
|
# Onboarding Telemetry API
|
|
|
|
First-party board endpoint for recording structured onboarding funnel and interaction events server-side on Paperclip Cloud.
|
|
|
|
## Record Onboarding Telemetry Event
|
|
|
|
```
|
|
POST /api/onboarding/telemetry
|
|
```
|
|
|
|
Requires an authenticated board session (`assertBoard`). When `companyId` is included in the request body, the caller must also have access to that company (`assertCompanyAccess`). Pre-company Step 1 (`name_org`) events omit `companyId`.
|
|
|
|
### Request Body
|
|
|
|
A strict discriminated union keyed by `event` (`onboardingTelemetryEventSchema` in `@paperclipai/shared`). All fields are constrained to enums, booleans, bounded integers, UUIDs (`companyId`), or lowercase safe slugs (`^[a-z0-9_-]{1,48}$`) — free-form text and secrets are rejected.
|
|
|
|
Supported `event` values:
|
|
|
|
| Event | Purpose |
|
|
| --- | --- |
|
|
| `onboarding_workspace_loaded` | Workspace arrival and auto-open eligibility (`hasCompany`, `hasAgent`, `willAutoOpen`, optional `skipReason`, `isMobile`) |
|
|
| `onboarding_opened` | Wizard opened (`step`, `source`, `isMobile`, `resumedDraft`, `hasExistingCompany`) |
|
|
| `onboarding_step_viewed` | Wizard step rendered (`step`, optional `cloudTrialAvailable`, `mobileHandoffShown`, `savedKeysCount`, `hasExistingSubscription`) |
|
|
| `onboarding_step_submitted` | Primary action submitted (`step`, optional `via`: `button` \| `enter`) |
|
|
| `onboarding_step_completed` | Step completed (`step`, optional `outcome`, `provider`, `adapterType`, `role`) |
|
|
| `onboarding_step_failed` | Structured step failure (`step`, `reason`, optional `provider`, `adapterType`, `method`) |
|
|
| `onboarding_step_navigated` | Back button or step indicator jump (`fromStep`, `toStep`, `direction`, `via`) |
|
|
| `onboarding_dismissed` | Wizard closed before launch (`step`, optional `connectPhase`, `hadError`, `method`) |
|
|
| `onboarding_mobile_handoff_action` | Mobile desktop-handoff card interaction (`action`) |
|
|
| `onboarding_trial_cta_clicked` | Cloud starter trial CTA clicked (`surface`) |
|
|
| `onboarding_model_source_selected` | Provider tile clicked on `connect_model` (`adapterType`, optional `provider`, `credentialMode`) |
|
|
| `onboarding_credential_mode_switched` | Switched between `subscription` and `api_key` (`fromMode`, `toMode`, optional `adapterType`) |
|
|
| `onboarding_oauth_progress` | Granular OAuth sub-step (`stage`, optional `provider`, `adapterType`, `phase`) |
|
|
| `onboarding_api_key_submitted` | API key submitted (`usedSavedKey`, optional `provider`, `adapterType`) |
|
|
| `onboarding_advanced_toggled` | Advanced gateway setup toggled (`open`, optional `adapterType`) |
|
|
| `onboarding_launch_clicked` | Launch CTA clicked on `review` (optional `adapterType`, `provider`, `outcome`) |
|
|
| `agent_chat_message_sent` | Chat message sent (`surface`: `onboarding_chat` \| `agent_chat`) |
|
|
|
|
Example:
|
|
|
|
```json
|
|
{
|
|
"event": "onboarding_step_completed",
|
|
"companyId": "11111111-1111-4111-8111-111111111111",
|
|
"step": "connect_model",
|
|
"outcome": "subscription",
|
|
"provider": "anthropic",
|
|
"adapterType": "claude_local"
|
|
}
|
|
```
|
|
|
|
### Responses
|
|
|
|
| Status | Meaning |
|
|
| --- | --- |
|
|
| `204` | Event accepted. Sets header `X-Paperclip-Telemetry-Delivered: 1` when direct server-side PostHog delivery is active, or `0` when direct delivery is inactive (self-hosted installs, `PAPERCLIP_POSTHOG_DIRECT=0`, or missing `PAPERCLIP_POSTHOG_ENVIRONMENT`). |
|
|
| `400` | Validation error (unknown event name, disallowed extra property, or invalid slug/enum). |
|
|
| `403` | Unauthenticated, non-board actor, or caller lacks access to `companyId`. |
|
|
| `429` | Per-actor rate limit exceeded (default 120 events per 60 seconds). |
|
|
|
|
The chat send event includes the submitted input UUID (`message_id`) and its
|
|
conversation UUID (`chat_id`). A composer send uses its saved comment ID. Explicitly sending an onboarding
|
|
welcome answer uses the answered interaction ID. Selecting an option alone
|
|
produces no event.
|
|
|
|
The Cloud browser emits `agent_chat_reply_displayed` when the signed-in chat
|
|
owner can see reply text in the focused onboarding chat. The event carries
|
|
`surface=onboarding_chat`, `reply_kind=response_to_user`, and the same chat
|
|
and triggering message IDs as the send. It uses the run's triggering input to
|
|
attribute the reply. A stored browser receipt suppresses repeat displays in
|
|
the same chat, including after reload. Count unique people in the funnel.
|
|
Historical run success cannot establish that a person saw a reply.
|
|
|
|
## Server-Emitted Chat Event
|
|
|
|
The browser events above depend on the wizard and on the browser. So Paperclip Cloud also emits `chat.started` from the server when a person actually starts a chat.
|
|
|
|
| Event | When | Properties |
|
|
| --- | --- | --- |
|
|
| `chat.started` | The chat owner's first turn in a chat with an agent commits: their first message (from the app or the headless API), or their first answer to a question card such as the onboarding welcome card. It fires once per chat. Opening or preparing a chat, cancelled or skipped question cards (including a headless one-line skip), system or agent messages, other people's messages, and rolled-back writes emit nothing. | `surface`: `onboarding_chat` \| `agent_chat`; `agent_id` |
|
|
|
|
Onboarding prepares the chat before the person clicks Launch, so chat creation is not used as the signal. `task.created` does not fire for chats, so `chat.started` is the server-side "chat started" step for activation funnels. The event goes only through direct PostHog delivery. Self-hosted installs and `PAPERCLIP_POSTHOG_DIRECT=0` emit nothing. The event uses the chat owner's user id as its distinct id, the same as the other server-side Cloud events.
|