Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

Chore/www session consent (#31082)

Terry Suttonde28174c1dc8
diff --git a/apps/docs/features/telemetry/telemetry.client.tsx b/apps/docs/features/telemetry/telemetry.client.tsx
index 0eb59b33ca7..5264e668827 100644
--- a/apps/docs/features/telemetry/telemetry.client.tsx
+++ b/apps/docs/features/telemetry/telemetry.client.tsx
@@ -1,6 +1,6 @@
'use client'
-import { isBrowser } from 'common'
+import { isBrowser, useTelemetryCookie } from 'common'
import { usePathname } from 'next/navigation'
import { useCallback, useEffect } from 'react'
import { useConsent } from 'ui-patterns/ConsentToast'
@@ -60,6 +60,12 @@ const useTelemetryWithConsent = () => {
const PageTelemetry = () => {
const pathname = usePathname()
+
+ const title = typeof document !== 'undefined' ? document?.title : ''
+ const referrer = typeof document !== 'undefined' ? document?.referrer : ''
+ const { hasAcceptedConsent } = useConsent()
+ useTelemetryCookie({ hasAcceptedConsent, title, referrer })
+
const { sendPageTelemetry, sendPageLeaveTelemetry } = useTelemetryWithConsent()
useEffect(() => {
diff --git a/apps/studio/components/ui/PageTelemetry.tsx b/apps/studio/components/ui/PageTelemetry.tsx
index 7bf41bf7844..0dc86e373e6 100644
--- a/apps/studio/components/ui/PageTelemetry.tsx
+++ b/apps/studio/components/ui/PageTelemetry.tsx
@@ -2,7 +2,7 @@ import * as Sentry from '@sentry/nextjs'
import { useRouter } from 'next/router'
import { PropsWithChildren, useEffect } from 'react'
-import { useParams, useUser } from 'common'
+import { useParams, useTelemetryCookie, useUser } from 'common'
import { useSendGroupsIdentifyMutation } from 'data/telemetry/send-groups-identify-mutation'
import { useSendGroupsResetMutation } from 'data/telemetry/send-groups-reset-mutation'
import { useSendPageLeaveMutation } from 'data/telemetry/send-page-leave-mutation'
@@ -39,6 +39,10 @@ const PageTelemetry = ({ children }: PropsWithChildren<{}>) => {
const { mutate: sendGroupsIdentify } = useSendGroupsIdentifyMutation()
const { mutate: sendGroupsReset } = useSendGroupsResetMutation()
+ const title = typeof document !== 'undefined' ? document?.title : ''
+ const referrer = typeof document !== 'undefined' ? document?.referrer : ''
+ useTelemetryCookie({ hasAcceptedConsent, title, referrer })
+
useEffect(() => {
if (consentValue !== null) {
snap.setIsOptedInTelemetry(hasAcceptedConsent)
diff --git a/apps/www/pages/_app.tsx b/apps/www/pages/_app.tsx
index 7b7619f77e2..b6692905a2c 100644
--- a/apps/www/pages/_app.tsx
+++ b/apps/www/pages/_app.tsx
@@ -6,8 +6,8 @@ import { SessionContextProvider } from '@supabase/auth-helpers-react'
import {
AuthProvider,
IS_PROD,
- isBrowser,
ThemeProvider,
+ useTelemetryCookie,
useTelemetryProps,
useThemeSandbox,
} from 'common'
@@ -24,17 +24,17 @@ import MetaFaviconsPagesRouter, {
DEFAULT_FAVICON_ROUTE,
DEFAULT_FAVICON_THEME_COLOR,
} from 'common/MetaFavicons/pages-router'
+import LW13CountdownBanner from 'ui/src/layout/banners/LW13CountdownBanner/LW13CountdownBanner'
import { WwwCommandMenu } from '~/components/CommandMenu'
import { API_URL, APP_NAME, DEFAULT_META_DESCRIPTION, IS_PREVIEW } from '~/lib/constants'
import { post } from '~/lib/fetchWrapper'
import supabase from '~/lib/supabase'
import useDarkLaunchWeeks from '../hooks/useDarkLaunchWeeks'
-import LW13CountdownBanner from 'ui/src/layout/banners/LW13CountdownBanner/LW13CountdownBanner'
export default function App({ Component, pageProps }: AppProps) {
const router = useRouter()
const telemetryProps = useTelemetryProps()
- const { consentValue, hasAcceptedConsent } = useConsent()
+ const { hasAcceptedConsent } = useConsent()
const IS_DEV = !IS_PROD && !IS_PREVIEW
const blockEvents = IS_DEV || !hasAcceptedConsent
@@ -45,6 +45,8 @@ export default function App({ Component, pageProps }: AppProps) {
useThemeSandbox()
+ useTelemetryCookie({ hasAcceptedConsent, title, referrer })
+
function handlePageTelemetry(url: string) {
return post(
`${API_URL}/telemetry/page`,
@@ -77,17 +79,13 @@ export default function App({ Component, pageProps }: AppProps) {
return () => {
router.events.off('routeChangeComplete', handleRouteChange)
}
- }, [router.events, consentValue])
+ }, [router.events, blockEvents])
useEffect(() => {
+ if (!router.isReady) return
if (blockEvents) return
- /**
- * Send page telemetry on first page load
- */
- if (router.isReady) {
- handlePageTelemetry(window.location.href)
- }
- }, [router.isReady, consentValue])
+ handlePageTelemetry(window.location.href)
+ }, [router.isReady, blockEvents])
useEffect(() => {
const handleBeforeUnload = async () => {
diff --git a/packages/common/constants/local-storage.ts b/packages/common/constants/local-storage.ts
index 8b7330118eb..2aa48c9d1ad 100644
--- a/packages/common/constants/local-storage.ts
+++ b/packages/common/constants/local-storage.ts
@@ -1,5 +1,6 @@
export const LOCAL_STORAGE_KEYS = {
TELEMETRY_CONSENT: 'supabase-consent-ph',
+ TELEMETRY_DATA: 'supabase-telemetry-data',
HIDE_PROMO_TOAST: 'supabase-hide-promo-toast-lw13-d1',
BLOG_VIEW: 'supabase-blog-view',
}
diff --git a/packages/common/fetchWrappers.tsx b/packages/common/fetchWrappers.tsx
index 54b8c1879f4..4afdf9a9acc 100644
--- a/packages/common/fetchWrappers.tsx
+++ b/packages/common/fetchWrappers.tsx
@@ -3,14 +3,17 @@ interface DataProps {
}
export const post = (url: string, data: DataProps, options = {} as { [key: string]: any }) => {
- const { optionHeaders, ...otherOptions } = options
+ const { headers: optionHeaders, ...otherOptions } = options
+
+ let headers = new Headers({
+ 'Content-Type': 'application/json',
+ Accept: 'application/json',
+ ...optionHeaders,
+ })
+
return fetch(url, {
method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- Accept: 'application/json',
- ...optionHeaders,
- },
+ headers,
credentials: 'include',
referrerPolicy: 'no-referrer-when-downgrade',
body: JSON.stringify(data),
diff --git a/packages/common/hooks/index.ts b/packages/common/hooks/index.ts
index 7953fdbf4e3..0dde8496c95 100644
--- a/packages/common/hooks/index.ts
+++ b/packages/common/hooks/index.ts
@@ -8,4 +8,5 @@ export * from './useIsomorphicLayoutEffect'
export * from './useParams'
export * from './useSearchParamsShallow'
export * from './useTelemetryProps'
+export * from './useTelemetryCookie'
export * from './useThemeSandbox'
diff --git a/packages/common/hooks/useTelemetryCookie.tsx b/packages/common/hooks/useTelemetryCookie.tsx
new file mode 100644
index 00000000000..3b301e126ee
--- /dev/null
+++ b/packages/common/hooks/useTelemetryCookie.tsx
@@ -0,0 +1,66 @@
+// hooks/useTelemetryCookie.ts
+import { IS_PROD, LOCAL_STORAGE_KEYS, useTelemetryProps } from 'common'
+import { useRouter } from 'next/compat/router'
+import { useEffect } from 'react'
+
+interface UseTelemetryCookieProps {
+ hasAcceptedConsent: boolean
+ title: string
+ referrer: string
+}
+
+/**
+ * This hook saves the telemetry data to a cookie. The cookie will be sent when the user consents to telemetry. If they
+ * don't consent, the cookie will never be sent.
+ */
+export function useTelemetryCookie({
+ hasAcceptedConsent,
+ title,
+ referrer,
+}: UseTelemetryCookieProps) {
+ const router = useRouter()
+ const { language, search, viewport_height, viewport_width } = useTelemetryProps()
+ const telemetryStorageKey = LOCAL_STORAGE_KEYS.TELEMETRY_DATA
+
+ useEffect(() => {
+ if (!router?.isReady) return
+
+ const cookies = document.cookie.split(';')
+ const cookieOptions = IS_PROD ? 'path=/; domain=supabase.com' : 'path=/'
+
+ const telemetryCookie = cookies.find((cookie) => cookie.trim().startsWith(telemetryStorageKey))
+ if (telemetryCookie) return
+
+ const telemetryData = {
+ page_url: window.location.href,
+ page_title: title,
+ pathname: router.pathname,
+ ph: {
+ referrer,
+ language,
+ search,
+ viewport_height,
+ viewport_width,
+ user_agent: navigator.userAgent,
+ },
+ }
+
+ if (!hasAcceptedConsent) {
+ const encodedData = encodeURIComponent(JSON.stringify(telemetryData))
+ document.cookie = `${telemetryStorageKey}=${encodedData}; ${cookieOptions}`
+ }
+ }, [
+ hasAcceptedConsent,
+ router?.isReady,
+ telemetryStorageKey,
+ title,
+ router?.pathname,
+ referrer,
+ language,
+ search,
+ viewport_height,
+ viewport_width,
+ ])
+}
+
+export default useTelemetryCookie
diff --git a/packages/common/telemetry.ts b/packages/common/telemetry.ts
index a2a8857d875..fa1909bfd32 100644
--- a/packages/common/telemetry.ts
+++ b/packages/common/telemetry.ts
@@ -1,7 +1,24 @@
import { post } from './fetchWrappers'
-export function handlePageTelemetry(API_URL: string, route: string, telemetryProps: any) {
- const { page_url, search, language, viewport_height, viewport_width } = telemetryProps
+interface TelemetryData {
+ page_url: string
+ page_title: string
+ pathname: string
+ ph: {
+ referrer: string
+ language: string
+ search: string
+ viewport_height: number
+ viewport_width: number
+ user_agent: string
+ }
+}
+
+export function handlePageTelemetry(API_URL: string, route: string, telemetryData: TelemetryData) {
+ const { page_url } = telemetryData
+
+ const { search, language, viewport_height, viewport_width } = telemetryData.ph
+
return post(
`${API_URL}/telemetry/page`,
{
@@ -10,16 +27,14 @@ export function handlePageTelemetry(API_URL: string, route: string, telemetryPro
pathname: route,
ph: {
referrer: document.referrer,
- user_agent: navigator.userAgent,
- search,
language,
+ search,
viewport_height,
viewport_width,
+ user_agent: navigator.userAgent,
},
},
- {
- headers: { Version: '2' },
- }
+ { headers: { Version: '2' }, credentials: 'include' }
)
}
diff --git a/packages/ui-patterns/ConsentToast/index.tsx b/packages/ui-patterns/ConsentToast/index.tsx
index 502c9146758..8f3d0376274 100644
--- a/packages/ui-patterns/ConsentToast/index.tsx
+++ b/packages/ui-patterns/ConsentToast/index.tsx
@@ -7,7 +7,9 @@ import {
useBreakpoint,
useTelemetryProps,
} from 'common'
+
import { noop } from 'lodash'
+import router from 'next/router'
import { useCallback, useEffect, useRef, useState } from 'react'
import { toast } from 'sonner'
import { Button, cn } from 'ui'
@@ -77,7 +79,7 @@ export const ConsentToast = ({ onAccept = noop, onOptOut = noop }: ConsentToastP
}
export const useConsent = () => {
- const { TELEMETRY_CONSENT } = LOCAL_STORAGE_KEYS
+ const { TELEMETRY_CONSENT, TELEMETRY_DATA } = LOCAL_STORAGE_KEYS
const consentToastId = useRef<string | number>()
const telemetryProps = useTelemetryProps()
@@ -86,12 +88,56 @@ export const useConsent = () => {
const handleConsent = (value: 'true' | 'false') => {
if (!isBrowser) return
+
+ if (value === 'true') {
+ const cookies = document.cookie.split(';')
+ const telemetryCookie = cookies.find((cookie) => cookie.trim().startsWith(TELEMETRY_DATA))
+ if (telemetryCookie) {
+ try {
+ const encodedData = telemetryCookie.split('=')[1]
+ const telemetryData = JSON.parse(decodeURIComponent(encodedData))
+ handlePageTelemetry(
+ process.env.NEXT_PUBLIC_API_URL!,
+ window.location.pathname,
+ telemetryData
+ )
+ // remove the telemetry cookie
+ document.cookie = `${TELEMETRY_DATA}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
+ } catch (error) {
+ console.error('Invalid telemetry data:', error)
+ }
+ } else {
+ const telemetryData = {
+ page_url: telemetryProps.page_url,
+ page_title: typeof document !== 'undefined' ? document?.title : '',
+ pathname: router.pathname,
+ ph: {
+ referrer: typeof document !== 'undefined' ? document?.referrer : '',
+ language: telemetryProps.language,
+ search: telemetryProps.search,
+ viewport_height: telemetryProps.viewport_height,
+ viewport_width: telemetryProps.viewport_width,
+ user_agent: navigator.userAgent,
+ },
+ }
+
+ handlePageTelemetry(
+ process.env.NEXT_PUBLIC_API_URL!,
+ window.location.pathname,
+ telemetryData
+ )
+ }
+ } else {
+ // remove the telemetry cookie
+ document.cookie = `${TELEMETRY_DATA}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
+ }
+
setConsentValue(value)
localStorage.setItem(TELEMETRY_CONSENT, value)
- if (consentToastId.current) toast.dismiss(consentToastId.current)
- if (value === 'true')
- handlePageTelemetry(process.env.NEXT_PUBLIC_API_URL!, location.pathname, telemetryProps)
+ if (consentToastId.current) {
+ toast.dismiss(consentToastId.current)
+ }
}
const triggerConsentToast = useCallback(() => {
@@ -135,7 +181,6 @@ export const useConsent = () => {
return {
consentValue,
- setConsentValue,
hasAcceptedConsent: consentValue === 'true',
triggerConsentToast,
}
@@ -151,8 +196,23 @@ export const useConsentValue = (KEY_NAME: string) => {
setConsentValue(value)
localStorage.setItem(KEY_NAME, value)
window.dispatchEvent(new Event('storage'))
- if (value === 'true')
- handlePageTelemetry(process.env.NEXT_PUBLIC_API_URL!, location.pathname, telemetryProps)
+ if (value === 'true') {
+ const telemetryData = {
+ page_url: telemetryProps.page_url,
+ page_title: typeof document !== 'undefined' ? document?.title : '',
+ pathname: router.pathname,
+ ph: {
+ referrer: typeof document !== 'undefined' ? document?.referrer : '',
+ language: telemetryProps.language,
+ search: telemetryProps.search,
+ viewport_height: telemetryProps.viewport_height,
+ viewport_width: telemetryProps.viewport_width,
+ user_agent: navigator.userAgent,
+ },
+ }
+
+ handlePageTelemetry(process.env.NEXT_PUBLIC_API_URL!, location.pathname, telemetryData)
+ }
}
return {
diff --git a/packages/ui-patterns/PrivacySettings/index.tsx b/packages/ui-patterns/PrivacySettings/index.tsx
index 3419d3065e4..fdd2eb58607 100644
--- a/packages/ui-patterns/PrivacySettings/index.tsx
+++ b/packages/ui-patterns/PrivacySettings/index.tsx
@@ -31,6 +31,8 @@ export const PrivacySettings = ({
}
const handleOptOutTelemetry = async () => {
+ // remove telemetry data from cookies
+ document.cookie = `${LOCAL_STORAGE_KEYS.TELEMETRY_DATA}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/`
handleResetTelemetry(process.env.NEXT_PUBLIC_API_URL!)
}