@od-oneapp/analytics 2026.1.1301
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/README.md +509 -0
- package/dist/ai-YMnynb-t.mjs +3347 -0
- package/dist/ai-YMnynb-t.mjs.map +1 -0
- package/dist/chunk-DQk6qfdC.mjs +18 -0
- package/dist/client-CTzJVFU5.mjs +9 -0
- package/dist/client-CTzJVFU5.mjs.map +1 -0
- package/dist/client-CcFTauAh.mjs +54 -0
- package/dist/client-CcFTauAh.mjs.map +1 -0
- package/dist/client-CeOLjbac.mjs +281 -0
- package/dist/client-CeOLjbac.mjs.map +1 -0
- package/dist/client-D339NFJS.mjs +267 -0
- package/dist/client-D339NFJS.mjs.map +1 -0
- package/dist/client-next.d.mts +62 -0
- package/dist/client-next.d.mts.map +1 -0
- package/dist/client-next.mjs +525 -0
- package/dist/client-next.mjs.map +1 -0
- package/dist/client.d.mts +30 -0
- package/dist/client.d.mts.map +1 -0
- package/dist/client.mjs +186 -0
- package/dist/client.mjs.map +1 -0
- package/dist/config-DPS6bSYo.d.mts +34 -0
- package/dist/config-DPS6bSYo.d.mts.map +1 -0
- package/dist/config-P6P5adJg.mjs +287 -0
- package/dist/config-P6P5adJg.mjs.map +1 -0
- package/dist/console-8bND3mMU.mjs +128 -0
- package/dist/console-8bND3mMU.mjs.map +1 -0
- package/dist/ecommerce-Cgu4wlux.mjs +993 -0
- package/dist/ecommerce-Cgu4wlux.mjs.map +1 -0
- package/dist/emitters-6-nKo8i-.mjs +208 -0
- package/dist/emitters-6-nKo8i-.mjs.map +1 -0
- package/dist/emitters-DldkVSPp.d.mts +12 -0
- package/dist/emitters-DldkVSPp.d.mts.map +1 -0
- package/dist/index-BfNWgfa5.d.mts +1494 -0
- package/dist/index-BfNWgfa5.d.mts.map +1 -0
- package/dist/index-BkIWe--N.d.mts +953 -0
- package/dist/index-BkIWe--N.d.mts.map +1 -0
- package/dist/index-jPzXRn52.d.mts +184 -0
- package/dist/index-jPzXRn52.d.mts.map +1 -0
- package/dist/manager-DvRRjza6.d.mts +76 -0
- package/dist/manager-DvRRjza6.d.mts.map +1 -0
- package/dist/posthog-bootstrap-CYfIy_WS.mjs +1769 -0
- package/dist/posthog-bootstrap-CYfIy_WS.mjs.map +1 -0
- package/dist/posthog-bootstrap-DWxFrxlt.d.mts +81 -0
- package/dist/posthog-bootstrap-DWxFrxlt.d.mts.map +1 -0
- package/dist/providers-http-client.d.mts +37 -0
- package/dist/providers-http-client.d.mts.map +1 -0
- package/dist/providers-http-client.mjs +320 -0
- package/dist/providers-http-client.mjs.map +1 -0
- package/dist/providers-http-server.d.mts +31 -0
- package/dist/providers-http-server.d.mts.map +1 -0
- package/dist/providers-http-server.mjs +297 -0
- package/dist/providers-http-server.mjs.map +1 -0
- package/dist/providers-http.d.mts +46 -0
- package/dist/providers-http.d.mts.map +1 -0
- package/dist/providers-http.mjs +4 -0
- package/dist/server-edge.d.mts +9 -0
- package/dist/server-edge.d.mts.map +1 -0
- package/dist/server-edge.mjs +373 -0
- package/dist/server-edge.mjs.map +1 -0
- package/dist/server-next.d.mts +67 -0
- package/dist/server-next.d.mts.map +1 -0
- package/dist/server-next.mjs +193 -0
- package/dist/server-next.mjs.map +1 -0
- package/dist/server.d.mts +10 -0
- package/dist/server.mjs +7 -0
- package/dist/service-cYtBBL8x.mjs +945 -0
- package/dist/service-cYtBBL8x.mjs.map +1 -0
- package/dist/shared.d.mts +16 -0
- package/dist/shared.d.mts.map +1 -0
- package/dist/shared.mjs +93 -0
- package/dist/shared.mjs.map +1 -0
- package/dist/types-BxBnNQ0V.d.mts +354 -0
- package/dist/types-BxBnNQ0V.d.mts.map +1 -0
- package/dist/types-CBvxUEaF.d.mts +216 -0
- package/dist/types-CBvxUEaF.d.mts.map +1 -0
- package/dist/types.d.mts +4 -0
- package/dist/types.mjs +0 -0
- package/dist/vercel-types-lwakUfoI.d.mts +102 -0
- package/dist/vercel-types-lwakUfoI.d.mts.map +1 -0
- package/package.json +129 -0
- package/src/client/index.ts +164 -0
- package/src/client/manager.ts +71 -0
- package/src/client/next/components.tsx +270 -0
- package/src/client/next/hooks.ts +217 -0
- package/src/client/next/manager.ts +141 -0
- package/src/client/next.ts +144 -0
- package/src/client-next.ts +101 -0
- package/src/client.ts +89 -0
- package/src/examples/ai-sdk-patterns.ts +583 -0
- package/src/examples/emitter-patterns.ts +476 -0
- package/src/examples/nextjs-emitter-patterns.tsx +403 -0
- package/src/next/app-router.tsx +564 -0
- package/src/next/client.ts +419 -0
- package/src/next/index.ts +84 -0
- package/src/next/middleware.ts +429 -0
- package/src/next/rsc.tsx +300 -0
- package/src/next/server.ts +253 -0
- package/src/next/types.d.ts +220 -0
- package/src/providers/base-provider.ts +419 -0
- package/src/providers/console/client.ts +10 -0
- package/src/providers/console/index.ts +152 -0
- package/src/providers/console/server.ts +6 -0
- package/src/providers/console/types.ts +15 -0
- package/src/providers/http/client.ts +464 -0
- package/src/providers/http/index.ts +30 -0
- package/src/providers/http/server.ts +396 -0
- package/src/providers/http/types.ts +135 -0
- package/src/providers/posthog/client.ts +518 -0
- package/src/providers/posthog/index.ts +11 -0
- package/src/providers/posthog/server.ts +329 -0
- package/src/providers/posthog/types.ts +104 -0
- package/src/providers/segment/client.ts +113 -0
- package/src/providers/segment/index.ts +11 -0
- package/src/providers/segment/server.ts +115 -0
- package/src/providers/segment/types.ts +51 -0
- package/src/providers/vercel/client.ts +102 -0
- package/src/providers/vercel/index.ts +11 -0
- package/src/providers/vercel/server.ts +89 -0
- package/src/providers/vercel/types.ts +27 -0
- package/src/server/index.ts +103 -0
- package/src/server/manager.ts +62 -0
- package/src/server/next.ts +210 -0
- package/src/server-edge.ts +442 -0
- package/src/server-next.ts +39 -0
- package/src/server.ts +106 -0
- package/src/shared/emitters/ai/README.md +981 -0
- package/src/shared/emitters/ai/events/agent.ts +130 -0
- package/src/shared/emitters/ai/events/artifacts.ts +167 -0
- package/src/shared/emitters/ai/events/chat.ts +126 -0
- package/src/shared/emitters/ai/events/chatbot-ecommerce.ts +133 -0
- package/src/shared/emitters/ai/events/completion.ts +103 -0
- package/src/shared/emitters/ai/events/content-generation.ts +347 -0
- package/src/shared/emitters/ai/events/conversation.ts +332 -0
- package/src/shared/emitters/ai/events/product-features.ts +1402 -0
- package/src/shared/emitters/ai/events/streaming.ts +114 -0
- package/src/shared/emitters/ai/events/tool.ts +93 -0
- package/src/shared/emitters/ai/index.ts +69 -0
- package/src/shared/emitters/ai/track-ai-sdk.ts +74 -0
- package/src/shared/emitters/ai/track-ai.ts +50 -0
- package/src/shared/emitters/ai/types.ts +1041 -0
- package/src/shared/emitters/ai/utils.ts +468 -0
- package/src/shared/emitters/ecommerce/events/cart-checkout.ts +106 -0
- package/src/shared/emitters/ecommerce/events/coupon.ts +49 -0
- package/src/shared/emitters/ecommerce/events/engagement.ts +61 -0
- package/src/shared/emitters/ecommerce/events/marketplace.ts +119 -0
- package/src/shared/emitters/ecommerce/events/order.ts +199 -0
- package/src/shared/emitters/ecommerce/events/product.ts +205 -0
- package/src/shared/emitters/ecommerce/events/registry.ts +123 -0
- package/src/shared/emitters/ecommerce/events/wishlist-sharing.ts +140 -0
- package/src/shared/emitters/ecommerce/index.ts +46 -0
- package/src/shared/emitters/ecommerce/track-ecommerce.ts +53 -0
- package/src/shared/emitters/ecommerce/types.ts +314 -0
- package/src/shared/emitters/ecommerce/utils.ts +216 -0
- package/src/shared/emitters/emitter-types.ts +974 -0
- package/src/shared/emitters/emitters.ts +292 -0
- package/src/shared/emitters/helpers.ts +419 -0
- package/src/shared/emitters/index.ts +66 -0
- package/src/shared/index.ts +142 -0
- package/src/shared/ingestion/index.ts +66 -0
- package/src/shared/ingestion/schemas.ts +386 -0
- package/src/shared/ingestion/service.ts +628 -0
- package/src/shared/node22-features.ts +848 -0
- package/src/shared/providers/console-provider.ts +160 -0
- package/src/shared/types/base-types.ts +54 -0
- package/src/shared/types/console-types.ts +19 -0
- package/src/shared/types/posthog-types.ts +131 -0
- package/src/shared/types/segment-types.ts +15 -0
- package/src/shared/types/types.ts +397 -0
- package/src/shared/types/vercel-types.ts +19 -0
- package/src/shared/utils/config-client.ts +19 -0
- package/src/shared/utils/config.ts +250 -0
- package/src/shared/utils/emitter-adapter.ts +212 -0
- package/src/shared/utils/manager.test.ts +36 -0
- package/src/shared/utils/manager.ts +1322 -0
- package/src/shared/utils/posthog-bootstrap.ts +136 -0
- package/src/shared/utils/posthog-client-utils.ts +48 -0
- package/src/shared/utils/posthog-next-utils.ts +282 -0
- package/src/shared/utils/posthog-server-utils.ts +210 -0
- package/src/shared/utils/rate-limit.ts +289 -0
- package/src/shared/utils/security.ts +545 -0
- package/src/shared/utils/validation-client.ts +161 -0
- package/src/shared/utils/validation.ts +399 -0
- package/src/shared.ts +155 -0
- package/src/types/index.ts +62 -0
|
@@ -0,0 +1,397 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Core types for the multi-provider analytics system
|
|
3
|
+
*
|
|
4
|
+
* This module defines the core TypeScript types and interfaces for the analytics system.
|
|
5
|
+
* It includes:
|
|
6
|
+
*
|
|
7
|
+
* - **Property Types**: PropertyValue, PropertyObject, Properties, UserTraits, GroupTraits, PageProperties
|
|
8
|
+
* - **Provider Interface**: AnalyticsProvider with all standard methods
|
|
9
|
+
* - **Configuration Types**: AnalyticsConfig, ProviderConfig, TrackingOptions
|
|
10
|
+
* - **Manager Interface**: AnalyticsManager with emitter and tracking methods
|
|
11
|
+
* - **Context Types**: AnalyticsContext for request-scoped data
|
|
12
|
+
* - **Error Types**: ErrorContext for error handling
|
|
13
|
+
* - **Ecommerce Types**: EcommerceEventSpec for ecommerce events
|
|
14
|
+
*
|
|
15
|
+
* **Optimized for**: React 19 and Node.js 22+ with strict TypeScript types
|
|
16
|
+
*
|
|
17
|
+
* @module @od-oneapp/analytics/shared/types/types
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// Re-export base types from base-types.ts to maintain API compatibility
|
|
21
|
+
export type {
|
|
22
|
+
GroupTraits,
|
|
23
|
+
PageProperties,
|
|
24
|
+
Properties,
|
|
25
|
+
PropertyObject,
|
|
26
|
+
PropertyValue,
|
|
27
|
+
UserTraits,
|
|
28
|
+
} from './base-types';
|
|
29
|
+
// Import base types for use in this file's interfaces
|
|
30
|
+
import type { GroupTraits, PageProperties, Properties, UserTraits } from './base-types';
|
|
31
|
+
// Import emitter types for the interface
|
|
32
|
+
import type {
|
|
33
|
+
EmitterAliasPayload,
|
|
34
|
+
EmitterGroupPayload,
|
|
35
|
+
EmitterIdentifyPayload,
|
|
36
|
+
EmitterPagePayload,
|
|
37
|
+
EmitterPayload,
|
|
38
|
+
EmitterScreenPayload,
|
|
39
|
+
EmitterTrackPayload,
|
|
40
|
+
} from '../emitters/emitter-types';
|
|
41
|
+
|
|
42
|
+
// Re-export emitter types for external consumption
|
|
43
|
+
export type { EmitterPayload };
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* PostHog bootstrap data structure.
|
|
47
|
+
*
|
|
48
|
+
* Contains PostHog-specific initialization data including distinct ID, feature flags,
|
|
49
|
+
* and identification status. Used for server-side rendering and hydration.
|
|
50
|
+
*/
|
|
51
|
+
export interface PostHogBootstrap {
|
|
52
|
+
distinctID?: string;
|
|
53
|
+
isIdentifiedID?: boolean;
|
|
54
|
+
featureFlags?: Record<string, string | boolean>;
|
|
55
|
+
featureFlagPayloads?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Provider configuration interface.
|
|
60
|
+
*
|
|
61
|
+
* Defines the configuration structure for analytics providers. Each provider
|
|
62
|
+
* may require different fields (apiKey, writeKey, token, etc.).
|
|
63
|
+
*/
|
|
64
|
+
export interface ProviderConfig {
|
|
65
|
+
// Provider-specific required fields
|
|
66
|
+
apiKey?: string | undefined;
|
|
67
|
+
measurementId?: string | undefined;
|
|
68
|
+
token?: string | undefined;
|
|
69
|
+
writeKey?: string | undefined;
|
|
70
|
+
enabled?: boolean | undefined;
|
|
71
|
+
|
|
72
|
+
// Optional configuration
|
|
73
|
+
events?: string[] | 'all' | undefined;
|
|
74
|
+
options?: Record<string, unknown> | undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Analytics provider interface.
|
|
79
|
+
*
|
|
80
|
+
* All analytics providers must implement this interface for consistent behavior
|
|
81
|
+
* across the analytics system. Providers can implement optional methods based on
|
|
82
|
+
* their capabilities.
|
|
83
|
+
*
|
|
84
|
+
* **Required Methods**:
|
|
85
|
+
* - `initialize()`: Initialize the provider
|
|
86
|
+
* - `track()`: Track events
|
|
87
|
+
*
|
|
88
|
+
* **Optional Methods**:
|
|
89
|
+
* - `identify()`: Identify users
|
|
90
|
+
* - `page()`: Track page views
|
|
91
|
+
* - `screen()`: Track screen views (mobile)
|
|
92
|
+
* - `group()`: Associate users with groups
|
|
93
|
+
* - `alias()`: Alias user IDs
|
|
94
|
+
* - `setContext()`: Set global context
|
|
95
|
+
*/
|
|
96
|
+
export interface AnalyticsProvider {
|
|
97
|
+
readonly name: string;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Initialize the provider with configuration
|
|
101
|
+
*/
|
|
102
|
+
initialize(config: ProviderConfig): Promise<void>;
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Track an event with properties
|
|
106
|
+
*/
|
|
107
|
+
track(event: string, properties: Properties, context?: AnalyticsContext): Promise<void>;
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Identify a user with traits (optional)
|
|
111
|
+
*/
|
|
112
|
+
identify?(userId: string, traits: UserTraits, context?: AnalyticsContext): Promise<void>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Track a page view (optional)
|
|
116
|
+
*/
|
|
117
|
+
page?(name: string, properties: PageProperties, context?: AnalyticsContext): Promise<void>;
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Track a screen view - mobile equivalent of page (optional)
|
|
121
|
+
*/
|
|
122
|
+
screen?(name: string, properties: PageProperties, context?: AnalyticsContext): Promise<void>;
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Associate user with a group/organization (optional)
|
|
126
|
+
*/
|
|
127
|
+
group?(groupId: string, traits: GroupTraits, context?: AnalyticsContext): Promise<void>;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Alias one user ID to another (optional)
|
|
131
|
+
*/
|
|
132
|
+
alias?(userId: string, previousId: string, context?: AnalyticsContext): Promise<void>;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Set global context for all events (optional)
|
|
136
|
+
*/
|
|
137
|
+
setContext?(context: AnalyticsContext): void;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Error context for error handlers.
|
|
142
|
+
*
|
|
143
|
+
* Provides context about errors that occur during analytics operations,
|
|
144
|
+
* including which provider and method failed.
|
|
145
|
+
*/
|
|
146
|
+
export interface ErrorContext {
|
|
147
|
+
provider: string;
|
|
148
|
+
method: string;
|
|
149
|
+
[key: string]: unknown;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Analytics configuration interface.
|
|
154
|
+
*
|
|
155
|
+
* Defines the complete configuration structure for the analytics system.
|
|
156
|
+
* Includes provider configurations, debug settings, error handlers, and Next.js-specific options.
|
|
157
|
+
*/
|
|
158
|
+
export interface AnalyticsConfig {
|
|
159
|
+
/**
|
|
160
|
+
* Enable debug logging
|
|
161
|
+
*/
|
|
162
|
+
debug?: boolean | undefined;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Next.js specific configuration
|
|
166
|
+
*/
|
|
167
|
+
nextjs?:
|
|
168
|
+
| {
|
|
169
|
+
deferUntilConsent?: boolean | undefined;
|
|
170
|
+
bufferEvents?: boolean | undefined;
|
|
171
|
+
maxBufferSize?: number | undefined;
|
|
172
|
+
checkConsent?: (() => boolean | Promise<boolean>) | undefined;
|
|
173
|
+
posthog?:
|
|
174
|
+
| {
|
|
175
|
+
bootstrap?: PostHogBootstrap | undefined;
|
|
176
|
+
apiKey?: string | undefined;
|
|
177
|
+
host?: string | undefined;
|
|
178
|
+
}
|
|
179
|
+
| undefined;
|
|
180
|
+
[key: string]: unknown;
|
|
181
|
+
}
|
|
182
|
+
| undefined;
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Error handler callback
|
|
186
|
+
*/
|
|
187
|
+
onError?: ((error: unknown, context: ErrorContext) => void) | undefined;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Info logging callback
|
|
191
|
+
*/
|
|
192
|
+
onInfo?: ((message: string) => void) | undefined;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Provider configurations
|
|
196
|
+
*/
|
|
197
|
+
providers: Record<string, ProviderConfig>;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Options for individual tracking calls.
|
|
202
|
+
*
|
|
203
|
+
* Allows per-call customization of provider selection and context.
|
|
204
|
+
* Useful for sending events to specific providers or excluding certain providers.
|
|
205
|
+
*/
|
|
206
|
+
export interface TrackingOptions {
|
|
207
|
+
/**
|
|
208
|
+
* Override: add providers not in global config
|
|
209
|
+
*/
|
|
210
|
+
providers?: Record<string, ProviderConfig> | undefined;
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Use shorthand for configured providers only
|
|
214
|
+
*/
|
|
215
|
+
only?: string[] | undefined;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Send to all except these
|
|
219
|
+
*/
|
|
220
|
+
exclude?: string[] | undefined;
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Context to merge with global context
|
|
224
|
+
*/
|
|
225
|
+
context?: AnalyticsContext | undefined;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Factory function to create a provider instance.
|
|
230
|
+
*
|
|
231
|
+
* Takes provider configuration and returns an initialized provider instance.
|
|
232
|
+
*/
|
|
233
|
+
export type ProviderFactory = (config: ProviderConfig) => AnalyticsProvider;
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Registry mapping provider names to factory functions.
|
|
237
|
+
*
|
|
238
|
+
* Used internally to dynamically create provider instances based on configuration.
|
|
239
|
+
*/
|
|
240
|
+
export type ProviderRegistry = Record<string, ProviderFactory>;
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Analytics context passed with all events.
|
|
244
|
+
*
|
|
245
|
+
* Provides request-scoped or user-scoped context that is automatically
|
|
246
|
+
* included with all analytics events. Can include userId, sessionId, organizationId,
|
|
247
|
+
* environment, traits, and custom properties.
|
|
248
|
+
*/
|
|
249
|
+
export interface AnalyticsContext {
|
|
250
|
+
userId?: string;
|
|
251
|
+
sessionId?: string;
|
|
252
|
+
organizationId?: string;
|
|
253
|
+
environment?: string;
|
|
254
|
+
traits?: UserTraits;
|
|
255
|
+
[key: string]: unknown;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Ecommerce event specification.
|
|
260
|
+
*
|
|
261
|
+
* Defines the structure for ecommerce events. Used by the legacy `trackEcommerce()`
|
|
262
|
+
* method. New code should use `emit(ecommerce.EVENT_NAME(...))` instead.
|
|
263
|
+
*
|
|
264
|
+
* @deprecated Use `emit(ecommerce.EVENT_NAME(...))` for better type safety
|
|
265
|
+
*/
|
|
266
|
+
export interface EcommerceEventSpec {
|
|
267
|
+
name: string;
|
|
268
|
+
properties: Properties;
|
|
269
|
+
category?: string;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Analytics Manager interface.
|
|
274
|
+
*
|
|
275
|
+
* Main entry point for analytics tracking. Provides methods for:
|
|
276
|
+
* - Initializing providers
|
|
277
|
+
* - Tracking events (track, identify, page, screen, group, alias)
|
|
278
|
+
* - Managing context
|
|
279
|
+
* - Emitting emitter payloads (recommended approach)
|
|
280
|
+
* - Batch operations
|
|
281
|
+
*
|
|
282
|
+
* **Recommended Usage**: Use `emit()` with emitter payloads for type-safe event tracking.
|
|
283
|
+
*/
|
|
284
|
+
export interface AnalyticsManager {
|
|
285
|
+
/**
|
|
286
|
+
* Get current analytics context
|
|
287
|
+
*/
|
|
288
|
+
getContext(): AnalyticsContext;
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Initialize all configured providers
|
|
292
|
+
*/
|
|
293
|
+
initialize(): Promise<void>;
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Set global analytics context
|
|
297
|
+
*/
|
|
298
|
+
setContext(context: AnalyticsContext): void;
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Create a bound emitter function
|
|
302
|
+
*/
|
|
303
|
+
createEmitter(): (payload: EmitterPayload) => Promise<void>;
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Emit an emitter payload (recommended)
|
|
307
|
+
*/
|
|
308
|
+
emit(payload: EmitterPayload, options?: { timeout?: number }): Promise<void>;
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Emit multiple payloads in batch
|
|
312
|
+
*/
|
|
313
|
+
emitBatch(
|
|
314
|
+
payloads: EmitterPayload[],
|
|
315
|
+
options?: { timeout?: number; concurrency?: number; failFast?: boolean },
|
|
316
|
+
): Promise<void>;
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Track an event
|
|
320
|
+
* Overload 1: Using emitter payload (recommended)
|
|
321
|
+
* Overload 2: Direct call with event name and properties
|
|
322
|
+
*/
|
|
323
|
+
track(payload: EmitterTrackPayload): Promise<void>;
|
|
324
|
+
track(event: string, properties?: Properties, options?: TrackingOptions): Promise<void>;
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Identify a user
|
|
328
|
+
* Overload 1: Using emitter payload (recommended)
|
|
329
|
+
* Overload 2: Direct call with user ID and traits
|
|
330
|
+
*/
|
|
331
|
+
identify(payload: EmitterIdentifyPayload): Promise<void>;
|
|
332
|
+
identify(userId: string, traits?: UserTraits, options?: TrackingOptions): Promise<void>;
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Track a page view
|
|
336
|
+
* Overload 1: Using emitter payload (recommended)
|
|
337
|
+
* Overload 2: Direct call with page name and properties
|
|
338
|
+
*/
|
|
339
|
+
page(payload: EmitterPagePayload): Promise<void>;
|
|
340
|
+
page(name?: string, properties?: PageProperties, options?: TrackingOptions): Promise<void>;
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Track a screen view (mobile equivalent of page)
|
|
344
|
+
* Overload 1: Using emitter payload (recommended)
|
|
345
|
+
* Overload 2: Direct call with screen name and properties
|
|
346
|
+
*/
|
|
347
|
+
screen?(payload: EmitterScreenPayload): Promise<void>;
|
|
348
|
+
screen?(name?: string, properties?: PageProperties, options?: TrackingOptions): Promise<void>;
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Associate user with a group
|
|
352
|
+
* Overload 1: Using emitter payload (recommended)
|
|
353
|
+
* Overload 2: Direct call with group ID and traits
|
|
354
|
+
*/
|
|
355
|
+
group(payload: EmitterGroupPayload): Promise<void>;
|
|
356
|
+
group(groupId: string, traits?: GroupTraits, options?: TrackingOptions): Promise<void>;
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* Alias one user ID to another
|
|
360
|
+
* Overload 1: Using emitter payload (recommended)
|
|
361
|
+
* Overload 2: Direct call with user ID and previous ID
|
|
362
|
+
*/
|
|
363
|
+
alias(payload: EmitterAliasPayload): Promise<void>;
|
|
364
|
+
alias(userId: string, previousId: string, options?: TrackingOptions): Promise<void>;
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Get list of active provider names
|
|
368
|
+
*/
|
|
369
|
+
getActiveProviders(): string[];
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Get a specific provider instance
|
|
373
|
+
*/
|
|
374
|
+
getProvider(name: string): AnalyticsProvider | undefined;
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Reset analytics context and identity
|
|
378
|
+
*/
|
|
379
|
+
reset(): void;
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Shutdown all providers and cleanup resources
|
|
383
|
+
*/
|
|
384
|
+
shutdown(): Promise<void>;
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Process an emitter payload (legacy, use emit instead)
|
|
388
|
+
* @deprecated Use emit() for better type safety
|
|
389
|
+
*/
|
|
390
|
+
processEmitterPayload(payload: EmitterPayload): Promise<void>;
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Track an ecommerce event (legacy, use emit with ecommerce emitters)
|
|
394
|
+
* @deprecated Use emit(ecommerce.EVENT_NAME(...)) instead
|
|
395
|
+
*/
|
|
396
|
+
trackEcommerce(eventSpec: EcommerceEventSpec): Promise<void>;
|
|
397
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Vercel Analytics-specific types
|
|
3
|
+
* Vercel Analytics-specific types
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
export interface VercelConfig {
|
|
7
|
+
// Vercel Analytics doesn't require API keys for basic usage
|
|
8
|
+
// But can be configured with options
|
|
9
|
+
options?: {
|
|
10
|
+
mode?: 'auto' | 'production' | 'development';
|
|
11
|
+
debug?: boolean;
|
|
12
|
+
beforeSend?: (event: any) => any | null;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Vercel Analytics has limited server-side support
|
|
17
|
+
// Mainly focused on web vitals and page views on client
|
|
18
|
+
|
|
19
|
+
export type VercelOptions = Record<string, any>;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Client-safe configuration utilities for the analytics system
|
|
3
|
+
* Client-safe configuration utilities for the analytics system
|
|
4
|
+
*
|
|
5
|
+
* This file re-exports from the unified config module.
|
|
6
|
+
* Maintained for backwards compatibility with existing imports.
|
|
7
|
+
*
|
|
8
|
+
* @deprecated Import from './config' directly instead
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
PROVIDER_REQUIREMENTS,
|
|
13
|
+
createConfigBuilder,
|
|
14
|
+
getAnalyticsConfig,
|
|
15
|
+
validateConfig,
|
|
16
|
+
type AnalyticsConfigOptions,
|
|
17
|
+
type ConfigBuilder,
|
|
18
|
+
type ConfigRequirements,
|
|
19
|
+
} from './config';
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Configuration utilities for the analytics system
|
|
3
|
+
*
|
|
4
|
+
* This module provides configuration utilities that work in both client and server
|
|
5
|
+
* environments. It includes:
|
|
6
|
+
*
|
|
7
|
+
* - **Provider Requirements**: Required fields for each provider type
|
|
8
|
+
* - **Configuration Validation**: Validates provider configurations
|
|
9
|
+
* - **Config Builder**: Fluent API for building configurations
|
|
10
|
+
* - **Config Factory**: Environment-aware configuration factory
|
|
11
|
+
*
|
|
12
|
+
* **Usage**: Use `createConfigBuilder()` for programmatic config creation or
|
|
13
|
+
* `getAnalyticsConfig()` for environment-aware configuration.
|
|
14
|
+
*
|
|
15
|
+
* @module @od-oneapp/analytics/shared/utils/config
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import type { AnalyticsConfig, ProviderConfig } from '../types/types';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Type for provider requirements mapping.
|
|
22
|
+
*
|
|
23
|
+
* Maps provider names to arrays of required configuration field names.
|
|
24
|
+
*/
|
|
25
|
+
export type ConfigRequirements = Record<string, string[]>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Provider requirements map - defines required fields for each provider.
|
|
29
|
+
*
|
|
30
|
+
* Used during configuration validation to ensure all required fields are present.
|
|
31
|
+
* Providers not listed here have no required fields.
|
|
32
|
+
*/
|
|
33
|
+
export const PROVIDER_REQUIREMENTS: ConfigRequirements = {
|
|
34
|
+
console: [], // No required fields
|
|
35
|
+
mixpanel: [], // Server-only provider
|
|
36
|
+
posthog: ['apiKey'],
|
|
37
|
+
segment: ['writeKey'],
|
|
38
|
+
vercel: [], // No required fields
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Validates analytics configuration.
|
|
43
|
+
*
|
|
44
|
+
* Accepts `unknown` for runtime validation of potentially malformed configs.
|
|
45
|
+
* Throws descriptive errors if configuration is invalid.
|
|
46
|
+
*
|
|
47
|
+
* @param {unknown} config - Configuration object to validate
|
|
48
|
+
* @throws {Error} If configuration is invalid or missing required fields
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```typescript
|
|
52
|
+
* try {
|
|
53
|
+
* validateConfig(config);
|
|
54
|
+
* } catch (error) {
|
|
55
|
+
* console.error('Invalid config:', error.message);
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export function validateConfig(config: unknown): void {
|
|
60
|
+
if (!config || typeof config !== 'object') {
|
|
61
|
+
throw new Error('Analytics config must be an object');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const typedConfig = config as AnalyticsConfig;
|
|
65
|
+
|
|
66
|
+
if (!typedConfig.providers || typeof typedConfig.providers !== 'object') {
|
|
67
|
+
throw new Error('Analytics config must have a providers object');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
for (const [providerName, providerConfig] of Object.entries(typedConfig.providers)) {
|
|
71
|
+
validateProviderConfig(providerName, providerConfig);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Validates a single provider configuration.
|
|
77
|
+
*
|
|
78
|
+
* Checks that all required fields for the provider are present in the configuration.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} providerName - Name of the provider to validate
|
|
81
|
+
* @param {ProviderConfig} config - Provider configuration to validate
|
|
82
|
+
* @throws {Error} If required fields are missing
|
|
83
|
+
*
|
|
84
|
+
* @internal
|
|
85
|
+
*/
|
|
86
|
+
function validateProviderConfig(providerName: string, config: ProviderConfig): void {
|
|
87
|
+
const requiredFields = PROVIDER_REQUIREMENTS[providerName] ?? [];
|
|
88
|
+
|
|
89
|
+
for (const field of requiredFields) {
|
|
90
|
+
if (!config[field as keyof ProviderConfig]) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
`Provider '${providerName}' missing required field '${field}'. ` +
|
|
93
|
+
`Remove the provider from config or provide the required field.`,
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Configuration builder interface.
|
|
101
|
+
*
|
|
102
|
+
* Provides a fluent API for building analytics configurations programmatically.
|
|
103
|
+
* Methods return `this` to enable method chaining.
|
|
104
|
+
*/
|
|
105
|
+
export interface ConfigBuilder {
|
|
106
|
+
addConsole(options?: Record<string, unknown>): ConfigBuilder;
|
|
107
|
+
addPostHog(apiKey: string, options?: Record<string, unknown>): ConfigBuilder;
|
|
108
|
+
addProvider(name: string, config: ProviderConfig): ConfigBuilder;
|
|
109
|
+
addSegment(writeKey: string, options?: Record<string, unknown>): ConfigBuilder;
|
|
110
|
+
addVercel(options?: Record<string, unknown>): ConfigBuilder;
|
|
111
|
+
build(): AnalyticsConfig;
|
|
112
|
+
providers: Record<string, ProviderConfig>;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Creates a fluent configuration builder.
|
|
117
|
+
*
|
|
118
|
+
* Returns a builder instance that allows chaining provider configuration methods.
|
|
119
|
+
* Call `build()` at the end to get the final configuration object.
|
|
120
|
+
*
|
|
121
|
+
* @returns {ConfigBuilder} Configuration builder instance
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const config = createConfigBuilder()
|
|
126
|
+
* .addPostHog('phc_xxx')
|
|
127
|
+
* .addSegment('xxx')
|
|
128
|
+
* .addConsole({ pretty: true })
|
|
129
|
+
* .build();
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export function createConfigBuilder(): ConfigBuilder {
|
|
133
|
+
const builder: ConfigBuilder = {
|
|
134
|
+
providers: {},
|
|
135
|
+
|
|
136
|
+
addProvider(name: string, config: ProviderConfig): ConfigBuilder {
|
|
137
|
+
this.providers[name] = config;
|
|
138
|
+
return this;
|
|
139
|
+
},
|
|
140
|
+
|
|
141
|
+
addSegment(writeKey: string, options?: Record<string, unknown>): ConfigBuilder {
|
|
142
|
+
return this.addProvider('segment', { options, writeKey });
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
addPostHog(apiKey: string, options?: Record<string, unknown>): ConfigBuilder {
|
|
146
|
+
return this.addProvider('posthog', { apiKey, options });
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
addVercel(options?: Record<string, unknown>): ConfigBuilder {
|
|
150
|
+
return this.addProvider('vercel', { options });
|
|
151
|
+
},
|
|
152
|
+
|
|
153
|
+
addConsole(options?: Record<string, unknown>): ConfigBuilder {
|
|
154
|
+
return this.addProvider('console', { options });
|
|
155
|
+
},
|
|
156
|
+
|
|
157
|
+
build(): AnalyticsConfig {
|
|
158
|
+
const config = { providers: { ...this.providers } };
|
|
159
|
+
validateConfig(config);
|
|
160
|
+
return config;
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
return builder;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Options for client-side configuration.
|
|
169
|
+
*
|
|
170
|
+
* Allows explicit configuration values to be passed instead of reading from
|
|
171
|
+
* environment variables. Useful for client-side code where environment variables
|
|
172
|
+
* may not be available.
|
|
173
|
+
*/
|
|
174
|
+
export interface AnalyticsConfigOptions {
|
|
175
|
+
isDevelopment?: boolean;
|
|
176
|
+
isStaging?: boolean;
|
|
177
|
+
segmentWriteKey?: string;
|
|
178
|
+
posthogApiKey?: string;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Get analytics configuration
|
|
183
|
+
*
|
|
184
|
+
* When called with options (client-safe), uses the provided values.
|
|
185
|
+
* When called without options (server), reads from process.env.
|
|
186
|
+
*
|
|
187
|
+
* @param options - Optional configuration options for client-side usage
|
|
188
|
+
* @returns Analytics configuration object
|
|
189
|
+
*
|
|
190
|
+
* @example Server usage (reads from process.env):
|
|
191
|
+
* ```typescript
|
|
192
|
+
* const config = getAnalyticsConfig();
|
|
193
|
+
* ```
|
|
194
|
+
*
|
|
195
|
+
* @example Client usage (explicit options):
|
|
196
|
+
* ```typescript
|
|
197
|
+
* const config = getAnalyticsConfig({
|
|
198
|
+
* isDevelopment: process.env.NODE_ENV === 'development',
|
|
199
|
+
* posthogApiKey: process.env.NEXT_PUBLIC_POSTHOG_KEY,
|
|
200
|
+
* });
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
export function getAnalyticsConfig(options?: AnalyticsConfigOptions): AnalyticsConfig {
|
|
204
|
+
const builder = createConfigBuilder();
|
|
205
|
+
|
|
206
|
+
// Determine environment from options or process.env
|
|
207
|
+
const isDevelopment = options?.isDevelopment ?? process.env.NODE_ENV === 'development';
|
|
208
|
+
const isStaging = options?.isStaging ?? process.env.APP_ENV === 'staging';
|
|
209
|
+
|
|
210
|
+
// Get API keys from options or process.env
|
|
211
|
+
const posthogApiKey = options?.posthogApiKey ?? process.env.POSTHOG_API_KEY;
|
|
212
|
+
const segmentWriteKey = options?.segmentWriteKey ?? process.env.SEGMENT_WRITE_KEY;
|
|
213
|
+
|
|
214
|
+
// Development - minimal providers
|
|
215
|
+
if (isDevelopment) {
|
|
216
|
+
builder.addConsole({
|
|
217
|
+
prefix: '[Dev Analytics]',
|
|
218
|
+
pretty: true,
|
|
219
|
+
});
|
|
220
|
+
return builder.build();
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Staging - selective providers
|
|
224
|
+
if (isStaging) {
|
|
225
|
+
if (posthogApiKey) {
|
|
226
|
+
builder.addPostHog(posthogApiKey);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
builder.addConsole({
|
|
230
|
+
prefix: '[Staging Analytics]',
|
|
231
|
+
pretty: true,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
return builder.build();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Production - multiple providers
|
|
238
|
+
if (segmentWriteKey) {
|
|
239
|
+
builder.addSegment(segmentWriteKey);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (posthogApiKey) {
|
|
243
|
+
builder.addPostHog(posthogApiKey);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Add Vercel Analytics in production
|
|
247
|
+
builder.addVercel();
|
|
248
|
+
|
|
249
|
+
return builder.build();
|
|
250
|
+
}
|