-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstrumentation.ts
More file actions
34 lines (29 loc) · 949 Bytes
/
instrumentation.ts
File metadata and controls
34 lines (29 loc) · 949 Bytes
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
import type { Instrumentation } from 'next';
export function register() {
// No-op for initialization
}
export const onRequestError: Instrumentation.onRequestError = async (
err,
request,
) => {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { postHogClient } = await import('./lib/posthog');
let distinctId = null;
if (request.headers.cookie) {
const cookieString = request.headers.cookie as string;
const postHogCookieMatch = cookieString.match(
/ph_phc_.*?_posthog=([^;]+)/,
);
if (postHogCookieMatch?.[1]) {
try {
const decodedCookie = decodeURIComponent(postHogCookieMatch[1]);
const postHogData = JSON.parse(decodedCookie);
distinctId = postHogData.distinct_id;
} catch (e) {
console.error('Error parsing PostHog cookie:', e);
}
}
}
postHogClient().captureException(err, distinctId || undefined);
}
};