-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsearch.ts
50 lines (43 loc) · 1.29 KB
/
search.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 type { OramaClient } from '@oramacloud/client';
export function getOramaUserId(): string | undefined {
if (typeof document === 'undefined' || typeof window === 'undefined') return;
const cookies = document.cookie.split(';');
const oid = cookies.find((cookie) => cookie.trim().startsWith('oid='));
if (oid) {
return oid.split('=')[1];
}
return;
}
export function userSessionRefresh(
client: OramaClient,
userId: string | undefined,
updateCallback: (userId: string | undefined) => void
) {
const currentUserId = getOramaUserId();
if (currentUserId !== userId) {
console.warn('User ID changed:', currentUserId);
client.reset();
updateCallback(currentUserId);
}
}
declare global {
interface Window {
posthog: any;
}
}
export function searchSessionTracking(client: OramaClient, userId: string) {
if (!window?.posthog) return;
try {
if (userId) {
// TODO: remove this console.log
console.log('Identifying user with Cookie ID:', userId);
client.identify(userId);
} else {
// TODO: remove this console.log
console.log('Identifying session with PostHog:', window.posthog.get_distinct_id());
client.alias(window.posthog.get_distinct_id());
}
} catch (error) {
console.log(`Error setting identity: ${error}`);
}
}