Files
supabase/apps/docs/lib/telemetry.ts
Charis 3124f3dad7 feat: add docs feedback widget (#21131)
Add a docs feedback widget to allow upvoting and downvoting pages.

Votes (completely anonymized, barely more than a counter) go to a database in the main supabase-com project. If the user has accepted telemetry, the votes also go to Logflare with a bit more info. Post-vote, logged-in users can leave a comment, which goes to the `platform/feedback/send` endpoint. There is a warning in the feedback modal that the feedback is not anonymous.
2024-03-11 20:28:25 -04:00

42 lines
990 B
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,
page_location: pathname,
},
})
.then(({ error }) => {
if (error) console.error(error)
})
.catch((error) => console.error(error))
}
export { useSendTelemetryEvent }