@aranova/tracking-next 0.2.3 → 0.4.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/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import { C as ConsentState, T as TrackingInstallSurface, a as TrackingClientContext, b as TrackingParams, c as TrackingEventCreatePayload, d as TrackingSessionUpsertPayload } from './server-BMTa7njW.js';
2
- export { e as TrackingInitConfig, g as getTrackingParamsServer } from './server-BMTa7njW.js';
1
+ import { C as ConsentState, T as TrackingInstallSurface, a as TrackingClientContext, b as TrackingParams, c as TrackingEventCreatePayload, d as TrackingSessionUpsertPayload } from './types-Gp0ioRiQ.js';
2
+ export { e as TrackingInitConfig } from './types-Gp0ioRiQ.js';
3
+ import { z } from 'zod';
3
4
  import * as react_jsx_runtime from 'react/jsx-runtime';
4
- export { createTrackingMiddleware } from './middleware.js';
5
- import 'next/server';
5
+ import { ReactNode } from 'react';
6
6
 
7
7
  type GtagConsentValue = 'granted' | 'denied';
8
8
  declare function getConsentState(): ConsentState;
@@ -25,6 +25,7 @@ interface TrackingSessionInput {
25
25
  consentState?: Record<string, unknown> | null;
26
26
  firstPage?: string | null;
27
27
  sessionId: string;
28
+ visitorId?: string | null;
28
29
  }
29
30
  declare function createTrackingClientContext(surface: TrackingInstallSurface, input?: TrackingContextInput): TrackingClientContext;
30
31
  declare function createTrackingSessionUpsertPayload(trackingParams: TrackingParams, input: TrackingSessionInput, context: TrackingClientContext): TrackingSessionUpsertPayload;
@@ -33,6 +34,472 @@ declare function createTrackingEventCreatePayload(trackingParams: TrackingParams
33
34
  declare const TRACKING_PARAM_KEYS: readonly ["gclid", "fbclid", "utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
34
35
  declare function captureTrackingParamsFromLocation(url?: string, maxAgeSeconds?: number): TrackingParams;
35
36
 
37
+ interface TrackEventInput {
38
+ eventType: string;
39
+ pageUrl?: string | null;
40
+ metadata?: Record<string, unknown> | null;
41
+ occurredAt?: Date | string | null;
42
+ }
43
+ interface TrackingClient {
44
+ trackEvent: (input: TrackEventInput) => void;
45
+ flush: () => Promise<void>;
46
+ getSessionId: () => string;
47
+ getVisitorId: () => string;
48
+ destroy: () => void;
49
+ }
50
+
51
+ declare const pageViewMetadataSchema: z.ZodObject<{
52
+ page: z.ZodObject<{
53
+ title: z.ZodNullable<z.ZodString>;
54
+ path: z.ZodString;
55
+ search: z.ZodString;
56
+ hash: z.ZodString;
57
+ }, "strip", z.ZodTypeAny, {
58
+ search: string;
59
+ title: string | null;
60
+ path: string;
61
+ hash: string;
62
+ }, {
63
+ search: string;
64
+ title: string | null;
65
+ path: string;
66
+ hash: string;
67
+ }>;
68
+ referrer: z.ZodNullable<z.ZodString>;
69
+ viewport: z.ZodOptional<z.ZodNullable<z.ZodObject<{
70
+ w: z.ZodNumber;
71
+ h: z.ZodNumber;
72
+ }, "strip", z.ZodTypeAny, {
73
+ w: number;
74
+ h: number;
75
+ }, {
76
+ w: number;
77
+ h: number;
78
+ }>>>;
79
+ }, "strict", z.ZodTypeAny, {
80
+ page: {
81
+ search: string;
82
+ title: string | null;
83
+ path: string;
84
+ hash: string;
85
+ };
86
+ referrer: string | null;
87
+ viewport?: {
88
+ w: number;
89
+ h: number;
90
+ } | null | undefined;
91
+ }, {
92
+ page: {
93
+ search: string;
94
+ title: string | null;
95
+ path: string;
96
+ hash: string;
97
+ };
98
+ referrer: string | null;
99
+ viewport?: {
100
+ w: number;
101
+ h: number;
102
+ } | null | undefined;
103
+ }>;
104
+ type PageViewMetadata = z.infer<typeof pageViewMetadataSchema>;
105
+ declare const pageViewConfigSchema: z.ZodObject<{}, "strict", z.ZodTypeAny, {}, {}>;
106
+ type PageViewConfig = z.infer<typeof pageViewConfigSchema>;
107
+
108
+ declare const contactPageVisitMetadataSchema: z.ZodObject<{
109
+ page: z.ZodObject<{
110
+ path: z.ZodString;
111
+ }, "strip", z.ZodTypeAny, {
112
+ path: string;
113
+ }, {
114
+ path: string;
115
+ }>;
116
+ }, "strict", z.ZodTypeAny, {
117
+ page: {
118
+ path: string;
119
+ };
120
+ }, {
121
+ page: {
122
+ path: string;
123
+ };
124
+ }>;
125
+ type ContactPageVisitMetadata = z.infer<typeof contactPageVisitMetadataSchema>;
126
+ declare const contactPageVisitConfigSchema: z.ZodObject<{
127
+ pathPattern: z.ZodType<RegExp, z.ZodTypeDef, RegExp>;
128
+ }, "strict", z.ZodTypeAny, {
129
+ pathPattern: RegExp;
130
+ }, {
131
+ pathPattern: RegExp;
132
+ }>;
133
+ type ContactPageVisitConfig = z.infer<typeof contactPageVisitConfigSchema>;
134
+
135
+ declare const formSubmitMetadataSchema: z.ZodObject<{
136
+ form: z.ZodObject<{
137
+ id: z.ZodString;
138
+ action: z.ZodNullable<z.ZodString>;
139
+ }, "strip", z.ZodTypeAny, {
140
+ id: string;
141
+ action: string | null;
142
+ }, {
143
+ id: string;
144
+ action: string | null;
145
+ }>;
146
+ page: z.ZodObject<{
147
+ path: z.ZodString;
148
+ }, "strip", z.ZodTypeAny, {
149
+ path: string;
150
+ }, {
151
+ path: string;
152
+ }>;
153
+ }, "strict", z.ZodTypeAny, {
154
+ form: {
155
+ id: string;
156
+ action: string | null;
157
+ };
158
+ page: {
159
+ path: string;
160
+ };
161
+ }, {
162
+ form: {
163
+ id: string;
164
+ action: string | null;
165
+ };
166
+ page: {
167
+ path: string;
168
+ };
169
+ }>;
170
+ type FormSubmitMetadata = z.infer<typeof formSubmitMetadataSchema>;
171
+ declare const formSubmitConfigSchema: z.ZodObject<{}, "strict", z.ZodTypeAny, {}, {}>;
172
+ type FormSubmitConfig = z.infer<typeof formSubmitConfigSchema>;
173
+
174
+ declare const phoneClickMetadataSchema: z.ZodObject<{
175
+ element: z.ZodObject<{
176
+ tag: z.ZodString;
177
+ text: z.ZodNullable<z.ZodString>;
178
+ href: z.ZodString;
179
+ }, "strip", z.ZodTypeAny, {
180
+ text: string | null;
181
+ tag: string;
182
+ href: string;
183
+ }, {
184
+ text: string | null;
185
+ tag: string;
186
+ href: string;
187
+ }>;
188
+ page: z.ZodObject<{
189
+ path: z.ZodString;
190
+ }, "strip", z.ZodTypeAny, {
191
+ path: string;
192
+ }, {
193
+ path: string;
194
+ }>;
195
+ }, "strict", z.ZodTypeAny, {
196
+ page: {
197
+ path: string;
198
+ };
199
+ element: {
200
+ text: string | null;
201
+ tag: string;
202
+ href: string;
203
+ };
204
+ }, {
205
+ page: {
206
+ path: string;
207
+ };
208
+ element: {
209
+ text: string | null;
210
+ tag: string;
211
+ href: string;
212
+ };
213
+ }>;
214
+ type PhoneClickMetadata = z.infer<typeof phoneClickMetadataSchema>;
215
+ declare const phoneClickConfigSchema: z.ZodObject<{}, "strict", z.ZodTypeAny, {}, {}>;
216
+ type PhoneClickConfig = z.infer<typeof phoneClickConfigSchema>;
217
+
218
+ declare const timeOnSiteMetadataSchema: z.ZodObject<{
219
+ duration_ms: z.ZodNumber;
220
+ page: z.ZodObject<{
221
+ path: z.ZodString;
222
+ }, "strip", z.ZodTypeAny, {
223
+ path: string;
224
+ }, {
225
+ path: string;
226
+ }>;
227
+ }, "strict", z.ZodTypeAny, {
228
+ page: {
229
+ path: string;
230
+ };
231
+ duration_ms: number;
232
+ }, {
233
+ page: {
234
+ path: string;
235
+ };
236
+ duration_ms: number;
237
+ }>;
238
+ type TimeOnSiteMetadata = z.infer<typeof timeOnSiteMetadataSchema>;
239
+ declare const timeOnSiteConfigSchema: z.ZodObject<{
240
+ thresholdSeconds: z.ZodNumber;
241
+ }, "strict", z.ZodTypeAny, {
242
+ thresholdSeconds: number;
243
+ }, {
244
+ thresholdSeconds: number;
245
+ }>;
246
+ type TimeOnSiteConfig = z.infer<typeof timeOnSiteConfigSchema>;
247
+
248
+ declare const EVENT_REGISTRY: {
249
+ readonly page_view: {
250
+ readonly kind: "automatic";
251
+ readonly metadataSchema: z.ZodObject<{
252
+ page: z.ZodObject<{
253
+ title: z.ZodNullable<z.ZodString>;
254
+ path: z.ZodString;
255
+ search: z.ZodString;
256
+ hash: z.ZodString;
257
+ }, "strip", z.ZodTypeAny, {
258
+ search: string;
259
+ title: string | null;
260
+ path: string;
261
+ hash: string;
262
+ }, {
263
+ search: string;
264
+ title: string | null;
265
+ path: string;
266
+ hash: string;
267
+ }>;
268
+ referrer: z.ZodNullable<z.ZodString>;
269
+ viewport: z.ZodOptional<z.ZodNullable<z.ZodObject<{
270
+ w: z.ZodNumber;
271
+ h: z.ZodNumber;
272
+ }, "strip", z.ZodTypeAny, {
273
+ w: number;
274
+ h: number;
275
+ }, {
276
+ w: number;
277
+ h: number;
278
+ }>>>;
279
+ }, "strict", z.ZodTypeAny, {
280
+ page: {
281
+ search: string;
282
+ title: string | null;
283
+ path: string;
284
+ hash: string;
285
+ };
286
+ referrer: string | null;
287
+ viewport?: {
288
+ w: number;
289
+ h: number;
290
+ } | null | undefined;
291
+ }, {
292
+ page: {
293
+ search: string;
294
+ title: string | null;
295
+ path: string;
296
+ hash: string;
297
+ };
298
+ referrer: string | null;
299
+ viewport?: {
300
+ w: number;
301
+ h: number;
302
+ } | null | undefined;
303
+ }>;
304
+ readonly configSchema: z.ZodObject<{}, "strict", z.ZodTypeAny, {}, {}>;
305
+ };
306
+ readonly time_on_site: {
307
+ readonly kind: "automatic";
308
+ readonly metadataSchema: z.ZodObject<{
309
+ duration_ms: z.ZodNumber;
310
+ page: z.ZodObject<{
311
+ path: z.ZodString;
312
+ }, "strip", z.ZodTypeAny, {
313
+ path: string;
314
+ }, {
315
+ path: string;
316
+ }>;
317
+ }, "strict", z.ZodTypeAny, {
318
+ page: {
319
+ path: string;
320
+ };
321
+ duration_ms: number;
322
+ }, {
323
+ page: {
324
+ path: string;
325
+ };
326
+ duration_ms: number;
327
+ }>;
328
+ readonly configSchema: z.ZodObject<{
329
+ thresholdSeconds: z.ZodNumber;
330
+ }, "strict", z.ZodTypeAny, {
331
+ thresholdSeconds: number;
332
+ }, {
333
+ thresholdSeconds: number;
334
+ }>;
335
+ };
336
+ readonly contact_page_visit: {
337
+ readonly kind: "automatic";
338
+ readonly metadataSchema: z.ZodObject<{
339
+ page: z.ZodObject<{
340
+ path: z.ZodString;
341
+ }, "strip", z.ZodTypeAny, {
342
+ path: string;
343
+ }, {
344
+ path: string;
345
+ }>;
346
+ }, "strict", z.ZodTypeAny, {
347
+ page: {
348
+ path: string;
349
+ };
350
+ }, {
351
+ page: {
352
+ path: string;
353
+ };
354
+ }>;
355
+ readonly configSchema: z.ZodObject<{
356
+ pathPattern: z.ZodType<RegExp, z.ZodTypeDef, RegExp>;
357
+ }, "strict", z.ZodTypeAny, {
358
+ pathPattern: RegExp;
359
+ }, {
360
+ pathPattern: RegExp;
361
+ }>;
362
+ };
363
+ readonly form_submit: {
364
+ readonly kind: "manual";
365
+ readonly metadataSchema: z.ZodObject<{
366
+ form: z.ZodObject<{
367
+ id: z.ZodString;
368
+ action: z.ZodNullable<z.ZodString>;
369
+ }, "strip", z.ZodTypeAny, {
370
+ id: string;
371
+ action: string | null;
372
+ }, {
373
+ id: string;
374
+ action: string | null;
375
+ }>;
376
+ page: z.ZodObject<{
377
+ path: z.ZodString;
378
+ }, "strip", z.ZodTypeAny, {
379
+ path: string;
380
+ }, {
381
+ path: string;
382
+ }>;
383
+ }, "strict", z.ZodTypeAny, {
384
+ form: {
385
+ id: string;
386
+ action: string | null;
387
+ };
388
+ page: {
389
+ path: string;
390
+ };
391
+ }, {
392
+ form: {
393
+ id: string;
394
+ action: string | null;
395
+ };
396
+ page: {
397
+ path: string;
398
+ };
399
+ }>;
400
+ readonly configSchema: z.ZodObject<{}, "strict", z.ZodTypeAny, {}, {}>;
401
+ };
402
+ readonly phone_click: {
403
+ readonly kind: "manual";
404
+ readonly metadataSchema: z.ZodObject<{
405
+ element: z.ZodObject<{
406
+ tag: z.ZodString;
407
+ text: z.ZodNullable<z.ZodString>;
408
+ href: z.ZodString;
409
+ }, "strip", z.ZodTypeAny, {
410
+ text: string | null;
411
+ tag: string;
412
+ href: string;
413
+ }, {
414
+ text: string | null;
415
+ tag: string;
416
+ href: string;
417
+ }>;
418
+ page: z.ZodObject<{
419
+ path: z.ZodString;
420
+ }, "strip", z.ZodTypeAny, {
421
+ path: string;
422
+ }, {
423
+ path: string;
424
+ }>;
425
+ }, "strict", z.ZodTypeAny, {
426
+ page: {
427
+ path: string;
428
+ };
429
+ element: {
430
+ text: string | null;
431
+ tag: string;
432
+ href: string;
433
+ };
434
+ }, {
435
+ page: {
436
+ path: string;
437
+ };
438
+ element: {
439
+ text: string | null;
440
+ tag: string;
441
+ href: string;
442
+ };
443
+ }>;
444
+ readonly configSchema: z.ZodObject<{}, "strict", z.ZodTypeAny, {}, {}>;
445
+ };
446
+ };
447
+ type EventName = keyof typeof EVENT_REGISTRY;
448
+ type AutomaticEventName = {
449
+ [K in EventName]: (typeof EVENT_REGISTRY)[K]['kind'] extends 'automatic' ? K : never;
450
+ }[EventName];
451
+ type ManualEventName = {
452
+ [K in EventName]: (typeof EVENT_REGISTRY)[K]['kind'] extends 'manual' ? K : never;
453
+ }[EventName];
454
+ type MetadataByName = {
455
+ page_view: PageViewMetadata;
456
+ time_on_site: TimeOnSiteMetadata;
457
+ contact_page_visit: ContactPageVisitMetadata;
458
+ form_submit: FormSubmitMetadata;
459
+ phone_click: PhoneClickMetadata;
460
+ };
461
+ type ConfigByName = {
462
+ page_view: PageViewConfig;
463
+ time_on_site: TimeOnSiteConfig;
464
+ contact_page_visit: ContactPageVisitConfig;
465
+ form_submit: FormSubmitConfig;
466
+ phone_click: PhoneClickConfig;
467
+ };
468
+ type EventMetadata<K extends EventName> = MetadataByName[K];
469
+ type EventConfig<K extends EventName> = ConfigByName[K];
470
+ type TriggerRegistryConfig = {
471
+ automatic: {
472
+ page_view: EventConfig<'page_view'>;
473
+ } & Partial<{
474
+ time_on_site: EventConfig<'time_on_site'>;
475
+ contact_page_visit: EventConfig<'contact_page_visit'>;
476
+ }>;
477
+ manual?: Partial<{
478
+ form_submit: EventConfig<'form_submit'>;
479
+ phone_click: EventConfig<'phone_click'>;
480
+ }>;
481
+ };
482
+ type RegisteredManualEvents<TRegistry extends TriggerRegistryConfig> = Extract<keyof NonNullable<TRegistry['manual']>, ManualEventName>;
483
+ type RegisteredAutomaticEvents<TRegistry extends TriggerRegistryConfig> = Extract<keyof TRegistry['automatic'], AutomaticEventName>;
484
+
485
+ interface TypedTrackEventOptions {
486
+ /** Override the page URL captured automatically. Rarely needed. */
487
+ pageUrl?: string | null;
488
+ /** Event timestamp override. Defaults to "now" at queue time. */
489
+ occurredAt?: Date | string | null;
490
+ }
491
+ interface TypedTrackingClient<TRegistry extends TriggerRegistryConfig> {
492
+ /**
493
+ * Fire a manually-registered event. The event name must be present in
494
+ * the registry's `manual` map and the metadata must match that event's
495
+ * canonical Zod-derived shape.
496
+ */
497
+ trackEvent<K extends RegisteredManualEvents<TRegistry>>(eventType: K, metadata: EventMetadata<K>, options?: TypedTrackEventOptions): void;
498
+ flush(): Promise<void>;
499
+ getSessionId(): string;
500
+ getVisitorId(): string;
501
+ }
502
+
36
503
  declare function ConsentBanner(): react_jsx_runtime.JSX.Element | null;
37
504
 
38
505
  declare function useGclid(): string | null;
@@ -44,4 +511,31 @@ interface GoogleAdsTrackingProps {
44
511
  }
45
512
  declare function GoogleAdsTracking({ gtagId }: GoogleAdsTrackingProps): react_jsx_runtime.JSX.Element;
46
513
 
47
- export { ConsentBanner, ConsentState, GoogleAdsTracking, type GoogleAdsTrackingProps, TRACKING_PARAM_KEYS, TrackingClientContext, TrackingEventCreatePayload, TrackingInstallSurface, TrackingParams, TrackingSessionUpsertPayload, captureTrackingParamsFromLocation, createTrackingClientContext, createTrackingEventCreatePayload, createTrackingSessionUpsertPayload, getConsentState, setConsentState, useConsentState, useGclid, useTrackingParams };
514
+ interface CreateTrackingOptions<TRegistry extends TriggerRegistryConfig> {
515
+ apiKey: string;
516
+ endpoint: string;
517
+ triggers: TRegistry;
518
+ debug?: boolean;
519
+ }
520
+ interface TrackingProviderProps {
521
+ children: ReactNode;
522
+ }
523
+ interface CreateTrackingResult<TRegistry extends TriggerRegistryConfig> {
524
+ TrackingProvider: (props: TrackingProviderProps) => ReactNode;
525
+ useTracking: () => TypedTrackingClient<TRegistry>;
526
+ }
527
+ /**
528
+ * Next.js App Router version of the createTracking factory. Uses
529
+ * `usePathname` + `useSearchParams` from `next/navigation` for SPA route
530
+ * detection rather than patching `history.pushState`, because Next's
531
+ * router does not always go through the History API for transitions.
532
+ *
533
+ * Usage is identical to the React factory — call this once at app
534
+ * startup and export the returned `TrackingProvider` + `useTracking`
535
+ * from your own module, then import them everywhere else. `GoogleAdsTracking`
536
+ * is still imported separately and placed in the root layout's `<head>`
537
+ * when the client runs paid ads.
538
+ */
539
+ declare function createTracking<TRegistry extends TriggerRegistryConfig>(options: CreateTrackingOptions<TRegistry>): CreateTrackingResult<TRegistry>;
540
+
541
+ export { type AutomaticEventName, ConsentBanner, ConsentState, type ContactPageVisitConfig, type ContactPageVisitMetadata, type CreateTrackingOptions, type CreateTrackingResult, type EventConfig, type EventMetadata, type EventName, type FormSubmitConfig, type FormSubmitMetadata, GoogleAdsTracking, type GoogleAdsTrackingProps, type ManualEventName, type PageViewConfig, type PageViewMetadata, type PhoneClickConfig, type PhoneClickMetadata, type RegisteredAutomaticEvents, type RegisteredManualEvents, TRACKING_PARAM_KEYS, type TimeOnSiteConfig, type TimeOnSiteMetadata, type TrackingClient, TrackingClientContext, TrackingEventCreatePayload, TrackingInstallSurface, TrackingParams, type TrackingProviderProps, TrackingSessionUpsertPayload, type TriggerRegistryConfig, type TypedTrackEventOptions, type TypedTrackingClient, captureTrackingParamsFromLocation, createTracking, createTrackingClientContext, createTrackingEventCreatePayload, createTrackingSessionUpsertPayload, getConsentState, setConsentState, useConsentState, useGclid, useTrackingParams };