The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Chore/add new telemetry triggers for ph (#29769)
Joshen Lim9b3d87207f00
diff --git a/apps/studio/components/layouts/AccountLayout/AccountLayout.tsx b/apps/studio/components/layouts/AccountLayout/AccountLayout.tsxindex 79062d6548b..3debb82a26a 100644--- a/apps/studio/components/layouts/AccountLayout/AccountLayout.tsx+++ b/apps/studio/components/layouts/AccountLayout/AccountLayout.tsx@@ -12,6 +12,7 @@ import { IS_PLATFORM } from 'lib/constants'import SettingsLayout from '../SettingsLayout/SettingsLayout'import type { SidebarSection } from './AccountLayout.types'import WithSidebar from './WithSidebar'+import { useSendResetMutation } from 'data/telemetry/send-reset-mutation'export interface AccountLayoutProps {title: string@@ -27,9 +28,12 @@ const AccountLayout = ({ children, title, breadcrumbs }: PropsWithChildren<Accouconst selectedOrganization = useSelectedOrganization()const navLayoutV2 = useFlag('navigationLayoutV2')+ const enablePostHogTelemetry = useFlag('enablePosthogChanges')+ const { mutateAsync: sendReset } = useSendResetMutation()const signOut = useSignOut()const onClickLogout = async () => {+ if (enablePostHogTelemetry) await sendReset()await signOut()await router.push('/sign-in')}diff --git a/apps/studio/components/ui/PageTelemetry.tsx b/apps/studio/components/ui/PageTelemetry.tsxindex a37da673715..88e10f0beab 100644--- a/apps/studio/components/ui/PageTelemetry.tsx+++ b/apps/studio/components/ui/PageTelemetry.tsx@@ -1,10 +1,16 @@import { Sha256 } from '@aws-crypto/sha256-browser'import * as Sentry from '@sentry/nextjs'import { useRouter } from 'next/router'-import { PropsWithChildren, useEffect } from 'react'+import { PropsWithChildren, useEffect, useState } from 'react'-import { useUser } from 'common'+import { useParams, 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'import { useSendPageMutation } from 'data/telemetry/send-page-mutation'+import { usePrevious } from 'hooks/deprecated'+import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization'+import { useFlag } from 'hooks/ui/useFlag'import { IS_PLATFORM, LOCAL_STORAGE_KEYS } from 'lib/constants'import { useAppStateSnapshot } from 'state/app-state'@@ -20,9 +26,22 @@ const getAnonId = async (id: string) => {const PageTelemetry = ({ children }: PropsWithChildren<{}>) => {const user = useUser()const router = useRouter()+ const { ref, slug } = useParams()const snap = useAppStateSnapshot()+ const organization = useSelectedOrganization()++ const previousPathname = usePrevious(router.pathname)+ const enablePostHogTelemetry = useFlag('enablePosthogChanges')+ const consent =+ typeof window !== 'undefined'+ ? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)+ : null+ const trackTelemetryPH = enablePostHogTelemetry && consent === 'true'const { mutate: sendPage } = useSendPageMutation()+ const { mutateAsync: sendPageLeave } = useSendPageLeaveMutation()+ const { mutate: sendGroupsIdentify } = useSendGroupsIdentifyMutation()+ const { mutate: sendGroupsReset } = useSendGroupsResetMutation()useEffect(() => {const consent =@@ -75,6 +94,48 @@ const PageTelemetry = ({ children }: PropsWithChildren<{}>) => {setSentryId().catch((e) => console.error(e))}, [user?.id])+ useEffect(() => {+ const isLandingOnProjectRoute =+ router.pathname.includes('[ref]') && previousPathname === router.pathname+ const isEnteringProjectRoute =+ !(previousPathname ?? '').includes('[ref]') && router.pathname.includes('[ref]')+ const isLeavingProjectRoute =+ (previousPathname ?? '').includes('[ref]') && !router.pathname.includes('[ref]')++ const isLandingOnOrgRoute =+ router.pathname.includes('[slug]') && previousPathname === router.pathname+ const isEnteringOrgRoute =+ !(previousPathname ?? '').includes('[slug]') && router.pathname.includes('[slug]')+ const isLeavingOrgRoute =+ (previousPathname ?? '').includes('[slug]') && !router.pathname.includes('[slug]')++ if (trackTelemetryPH) {+ if (ref && (isLandingOnProjectRoute || isEnteringProjectRoute)) {+ sendGroupsIdentify({ organization_slug: organization?.slug, project_ref: ref as string })+ } else if (slug && (isLandingOnOrgRoute || isEnteringOrgRoute)) {+ sendGroupsIdentify({ organization_slug: slug, project_ref: undefined })+ } else if (isLeavingProjectRoute || isLeavingOrgRoute) {+ sendGroupsReset({+ reset_organization: isLeavingOrgRoute || isLeavingProjectRoute,+ reset_project: isLeavingProjectRoute,+ })+ }+ }++ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [trackTelemetryPH, slug, ref, router.pathname])++ useEffect(() => {+ const handleBeforeUnload = async () => {+ if (enablePostHogTelemetry && snap.isOptedInTelemetry) await sendPageLeave()+ }+ window.addEventListener('beforeunload', handleBeforeUnload)++ return () => {+ window.removeEventListener('beforeunload', handleBeforeUnload)+ }+ }, [])+const handlePageTelemetry = async (route: string) => {if (IS_PLATFORM) sendPage({ url: route })}diff --git a/apps/studio/data/telemetry/send-groups-identify-mutation.ts b/apps/studio/data/telemetry/send-groups-identify-mutation.tsnew file mode 100644index 00000000000..3d9130f18e7--- /dev/null+++ b/apps/studio/data/telemetry/send-groups-identify-mutation.ts@@ -0,0 +1,44 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'++import { components } from 'api-types'+import { handleError, post } from 'data/fetchers'+import type { ResponseError } from 'types'++export type SendGroupsIdentifyVariables = components['schemas']['TelemetryGroupsIdentityBody']++export async function sendGroupsIdentify(body: SendGroupsIdentifyVariables) {+ const { data, error } = await post(`/platform/telemetry/groups/identify`, {+ body,+ credentials: 'include',+ })+ if (error) handleError(error)+ return data+}++type SendGroupsIdentifyData = Awaited<ReturnType<typeof sendGroupsIdentify>>++export const useSendGroupsIdentifyMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<SendGroupsIdentifyData, ResponseError, SendGroupsIdentifyVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<SendGroupsIdentifyData, ResponseError, SendGroupsIdentifyVariables>(+ (vars) => sendGroupsIdentify(vars),+ {+ async onSuccess(data, variables, context) {+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ console.error(`Failed to send Telemetry groups identify: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}diff --git a/apps/studio/data/telemetry/send-groups-reset-mutation.ts b/apps/studio/data/telemetry/send-groups-reset-mutation.tsnew file mode 100644index 00000000000..0c6a157ae31--- /dev/null+++ b/apps/studio/data/telemetry/send-groups-reset-mutation.ts@@ -0,0 +1,44 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'++import { components } from 'api-types'+import { handleError, post } from 'data/fetchers'+import type { ResponseError } from 'types'++export type SendGroupsResetVariables = components['schemas']['TelemetryGroupsResetBody']++export async function sendGroupsReset(body: SendGroupsResetVariables) {+ const { data, error } = await post(`/platform/telemetry/groups/reset`, {+ body,+ credentials: 'include',+ })+ if (error) handleError(error)+ return data+}++type SendGroupsResetData = Awaited<ReturnType<typeof sendGroupsReset>>++export const useSendGroupsResetMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<+ UseMutationOptions<SendGroupsResetData, ResponseError, SendGroupsResetVariables>,+ 'mutationFn'+> = {}) => {+ return useMutation<SendGroupsResetData, ResponseError, SendGroupsResetVariables>(+ (vars) => sendGroupsReset(vars),+ {+ async onSuccess(data, variables, context) {+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ console.error(`Failed to send Telemetry groups reset: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ }+ )+}diff --git a/apps/studio/data/telemetry/send-page-leave-mutation.ts b/apps/studio/data/telemetry/send-page-leave-mutation.tsnew file mode 100644index 00000000000..9eaa46fcdd4--- /dev/null+++ b/apps/studio/data/telemetry/send-page-leave-mutation.ts@@ -0,0 +1,49 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import { components } from 'api-types'++import { handleError, post } from 'data/fetchers'+import { useRouter } from 'next/router'+import type { ResponseError } from 'types'++type SendPageLeaveBody = components['schemas']['TelemetryPageLeaveBody']++export async function sendPageLeave(body: SendPageLeaveBody) {+ const { data, error } = await post(`/platform/telemetry/page-leave`, {+ body,+ credentials: 'include',+ })+ if (error) handleError(error)+ return data+}++type SendPageLeaveData = Awaited<ReturnType<typeof sendPageLeave>>++export const useSendPageLeaveMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<UseMutationOptions<SendPageLeaveData, ResponseError>, 'mutationFn'> = {}) => {+ const router = useRouter()+ const url = typeof window !== 'undefined' ? window.location.href : ''+ const title = typeof document !== 'undefined' ? document?.title : ''++ const body = {+ page_url: url,+ page_title: title,+ pathname: router.pathname,+ } as SendPageLeaveBody++ return useMutation<SendPageLeaveData, ResponseError>((vars) => sendPageLeave(body), {+ async onSuccess(data, variables, context) {+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ console.error(`Failed to send Telemetry page leave: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ })+}diff --git a/apps/studio/data/telemetry/send-reset-mutation.ts b/apps/studio/data/telemetry/send-reset-mutation.tsnew file mode 100644index 00000000000..43b376d2ce7--- /dev/null+++ b/apps/studio/data/telemetry/send-reset-mutation.ts@@ -0,0 +1,32 @@+import { useMutation, UseMutationOptions } from '@tanstack/react-query'++import { handleError, post } from 'data/fetchers'+import type { ResponseError } from 'types'++export async function sendReset() {+ const { data, error } = await post(`/platform/telemetry/reset`, { credentials: 'include' })+ if (error) handleError(error)+ return data+}++type SendResetData = Awaited<ReturnType<typeof sendReset>>++export const useSendResetMutation = ({+ onSuccess,+ onError,+ ...options+}: Omit<UseMutationOptions<SendResetData, ResponseError>, 'mutationFn'> = {}) => {+ return useMutation<SendResetData, ResponseError>((vars) => sendReset(), {+ async onSuccess(data, variables, context) {+ await onSuccess?.(data, variables, context)+ },+ async onError(data, variables, context) {+ if (onError === undefined) {+ console.error(`Failed to send Telemetry reset: ${data.message}`)+ } else {+ onError(data, variables, context)+ }+ },+ ...options,+ })+}