mirror of
https://github.com/supabase/supabase.git
synced 2026-12-04 23:49:03 +00:00
create-pull-request/patch
8
Commits
| Author | SHA1 | Message | Date | |
|---|---|---|---|---|
|
|
62d6bf56f8 |
fix(telemetry): send the live posthog session id on custom events (#53504)
Custom events sent through the telemetry endpoint often carry a session id the browser's PostHog session has already left: 8.9% of server-captured events in a recent week had an id more than 24 hours old, against 0.046% of browser-captured ones. Two things on the client cause it. The `session_id` in the request body is parsed out of the PostHog cookie without going through posthog-js, so a custom event never rotates an expired session. And the `session_id` cookie, which the API reads for the events it emits itself, was written on each pageview before posthog-js rotated the session, so the first pageview after an idle period stored the previous session's id. **Changed:** - **Custom events carry the live session id**: I changed `buildTelemetryEventRequest` to ask posthog-js for the session id the way a captured event does, so an expired session rotates first. Until posthog-js has loaded it sends the cookie-parsed id, as before. - **The `session_id` cookie follows the session**: I moved the write from the pageview handler to posthog-js's session-change callback, plus one write when posthog-js loads, so the cookie changes when the session does. - **The cookie stays cleared after an opt-out**: when a visitor withdraws consent, posthog-js resets the session, and the new id would have been written back over the cleared cookie. I skip the write once the visitor has opted out, in this tab or another. Unit tests cover this one: preview builds accept consent by default, so withdrawal can't be exercised there. - **Session metrics will step**: custom events now count as session activity. A long-lived tab that sends events without navigating stays in one session, so expect fewer, longer sessions from the day this reaches each app. **Note:** The API starts reading the body's `session_id` in a companion change on its side. Either can deploy first: the field is already sent today and ignored. ## To test Tested on the PR preview: - [x] In Studio with the Network tab open, open the user dropdown in the header. Expect the `telemetry/event` request body's `session_id` to equal the `session_id` cookie. - [x] Open the dropdown again right away. Expect the same `session_id` as in the first request. - [x] Leave that tab untouched for 35 minutes, then open the user dropdown again without navigating. Expect a different `session_id` in the request body than in the first step, and the `session_id` cookie to equal the new one. Observed after 38 idle minutes. - [x] Navigate to another Studio page. Expect the `session_id` cookie to keep the value from the previous step. Observed on an in-app navigation to the account page. ## Linear - fixes GROWTH-1509 |
||
|
|
7b1def73c8 |
fix(common): add queryWordCount to the telemetry flag-stamping test fixture (#53619)
## Problem `SearchQueryProperties` now requires `queryWordCount`, but the fixture in the `stamps each PostHog flag as $feature/<key>` test in `packages/common/telemetry-event.test.ts` still used the old shape. `tsc --noEmit` in `packages/common` fails with TS2322/TS2352 on that test, which breaks the `common#typecheck` task. ## Solution Add `queryWordCount: 1` to the fixture so it matches `SearchQueryProperties` and the other `kb_search_submitted` fixtures in the same file. This changes only the test, not runtime code. ## How to verify - `pnpm --filter common run typecheck` passes - `pnpm --filter common exec vitest run telemetry-event.test.ts` passes (9 tests) Also ran `pnpm turbo run typecheck lint --filter='[origin/master]'` and a Prettier check on the changed file. Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com> |
||
|
|
27578cdfb5 |
fix(telemetry): send search query shape instead of raw text (#53500)
Search telemetry sent whatever people typed, and search boxes are a place where people paste credentials and personal data. I replace the raw text with a description of it. #53336 added the raw `query` to `kb_search_submitted`; docs search and the command menu already sent it. **Changed:** - **I send a description of the search term**: `kb_search_submitted`, `docs_search_v2_search_submitted`, `docs_search_v2_result_clicked` and `command_menu_search_submitted` now send `queryLength` and `queryWordCount`, measured after trimming and collapsing repeated whitespace. - **Text only for keywords**: the term itself (`query`, `value`, and `search_query` on `command_menu_command_clicked`) is sent only when it is up to 4 letters-only parts of at most 16 letters each, 40 characters in total. Keys, tokens, emails, IDs, project refs and connection strings never match. Over the last 30 days that shape covers about 96% of command menu searches, 88% of docs searches and 73% of KB searches, so term-level analysis such as no-result queries keeps most of its data. - **I removed `keywords` from the auth users search event**: `auth_users_search_submitted` no longer carries it. Those terms identify customers' end users, and I confirmed nothing downstream reads them. - **The `keywords` URL parameter is redacted wherever telemetry copies a URL**: the Auth users page keeps the search term in its URL, so I replace the value with `keywords=redacted` in the page URL, query string and referrer we send, and in the posthog-js `before_send` hook for the URL properties the SDK fills in on its own (`$session_entry_url`, `$initial_current_url`, `$referrer`, `$initial_referrer`). The URL in the browser is unchanged. **Note:** a letters-only name or passphrase still matches the keyword shape, and longer questions lose their text. I opened this in place of #53435, which removed `query` from the KB event only. ## To test Tested on the PR previews (docs, kb, studio-staging), reading the request bodies sent to `/platform/telemetry/event`: - [x] Docs: open search and type `auth`, expect `docs_search_v2_search_submitted` with `query: "auth"`, `queryLength: 4` and `queryWordCount: 1`. Observed exactly that, with `hasResults: true`. - [x] Docs: type `person@example.com`, expect the same event with `queryLength: 18` and no `query`. Observed, and no telemetry body in that step contained the address. - [x] KB: search `how do I reset my database password`, expect `kb_search_submitted` with `queryLength: 35`, `queryWordCount: 7` and no `query`. Observed. - [x] KB: search `storage policies` with two spaces, expect `kb_search_submitted` with `query: "storage policies"` and `queryLength: 16`. Observed, with `queryWordCount: 2`. - [x] Studio: open the command menu and type `abcdefghijklmnopqrst`, expect `command_menu_search_submitted` with no `value`, then type `auth` and expect `value: "auth"`. Observed both, with `queryLength` 20 and 4. - [x] Studio: on Auth > Users, search `person@example.com`, expect `auth_users_search_submitted` with no `keywords`. Observed: `trigger`, `sort_column`, `sort_order`, `providers` and `filter_column` only. - [x] Studio: with `?keywords=person%40example.com` in the Auth > Users URL, reload, then trigger an event on the page, expect `page_url` and `ph.search` in the request body to carry `keywords=redacted` and the address bar to keep the real term. Observed on all three events from that page, and the address bar kept the term. - [x] Studio: from that URL, do a full page load to another project page and trigger an event, expect `ph.referrer` to carry `keywords=redacted` while `document.referrer` keeps the real term. Observed on all three events from the new page. - [x] Studio: after that session, query the staging analytics project for events from the preview host, expect no property value to carry the address or anything but `keywords=redacted`. Observed: none of the events in the test window carried the address, and the SDK's own `$set`, `$pageview` and `$pageleave` events arrived with `keywords=redacted` in `$current_url`, `$referrer` and `$initial_current_url`. The session started on a page without the parameter, so `$session_entry_url` was not exercised here; the unit test on the `before_send` hook covers it. ## Linear - fixes GROWTH-1501 |
||
|
|
8c2d8d51ac |
feat(telemetry): stamp PostHog flags on tracked events (#53588)
Events sent through `sendTelemetryEvent` now include the app's loaded
PostHog flags as `$feature/<key>` properties, as `$pageview` already
does. The backend already adds these keys by evaluating flags remotely
for each event. The browser's values now take precedence over that
evaluation, so a follow-up can turn the per-event lookup off without
losing flag properties on events.
**Changed:**
- **Browser flag values on every tracked event**: `FeatureFlagProvider`
passes its PostHog flag map to the event builder each time flags load,
and the builder adds one `$feature/<key>` property per flag. A property
the caller sends under the same key wins.
- **Value source**: `$feature/*` on an event is now the value the app
rendered with. It follows the flag store, so for a moment after sign-in,
sign-out or an org or project switch it holds the previous values until
the refetch lands. The backend-computed `$active_feature_flags` is
unchanged and can differ from it in that window.
- **Logged-out docs, learn and ui-library events**: `$feature/*` now
holds the flag value the page was served. Until now the backend
evaluated it per visitor, so trends on those properties shift on each
app's deploy date.
- **Apps without PostHog flags**: www (`ph: false`) and kb (no provider)
keep an empty map, so their events get no new properties.
**Note:** I set the flags in the provider right before its state update
because an exposure event fired from a child effect, on the render where
its flag first resolves, must already see them. An effect in the
provider would run after that child effect. Events sent before the first
flag load still go out without browser flag values.
## To test
Tested on the PR preview:
- [x] Sign in to Studio and open the new project form. In the network
panel, find the `POST /platform/telemetry/event` request with action
`project_creation_default_privileges_exposed`: expect
`custom_properties` to include `$feature/dataApiRevokeOnCreateDefault`,
equal to `dataApiRevokeOnCreateDefaultEnabled`. Observed: both `false`,
response 201.
- [x] In Studio, after `GET /platform/telemetry/feature-flags` returns,
trigger any tracked event: expect its `custom_properties` to hold one
`$feature/<key>` entry per key in that response, with the same values.
Observed on `command_menu_opened` and the exposure event: every key in
the flags response present with the same value, no extra keys.
- [x] On the docs preview, trigger a tracked event after the flags
request returns: expect `$feature/<key>` entries matching the docs flags
response. Observed on `docs_search_v2_opened`: all keys match the
logged-out flags response.
- [x] On the www preview, trigger a tracked event: expect no
`$feature/*` keys in `custom_properties`. Observed on
`start_project_button_clicked`: `custom_properties` is
`{"buttonLocation":"Header Nav","platform":"www"}`, and www made no
feature-flags request.
## Linear
- fixes GROWTH-1395
|
||
|
|
15bee88898 |
feat(kb): send search query text on kb_search_submitted (#53336)
Adds the trimmed search term as a query property on kb_search_submitted, matching docs search. Goes through the existing consent-gated kb telemetry path. Refs DOCS-1588. |
||
|
|
04a58c8d8f |
fix(common): ignore trailing slashes in ensurePlatformSuffix (#53065)
## Problem `ensurePlatformSuffix` appends `/platform` to the API URL as-is. With `NEXT_PUBLIC_API_URL` set to `https://api.supabase.com/` (trailing slash), the kb search events from #52981 post to `https://api.supabase.com//platform/telemetry/event`, which returns 404. Nothing logs it, because the kb send path swallows errors. Related: DOCS-1580 ## Solution Strip all trailing slashes before checking for or adding `/platform`, using a loop instead of a regex (CodeQL flagged `/\/+$/` as polynomial). `https://api.supabase.com/`, `//`, `.../platform`, `.../platform/` and `.../platform//` now all resolve to `https://api.supabase.com/platform`. ## Review instructions 1. In `packages/common`, run `pnpm vitest run telemetry-event.test.ts`. The new trailing-slash case fails on master and passes here. |
||
|
|
d862ac18f2 |
feat(telemetry): stamp the app's platform on every event (#53078)
Every first-party PostHog event now carries a `platform` property naming
the supabase.com app that sent it. Each app passes its directory name
(`studio`, `www`, `docs`, `kb`, `learn`, `ui-library`) once at telemetry
setup, and I stamp it on both send paths.
**Changed:**
- **Events sent through the Management API**
(`/platform/telemetry/event`): `buildTelemetryEventRequest` adds
`platform` to `custom_properties`. This also covers kb's search events,
which build the request without `sendTelemetryEvent`.
- **Events sent by posthog-js** (pageviews, page leaves, identify,
experiment exposures): a `before_send` hook stamps `platform` at send
time.
- **App setup**: `PageTelemetry` takes a required `platform` prop, and
kb's island sets the value directly. While unset, the property is left
off instead of defaulting to an app.
**Note:** I used `before_send` instead of `posthog.register()`. Super
properties persist in localStorage, which every supabase.com app shares,
so events sent before `register()` runs would carry the previous app's
value.
**Do not merge yet.** Studio's `trackFeatureFlag` body still needs
`platform`. That commit waits for the Management API change that accepts
the field to reach production, plus an `api-types` regen. The flag-track
body is strict, so sending `platform` early returns 400 on every flag
call.
## To test
On each app's preview, accept cookies and add `?__posthog_debug=true` to
the URL.
- [x] Studio: load a page, expect `$pageview` with `properties.platform:
"studio"` (seen at ingestion: the staging PostHog project has
`$pageview` and `$pageleave` events from the studio-staging preview
host, all with `platform: "studio"`)
- [x] www: load the homepage, expect `send "$pageview"` with `platform:
"www"`; click "Start your project", expect the
`/platform/telemetry/event` request body to have
`custom_properties.platform: "www"` (both seen)
- [x] docs: load a guide, expect `send "$pageview"` with `platform:
"docs"` (seen); added: "Copy as Markdown" sends `custom_properties:
{"pageType":"guide","platform":"docs"}`
- [x] kb: load a page, expect `send "$pageview"` with `platform: "kb"`;
run a search, expect the `/platform/telemetry/event` body to have
`custom_properties.platform: "kb"` (seen on both `kb_search_submitted`
and `kb_search_result_clicked`)
- [ ] learn and ui-library: load a page, expect `send "$pageview"` with
`platform: "learn"` and `"ui-library"` respectively. Not checkable on
previews: learn has no preview deployment, and the ui-library preview
host is not on the consent provider's allowlist, so telemetry never
starts there.
- [x] Added, negative check: on docs, kb and www, no captured posthog-js
event had `platform` unset or set to another app
- [x] Added, ingestion: in the staging PostHog project,
`copy_as_markdown_clicked` (docs) and `start_project_button_clicked`
(www) carry `platform` as a top-level event property
## Linear
- fixes GROWTH-1450
|
||
|
|
5ac0a4843b |
feat(kb): send search and result click telemetry events (#52981)
## Problem KB search sends no events. Part of [DOCS-1514](https://linear.app/supabase/issue/DOCS-1514) (Add telemetry to search). ## Solution Two events through `/telemetry/event`, sent only on platform and after consent: - `kb_search_submitted`: `queryLength`, `resultCount`, `hasResults` - `kb_search_result_clicked`: `resultPath`, `resultPosition` (1-based), `resultCount`, sent with keepalive Query text isn't sent: it can contain personal data. New `buildTelemetryEventRequest` builds the body for `sendTelemetryEvent`. KB posts it with plain `fetch` and no auth header, so no GoTrue client starts on the shared dashboard session. ## Additional context Tested on `astro dev` with Playwright: - `rls`: `{queryLength: 3, resultCount: 10, hasResults: true}` - `zzqxjv`: `{queryLength: 6, resultCount: 0, hasResults: false}` - 2nd result clicked: `{resultPosition: 2, resultCount: 10}`, no `Authorization` header - No consent: nothing sent Also ran `common` and Studio typecheck, KB tests, and `astro build` (no GoTrue in the bundle). ## Review instructions 1. `pnpm --filter kb dev` on platform, accept cookies. 2. Search `rls`, click a result, and check both `/telemetry/event` requests. 3. Decline cookies and repeat: nothing sends. ## Checklist - [x] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [x] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide) --------- Co-authored-by: Kody Jackson <kody.jackson@supabase.io> |