|
1
|
import { NextRequest } from "next/server" |
|
2
|
import { PostHog } from "posthog-node" |
|
3
|
|
|
4
|
type Body = { |
|
5
|
agent?: string |
|
6
|
path?: string |
|
7
|
feedback?: string |
|
8
|
[key: string]: unknown |
|
9
|
} |
|
10
|
|
|
11
|
export async function POST(req: NextRequest) { |
|
12
|
const { agent, path, feedback, ...rest } = (await req.json()) as Body |
|
13
|
|
|
14
|
if (!agent || !path || !feedback) { |
|
15
|
return new Response("Missing required fields", { status: 400 }) |
|
16
|
} |
|
17
|
const client = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY!, { |
|
18
|
host: process.env.NEXT_PUBLIC_POSTHOG_HOST, |
|
19
|
}) |
|
20
|
|
|
21
|
const urlObj = new URL( |
|
22
|
`${process.env.NEXT_PUBLIC_BASE_URL || ""}${process.env.NEXT_PUBLIC_BASE_PATH || ""}${path}` |
|
23
|
) |
|
24
|
|
|
25
|
client.capture({ |
|
26
|
distinctId: "anonymous", |
|
27
|
event: "agent_feedback_submitted", |
|
28
|
properties: { |
|
29
|
$current_url: urlObj.toString(), |
|
30
|
$raw_user_agent: req.headers.get("user-agent") || undefined, |
|
31
|
agent, |
|
32
|
feedback, |
|
33
|
other: JSON.stringify(rest), |
|
34
|
}, |
|
35
|
}) |
|
36
|
|
|
37
|
await client.shutdown() |
|
38
|
|
|
39
|
return new Response("Feedback submitted successfully", { status: 200 }) |
|
40
|
} |