create-db 0.0.11-pr27-feat-posthog-16470161310.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 (2) hide show
  1. package/analytics.js +56 -0
  2. 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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-db",
3
- "version": "0.0.11-pr27-feat-posthog-16470161310.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
  }