1
- import { buildDynamicUpdateQuery , dbExecute } from "@/lib/api/db/dbExecute" ;
2
1
import {
3
2
getEvaluatorUsage ,
4
3
getExperimentUsage ,
@@ -7,6 +6,8 @@ import { getHeliconeAuthClient } from "@/packages/common/auth/server/AuthClientF
7
6
import { costOf } from "@/packages/cost" ;
8
7
import { OnboardingState } from "@/services/hooks/useOrgOnboarding" ;
9
8
import { WebClient } from "@slack/web-api" ;
9
+ import { buildDynamicUpdateQuery , dbExecute } from "@/lib/api/db/dbExecute" ;
10
+ import { PosthogClient } from "@/lib/clients/posthogClient" ;
10
11
import generateApiKey from "generate-api-key" ;
11
12
import { buffer } from "micro" ;
12
13
import { NextApiRequest , NextApiResponse } from "next" ;
@@ -37,47 +38,6 @@ async function getUserIdFromEmail(email: string): Promise<string | null> {
37
38
}
38
39
}
39
40
40
- async function sendPosthogEvent (
41
- event : string ,
42
- properties : Record < string , any > ,
43
- userId : string ,
44
- orgId ?: string
45
- ) {
46
- try {
47
- if ( ! userId ) {
48
- console . error (
49
- `Cannot send PostHog event: missing userId for event ${ event } `
50
- ) ;
51
- return ;
52
- }
53
-
54
- const posthogPayload = {
55
- api_key : process . env . NEXT_PUBLIC_POSTHOG_API_KEY ,
56
- event : event ,
57
- distinct_id : userId ,
58
- properties : {
59
- ...properties ,
60
- ...( orgId ? { $groups : { organization : orgId } } : { } ) ,
61
- } ,
62
- } ;
63
-
64
- const response = await fetch ( POSTHOG_EVENT_API , {
65
- method : "POST" ,
66
- headers : { "Content-Type" : "application/json" } ,
67
- body : JSON . stringify ( posthogPayload ) ,
68
- } ) ;
69
-
70
- console . log ( `PostHog: Event response for ${ event } : ${ response . status } ` ) ;
71
-
72
- if ( ! response . ok ) {
73
- const responseText = await response . text ( ) ;
74
- console . error ( `PostHog error: ${ responseText } ` ) ;
75
- }
76
- } catch ( error ) {
77
- console . error ( `Error sending event to PostHog:` , error ) ;
78
- }
79
- }
80
-
81
41
const ADDON_PRICES : Record < string , keyof Addons > = {
82
42
[ process . env . PRICE_PROD_ALERTS_ID ! ] : "alerts" ,
83
43
[ process . env . PRICE_PROD_PROMPTS_ID ! ] : "prompts" ,
@@ -165,8 +125,8 @@ async function sendSubscriptionEvent(
165
125
: "immediate" ;
166
126
}
167
127
168
- // Send the event
169
- await sendPosthogEvent (
128
+ const analytics = PosthogClient . getInstance ( ) ;
129
+ await analytics . captureEvent (
170
130
eventType ,
171
131
{
172
132
...baseProperties ,
0 commit comments