Files
supabase/apps/www/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

57 lines
1.6 KiB
TypeScript

import { LOCAL_STORAGE_KEYS, TelemetryProps } from 'common'
import { API_URL, IS_PREVIEW, IS_PROD } from 'lib/constants'
import { NextRouter } from 'next/router'
import { post } from '~/lib/fetchWrapper'
export interface TelemetryEvent {
category: string
action: string
label: string
value?: string
}
const noop = () => {}
// This event is the same as in studio/lib/telemetry.tx
// but uses different ENV variables for www
const sendEvent = (event: TelemetryEvent, telemetryProps: TelemetryProps, router: NextRouter) => {
const consent =
typeof window !== 'undefined'
? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)
: null
const hasAcceptedConsent = consent === 'true'
const IS_DEV = !IS_PROD && !IS_PREVIEW
const blockEvent = IS_DEV || !hasAcceptedConsent
if (blockEvent) return noop
const { category, action, label, value } = event
const title = typeof document !== 'undefined' ? document?.title : ''
const referrer = typeof document !== 'undefined' ? document?.referrer : ''
const { page_url, search, language, viewport_height, viewport_width } = telemetryProps
return post(
`${API_URL}/telemetry/event`,
{
page_url,
action: action,
page_title: title,
pathname: router.pathname,
ph: {
search,
referrer,
language,
viewport_height,
viewport_width,
user_agent: navigator.userAgent,
},
custom_properties: { category, label, value } as any,
},
{ headers: { Version: '2' }, credentials: 'include' }
)
}
export default { sendEvent }