@codaco/analytics 8.0.0 → 9.0.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.
package/src/types.ts ADDED
@@ -0,0 +1,183 @@
1
+ import z from "zod";
2
+
3
+ /**
4
+ * Event types supported by the analytics system.
5
+ * These are converted to snake_case for PostHog.
6
+ */
7
+ export const eventTypes = [
8
+ "app_setup",
9
+ "protocol_installed",
10
+ "interview_started",
11
+ "interview_completed",
12
+ "data_exported",
13
+ "error",
14
+ ] as const;
15
+
16
+ export type EventType = (typeof eventTypes)[number];
17
+
18
+ /**
19
+ * Legacy event type mapping for backward compatibility
20
+ */
21
+ export const legacyEventTypeMap: Record<string, EventType> = {
22
+ AppSetup: "app_setup",
23
+ ProtocolInstalled: "protocol_installed",
24
+ InterviewStarted: "interview_started",
25
+ InterviewCompleted: "interview_completed",
26
+ DataExported: "data_exported",
27
+ Error: "error",
28
+ };
29
+
30
+ /**
31
+ * Standard event properties that can be sent with any event
32
+ */
33
+ export const EventPropertiesSchema = z.object({
34
+ metadata: z.record(z.string(), z.unknown()).optional(),
35
+ });
36
+
37
+ export type EventProperties = z.infer<typeof EventPropertiesSchema>;
38
+
39
+ /**
40
+ * Error-specific properties for error tracking
41
+ */
42
+ export const ErrorPropertiesSchema = EventPropertiesSchema.extend({
43
+ message: z.string(),
44
+ name: z.string(),
45
+ stack: z.string().optional(),
46
+ cause: z.string().optional(),
47
+ });
48
+
49
+ export type ErrorProperties = z.infer<typeof ErrorPropertiesSchema>;
50
+
51
+ /**
52
+ * Analytics configuration options
53
+ *
54
+ * This package is designed to work exclusively with the Cloudflare Worker
55
+ * reverse proxy at ph-relay.networkcanvas.com. All authentication is handled
56
+ * by the worker, so the API key is optional.
57
+ */
58
+ export interface AnalyticsConfig {
59
+ /**
60
+ * PostHog API host - should point to the Cloudflare Worker reverse proxy
61
+ * Defaults to "https://ph-relay.networkcanvas.com"
62
+ */
63
+ apiHost?: string;
64
+
65
+ /**
66
+ * PostHog project API key (optional)
67
+ *
68
+ * When using the reverse proxy (default), authentication is handled by the
69
+ * Cloudflare Worker. A placeholder key will be used for client-side PostHog
70
+ * initialization if not provided.
71
+ *
72
+ * Only set this if you need to override the default behavior.
73
+ */
74
+ apiKey?: string;
75
+
76
+ /**
77
+ * Unique identifier for this installation/deployment
78
+ * This is included with every event as a super property
79
+ */
80
+ installationId: string;
81
+
82
+ /**
83
+ * Disable all analytics tracking
84
+ * Can be set via DISABLE_ANALYTICS or NEXT_PUBLIC_DISABLE_ANALYTICS env var
85
+ */
86
+ disabled?: boolean;
87
+
88
+ /**
89
+ * Enable debug mode for PostHog
90
+ */
91
+ debug?: boolean;
92
+
93
+ /**
94
+ * Additional options to pass to PostHog initialization
95
+ */
96
+ posthogOptions?: {
97
+ /**
98
+ * Disable session recording
99
+ */
100
+ disable_session_recording?: boolean;
101
+
102
+ /**
103
+ * Autocapture settings
104
+ */
105
+ autocapture?: boolean;
106
+
107
+ /**
108
+ * Capture pageviews automatically
109
+ */
110
+ capture_pageview?: boolean;
111
+
112
+ /**
113
+ * Capture pageleave events
114
+ */
115
+ capture_pageleave?: boolean;
116
+
117
+ /**
118
+ * Cross-subdomain cookie
119
+ */
120
+ cross_subdomain_cookie?: boolean;
121
+
122
+ /**
123
+ * Advanced feature flags support
124
+ */
125
+ advanced_disable_feature_flags?: boolean;
126
+
127
+ /**
128
+ * Other PostHog options
129
+ */
130
+ [key: string]: unknown;
131
+ };
132
+ }
133
+
134
+ /**
135
+ * Analytics instance interface
136
+ */
137
+ export interface Analytics {
138
+ /**
139
+ * Track a custom event
140
+ */
141
+ trackEvent: (eventType: EventType | string, properties?: EventProperties) => void;
142
+
143
+ /**
144
+ * Track an error with full stack trace
145
+ */
146
+ trackError: (error: Error, additionalProperties?: EventProperties) => void;
147
+
148
+ /**
149
+ * Check if a feature flag is enabled
150
+ */
151
+ isFeatureEnabled: (flagKey: string) => boolean | undefined;
152
+
153
+ /**
154
+ * Get the value of a feature flag
155
+ */
156
+ getFeatureFlag: (flagKey: string) => string | boolean | undefined;
157
+
158
+ /**
159
+ * Reload feature flags from PostHog
160
+ */
161
+ reloadFeatureFlags: () => void;
162
+
163
+ /**
164
+ * Identify a user (optional - for advanced use cases)
165
+ * Note: By default we only track installations, not users
166
+ */
167
+ identify: (distinctId: string, properties?: Record<string, unknown>) => void;
168
+
169
+ /**
170
+ * Reset the user identity
171
+ */
172
+ reset: () => void;
173
+
174
+ /**
175
+ * Check if analytics is enabled
176
+ */
177
+ isEnabled: () => boolean;
178
+
179
+ /**
180
+ * Get the installation ID
181
+ */
182
+ getInstallationId: () => string;
183
+ }
package/src/utils.ts CHANGED
@@ -11,6 +11,7 @@ export function ensureError(value: unknown): Error {
11
11
  try {
12
12
  stringified = JSON.stringify(value);
13
13
  } catch (e) {
14
+ // biome-ignore lint/suspicious/noConsole: logging
14
15
  console.error(e);
15
16
  }
16
17
 
package/tsconfig.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "extends": "@codaco/tsconfig/base.json",
2
+ "extends": "@codaco/tsconfig/web.json",
3
3
  "compilerOptions": {
4
4
  "baseUrl": ".",
5
5
  "tsBuildInfoFile": "node_modules/.cache/tsbuildinfo.json"
@@ -0,0 +1,18 @@
1
+ /// <reference types="vitest" />
2
+
3
+ import { dirname, resolve } from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+ import { defineConfig } from "vitest/config";
6
+
7
+ const __dirname = dirname(fileURLToPath(import.meta.url));
8
+
9
+ export default defineConfig({
10
+ resolve: {
11
+ alias: {
12
+ "~": resolve(__dirname, "./src"),
13
+ },
14
+ },
15
+ test: {
16
+ disableConsoleIntercept: true,
17
+ },
18
+ });