-
Notifications
You must be signed in to change notification settings - Fork 2
/
hooks.ts
50 lines (40 loc) · 1.59 KB
/
hooks.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { useEffect, useLayoutEffect } from 'react'
import { useClientSession } from 'lib/seam/client-sessions/use-client-session.js'
import { useSeamContext } from 'lib/seam/SeamProvider.js'
import type { TelemetryClient } from './client.js'
import { useTelemetryContext } from './TelemetryProvider.js'
export function useTelemetryClient(): TelemetryClient {
const { client } = useTelemetryContext()
return client
}
export function useComponentTelemetry(name: string): void {
const { clientSession } = useClientSession()
const telemetry = useTelemetryClient()
useEffect(() => {
// Ensure the client session loaded to avoid anonymous telemetry data.
if (clientSession == null) return
telemetry.screen(name)
}, [name, telemetry, clientSession])
}
export function useUserTelemetry(): void {
const telemetry = useTelemetryClient()
const { publishableKey } = useSeamContext()
const { clientSession } = useClientSession()
// Ensure identify runs earlier than other effects to avoid anonymous telemetry data.
useLayoutEffect(() => {
if (clientSession == null) return
const telemetryUserId = [
clientSession.workspace_id,
clientSession.user_identifier_key ?? 'unknown',
].join('.')
telemetry.alias(telemetryUserId)
telemetry.identify(telemetryUserId, {
workspace_id: clientSession.workspace_id,
user_identifier_key: clientSession.user_identifier_key,
publishable_key: publishableKey,
})
telemetry.group(clientSession.workspace_id, {
workspace_id: clientSession.workspace_id,
})
}, [clientSession, publishableKey, telemetry])
}