|
1 | 1 | import pkg from "node-machine-id"; |
2 | 2 | const { machineIdSync } = pkg; |
3 | 3 | import https from "https"; |
| 4 | +import crypto from "crypto"; |
4 | 5 | import { getOrgId } from "./org-id"; |
5 | 6 |
|
6 | 7 | const POSTHOG_API_KEY = "phc_eR0iSoQufBxNY36k0f0T15UvHJdTfHlh8rJcxsfhfXk"; |
@@ -70,7 +71,7 @@ export default function trackEvent( |
70 | 71 | distinct_id: identityInfo.distinct_id, |
71 | 72 | properties: { |
72 | 73 | ...properties, |
73 | | - ...(email ? { $set: { email } } : {}), |
| 74 | + $set: { ...(properties?.$set || {}), ...(email ? { email } : {}) }, |
74 | 75 | $lib: "lingo.dev-cli", |
75 | 76 | $lib_version: process.env.npm_package_version || "unknown", |
76 | 77 | tracking_version: TRACKING_VERSION, |
@@ -111,6 +112,33 @@ export default function trackEvent( |
111 | 112 | req.write(payload); |
112 | 113 | req.end(); |
113 | 114 |
|
| 115 | + // TODO: remove after 2026-03-25 — temporary alias to merge old hashed distinct_ids with new raw email |
| 116 | + if (email) { |
| 117 | + const hashedEmail = crypto.createHash("sha256").update(email).digest("hex"); |
| 118 | + const aliasData = JSON.stringify({ |
| 119 | + api_key: POSTHOG_API_KEY, |
| 120 | + event: "$create_alias", |
| 121 | + distinct_id: email, |
| 122 | + properties: { |
| 123 | + alias: hashedEmail, |
| 124 | + }, |
| 125 | + timestamp: new Date().toISOString(), |
| 126 | + }); |
| 127 | + |
| 128 | + const aliasReq = https.request({ |
| 129 | + ...options, |
| 130 | + headers: { |
| 131 | + "Content-Type": "application/json", |
| 132 | + "Content-Length": Buffer.byteLength(aliasData).toString(), |
| 133 | + }, |
| 134 | + }); |
| 135 | + aliasReq.on("timeout", () => aliasReq.destroy()); |
| 136 | + aliasReq.on("error", () => {}); |
| 137 | + aliasReq.write(aliasData); |
| 138 | + aliasReq.end(); |
| 139 | + setTimeout(() => { if (!aliasReq.destroyed) aliasReq.destroy(); }, REQUEST_TIMEOUT_MS); |
| 140 | + } |
| 141 | + |
114 | 142 | setTimeout(() => { |
115 | 143 | if (!req.destroyed) { |
116 | 144 | req.destroy(); |
|
0 commit comments