feat(cli): allow disable telemetry

This commit is contained in:
Aman Varshney
2025-07-13 18:56:57 +05:30
parent 864e863656
commit 65c29c26c5
6 changed files with 71 additions and 5 deletions

View File

@@ -0,0 +1,5 @@
---
"create-better-t-stack": patch
---
Allow disabling telemetry with BTS_TELEMETRY_DISABLED=1

View File

@@ -36,7 +36,7 @@ jobs:
with: with:
publish: bun run publish-packages publish: bun run publish-packages
env: env:
TELEMETRY: "true" BTS_TELEMETRY: 1
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }} POSTHOG_API_KEY: ${{ secrets.POSTHOG_API_KEY }}

View File

@@ -66,6 +66,32 @@ Options:
-h, --help Display help -h, --help Display help
``` ```
## Telemetry
This CLI collects anonymous usage data to help improve the tool. The data collected includes:
- Configuration options selected
- CLI version
- Node.js version
- Platform (OS)
**Telemetry is enabled by default in published versions** to help us understand usage patterns and improve the tool.
### Disabling Telemetry
You can disable telemetry by setting the `BTS_TELEMETRY` environment variable:
```bash
# Disable telemetry for a single run
BTS_TELEMETRY=0 npx create-better-t-stack my-app
# Disable telemetry globally in your shell profile (.bashrc, .zshrc, etc.)
export BTS_TELEMETRY=0
```
### Development
During development, telemetry is automatically disabled when `NODE_ENV=development`.
## Examples ## Examples
Create a project with default configuration: Create a project with default configuration:

View File

@@ -1,6 +1,7 @@
import { PostHog } from "posthog-node"; import { PostHog } from "posthog-node";
import type { ProjectConfig } from "../types"; import type { ProjectConfig } from "../types";
import { getLatestCLIVersion } from "./get-latest-cli-version"; import { getLatestCLIVersion } from "./get-latest-cli-version";
import { isTelemetryEnabled } from "./telemetry";
const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY || ""; const POSTHOG_API_KEY = process.env.POSTHOG_API_KEY || "";
const POSTHOG_HOST = process.env.POSTHOG_HOST; const POSTHOG_HOST = process.env.POSTHOG_HOST;
@@ -14,7 +15,7 @@ export async function trackProjectCreation(
flushInterval: 0, flushInterval: 0,
privacyMode: true, privacyMode: true,
disableGeoip: true, disableGeoip: true,
disabled: process.env.TELEMETRY !== "true", disabled: !isTelemetryEnabled(),
}); });
try { try {

View File

@@ -0,0 +1,34 @@
/**
* Utility function to determine if telemetry should be enabled.
*
* Telemetry is enabled by default in production, but can be disabled by:
* - Setting BTS_TELEMETRY=0 or BTS_TELEMETRY=false
* - Running in development environment (NODE_ENV=development)
*/
export function isTelemetryEnabled(): boolean {
// If user explicitly disabled telemetry
if (
process.env.BTS_TELEMETRY === "0" ||
process.env.BTS_TELEMETRY === "false"
) {
return false;
}
// If user explicitly enabled telemetry
if (
process.env.BTS_TELEMETRY === "1" ||
process.env.BTS_TELEMETRY === "true"
) {
return true;
}
if (
process.env.BTS_TELEMETRY_DISABLED === "1" ||
process.env.BTS_TELEMETRY_DISABLED === "true"
) {
return false;
}
// Default to enabled in production
return true;
}

View File

@@ -10,8 +10,8 @@ export default defineConfig({
banner: "#!/usr/bin/env node", banner: "#!/usr/bin/env node",
}, },
env: { env: {
POSTHOG_API_KEY: process.env.POSTHOG_API_KEY || "lol", POSTHOG_API_KEY: process.env.POSTHOG_API_KEY || "random",
POSTHOG_HOST: process.env.POSTHOG_HOST || "lool", POSTHOG_HOST: process.env.POSTHOG_HOST || "random",
TELEMETRY: process.env.TELEMETRY || "false", // wierd trick i know BTS_TELEMETRY: process.env.BTS_TELEMETRY || "0",
}, },
}); });