| 51 | } |
| 52 | |
| 53 | export default function trackEvent( |
| 54 | user: UserIdentity, |
| 55 | event: string, |
| 56 | properties?: Record<string, any>, |
| 57 | ): void { |
| 58 | if (process.env.DO_NOT_TRACK === "1") { |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | setImmediate(() => { |
| 63 | try { |
| 64 | const identityInfo = determineDistinctId(user); |
| 65 | |
| 66 | if (process.env.DEBUG === "true") { |
| 67 | console.log( |
| 68 | `[Tracking] Event: ${event}, ID: ${identityInfo.distinct_id}, Source: ${identityInfo.distinct_id_source}`, |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | const eventData = { |
| 73 | api_key: POSTHOG_API_KEY, |
| 74 | event, |
| 75 | distinct_id: identityInfo.distinct_id, |
| 76 | properties: { |
| 77 | ...properties, |
| 78 | $set: { |
| 79 | ...(properties?.$set || {}), |
| 80 | ...(user ? { email: user.email } : {}), |
| 81 | }, |
| 82 | $lib: "lingo.dev-cli", |
| 83 | $lib_version: process.env.npm_package_version || "unknown", |
| 84 | tracking_version: TRACKING_VERSION, |
| 85 | distinct_id_source: identityInfo.distinct_id_source, |
| 86 | org_id: identityInfo.org_id, |
| 87 | node_version: process.version, |
| 88 | is_ci: !!process.env.CI, |
| 89 | debug_enabled: process.env.DEBUG === "true", |
| 90 | }, |
| 91 | timestamp: new Date().toISOString(), |
| 92 | }; |
| 93 | |
| 94 | const payload = JSON.stringify(eventData); |
| 95 | |
| 96 | const options: https.RequestOptions = { |
| 97 | hostname: POSTHOG_HOST, |
| 98 | path: POSTHOG_PATH, |
| 99 | method: "POST", |
| 100 | headers: { |
| 101 | "Content-Type": "application/json", |
| 102 | "Content-Length": Buffer.byteLength(payload).toString(), |
| 103 | }, |
| 104 | timeout: REQUEST_TIMEOUT_MS, |
| 105 | }; |
| 106 | |
| 107 | const req = https.request(options); |
| 108 | |
| 109 | req.on("timeout", () => { |
| 110 | req.destroy(); |