Files
supabase/apps/docs/lib/telemetry.ts
Joshen Lim ed207751ad Update telemetry calls for docs to support PH (#29749)
* Update telemetry calls for docs to support PH

* Update telemetry calls for www to support PH

* Add ts ignore

* Remove use of useRouter for docs

* Add credentials include for www and docs for telemetry calls

* Update TELEMETRY_CONSENT in common for www and docs to reset telemetry opt in preference

* Update common telemetry to use new endpoint and payload, and trigger reset request when opting out of telemetry from ui patterns PrigacySettings

* Fix

* Fix

* Fix

* Fix build issue in docs

* Fix build issue in docs

* I hope this fixes the build issues

* once more...

* Fix

* Add credentials include

* Fix
2024-10-16 15:16:33 +08:00

58 lines
1.6 KiB
TypeScript

import { isBrowser } from 'common'
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 pathname = usePathname()
const { hasAcceptedConsent } = useConsent()
if (!hasAcceptedConsent) return noop
const title = typeof document !== 'undefined' ? document?.title : ''
const referrer = typeof document !== 'undefined' ? document?.referrer : ''
return (event: TelemetryEvent) =>
unauthedAllowedPost('/platform/telemetry/event', {
body: {
pathname,
action: event.action,
page_url: isBrowser ? window.location.href : '',
page_title: title,
ph: {
referrer,
language: navigator.language ?? 'en-US',
user_agent: navigator.userAgent,
search: isBrowser ? window.location.search : '',
viewport_height: isBrowser ? window.innerHeight : 0,
viewport_width: isBrowser ? window.innerWidth : 0,
},
custom_properties: {
category: event.category,
label: event.label,
} as any,
},
headers: { Version: '2' },
credentials: 'include',
})
.then(({ error }) => {
if (error) console.error(error)
})
.catch((error) => console.error(error))
}
export { useSendTelemetryEvent }