2023-08-15 02:42:17 +02:00
|
|
|
const { v4 } = require("uuid");
|
|
|
|
const { SystemSettings } = require("./systemSettings");
|
|
|
|
|
|
|
|
const Telemetry = {
|
|
|
|
// Write-only key. It can't read events or any of your other data, so it's safe to use in public apps.
|
|
|
|
pubkey: "phc_9qu7QLpV8L84P3vFmEiZxL020t2EqIubP7HHHxrSsqS",
|
|
|
|
stubDevelopmentEvents: true, // [DO NOT TOUCH] Core team only.
|
|
|
|
label: "telemetry_id",
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-08-15 02:42:17 +02:00
|
|
|
id: async function () {
|
2023-09-28 23:00:03 +02:00
|
|
|
const result = await SystemSettings.get({ label: this.label });
|
|
|
|
return result?.value || null;
|
2023-08-15 02:42:17 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-08-15 02:42:17 +02:00
|
|
|
connect: async function () {
|
|
|
|
const client = this.client();
|
|
|
|
const distinctId = await this.findOrCreateId();
|
|
|
|
return { client, distinctId };
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-08-15 02:42:17 +02:00
|
|
|
isDev: function () {
|
2023-09-28 23:00:03 +02:00
|
|
|
return process.env.NODE_ENV === "development" && this.stubDevelopmentEvents;
|
2023-08-15 02:42:17 +02:00
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-08-15 02:42:17 +02:00
|
|
|
client: function () {
|
|
|
|
if (process.env.DISABLE_TELEMETRY === "true" || this.isDev()) return null;
|
|
|
|
const { PostHog } = require("posthog-node");
|
|
|
|
return new PostHog(this.pubkey);
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-08-15 02:42:17 +02:00
|
|
|
sendTelemetry: async function (event, properties = {}) {
|
|
|
|
try {
|
|
|
|
const { client, distinctId } = await this.connect();
|
|
|
|
if (!client) return;
|
|
|
|
console.log(`\x1b[32m[TELEMETRY SENT]\x1b[0m`, {
|
|
|
|
event,
|
|
|
|
properties,
|
|
|
|
});
|
|
|
|
client.capture({
|
|
|
|
event,
|
|
|
|
distinctId,
|
|
|
|
properties,
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-08-15 02:42:17 +02:00
|
|
|
flush: async function () {
|
2023-09-28 23:00:03 +02:00
|
|
|
const client = this.client();
|
2023-08-15 02:42:17 +02:00
|
|
|
if (!client) return;
|
|
|
|
await client.shutdownAsync();
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-08-15 02:42:17 +02:00
|
|
|
setUid: async function () {
|
|
|
|
const newId = v4();
|
|
|
|
await SystemSettings.updateSettings({ [this.label]: newId });
|
|
|
|
return newId;
|
|
|
|
},
|
2023-09-28 23:00:03 +02:00
|
|
|
|
2023-08-15 02:42:17 +02:00
|
|
|
findOrCreateId: async function () {
|
2023-09-28 23:00:03 +02:00
|
|
|
let currentId = await this.id();
|
|
|
|
if (currentId) return currentId;
|
|
|
|
|
|
|
|
currentId = await this.setUid();
|
|
|
|
return currentId;
|
2023-08-15 02:42:17 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = { Telemetry };
|