11import { NextRequest , NextResponse } from "next/server" ;
22import { getEnv } from "@/lib/env" ;
33import { exchangeCodeForToken , validateProject } from "@/lib/auth-utils" ;
4+ import { sendAnalyticsEvent } from "@/lib/analytics" ;
45import {
56 redirectToError ,
67 redirectToSuccess ,
@@ -9,41 +10,6 @@ import {
910import { transferProject } from "@/lib/project-transfer" ;
1011import { buildRateLimitKey } from "@/lib/server/ratelimit" ;
1112
12- async function sendServerAnalyticsEvent (
13- event : string ,
14- properties : Record < string , any > ,
15- request : NextRequest
16- ) {
17- const env = getEnv ( ) ;
18-
19- if ( ! env . POSTHOG_API_KEY || ! env . POSTHOG_API_HOST ) {
20- return ;
21- }
22-
23- try {
24- await fetch ( `${ env . POSTHOG_API_HOST } /e` , {
25- method : "POST" ,
26- headers : {
27- "Content-Type" : "application/json" ,
28- Authorization : `Bearer ${ env . POSTHOG_API_KEY } ` ,
29- } ,
30- body : JSON . stringify ( {
31- api_key : env . POSTHOG_API_KEY ,
32- event,
33- properties : {
34- ...properties ,
35- $current_url : request . url ,
36- $user_agent : request . headers . get ( "user-agent" ) ,
37- } ,
38- distinct_id : "server-claim" ,
39- timestamp : new Date ( ) . toISOString ( ) ,
40- } ) ,
41- } ) ;
42- } catch ( error ) {
43- console . error ( "Failed to send server analytics event:" , error ) ;
44- }
45- }
46-
4713export async function GET ( request : NextRequest ) {
4814 try {
4915 const env = getEnv ( ) ;
@@ -98,7 +64,7 @@ export async function GET(request: NextRequest) {
9864 } catch ( error ) {
9965 const errorMessage =
10066 error instanceof Error ? error . message : "Unknown error" ;
101- await sendServerAnalyticsEvent (
67+ await sendAnalyticsEvent (
10268 "create_db:claim_failed" ,
10369 {
10470 "project-id" : projectID ,
@@ -115,13 +81,12 @@ export async function GET(request: NextRequest) {
11581 }
11682
11783 // Validate project exists and get project data
118- let projectData ;
11984 try {
120- projectData = await validateProject ( projectID ) ;
85+ await validateProject ( projectID ) ;
12186 } catch ( error ) {
12287 const errorMessage =
12388 error instanceof Error ? error . message : "Unknown error" ;
124- await sendServerAnalyticsEvent (
89+ await sendAnalyticsEvent (
12590 "create_db:claim_failed" ,
12691 {
12792 "project-id" : projectID ,
@@ -176,7 +141,7 @@ export async function GET(request: NextRequest) {
176141 } ;
177142 const databaseId = ( databases . data ?. [ 0 ] ?. id ?? "" ) . replace ( / ^ d b _ / , "" ) ;
178143
179- await sendServerAnalyticsEvent (
144+ await sendAnalyticsEvent (
180145 "create_db:claim_successful" ,
181146 {
182147 "project-id" : projectID ,
@@ -194,7 +159,7 @@ export async function GET(request: NextRequest) {
194159 databaseId
195160 ) ;
196161 } else {
197- await sendServerAnalyticsEvent (
162+ await sendAnalyticsEvent (
198163 "create_db:claim_failed" ,
199164 {
200165 "project-id" : projectID ,
0 commit comments