mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add posthog analytics
This commit is contained in:
43
apps/cli/src/utils/analytics.ts
Normal file
43
apps/cli/src/utils/analytics.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import consola from "consola";
|
||||
import { PostHog } from "posthog-node";
|
||||
import type { ProjectConfig } from "../types";
|
||||
import { getLatestCLIVersion } from "./get-latest-cli-version";
|
||||
|
||||
const POSTHOG_API_KEY = "phc_8ZUxEwwfKMajJLvxz1daGd931dYbQrwKNficBmsdIrs";
|
||||
const POSTHOG_HOST = "https://us.i.posthog.com";
|
||||
|
||||
export async function trackProjectCreation(
|
||||
config: ProjectConfig,
|
||||
): Promise<void> {
|
||||
const posthog = new PostHog(POSTHOG_API_KEY, {
|
||||
host: POSTHOG_HOST,
|
||||
flushAt: 1,
|
||||
flushInterval: 0,
|
||||
privacyMode: true,
|
||||
disableGeoip: true,
|
||||
});
|
||||
|
||||
try {
|
||||
const sessionId = `cli_${Date.now()}_${crypto
|
||||
.randomUUID()
|
||||
.replace(/-/g, "")}`;
|
||||
|
||||
const { projectName, projectDir, relativePath, ...safeConfig } = config;
|
||||
|
||||
posthog.capture({
|
||||
distinctId: sessionId,
|
||||
event: "project_created",
|
||||
properties: {
|
||||
...safeConfig,
|
||||
cli_version: getLatestCLIVersion(),
|
||||
node_version: process.version,
|
||||
platform: process.platform,
|
||||
$ip: null,
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
consola.debug("Analytics tracking failed:", error);
|
||||
} finally {
|
||||
await posthog.shutdown();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user