add posthog analytics

This commit is contained in:
Aman Varshney
2025-05-28 20:41:03 +05:30
parent b8532f24f5
commit 591ecf8a09
6 changed files with 153 additions and 70 deletions

View File

@@ -55,19 +55,20 @@
"dependencies": {
"@clack/prompts": "^0.11.0",
"consola": "^3.4.2",
"execa": "^9.5.3",
"execa": "^9.6.0",
"fs-extra": "^11.3.0",
"globby": "^14.1.0",
"gradient-string": "^3.0.0",
"handlebars": "^4.7.8",
"picocolors": "^1.1.1",
"yargs": "^17.7.2"
"posthog-node": "^4.18.0",
"yargs": "^18.0.0"
},
"devDependencies": {
"@types/fs-extra": "^11.0.4",
"@types/node": "^22.15.21",
"@types/node": "^22.15.23",
"@types/yargs": "^17.0.33",
"tsdown": "^0.12.3",
"tsdown": "^0.12.4",
"typescript": "^5.8.3"
}
}

View File

@@ -17,6 +17,7 @@ import { createProject } from "./helpers/project-generation/create-project";
import { gatherConfig } from "./prompts/config-prompts";
import { getProjectName } from "./prompts/project-name";
import type { ProjectConfig } from "./types";
import { trackProjectCreation } from "./utils/analytics";
import { displayConfig } from "./utils/display-config";
import { generateReproducibleCommand } from "./utils/generate-reproducible-command";
import { renderTitle } from "./utils/render-title";
@@ -197,11 +198,13 @@ async function main() {
await createProject(config);
await trackProjectCreation(config);
const reproducibleCommand = generateReproducibleCommand(config);
log.success(
pc.blue(
`You can reproduce this setup with the following command:\n${generateReproducibleCommand(
config,
)}`,
`You can reproduce this setup with the following command:\n${reproducibleCommand}`,
),
);

View 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();
}
}

View File

@@ -27,9 +27,9 @@ export default function HomePage() {
const [selectedPM, setSelectedPM] = useState<"npm" | "pnpm" | "bun">("bun");
const commands = {
npm: "npx create-better-t-stack@latest my-app",
pnpm: "pnpm create better-t-stack@latest my-app",
bun: "bun create better-t-stack@latest my-app",
npm: "npx create-better-t-stack@latest",
pnpm: "pnpm create better-t-stack@latest",
bun: "bun create better-t-stack@latest",
};
useEffect(() => {