Files
supabase/apps/docs/lib/telemetry.ts
T
Joshen Lim 336d623910 Chore/refactor telemetry calls for dashboard to support PH (#29747)
* Refactor telemetry event into RQ hook, and update to support PH

* Refactor telemetry identify into RQ hook, and update to support PH

* Refactor telemetry page into RQ hook, and update to support PH

* Clean up and deprecate lib/telemetry

* Pretty

* Add ts-ignore in docs for telemetry calls temporarily

* Add credentials include for send event, page and identify mutation hooks

* Add consent check for event and page telemetry, allow RQ mutations for those to fail silently without a toast
2024-10-09 14:32:30 +08:00

43 lines
1.0 KiB
TypeScript

import { usePathname } from 'next/navigation'
import { useConsent } from 'ui-patterns/ConsentToast'
import { unauthedAllowedPost } from './fetch/fetchWrappers'
type TelemetryEvent = {
action: string
category: string
label: string
}
const noop = () => {}
/**
* Sends a telemetry event to Logflare for tracking by the product team.
*
* Checks for user consent to telemetry before sending.
*/
const useSendTelemetryEvent = () => {
const { hasAcceptedConsent } = useConsent()
const pathname = usePathname()
if (!hasAcceptedConsent) return noop
return (event: TelemetryEvent) =>
unauthedAllowedPost('/platform/telemetry/event', {
// @ts-ignore - endpoint will accept this just fine
body: {
...event,
page_title: document?.title,
// @ts-ignore [JOSHEN] To be fixed for PH
page_location: pathname,
},
})
.then(({ error }) => {
if (error) console.error(error)
})
.catch((error) => console.error(error))
}
export { useSendTelemetryEvent }