Skip to content

UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.

feat: add cli analytics tracking (#198)

Dillion Vermab09c89a9b4e3
diff --git a/packages/cli/package.json b/packages/cli/package.json
index 62fd6a55..88dd2d3c 100644
--- a/packages/cli/package.json
+++ b/packages/cli/package.json
@@ -33,7 +33,7 @@
"bin": "./dist/index.js",
"scripts": {
"dev": "tsup --watch --env.COMPONENTS_REGISTRY_URL http://localhost:3000 --env.PRO_REGISTRY_URL http://localhost:3001",
- "build": "tsup --env.COMPONENTS_REGISTRY_URL https://magicui.design --env.PRO_REGISTRY_URL https://pro.magicui.design",
+ "build": "tsup --env.COMPONENTS_REGISTRY_URL https://magicui.design --env.PRO_REGISTRY_URL https://pro.magicui.design --env.POSTHOG_API_KEY $POSTHOG_API_KEY",
"typecheck": "tsc --noEmit",
"format:write": "prettier --write \"**/*.{ts,tsx,mdx}\" --cache",
"format:check": "prettier --check \"**/*.{ts,tsx,mdx}\" --cache",
@@ -61,6 +61,7 @@
"lodash.template": "^4.5.0",
"node-fetch": "^3.3.0",
"ora": "^6.1.2",
+ "posthog-node": "^4.0.1",
"prompts": "^2.4.2",
"recast": "^0.23.2",
"ts-morph": "^18.0.0",
diff --git a/packages/cli/pnpm-lock.yaml b/packages/cli/pnpm-lock.yaml
index fb5a242e..e1c00fb7 100644
--- a/packages/cli/pnpm-lock.yaml
+++ b/packages/cli/pnpm-lock.yaml
@@ -62,6 +62,9 @@ importers:
ora:
specifier: ^6.1.2
version: 6.3.1
+ posthog-node:
+ specifier: ^4.0.1
+ version: 4.0.1
prompts:
specifier: ^2.4.2
version: 2.4.2
@@ -501,6 +504,12 @@ packages:
resolution: {integrity: sha512-6t10qk83GOG8p0vKmaCr8eiilZwO171AvbROMtvvNiwrTly62t+7XkA8RdIIVbpMhCASAsxgAzdRSwh6nw/5Dg==}
engines: {node: '>=4'}
+ resolution: {integrity: sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==}
+
+ resolution: {integrity: sha512-2A8QhOMrbomlDuiLeK9XibIBzuHeRcqqNOHp0Cyp5EoJ1IFDh+XZH3A6BkXtv0K4gFGCI0Y4BM7B1wOEi0Rmgw==}
+
resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==}
@@ -590,6 +599,10 @@ packages:
resolution: {integrity: sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==}
+ resolution: {integrity: sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==}
+ engines: {node: '>= 0.8'}
+
resolution: {integrity: sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==}
engines: {node: '>=14'}
@@ -638,6 +651,10 @@ packages:
resolution: {integrity: sha512-eFuaLoy/Rxalv2kr+lqMlUnrDWV+3j4pljOIJgLIhI058IQfWJ7vXhyEIHu+HtC738klGALYxOKDO0bQP3tg8A==}
+ resolution: {integrity: sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==}
+ engines: {node: '>=0.4.0'}
+
resolution: {integrity: sha512-uIFDxqpRZGZ6ThOk84hEfqWoHx2devRFvpTZcTHur85vImfaxUbTW9Ryh4CpCuDnToOP1CEtXKIgytHBPVff5A==}
engines: {node: '>=0.3.1'}
@@ -710,10 +727,23 @@ packages:
resolution: {integrity: sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==}
engines: {node: '>=8'}
+ resolution: {integrity: sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==}
+ engines: {node: '>=4.0'}
+ peerDependencies:
+ debug: '*'
+ peerDependenciesMeta:
+ debug:
+ optional: true
+
resolution: {integrity: sha512-TMKDUnIte6bfb5nWv7V/caI169OHgvwjb7V4WkeUvbQQdjr5rWKqHFiKWb/fcOwB+CzBT+qbWjvj+DVwRskpIg==}
engines: {node: '>=14'}
+ resolution: {integrity: sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==}
+ engines: {node: '>= 6'}
+
resolution: {integrity: sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==}
engines: {node: '>=12.20.0'}
@@ -926,6 +956,14 @@ packages:
resolution: {integrity: sha512-LPP/3KorzCwBxfeUuZmaR6bG2kdeHSbe0P2tY3FLRU4vYrjYz5hI4QZwV0njUx3jeuKe67YukQ1LSPZBKDqO/Q==}
engines: {node: '>=8.6'}
+ resolution: {integrity: sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==}
+ engines: {node: '>= 0.6'}
+
+ resolution: {integrity: sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==}
+ engines: {node: '>= 0.6'}
+
resolution: {integrity: sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==}
engines: {node: '>=6'}
@@ -1049,10 +1087,17 @@ packages:
ts-node:
optional: true
+ resolution: {integrity: sha512-rtqm2h22QxLGBrW2bLYzbRhliIrqgZ0k+gF0LkQ1SNdeD06YE5eilV0MxZppFSxC8TfH0+B0cWCuebEnreIDgQ==}
+ engines: {node: '>=15.0.0'}
+
resolution: {integrity: sha512-NxNv/kLguCA7p3jE8oL2aEBsrJWgAakBpgmgK6lpPWV+WuOmY6r2/zbAVnP+T8bQlA0nzHXSJSJW0Hq7ylaD2Q==}
engines: {node: '>= 6'}
+ resolution: {integrity: sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==}
+
resolution: {integrity: sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg==}
engines: {node: '>=6'}
@@ -1096,6 +1141,9 @@ packages:
resolution: {integrity: sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==}
+ resolution: {integrity: sha512-cLgakCUf6PedEu15t8kbsjnwIFFR2D4RfL+W3iWFJ4iac7z4B0ZI8fxy4R3J956kAI68HclCFGL8MPoUVC3qVA==}
+
resolution: {integrity: sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==}
@@ -1703,6 +1751,16 @@ snapshots:
dependencies:
tslib: 2.6.2
+
+ dependencies:
+ follow-redirects: 1.15.6
+ form-data: 4.0.0
+ proxy-from-env: 1.1.0
+ transitivePeerDependencies:
+ - debug
+
@@ -1793,6 +1851,10 @@ snapshots:
+ dependencies:
+ delayed-stream: 1.0.0
+
@@ -1836,6 +1898,8 @@ snapshots:
dependencies:
clone: 1.0.4
+
@@ -1936,11 +2000,19 @@ snapshots:
dependencies:
to-regex-range: 5.0.1
+
dependencies:
cross-spawn: 7.0.3
signal-exit: 4.1.0
+ dependencies:
+ asynckit: 0.4.0
+ combined-stream: 1.0.8
+ mime-types: 2.1.35
+
dependencies:
fetch-blob: 3.2.0
@@ -2113,6 +2185,12 @@ snapshots:
braces: 3.0.3
picomatch: 2.3.1
+
+ dependencies:
+ mime-db: 1.52.0
+
@@ -2216,11 +2294,20 @@ snapshots:
lilconfig: 2.1.0
yaml: 1.10.2
+ dependencies:
+ axios: 1.7.2
+ rusha: 0.8.14
+ transitivePeerDependencies:
+ - debug
+
dependencies:
kleur: 3.0.3
sisteransi: 1.0.5
+
@@ -2262,6 +2349,8 @@ snapshots:
dependencies:
queue-microtask: 1.2.3
+
diff --git a/packages/cli/src/commands/add.ts b/packages/cli/src/commands/add.ts
index 87538cdf..142334e0 100644
--- a/packages/cli/src/commands/add.ts
+++ b/packages/cli/src/commands/add.ts
@@ -1,5 +1,6 @@
import { existsSync, promises as fs } from "fs";
import path from "path";
+import { generateDistinctId } from "@/src/utils/distinct-id";
import { getConfig } from "@/src/utils/get-config";
import { getEnv } from "@/src/utils/get-env";
import { getPackageManager } from "@/src/utils/get-package-manager";
@@ -12,6 +13,7 @@ import {
logger,
tryPro,
} from "@/src/utils/logger";
+import { posthog } from "@/src/utils/posthog";
import {
fetchTree,
fetchTreeFromShadcn,
@@ -295,6 +297,17 @@ export const add = new Command()
}
}
}
+
+ posthog.capture({
+ event: "cli_add",
+ distinctId: generateDistinctId(),
+ properties: {
+ components: selectedComponents,
+ style: config.style,
+ baseColor: config.tailwind.baseColor,
+ },
+ });
+
spinner.succeed(`Done.`);
} catch (error) {
handleError(error);
diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts
index 352ceb14..8666981c 100644
--- a/packages/cli/src/commands/init.ts
+++ b/packages/cli/src/commands/init.ts
@@ -1,5 +1,6 @@
import { existsSync, promises as fs } from "fs";
import path from "path";
+import { generateDistinctId } from "@/src/utils/distinct-id";
import {
DEFAULT_COMPONENTS,
DEFAULT_TAILWIND_CONFIG,
@@ -14,6 +15,7 @@ import { getPackageManager } from "@/src/utils/get-package-manager";
import { getProjectConfig, preFlight } from "@/src/utils/get-project-info";
import { handleError } from "@/src/utils/handle-error";
import { logger } from "@/src/utils/logger";
+import { posthog } from "@/src/utils/posthog";
import {
getRegistryBaseColor,
getRegistryBaseColors,
@@ -82,6 +84,15 @@ export const init = new Command()
await runInit(cwd, config);
}
+ posthog.capture({
+ distinctId: generateDistinctId(),
+ event: "cli_init",
+ properties: {
+ cwd,
+ options,
+ },
+ });
+
logger.info("");
logger.info(
`${chalk.green(
diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts
index bb8806b5..bc3f32cb 100644
--- a/packages/cli/src/index.ts
+++ b/packages/cli/src/index.ts
@@ -1,6 +1,7 @@
#!/usr/bin/env node
import { add } from "@/src/commands/add";
import { init } from "@/src/commands/init";
+import { posthog } from "@/src/utils/posthog";
import { Command } from "commander";
import { getEnv } from "./utils/get-env";
@@ -13,8 +14,15 @@ import {
tryPro,
} from "./utils/logger";
-process.on("SIGINT", () => process.exit(0));
-process.on("SIGTERM", () => process.exit(0));
+process.on("SIGINT", async () => {
+ await posthog.shutdown();
+ process.exit(0);
+});
+
+process.on("SIGTERM", async () => {
+ await posthog.shutdown();
+ process.exit(0);
+});
async function main() {
const packageInfo = await getPackageInfo();
diff --git a/packages/cli/src/utils/distinct-id.ts b/packages/cli/src/utils/distinct-id.ts
new file mode 100644
index 00000000..633955e6
--- /dev/null
+++ b/packages/cli/src/utils/distinct-id.ts
@@ -0,0 +1,10 @@
+import crypto from "crypto";
+import os from "os";
+
+export function generateDistinctId(): string {
+ const username = os.userInfo().username;
+ const hostname = os.hostname();
+ const platform = os.platform();
+ const uniqueString = `${username}-${hostname}-${platform}`;
+ return crypto.createHash("md5").update(uniqueString).digest("hex");
+}
diff --git a/packages/cli/src/utils/get-env.ts b/packages/cli/src/utils/get-env.ts
index 92fa2efc..6825fabb 100644
--- a/packages/cli/src/utils/get-env.ts
+++ b/packages/cli/src/utils/get-env.ts
@@ -1,7 +1,4 @@
-import fs from "fs";
-import path from "path";
import Configstore from "configstore";
-import dotenv from "dotenv";
const envName = "MAGICUI_PRO_ENV";
const config = new Configstore("magicui");
diff --git a/packages/cli/src/utils/posthog.ts b/packages/cli/src/utils/posthog.ts
new file mode 100644
index 00000000..16269520
--- /dev/null
+++ b/packages/cli/src/utils/posthog.ts
@@ -0,0 +1,7 @@
+import { PostHog } from "posthog-node";
+
+export const posthog = new PostHog(process.env.POSTHOG_API_KEY!, {
+ host: "https://us.i.posthog.com",
+});
+
+posthog.debug();