Skip to content

Commit 591ecf8

Browse files
add posthog analytics
1 parent b8532f2 commit 591ecf8

File tree

6 files changed

+153
-70
lines changed

6 files changed

+153
-70
lines changed

.changeset/clean-knives-find.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"create-better-t-stack": minor
3+
---
4+
5+
add posthog analytics

apps/cli/package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,20 @@
5555
"dependencies": {
5656
"@clack/prompts": "^0.11.0",
5757
"consola": "^3.4.2",
58-
"execa": "^9.5.3",
58+
"execa": "^9.6.0",
5959
"fs-extra": "^11.3.0",
6060
"globby": "^14.1.0",
6161
"gradient-string": "^3.0.0",
6262
"handlebars": "^4.7.8",
6363
"picocolors": "^1.1.1",
64-
"yargs": "^17.7.2"
64+
"posthog-node": "^4.18.0",
65+
"yargs": "^18.0.0"
6566
},
6667
"devDependencies": {
6768
"@types/fs-extra": "^11.0.4",
68-
"@types/node": "^22.15.21",
69+
"@types/node": "^22.15.23",
6970
"@types/yargs": "^17.0.33",
70-
"tsdown": "^0.12.3",
71+
"tsdown": "^0.12.4",
7172
"typescript": "^5.8.3"
7273
}
7374
}

apps/cli/src/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { createProject } from "./helpers/project-generation/create-project";
1717
import { gatherConfig } from "./prompts/config-prompts";
1818
import { getProjectName } from "./prompts/project-name";
1919
import type { ProjectConfig } from "./types";
20+
import { trackProjectCreation } from "./utils/analytics";
2021
import { displayConfig } from "./utils/display-config";
2122
import { generateReproducibleCommand } from "./utils/generate-reproducible-command";
2223
import { renderTitle } from "./utils/render-title";
@@ -197,11 +198,13 @@ async function main() {
197198

198199
await createProject(config);
199200

201+
await trackProjectCreation(config);
202+
203+
const reproducibleCommand = generateReproducibleCommand(config);
204+
200205
log.success(
201206
pc.blue(
202-
`You can reproduce this setup with the following command:\n${generateReproducibleCommand(
203-
config,
204-
)}`,
207+
`You can reproduce this setup with the following command:\n${reproducibleCommand}`,
205208
),
206209
);
207210

apps/cli/src/utils/analytics.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import consola from "consola";
2+
import { PostHog } from "posthog-node";
3+
import type { ProjectConfig } from "../types";
4+
import { getLatestCLIVersion } from "./get-latest-cli-version";
5+
6+
const POSTHOG_API_KEY = "phc_8ZUxEwwfKMajJLvxz1daGd931dYbQrwKNficBmsdIrs";
7+
const POSTHOG_HOST = "https://us.i.posthog.com";
8+
9+
export async function trackProjectCreation(
10+
config: ProjectConfig,
11+
): Promise<void> {
12+
const posthog = new PostHog(POSTHOG_API_KEY, {
13+
host: POSTHOG_HOST,
14+
flushAt: 1,
15+
flushInterval: 0,
16+
privacyMode: true,
17+
disableGeoip: true,
18+
});
19+
20+
try {
21+
const sessionId = `cli_${Date.now()}_${crypto
22+
.randomUUID()
23+
.replace(/-/g, "")}`;
24+
25+
const { projectName, projectDir, relativePath, ...safeConfig } = config;
26+
27+
posthog.capture({
28+
distinctId: sessionId,
29+
event: "project_created",
30+
properties: {
31+
...safeConfig,
32+
cli_version: getLatestCLIVersion(),
33+
node_version: process.version,
34+
platform: process.platform,
35+
$ip: null,
36+
},
37+
});
38+
} catch (error) {
39+
consola.debug("Analytics tracking failed:", error);
40+
} finally {
41+
await posthog.shutdown();
42+
}
43+
}

apps/web/src/app/(home)/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export default function HomePage() {
2727
const [selectedPM, setSelectedPM] = useState<"npm" | "pnpm" | "bun">("bun");
2828

2929
const commands = {
30-
npm: "npx create-better-t-stack@latest my-app",
31-
pnpm: "pnpm create better-t-stack@latest my-app",
32-
bun: "bun create better-t-stack@latest my-app",
30+
npm: "npx create-better-t-stack@latest",
31+
pnpm: "pnpm create better-t-stack@latest",
32+
bun: "bun create better-t-stack@latest",
3333
};
3434

3535
useEffect(() => {

0 commit comments

Comments
 (0)