( event: string, properties?: Record<string, any>, )
| 6 | const TRACKING_VERSION = "2.0"; |
| 7 | |
| 8 | export default async function trackEvent( |
| 9 | event: string, |
| 10 | properties?: Record<string, any>, |
| 11 | ) { |
| 12 | if (process.env.DO_NOT_TRACK === "1") { |
| 13 | return; |
| 14 | } |
| 15 | |
| 16 | try { |
| 17 | const identityInfo = await getDistinctId(); |
| 18 | |
| 19 | if (process.env.DEBUG === "true") { |
| 20 | console.log( |
| 21 | `[Tracking] Event: ${event}, ID: ${identityInfo.distinct_id}, Source: ${identityInfo.distinct_id_source}`, |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | const { PostHog } = await import("posthog-node"); |
| 26 | const posthog = new PostHog( |
| 27 | "phc_eR0iSoQufBxNY36k0f0T15UvHJdTfHlh8rJcxsfhfXk", |
| 28 | { |
| 29 | host: "https://eu.i.posthog.com", |
| 30 | flushAt: 1, |
| 31 | flushInterval: 0, |
| 32 | }, |
| 33 | ); |
| 34 | |
| 35 | await posthog.capture({ |
| 36 | distinctId: identityInfo.distinct_id, |
| 37 | event, |
| 38 | properties: { |
| 39 | ...properties, |
| 40 | isByokMode: properties?.models !== "lingo.dev", |
| 41 | tracking_version: TRACKING_VERSION, |
| 42 | distinct_id_source: identityInfo.distinct_id_source, |
| 43 | org_id: identityInfo.org_id, |
| 44 | meta: { |
| 45 | version: process.env.npm_package_version, |
| 46 | isCi: process.env.CI === "true", |
| 47 | }, |
| 48 | }, |
| 49 | }); |
| 50 | |
| 51 | await posthog.shutdown(); |
| 52 | } catch (error) { |
| 53 | if (process.env.DEBUG === "true") { |
| 54 | console.error("[Tracking] Error:", error); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | async function getDistinctId(): Promise<{ |
| 60 | distinct_id: string; |
no test coverage detected