Files
supabase/apps/studio/components/interfaces/Account/Preferences/AnalyticsSettings.tsx
Sean Oliver b23e9e8d94 fix(consent): clear tracking state and reload when consent is withdrawn (#53294)
## Problem

Opting out from the Privacy Settings modal (www, docs, kb) clears no
cookies, and GTM keeps running. The banner's Opt out misses `_ga_<id>`,
`_dcid`, and PostHog.

## Solution

- `revokeTrackingConsent()` runs from `denyAll`, and from
`updateServices` unless every service is accepted. It clears `_ga`,
`_ga_*`, `_gcl_*`, `FP*`, `_dcid`, `session_id`, `ph_*_posthog`,
PostHog's localStorage, stale consent copies.
- PostHog resets and opts out, with persistence off. The opt-out
survives sign-out. A saved accept opts back in.
- Every opt-out calls `POST /platform/telemetry/reset` (keepalive,
errors ignored) to clear the API's HttpOnly `session_id`,
`anonymous_id`, and `user_id`. Studio's own call is removed.
- To stop GTM, a saved opt-out reloads the page if tracking was on,
waiting at most 1s for the reset.

## Review instructions

1. `cd packages/common && npx vitest run consent-state.test.ts
posthog-client.test.ts`
2. On prod: accept, open Privacy Settings, turn Marketing off, Confirm.
The page reloads with cookies gone and no `collect` request.

Tested: `common` (237 pass), `AnalyticsSettings` tests, CI unit tests
and typecheck. No browser test (previews lack Usercentrics).

## 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
2026-11-17 16:38:23 -08:00

94 lines
2.9 KiB
TypeScript

import { zodResolver } from '@hookform/resolvers/zod'
import { useConsentState } from 'common'
import { useForm } from 'react-hook-form'
import { toast } from 'sonner'
import { Card, CardContent, Form, FormControl, FormField, Switch } from 'ui'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import {
PageSection,
PageSectionContent,
PageSectionDescription,
PageSectionMeta,
PageSectionSummary,
PageSectionTitle,
} from 'ui-patterns/PageSection'
import * as z from 'zod'
const AnalyticsSchema = z.object({
telemetryEnabled: z.boolean(),
})
export const AnalyticsSettings = () => {
const { hasAccepted, acceptAll, denyAll, categories } = useConsentState()
const hasLoaded = categories !== null
const form = useForm<z.infer<typeof AnalyticsSchema>>({
resolver: zodResolver(AnalyticsSchema),
values: { telemetryEnabled: hasAccepted },
})
const handleToggle = (value: boolean) => {
if (!hasLoaded) {
toast.error(
"We couldn't load the privacy settings due to an ad blocker or network error. Please disable any ad blockers and try again. If the problem persists, please contact support."
)
form.setValue('telemetryEnabled', !value)
return
}
if (value) {
acceptAll()
} else {
denyAll().then((isSaved) => {
if (isSaved) window.location.reload()
})
}
form.setValue('telemetryEnabled', value)
}
return (
<PageSection>
<PageSectionMeta>
<PageSectionSummary>
<PageSectionTitle>Analytics and Marketing</PageSectionTitle>
<PageSectionDescription>
Control whether telemetry and marketing data is sent from Supabase services.
</PageSectionDescription>
</PageSectionSummary>
</PageSectionMeta>
<PageSectionContent>
<Form {...form}>
<Card>
<CardContent>
<FormField
control={form.control}
name="telemetryEnabled"
render={({ field }) => (
<FormItemLayout
layout="flex-row-reverse"
label="Send telemetry data from Supabase services"
description="By opting in to sharing telemetry data, Supabase can analyze usage patterns to enhance user experience and use it for marketing and advertising purposes"
id="telemetry"
>
<FormControl>
<Switch
checked={field.value}
onCheckedChange={(value) => {
field.onChange(value)
handleToggle(value)
}}
id="telemetry"
/>
</FormControl>
</FormItemLayout>
)}
/>
</CardContent>
</Card>
</Form>
</PageSectionContent>
</PageSection>
)
}