create-db 0.0.10-pr26-add-more-improvements-16466284110.0 → 0.0.11-pr27-feat-posthog-16470238868.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/analytics.js +56 -0
  2. package/index.js +68 -4
  3. package/package.json +3 -2
package/analytics.js ADDED
@@ -0,0 +1,56 @@
1
+ import { randomUUID } from 'crypto';
2
+
3
+ class EventCaptureError extends Error {
4
+ constructor(event, status) {
5
+ super(`Failed to submit PostHog event '${event}': ${status}`);
6
+ }
7
+ }
8
+
9
+ class PosthogEventCapture {
10
+ async capture(eventName, properties = {}) {
11
+ const POSTHOG_CAPTURE_URL = process.env.POSTHOG_API_HOST ? process.env.POSTHOG_API_HOST + '/capture' : null
12
+ const POSTHOG_KEY = process.env.POSTHOG_API_KEY
13
+
14
+ // Skip if environment variables are not set
15
+ if (!POSTHOG_CAPTURE_URL || !POSTHOG_KEY) {
16
+ if (process.env.NODE_ENV === 'development') {
17
+ console.warn('Analytics skipped: POSTHOG_API_HOST or POSTHOG_API_KEY not set');
18
+ }
19
+ return;
20
+ }
21
+
22
+ const payload = {
23
+ api_key: POSTHOG_KEY,
24
+ event: eventName,
25
+ distinct_id: randomUUID(),
26
+ properties: {
27
+ $process_person_profile: false,
28
+ ...properties
29
+ }
30
+ };
31
+
32
+ try {
33
+ const response = await fetch(POSTHOG_CAPTURE_URL, {
34
+ method: 'POST',
35
+ headers: {
36
+ 'Content-Type': 'application/json',
37
+ },
38
+ body: JSON.stringify(payload),
39
+ });
40
+
41
+ if (!response.ok) {
42
+ throw new EventCaptureError(eventName, response.statusText);
43
+ }
44
+ } catch (error) {
45
+ // Silently fail analytics to not disrupt user experience
46
+ if (process.env.NODE_ENV === 'development') {
47
+ console.error('Analytics error:', error.message);
48
+ }
49
+ }
50
+ }
51
+ }
52
+
53
+ // Create a singleton instance
54
+ const analytics = new PosthogEventCapture();
55
+
56
+ export { analytics, EventCaptureError };
package/index.js CHANGED
@@ -1,5 +1,8 @@
1
1
  #!/usr/bin/env node
2
2
 
3
+ import dotenv from "dotenv";
4
+ dotenv.config();
5
+
3
6
  import {
4
7
  select,
5
8
  spinner,
@@ -7,13 +10,10 @@ import {
7
10
  outro,
8
11
  log,
9
12
  cancel,
10
- confirm,
11
13
  } from "@clack/prompts";
12
14
  import chalk from "chalk";
13
- import dotenv from "dotenv";
14
15
  import terminalLink from "terminal-link";
15
-
16
- dotenv.config();
16
+ import { analytics } from "./analytics.js";
17
17
 
18
18
  const CREATE_DB_WORKER_URL =
19
19
  process.env.CREATE_DB_WORKER_URL || "https://create-db-temp.prisma.io";
@@ -265,6 +265,17 @@ async function promptForRegion(defaultRegion) {
265
265
  process.exit(0);
266
266
  }
267
267
 
268
+ // Track region selection event
269
+ try {
270
+ await analytics.capture("create_db:region_selected", {
271
+ command: CLI_NAME,
272
+ region: region,
273
+ "selection-method": "interactive"
274
+ });
275
+ } catch (error) {
276
+ // Silently fail analytics
277
+ }
278
+
268
279
  return region;
269
280
  }
270
281
 
@@ -284,6 +295,19 @@ async function createDatabase(name, region) {
284
295
  s.stop(
285
296
  "We're experiencing a high volume of requests. Please try again later."
286
297
  );
298
+
299
+ // Track database creation failure
300
+ try {
301
+ await analytics.capture("create_db:database_creation_failed", {
302
+ command: CLI_NAME,
303
+ region: region,
304
+ "error-type": "rate_limit",
305
+ "status-code": 429,
306
+ });
307
+ } catch (error) {
308
+ // Silently fail analytics
309
+ }
310
+
287
311
  process.exit(1);
288
312
  }
289
313
 
@@ -293,6 +317,18 @@ async function createDatabase(name, region) {
293
317
  s.stop(
294
318
  `Error creating database: ${result.error.message || "Unknown error"}`
295
319
  );
320
+
321
+ // Track database creation failure
322
+ try {
323
+ await analytics.capture("create_db:database_creation_failed", {
324
+ command: CLI_NAME,
325
+ region: region,
326
+ "error-type": "api_error",
327
+ "error-message": result.error.message,
328
+ });
329
+ } catch (error) {
330
+ // Silently fail analytics
331
+ }
296
332
  process.exit(1);
297
333
  }
298
334
 
@@ -362,6 +398,23 @@ async function createDatabase(name, region) {
362
398
 
363
399
  async function main() {
364
400
  try {
401
+ const rawArgs = process.argv.slice(2);
402
+ try {
403
+ await analytics.capture("create_db:cli_command_ran", {
404
+ command: CLI_NAME,
405
+ "full-command": `${CLI_NAME} ${rawArgs.join(' ')}`.trim(),
406
+ "has-region-flag": rawArgs.includes('--region') || rawArgs.includes('-r'),
407
+ "has-interactive-flag": rawArgs.includes('--interactive') || rawArgs.includes('-i'),
408
+ "has-help-flag": rawArgs.includes('--help') || rawArgs.includes('-h'),
409
+ "has-list-regions-flag": rawArgs.includes('--list-regions'),
410
+ "node-version": process.version,
411
+ platform: process.platform,
412
+ arch: process.arch
413
+ });
414
+ } catch (error) {
415
+ // Silently fail analytics
416
+ }
417
+
365
418
  // Parse command line arguments
366
419
  const { flags } = await parseArgs();
367
420
 
@@ -386,6 +439,17 @@ async function main() {
386
439
  // Apply command line flags
387
440
  if (flags.region) {
388
441
  region = flags.region;
442
+
443
+ // Track region selection via flag
444
+ try {
445
+ await analytics.capture("create_db:region_selected", {
446
+ command: CLI_NAME,
447
+ region: region,
448
+ "selection-method": "flag"
449
+ });
450
+ } catch (error) {
451
+ // Silently fail analytics
452
+ }
389
453
  }
390
454
  if (flags.interactive) {
391
455
  chooseRegionPrompt = true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-db",
3
- "version": "0.0.10-pr26-add-more-improvements-16466284110.0",
3
+ "version": "0.0.11-pr27-feat-posthog-16470238868.0",
4
4
  "description": "Instantly create a temporary Prisma Postgres database with one command, then claim and persist it in your Prisma Data Platform project when ready.",
5
5
  "main": "index.js",
6
6
  "author": "",
@@ -38,6 +38,7 @@
38
38
  },
39
39
  "files": [
40
40
  "index.js",
41
- "README.md"
41
+ "README.md",
42
+ "analytics.js"
42
43
  ]
43
44
  }