Commit Graph
4 Commits
Author SHA1 Message Date
Pamela Chia 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
2026-11-20 16:22:11 +09:00
Sean Oliver f0f2b57939 test(telemetry): cover every ad click id in first-touch attribution (#51774)
## Problem

The ad click ids we send to PostHog for first-touch attribution have no
direct tests. A typo in a param name (say `getParam('opref')`) would
silently drop that click id and CI would stay green. Also, nothing in CI
ran `packages/common` tests at all, apart from `sentry.test.ts`.
GROWTH-1326.

## Solution

- First-touch click ids are now one exported list,
`FIRST_TOUCH_CLICK_ID_KEYS`, instead of 11 hand-written `getParam`
lines. Same ids, same output.
- New `telemetry.test.ts` checks that list equals `CLICK_ID_KEYS` plus
`bfcid`, and that every id in it comes back from
`getFirstTouchAttributionProps`.
- `first-referrer-cookie.test.ts` checks `buildFirstReferrerData`
extracts every `CLICK_ID_KEYS` entry, and that `bfcid` stays out of that
pre-consent list.
- Added a `test` script to `packages/common` and a `pnpm --filter common
test` step to `library-tests.yml`, which already triggers on
`packages/common/**`.

## Review instructions

1. Check the Library checks job runs the common tests (13 files).
2. Add a made-up id to `FIRST_TOUCH_CLICK_ID_KEYS` only, then run `pnpm
--filter common test`. It fails.

## Checklist

Check all before review:

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] 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)
2026-11-09 09:37:28 -08:00
Pamela Chia 7b37c789ed fix(telemetry): skip sign-in pages and send cookie write time (#52697)
This fixes two gaps in the first-referrer cookie. It skipped only
`accounts.google.com` and GitHub OAuth returns, so coming back from a
regional Google account page, YouTube's account page, Sign in with
ChatGPT (`auth.openai.com`) or Sign in with Apple stamped the write-once
cookie with the login page. It also stored its write time without ever
sending it, so there was no way to tell an old first-touch source from a
fresh one in the data.

**Changed:**
- **Sign-in pages are never a source**: I extended
`isOAuthRedirectReferrer` to also match Google's regional account hosts
(`accounts.google.com.br`, `accounts.google.co.uk` and so on),
`accounts.youtube.com`, `auth.openai.com` and `appleid.apple.com`. That
one function decides cookie stamping, skips a cookie that already holds
one of these hosts, and swaps such a live referrer for the cookie's
referrer. Unit tests cover the host list, including a lookalike domain;
`packages/common` tests run locally only until #51774 adds them to CI.
- **Cookie write time on the pageview**: I added `first_touch_timestamp`
(ISO 8601) to the pageview that consumes the cookie. A cookie without a
valid `ts` sends no timestamp. Parsing previously defaulted it to the
current time. The kb telemetry test now asserts the property, so CI
covers it.

**Note:** a pageview returning from these sign-in pages with a cookie
now reports the cookie's referrer, as `accounts.google.com` returns
already do, so `auth.openai.com` no longer shows up as `$referrer` on
those pageviews.

## To test
The preview never sets the cookie (not even for a
`https://www.google.com/` referrer), so the cookie checks run against
supabase.com.
- [x] Before, on supabase.com today: `curl -sI https://supabase.com/ -H
'Referer: https://auth.openai.com/'` returns `Set-Cookie:
_sb_first_referrer=...` holding `auth.openai.com`. Same for
`https://accounts.google.com.br/`.
- [ ] After deploy: the same two requests return no `_sb_first_referrer`
cookie, and the control `-H 'Referer: https://www.google.com/'` still
sets one.
- [ ] After deploy, in PostHog: a dashboard `$pageview` with
`first_referrer_cookie_consumed = true` carries `first_touch_timestamp`.

## Linear
- part of GROWTH-1433
2026-11-04 23:38:15 +08:00
Sean Oliver ab448c11af feat(kb): send PostHog pageview and pageleave after consent (#52533)
## Problem

The KB has a consent banner but sends no pageviews. `PageTelemetry`
imports Next modules, so it can't run in Astro.

Part of
[GROWTH-1352](https://linear.app/supabase/issue/GROWTH-1352/add-consent-posthog-tracking-to-new-kb-app).

## Solution

- Moved `handlePageTelemetry`, `handlePageLeaveTelemetry` and their
first-touch helper into `common/page-telemetry.ts`, with no Next
imports. Other apps are unchanged.
- Added `PageTelemetryIsland` to the KB layout. After consent it sends
one `$pageview` per page load and `$pageleave` on `beforeunload`.
First-touch UTMs and the `_sb_first_referrer` and mw-diag cookies go
only on a tab's first pageview after consent (a `sessionStorage` flag)
or on arrival from an external site.
- Needs `NEXT_PUBLIC_POSTHOG_KEY` and `NEXT_PUBLIC_POSTHOG_HOST`
(`https://ph.supabase.com`) on the KB Vercel project. Both are set on
Preview and Production.

## Review instructions

1. Check the `telemetry.tsx` removals match `page-telemetry.ts`.
2. On a KB preview page, accept cookies and check the network tab for
`$pageview`, then `$pageleave` after navigating.

KB and studio telemetry tests pass. From the kb preview, PostHog prod
received `$pageview` on `/kb`, `$pageleave` on `/kb`, then `$pageview`
on `/kb/guides/migrate-from-neon`.

## Checklist

- [x] I have read
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] 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)
2026-10-30 13:47:44 -07:00