Skip to content

Commit edddb5d

Browse files
committed
chore: add alias
1 parent 98863d2 commit edddb5d

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

packages/cli/src/cli/utils/observability.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pkg from "node-machine-id";
22
const { machineIdSync } = pkg;
33
import https from "https";
4+
import crypto from "crypto";
45
import { getOrgId } from "./org-id";
56

67
const POSTHOG_API_KEY = "phc_eR0iSoQufBxNY36k0f0T15UvHJdTfHlh8rJcxsfhfXk";
@@ -70,7 +71,7 @@ export default function trackEvent(
7071
distinct_id: identityInfo.distinct_id,
7172
properties: {
7273
...properties,
73-
...(email ? { $set: { email } } : {}),
74+
$set: { ...(properties?.$set || {}), ...(email ? { email } : {}) },
7475
$lib: "lingo.dev-cli",
7576
$lib_version: process.env.npm_package_version || "unknown",
7677
tracking_version: TRACKING_VERSION,
@@ -111,6 +112,33 @@ export default function trackEvent(
111112
req.write(payload);
112113
req.end();
113114

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+
114142
setTimeout(() => {
115143
if (!req.destroyed) {
116144
req.destroy();

0 commit comments

Comments
 (0)