Files
supabase/apps/studio/data/telemetry/send-event-mutation.ts
d633ced97d Feat/new project creation experiment (#31237)
* some early ideas

* better approach

* fix globe stuff

* add initial step

* reset and submit button for chat

* cleanup

* remove www

* more clean up

* mobile and more cleanup

* fixes

* update advanced and security

* fix project checks

* connect form

* fix reset and tidied up other things

* add layout options

* a/b test

* add offset

* add ab test

* remove www packages

* remove www

* package

* update coordinates

* feat: create ProjectCreationInitialStepSubmittedEvent

* feat: add event for capturing intention of prompting

* fix: bring pnpm-lock up to date

* fix ts errors

* fix globe

* fix globe animation

* fix ts

* remove imports and tools

* update module resolution

* global redirect

* delay rendering

* Update apps/studio/pages/new/[slug].tsx

Co-authored-by: Alaister Young <alaister@users.noreply.github.com>

* take directly to v2 via project create dropdown

* move redirect to useEffect

* feat: distinguish events between initial and second step

* feat: emit an event when create project is clicked on new/v2

* fix: correct event type defs

* remove unused imports

* Various clean ups

* improvements

* Minor refactors

* clean up

* ts fixes

---------

Co-authored-by: Long Hoang <1732217+loong@users.noreply.github.com>
Co-authored-by: Alaister Young <alaister@users.noreply.github.com>
Co-authored-by: Joshen Lim <joshenlimek@gmail.com>
2025-01-17 09:59:21 +10:00

150 lines
4.7 KiB
TypeScript

import { useMutation, UseMutationOptions } from '@tanstack/react-query'
import { components } from 'api-types'
import { isBrowser, LOCAL_STORAGE_KEYS } from 'common'
import { handleError, post } from 'data/fetchers'
import { IS_PLATFORM } from 'lib/constants'
import {
ConnectionStringCopiedEvent,
CronJobCreateClickedEvent,
CronJobCreatedEvent,
CronJobDeleteClickedEvent,
CronJobDeletedEvent,
CronJobHistoryClickedEvent,
CronJobUpdateClickedEvent,
CronJobUpdatedEvent,
FeaturePreviewsClickedEvent,
FeaturePreviewEnabledEvent,
FeaturePreviewDisabledEvent,
SqlEditorQuickstartClickedEvent,
SqlEditorTemplateClickedEvent,
SqlEditorResultDownloadCsvClickedEvent,
SqlEditorResultCopyMarkdownClickedEvent,
SqlEditorResultCopyJsonClickedEvent,
SignUpEvent,
SignInEvent,
RealtimeInspectorListenChannelClickedEvent,
RealtimeInspectorBroadcastSentEvent,
RealtimeInspectorMessageClickedEvent,
RealtimeInspectorCopyMessageClickedEvent,
RealtimeInspectorFiltersAppliedEvent,
RealtimeInspectorDatabaseRoleUpdatedEvent,
ProjectCreationInitialStepPromptIntendedEvent,
ProjectCreationInitialStepSubmittedEvent,
ProjectCreationSecondStepPromptIntendedEvent,
ProjectCreationSecondStepSubmittedEvent,
AssistantPromptSubmittedEvent,
AssistantDebugSubmittedEvent,
AssistantSuggestionRunQueryClickedEvent,
AssistantSqlDiffHandlerEvaluatedEvent,
AssistantEditInSqlEditorClickedEvent,
} from 'lib/constants/telemetry'
import { useRouter } from 'next/router'
import type { ResponseError } from 'types'
export type SendEventVariables =
| SignUpEvent
| SignInEvent
| ConnectionStringCopiedEvent
| CronJobCreatedEvent
| CronJobUpdatedEvent
| CronJobDeletedEvent
| CronJobCreateClickedEvent
| CronJobUpdateClickedEvent
| CronJobDeleteClickedEvent
| CronJobHistoryClickedEvent
| FeaturePreviewsClickedEvent
| FeaturePreviewEnabledEvent
| FeaturePreviewDisabledEvent
| ProjectCreationInitialStepPromptIntendedEvent
| ProjectCreationInitialStepSubmittedEvent
| ProjectCreationSecondStepPromptIntendedEvent
| ProjectCreationSecondStepSubmittedEvent
| RealtimeInspectorListenChannelClickedEvent
| RealtimeInspectorBroadcastSentEvent
| RealtimeInspectorMessageClickedEvent
| RealtimeInspectorCopyMessageClickedEvent
| RealtimeInspectorFiltersAppliedEvent
| RealtimeInspectorDatabaseRoleUpdatedEvent
| SqlEditorQuickstartClickedEvent
| SqlEditorTemplateClickedEvent
| SqlEditorResultDownloadCsvClickedEvent
| SqlEditorResultCopyMarkdownClickedEvent
| SqlEditorResultCopyJsonClickedEvent
| AssistantPromptSubmittedEvent
| AssistantDebugSubmittedEvent
| AssistantSuggestionRunQueryClickedEvent
| AssistantSqlDiffHandlerEvaluatedEvent
| AssistantEditInSqlEditorClickedEvent
type SendEventPayload = components['schemas']['TelemetryEventBodyV2Dto']
export async function sendEvent({ body }: { body: SendEventPayload }) {
const consent =
(typeof window !== 'undefined'
? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)
: null) === 'true'
if (!consent || !IS_PLATFORM) return undefined
const headers = { Version: '2' }
const { data, error } = await post(`/platform/telemetry/event`, { body, headers })
if (error) handleError(error)
return data
}
type SendEventData = Awaited<ReturnType<typeof sendEvent>>
export const useSendEventMutation = ({
onSuccess,
onError,
...options
}: Omit<
UseMutationOptions<SendEventData, ResponseError, SendEventVariables>,
'mutationFn'
> = {}) => {
const router = useRouter()
const title = typeof document !== 'undefined' ? document?.title : ''
const referrer = typeof document !== 'undefined' ? document?.referrer : ''
return useMutation<SendEventData, ResponseError, SendEventVariables>(
(vars) => {
const { action } = vars
const properties = 'properties' in vars ? vars.properties : {}
const body: SendEventPayload = {
action,
page_url: window.location.href,
page_title: title,
pathname: router.pathname,
ph: {
referrer,
language: router?.locale ?? 'en-US',
user_agent: navigator.userAgent,
search: window.location.search,
viewport_height: isBrowser ? window.innerHeight : 0,
viewport_width: isBrowser ? window.innerWidth : 0,
},
custom_properties: properties as any,
}
return sendEvent({ 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 event: ${data.message}`)
} else {
onError(data, variables, context)
}
},
...options,
}
)
}