@codefast/tracking 0.5.0-canary.5 → 0.5.0-canary.7
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/CHANGELOG.md +469 -0
- package/README.md +176 -126
- package/dist/adapters/tanstack-start.d.ts +80 -0
- package/dist/adapters/tanstack-start.d.ts.map +1 -0
- package/dist/adapters/tanstack-start.js +74 -0
- package/dist/adapters/tanstack-start.js.map +1 -0
- package/dist/client/ad-framework-consent.d.ts +62 -0
- package/dist/client/ad-framework-consent.d.ts.map +1 -0
- package/dist/client/ad-framework-consent.js +40 -0
- package/dist/client/ad-framework-consent.js.map +1 -0
- package/dist/client/browser-cookie.d.ts +9 -0
- package/dist/client/browser-cookie.d.ts.map +1 -0
- package/dist/client/browser-cookie.js +15 -0
- package/dist/client/browser-cookie.js.map +1 -0
- package/dist/client/consent-runtime.d.ts +53 -0
- package/dist/client/consent-runtime.d.ts.map +1 -0
- package/dist/client/consent-runtime.js +33 -0
- package/dist/client/consent-runtime.js.map +1 -0
- package/dist/client/{consent-storage.d.mts → consent-storage.d.ts} +8 -6
- package/dist/client/consent-storage.d.ts.map +1 -0
- package/dist/client/consent-storage.js +109 -0
- package/dist/client/consent-storage.js.map +1 -0
- package/dist/client/consent-withdrawal.d.ts +18 -0
- package/dist/client/consent-withdrawal.d.ts.map +1 -0
- package/dist/client/consent-withdrawal.js +17 -0
- package/dist/client/consent-withdrawal.js.map +1 -0
- package/dist/client/cookie-anonymous-id.d.ts +41 -0
- package/dist/client/cookie-anonymous-id.d.ts.map +1 -0
- package/dist/client/cookie-anonymous-id.js +65 -0
- package/dist/client/cookie-anonymous-id.js.map +1 -0
- package/dist/client/create-client-tracker.d.ts +60 -0
- package/dist/client/create-client-tracker.d.ts.map +1 -0
- package/dist/client/create-client-tracker.js +91 -0
- package/dist/client/create-client-tracker.js.map +1 -0
- package/dist/client/gpc.d.ts +10 -0
- package/dist/client/gpc.d.ts.map +1 -0
- package/dist/client/gpc.js +12 -0
- package/dist/client/gpc.js.map +1 -0
- package/dist/client/initial-consent-store.d.ts +63 -0
- package/dist/client/initial-consent-store.d.ts.map +1 -0
- package/dist/client/initial-consent-store.js +138 -0
- package/dist/client/initial-consent-store.js.map +1 -0
- package/dist/client/is-analytics-allowed.d.ts +26 -0
- package/dist/client/is-analytics-allowed.d.ts.map +1 -0
- package/dist/client/is-analytics-allowed.js +22 -0
- package/dist/client/is-analytics-allowed.js.map +1 -0
- package/dist/client/server-persisted-anonymous-id.d.ts +30 -0
- package/dist/client/server-persisted-anonymous-id.d.ts.map +1 -0
- package/dist/client/server-persisted-anonymous-id.js +72 -0
- package/dist/client/server-persisted-anonymous-id.js.map +1 -0
- package/dist/core/consent-config.d.ts +24 -0
- package/dist/core/consent-config.d.ts.map +1 -0
- package/dist/core/consent-config.js +9 -0
- package/dist/core/consent-config.js.map +1 -0
- package/dist/core/consent-receipt.d.ts +71 -0
- package/dist/core/consent-receipt.d.ts.map +1 -0
- package/dist/core/consent-receipt.js +39 -0
- package/dist/core/consent-receipt.js.map +1 -0
- package/dist/core/consent.d.ts +182 -0
- package/dist/core/consent.d.ts.map +1 -0
- package/dist/core/consent.js +159 -0
- package/dist/core/consent.js.map +1 -0
- package/dist/core/cookie.d.ts +15 -0
- package/dist/core/cookie.d.ts.map +1 -0
- package/dist/core/cookie.js +26 -0
- package/dist/core/cookie.js.map +1 -0
- package/dist/core/destination.d.ts +29 -0
- package/dist/core/destination.d.ts.map +1 -0
- package/dist/core/destination.js +2 -0
- package/dist/core/destination.js.map +1 -0
- package/dist/core/event-catalog.d.ts +35 -0
- package/dist/core/event-catalog.d.ts.map +1 -0
- package/dist/core/event-catalog.js +30 -0
- package/dist/core/event-catalog.js.map +1 -0
- package/dist/core/event-id.d.ts +7 -0
- package/dist/core/event-id.d.ts.map +1 -0
- package/dist/core/event-id.js +9 -0
- package/dist/core/event-id.js.map +1 -0
- package/dist/core/tracked-event.d.ts +15 -0
- package/dist/core/tracked-event.d.ts.map +1 -0
- package/dist/core/tracked-event.js +2 -0
- package/dist/core/tracked-event.js.map +1 -0
- package/dist/destinations/ad-consent.d.ts +23 -0
- package/dist/destinations/ad-consent.d.ts.map +1 -0
- package/dist/destinations/ad-consent.js +12 -0
- package/dist/destinations/ad-consent.js.map +1 -0
- package/dist/destinations/google-analytics.d.ts +148 -0
- package/dist/destinations/google-analytics.d.ts.map +1 -0
- package/dist/destinations/google-analytics.js +156 -0
- package/dist/destinations/google-analytics.js.map +1 -0
- package/dist/destinations/google-consent.d.ts +62 -0
- package/dist/destinations/google-consent.d.ts.map +1 -0
- package/dist/destinations/google-consent.js +109 -0
- package/dist/destinations/google-consent.js.map +1 -0
- package/dist/destinations/meta.d.ts +63 -0
- package/dist/destinations/meta.d.ts.map +1 -0
- package/dist/destinations/meta.js +44 -0
- package/dist/destinations/meta.js.map +1 -0
- package/dist/destinations/microsoft-uet.d.ts +56 -0
- package/dist/destinations/microsoft-uet.d.ts.map +1 -0
- package/dist/destinations/microsoft-uet.js +41 -0
- package/dist/destinations/microsoft-uet.js.map +1 -0
- package/dist/destinations/shared.d.ts +18 -0
- package/dist/destinations/shared.d.ts.map +1 -0
- package/dist/destinations/shared.js +19 -0
- package/dist/destinations/shared.js.map +1 -0
- package/dist/destinations/tiktok.d.ts +55 -0
- package/dist/destinations/tiktok.d.ts.map +1 -0
- package/dist/destinations/tiktok.js +40 -0
- package/dist/destinations/tiktok.js.map +1 -0
- package/dist/destinations/vercel-analytics.d.ts +25 -0
- package/dist/destinations/vercel-analytics.d.ts.map +1 -0
- package/dist/destinations/vercel-analytics.js +23 -0
- package/dist/destinations/vercel-analytics.js.map +1 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/react/consent-banner.d.ts +155 -0
- package/dist/react/consent-banner.d.ts.map +1 -0
- package/dist/react/consent-banner.js +171 -0
- package/dist/react/consent-banner.js.map +1 -0
- package/dist/react/gtag-consent-bootstrap.d.ts +17 -0
- package/dist/react/gtag-consent-bootstrap.d.ts.map +1 -0
- package/dist/react/gtag-consent-bootstrap.js +22 -0
- package/dist/react/gtag-consent-bootstrap.js.map +1 -0
- package/dist/react/use-consent.d.ts +42 -0
- package/dist/react/use-consent.d.ts.map +1 -0
- package/dist/react/use-consent.js +60 -0
- package/dist/react/use-consent.js.map +1 -0
- package/dist/react/use-google-consent-sync.d.ts +20 -0
- package/dist/react/use-google-consent-sync.d.ts.map +1 -0
- package/dist/react/use-google-consent-sync.js +22 -0
- package/dist/react/use-google-consent-sync.js.map +1 -0
- package/dist/react/use-initial-consent.d.ts +11 -0
- package/dist/react/use-initial-consent.d.ts.map +1 -0
- package/dist/react/use-initial-consent.js +18 -0
- package/dist/react/use-initial-consent.js.map +1 -0
- package/dist/server/anonymous-id-cookie.d.ts +67 -0
- package/dist/server/anonymous-id-cookie.d.ts.map +1 -0
- package/dist/server/anonymous-id-cookie.js +71 -0
- package/dist/server/anonymous-id-cookie.js.map +1 -0
- package/dist/server/consent-receipt-store.d.ts +56 -0
- package/dist/server/consent-receipt-store.d.ts.map +1 -0
- package/dist/server/consent-receipt-store.js +44 -0
- package/dist/server/consent-receipt-store.js.map +1 -0
- package/dist/server/consent-receipt.d.ts +41 -0
- package/dist/server/consent-receipt.d.ts.map +1 -0
- package/dist/server/consent-receipt.js +60 -0
- package/dist/server/consent-receipt.js.map +1 -0
- package/dist/server/initial-consent.d.ts +26 -0
- package/dist/server/initial-consent.d.ts.map +1 -0
- package/dist/server/initial-consent.js +31 -0
- package/dist/server/initial-consent.js.map +1 -0
- package/dist/server/measurement-protocol.d.ts +69 -0
- package/dist/server/measurement-protocol.d.ts.map +1 -0
- package/dist/server/measurement-protocol.js +61 -0
- package/dist/server/measurement-protocol.js.map +1 -0
- package/dist/server/region.d.ts +27 -0
- package/dist/server/region.d.ts.map +1 -0
- package/dist/server/region.js +69 -0
- package/dist/server/region.js.map +1 -0
- package/dist/server/user-deletion.d.ts +64 -0
- package/dist/server/user-deletion.d.ts.map +1 -0
- package/dist/server/user-deletion.js +54 -0
- package/dist/server/user-deletion.js.map +1 -0
- package/dist/tooling/import-protection.d.ts +19 -0
- package/dist/tooling/import-protection.d.ts.map +1 -0
- package/dist/tooling/import-protection.js +23 -0
- package/dist/tooling/import-protection.js.map +1 -0
- package/package.json +205 -111
- package/src/adapters/tanstack-start.ts +135 -0
- package/src/client/ad-framework-consent.ts +86 -0
- package/src/client/browser-cookie.ts +16 -0
- package/src/client/consent-runtime.ts +80 -0
- package/src/client/consent-storage.ts +31 -1
- package/src/client/consent-withdrawal.ts +32 -0
- package/src/client/cookie-anonymous-id.ts +112 -0
- package/src/client/create-client-tracker.ts +128 -86
- package/src/client/gpc.ts +4 -2
- package/src/client/initial-consent-store.ts +218 -0
- package/src/client/is-analytics-allowed.ts +43 -0
- package/src/client/server-persisted-anonymous-id.ts +102 -0
- package/src/core/consent-config.ts +27 -0
- package/src/core/consent-receipt.ts +115 -0
- package/src/core/consent.ts +164 -10
- package/src/core/cookie.ts +29 -0
- package/src/core/destination.ts +17 -8
- package/src/core/event-catalog.ts +29 -15
- package/src/core/event-id.ts +1 -2
- package/src/core/tracked-event.ts +4 -4
- package/src/destinations/ad-consent.ts +29 -0
- package/src/destinations/google-analytics.ts +196 -133
- package/src/destinations/google-consent.ts +164 -0
- package/src/destinations/meta.ts +96 -0
- package/src/destinations/microsoft-uet.ts +86 -0
- package/src/destinations/shared.ts +45 -0
- package/src/destinations/tiktok.ts +85 -0
- package/src/destinations/vercel-analytics.ts +23 -24
- package/src/index.ts +165 -8
- package/src/react/consent-banner.tsx +40 -14
- package/src/react/gtag-consent-bootstrap.tsx +48 -0
- package/src/react/use-consent.ts +53 -45
- package/src/react/use-google-consent-sync.ts +38 -0
- package/src/react/use-initial-consent.ts +22 -0
- package/src/server/anonymous-id-cookie.ts +103 -0
- package/src/server/consent-receipt-store.ts +79 -0
- package/src/server/consent-receipt.ts +89 -0
- package/src/server/initial-consent.ts +46 -0
- package/src/server/measurement-protocol.ts +114 -0
- package/src/server/region.ts +22 -15
- package/src/server/user-deletion.ts +93 -0
- package/src/tooling/import-protection.ts +22 -0
- package/dist/client/consent-storage.mjs +0 -63
- package/dist/client/create-client-tracker.d.mts +0 -37
- package/dist/client/create-client-tracker.mjs +0 -66
- package/dist/client/gpc.d.mts +0 -10
- package/dist/client/gpc.mjs +0 -12
- package/dist/client/index.d.mts +0 -8
- package/dist/client/index.mjs +0 -8
- package/dist/client/lifecycle.d.mts +0 -23
- package/dist/client/lifecycle.mjs +0 -30
- package/dist/client/local-storage.d.mts +0 -13
- package/dist/client/local-storage.mjs +0 -31
- package/dist/client/queue.d.mts +0 -49
- package/dist/client/queue.mjs +0 -71
- package/dist/client/router.d.mts +0 -30
- package/dist/client/router.mjs +0 -17
- package/dist/core/consent.d.mts +0 -89
- package/dist/core/consent.mjs +0 -57
- package/dist/core/destination.d.mts +0 -23
- package/dist/core/destination.mjs +0 -1
- package/dist/core/event-catalog.d.mts +0 -36
- package/dist/core/event-catalog.mjs +0 -12
- package/dist/core/event-id.d.mts +0 -10
- package/dist/core/event-id.mjs +0 -12
- package/dist/core/index.d.mts +0 -6
- package/dist/core/index.mjs +0 -4
- package/dist/core/tracked-event.d.mts +0 -17
- package/dist/core/tracked-event.mjs +0 -1
- package/dist/destinations/ga4-measurement-protocol.d.mts +0 -54
- package/dist/destinations/ga4-measurement-protocol.mjs +0 -89
- package/dist/destinations/google-analytics.d.mts +0 -98
- package/dist/destinations/google-analytics.mjs +0 -126
- package/dist/destinations/http-destination.d.mts +0 -20
- package/dist/destinations/http-destination.mjs +0 -25
- package/dist/destinations/index.d.mts +0 -5
- package/dist/destinations/index.mjs +0 -5
- package/dist/destinations/vercel-analytics.d.mts +0 -13
- package/dist/destinations/vercel-analytics.mjs +0 -30
- package/dist/index.d.mts +0 -6
- package/dist/index.mjs +0 -4
- package/dist/react/consent-banner.d.mts +0 -163
- package/dist/react/consent-banner.mjs +0 -209
- package/dist/react/index.d.mts +0 -3
- package/dist/react/index.mjs +0 -3
- package/dist/react/use-consent.d.mts +0 -49
- package/dist/react/use-consent.mjs +0 -54
- package/dist/server/create-server-tracker.d.mts +0 -42
- package/dist/server/create-server-tracker.mjs +0 -67
- package/dist/server/index.d.mts +0 -3
- package/dist/server/index.mjs +0 -3
- package/dist/server/region.d.mts +0 -20
- package/dist/server/region.mjs +0 -56
- package/src/client/index.ts +0 -17
- package/src/client/lifecycle.ts +0 -50
- package/src/client/local-storage.ts +0 -44
- package/src/client/queue.ts +0 -117
- package/src/client/router.ts +0 -37
- package/src/core/index.ts +0 -24
- package/src/destinations/ga4-measurement-protocol.ts +0 -166
- package/src/destinations/http-destination.ts +0 -33
- package/src/destinations/index.ts +0 -20
- package/src/react/index.ts +0 -29
- package/src/server/create-server-tracker.ts +0 -128
- package/src/server/index.ts +0 -4
package/src/client/queue.ts
DELETED
|
@@ -1,117 +0,0 @@
|
|
|
1
|
-
import type { Destination } from "#/core/destination";
|
|
2
|
-
import type { TrackedEvent } from "#/core/tracked-event";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Persistence backend for the offline queue — a thin wrapper so tests and non-browser
|
|
6
|
-
* environments can swap in an in-memory implementation instead of real `localStorage`.
|
|
7
|
-
*
|
|
8
|
-
* @since 0.5.0-canary.4
|
|
9
|
-
*/
|
|
10
|
-
export interface EventQueueStorage {
|
|
11
|
-
load: () => Array<TrackedEvent>;
|
|
12
|
-
save: (events: Array<TrackedEvent>) => void;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @since 0.5.0-canary.4
|
|
17
|
-
*/
|
|
18
|
-
export interface EventQueueOptions {
|
|
19
|
-
destinations: Array<Destination>;
|
|
20
|
-
flushBatchSize?: number | undefined;
|
|
21
|
-
maxQueueSize?: number | undefined;
|
|
22
|
-
maxRetries?: number | undefined;
|
|
23
|
-
storage: EventQueueStorage;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
interface QueuedEvent {
|
|
27
|
-
attempts: number;
|
|
28
|
-
event: TrackedEvent;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Client-side batching/offline queue: persists across reloads, caps its size
|
|
33
|
-
* (drop-oldest), and retries a failed batch up to `maxRetries` times.
|
|
34
|
-
*
|
|
35
|
-
* @since 0.5.0-canary.4
|
|
36
|
-
*/
|
|
37
|
-
export class EventQueue {
|
|
38
|
-
private readonly destinations: Array<Destination>;
|
|
39
|
-
private readonly flushBatchSize: number;
|
|
40
|
-
private readonly maxQueueSize: number;
|
|
41
|
-
private readonly maxRetries: number;
|
|
42
|
-
private pending: Array<QueuedEvent>;
|
|
43
|
-
private readonly storage: EventQueueStorage;
|
|
44
|
-
|
|
45
|
-
constructor(options: EventQueueOptions) {
|
|
46
|
-
this.destinations = options.destinations;
|
|
47
|
-
this.storage = options.storage;
|
|
48
|
-
this.maxQueueSize = options.maxQueueSize ?? 500;
|
|
49
|
-
this.maxRetries = options.maxRetries ?? 3;
|
|
50
|
-
this.flushBatchSize = options.flushBatchSize ?? 20;
|
|
51
|
-
this.pending = this.storage.load().map((event) => ({ attempts: 0, event }));
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
get size(): number {
|
|
55
|
-
return this.pending.length;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/** Drops every pending event without sending it — used on consent revoke. */
|
|
59
|
-
clear(): void {
|
|
60
|
-
this.pending = [];
|
|
61
|
-
this.persist();
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** Empties the queue synchronously for `navigator.sendBeacon`, which can't be awaited. */
|
|
65
|
-
drain(): Array<TrackedEvent> {
|
|
66
|
-
const events = this.pending.map((queued) => queued.event);
|
|
67
|
-
|
|
68
|
-
this.pending = [];
|
|
69
|
-
this.persist();
|
|
70
|
-
|
|
71
|
-
return events;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
enqueue(event: TrackedEvent): void {
|
|
75
|
-
this.pending.push({ attempts: 0, event });
|
|
76
|
-
|
|
77
|
-
if (this.pending.length > this.maxQueueSize) {
|
|
78
|
-
this.pending = this.pending.slice(this.pending.length - this.maxQueueSize);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
this.persist();
|
|
82
|
-
|
|
83
|
-
if (this.pending.length >= this.flushBatchSize) {
|
|
84
|
-
void this.flush();
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async flush(): Promise<void> {
|
|
89
|
-
if (this.pending.length === 0) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
const batch = this.pending;
|
|
94
|
-
|
|
95
|
-
this.pending = [];
|
|
96
|
-
|
|
97
|
-
const results = await Promise.allSettled(
|
|
98
|
-
batch.map(async (queued) => {
|
|
99
|
-
await Promise.all(this.destinations.map(async (destination) => destination.send(queued.event)));
|
|
100
|
-
}),
|
|
101
|
-
);
|
|
102
|
-
|
|
103
|
-
for (const [index, result] of results.entries()) {
|
|
104
|
-
const queued = batch[index];
|
|
105
|
-
|
|
106
|
-
if (result.status === "rejected" && queued && queued.attempts < this.maxRetries) {
|
|
107
|
-
this.pending.push({ attempts: queued.attempts + 1, event: queued.event });
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
this.persist();
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
private persist(): void {
|
|
115
|
-
this.storage.save(this.pending.map((queued) => queued.event));
|
|
116
|
-
}
|
|
117
|
-
}
|
package/src/client/router.ts
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
import type { ClientTracker } from "#/client/create-client-tracker";
|
|
2
|
-
import type { EventCatalog } from "#/core/event-catalog";
|
|
3
|
-
|
|
4
|
-
interface ResolvedNavigation {
|
|
5
|
-
toLocation: {
|
|
6
|
-
href: string;
|
|
7
|
-
pathname: string;
|
|
8
|
-
};
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* Structurally matches `Router["subscribe"]` from `@tanstack/router-core` for the
|
|
13
|
-
* `"onResolved"` event — duck-typed so this package doesn't depend on TanStack Router.
|
|
14
|
-
*
|
|
15
|
-
* @since 0.5.0-canary.4
|
|
16
|
-
*/
|
|
17
|
-
export interface RouterLike {
|
|
18
|
-
subscribe: (eventType: "onResolved", fn: (event: ResolvedNavigation) => void) => () => void;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* `onResolved` (not `popstate`) is what fires on SPA navigation in TanStack Router;
|
|
23
|
-
* `popstate` only fires for browser back/forward. Page views are low-frequency, so this
|
|
24
|
-
* flushes right away instead of waiting on the batch queue's interval/size threshold —
|
|
25
|
-
* a slow navigation shouldn't leave a pageview stranded if the tab closes right after.
|
|
26
|
-
*
|
|
27
|
-
* @since 0.5.0-canary.4
|
|
28
|
-
*/
|
|
29
|
-
export function attachRouterPageTracking<Catalog extends EventCatalog>(
|
|
30
|
-
tracker: Pick<ClientTracker<Catalog>, "flush" | "page">,
|
|
31
|
-
router: RouterLike,
|
|
32
|
-
): () => void {
|
|
33
|
-
return router.subscribe("onResolved", (event) => {
|
|
34
|
-
tracker.page(event.toLocation.pathname, { href: event.toLocation.href });
|
|
35
|
-
void tracker.flush();
|
|
36
|
-
});
|
|
37
|
-
}
|
package/src/core/index.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
ConsentCategory,
|
|
3
|
-
ConsentDecision,
|
|
4
|
-
ConsentMode,
|
|
5
|
-
ConsentRecord,
|
|
6
|
-
ConsentRegion,
|
|
7
|
-
ConsentStorage,
|
|
8
|
-
} from "#/core/consent";
|
|
9
|
-
export {
|
|
10
|
-
CONSENT_CATEGORIES,
|
|
11
|
-
createConsentDecision,
|
|
12
|
-
isConsentDecision,
|
|
13
|
-
resolveConsentMode,
|
|
14
|
-
resolveDefaultConsent,
|
|
15
|
-
} from "#/core/consent";
|
|
16
|
-
|
|
17
|
-
export type { Destination } from "#/core/destination";
|
|
18
|
-
|
|
19
|
-
export type { EventCatalog, EventDefinition, EventsOf } from "#/core/event-catalog";
|
|
20
|
-
export { defineEventCatalog } from "#/core/event-catalog";
|
|
21
|
-
|
|
22
|
-
export { generateEventId } from "#/core/event-id";
|
|
23
|
-
|
|
24
|
-
export type { TrackedEvent } from "#/core/tracked-event";
|
|
@@ -1,166 +0,0 @@
|
|
|
1
|
-
import type { Destination } from "#/core/destination";
|
|
2
|
-
|
|
3
|
-
type MeasurementProtocolParamValue = boolean | number | string;
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Measurement Protocol event params must be flat string/number/boolean — stringify
|
|
7
|
-
* anything else instead of letting GA4 silently drop it.
|
|
8
|
-
*/
|
|
9
|
-
function toMeasurementProtocolParams(props: Record<string, unknown>): Record<string, MeasurementProtocolParamValue> {
|
|
10
|
-
const result: Record<string, MeasurementProtocolParamValue> = {};
|
|
11
|
-
|
|
12
|
-
for (const [key, value] of Object.entries(props)) {
|
|
13
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
14
|
-
result[key] = value;
|
|
15
|
-
} else if (value !== undefined && value !== null) {
|
|
16
|
-
result[key] = JSON.stringify(value);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return result;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function readCookie(cookieHeader: string | undefined, cookieName: string): string | undefined {
|
|
24
|
-
if (!cookieHeader) {
|
|
25
|
-
return undefined;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
for (const part of cookieHeader.split(";")) {
|
|
29
|
-
const separatorIndex = part.indexOf("=");
|
|
30
|
-
|
|
31
|
-
if (separatorIndex !== -1 && part.slice(0, separatorIndex).trim() === cookieName) {
|
|
32
|
-
return part.slice(separatorIndex + 1).trim();
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return undefined;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Reads gtag.js's own client ID from a request Cookie header — `_ga=GA1.1.123.456` →
|
|
41
|
-
* `123.456`. This (not an app-generated anonymous ID) is the value GA4 joins hits on, so
|
|
42
|
-
* pass it as `clientId` to make server events land on the same user as the visitor's
|
|
43
|
-
* client-side hits.
|
|
44
|
-
*
|
|
45
|
-
* @since 0.5.0-canary.4
|
|
46
|
-
*/
|
|
47
|
-
export function extractGa4ClientId(cookieHeader: string | undefined): string | undefined {
|
|
48
|
-
const value = readCookie(cookieHeader, "_ga");
|
|
49
|
-
|
|
50
|
-
if (!value) {
|
|
51
|
-
return undefined;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Format: GA1.<domain-depth>.<random>.<timestamp> — the client ID is the last two segments.
|
|
55
|
-
const segments = value.split(".");
|
|
56
|
-
|
|
57
|
-
return segments.length >= 4 ? segments.slice(-2).join(".") : undefined;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Reads the current GA4 session ID from the per-stream `_ga_<suffix>` cookie
|
|
62
|
-
* (`G-ABC123` → `_ga_ABC123`) — handles both the dot-delimited `GS1` and the
|
|
63
|
-
* `$`-delimited `GS2` value formats.
|
|
64
|
-
*
|
|
65
|
-
* @since 0.5.0-canary.4
|
|
66
|
-
*/
|
|
67
|
-
export function extractGa4SessionId(cookieHeader: string | undefined, measurementId: string): string | undefined {
|
|
68
|
-
const value = readCookie(cookieHeader, `_ga_${measurementId.replace(/^G-/, "")}`);
|
|
69
|
-
|
|
70
|
-
if (!value) {
|
|
71
|
-
return undefined;
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// GS1.1.<session_id>.<count>... vs GS2.1.s<session_id>$o<count>... — the session ID is
|
|
75
|
-
// the first number after the version prefix either way.
|
|
76
|
-
const match = /^GS\d+\.\d+\.s?(\d+)/.exec(value);
|
|
77
|
-
|
|
78
|
-
return match?.[1];
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* @since 0.5.0-canary.4
|
|
83
|
-
*/
|
|
84
|
-
export interface Ga4MeasurementProtocolDestinationOptions {
|
|
85
|
-
apiSecret: string;
|
|
86
|
-
/**
|
|
87
|
-
* gtag.js's client ID for this visitor (see `extractGa4ClientId`). Without it the
|
|
88
|
-
* event falls back to the tracker's anonymous ID, which GA4 treats as a *different
|
|
89
|
-
* user* than the visitor's client-side hits — pass it whenever the request carries a
|
|
90
|
-
* `_ga` cookie.
|
|
91
|
-
*/
|
|
92
|
-
clientId?: string | undefined;
|
|
93
|
-
/** Routes to GA4's `/debug/mp/collect` validation endpoint — hits are validated but never recorded. */
|
|
94
|
-
debug?: boolean;
|
|
95
|
-
measurementId: string;
|
|
96
|
-
name?: string;
|
|
97
|
-
/**
|
|
98
|
-
* Current GA4 session for this visitor (see `extractGa4SessionId`). Without a
|
|
99
|
-
* `session_id`, GA4 accepts the event but leaves it out of session-scoped and realtime
|
|
100
|
-
* reporting.
|
|
101
|
-
*/
|
|
102
|
-
sessionId?: string | undefined;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Server-side GA4 via the Measurement Protocol — for server-owned events (checkout,
|
|
107
|
-
* signup) that must land in GA4 without depending on client-side gtag.js. MP is designed
|
|
108
|
-
* to *augment* gtag.js data, so correlation hinges on echoing gtag's own identifiers:
|
|
109
|
-
* `clientId` from the `_ga` cookie and `sessionId` from the `_ga_<stream>` cookie.
|
|
110
|
-
*
|
|
111
|
-
* @since 0.5.0-canary.4
|
|
112
|
-
*/
|
|
113
|
-
export function createGa4MeasurementProtocolDestination(
|
|
114
|
-
options: Ga4MeasurementProtocolDestinationOptions,
|
|
115
|
-
): Destination {
|
|
116
|
-
const name = options.name ?? "ga4-measurement-protocol";
|
|
117
|
-
const host = options.debug
|
|
118
|
-
? "https://www.google-analytics.com/debug/mp/collect"
|
|
119
|
-
: "https://www.google-analytics.com/mp/collect";
|
|
120
|
-
const endpoint = `${host}?measurement_id=${encodeURIComponent(options.measurementId)}&api_secret=${encodeURIComponent(options.apiSecret)}`;
|
|
121
|
-
|
|
122
|
-
return {
|
|
123
|
-
name,
|
|
124
|
-
async send(event) {
|
|
125
|
-
// GA4 has no alias concept — identity merge happens via user_id on later events.
|
|
126
|
-
if (event.name === "$alias") {
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
// join_group is GA4's recommended-event equivalent of a group association.
|
|
131
|
-
const eventName = event.name === "$group" ? "join_group" : event.name;
|
|
132
|
-
const props = event.name === "$group" ? renameGroupIdProp(event.props) : event.props;
|
|
133
|
-
const params = toMeasurementProtocolParams(props);
|
|
134
|
-
|
|
135
|
-
if (options.sessionId !== undefined && params.session_id === undefined) {
|
|
136
|
-
params.session_id = options.sessionId;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Without an engagement time GA4 won't count the user as active in realtime;
|
|
140
|
-
// 100ms is the fallback Google's own MP examples use when elapsed time is unknown.
|
|
141
|
-
params.engagement_time_msec ??= 100;
|
|
142
|
-
|
|
143
|
-
const response = await fetch(endpoint, {
|
|
144
|
-
body: JSON.stringify({
|
|
145
|
-
client_id: options.clientId ?? event.anonymousId,
|
|
146
|
-
events: [{ name: eventName, params }],
|
|
147
|
-
// MP stamps receipt time otherwise — retried events would drift.
|
|
148
|
-
timestamp_micros: event.timestamp * 1000,
|
|
149
|
-
...(event.userId === undefined ? {} : { user_id: event.userId }),
|
|
150
|
-
}),
|
|
151
|
-
headers: { "content-type": "application/json" },
|
|
152
|
-
method: "POST",
|
|
153
|
-
});
|
|
154
|
-
|
|
155
|
-
if (!response.ok) {
|
|
156
|
-
throw new Error(`"${name}" destination responded with ${String(response.status)}`);
|
|
157
|
-
}
|
|
158
|
-
},
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
function renameGroupIdProp(props: Record<string, unknown>): Record<string, unknown> {
|
|
163
|
-
const { groupId, ...traits } = props;
|
|
164
|
-
|
|
165
|
-
return { group_id: groupId, ...traits };
|
|
166
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
import type { Destination } from "#/core/destination";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @since 0.5.0-canary.4
|
|
5
|
-
*/
|
|
6
|
-
export interface HttpDestinationOptions {
|
|
7
|
-
endpoint: string;
|
|
8
|
-
headers?: Record<string, string>;
|
|
9
|
-
name: string;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Generic fetch-based `Destination` — the building block for a real provider adapter
|
|
14
|
-
* (PostHog, GA4, ...); those ship their own payload shape and aren't implemented here.
|
|
15
|
-
*
|
|
16
|
-
* @since 0.5.0-canary.4
|
|
17
|
-
*/
|
|
18
|
-
export function createHttpDestination(options: HttpDestinationOptions): Destination {
|
|
19
|
-
return {
|
|
20
|
-
name: options.name,
|
|
21
|
-
async send(event) {
|
|
22
|
-
const response = await fetch(options.endpoint, {
|
|
23
|
-
body: JSON.stringify(event),
|
|
24
|
-
headers: { "content-type": "application/json", ...options.headers },
|
|
25
|
-
method: "POST",
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
if (!response.ok) {
|
|
29
|
-
throw new Error(`"${options.name}" destination responded with ${String(response.status)}`);
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
};
|
|
33
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export type { Ga4MeasurementProtocolDestinationOptions } from "#/destinations/ga4-measurement-protocol";
|
|
2
|
-
export {
|
|
3
|
-
createGa4MeasurementProtocolDestination,
|
|
4
|
-
extractGa4ClientId,
|
|
5
|
-
extractGa4SessionId,
|
|
6
|
-
} from "#/destinations/ga4-measurement-protocol";
|
|
7
|
-
|
|
8
|
-
export type { GoogleAnalyticsDestinationOptions, GoogleConsentDefaultOptions } from "#/destinations/google-analytics";
|
|
9
|
-
export {
|
|
10
|
-
createGoogleAnalyticsDestination,
|
|
11
|
-
setGoogleAdsDataRedaction,
|
|
12
|
-
setGoogleConsentDefault,
|
|
13
|
-
setGoogleUrlPassthrough,
|
|
14
|
-
updateGoogleConsent,
|
|
15
|
-
} from "#/destinations/google-analytics";
|
|
16
|
-
|
|
17
|
-
export type { HttpDestinationOptions } from "#/destinations/http-destination";
|
|
18
|
-
export { createHttpDestination } from "#/destinations/http-destination";
|
|
19
|
-
|
|
20
|
-
export { createVercelAnalyticsDestination } from "#/destinations/vercel-analytics";
|
package/src/react/index.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
export type {
|
|
2
|
-
ConsentBannerAcceptProps,
|
|
3
|
-
ConsentBannerActionsProps,
|
|
4
|
-
ConsentBannerCategoryProps,
|
|
5
|
-
ConsentBannerCustomizeProps,
|
|
6
|
-
ConsentBannerDescriptionProps,
|
|
7
|
-
ConsentBannerPreferencesProps,
|
|
8
|
-
ConsentBannerProps,
|
|
9
|
-
ConsentBannerRejectProps,
|
|
10
|
-
ConsentBannerSaveProps,
|
|
11
|
-
ConsentBannerTitleProps,
|
|
12
|
-
ConsentToggleProps,
|
|
13
|
-
} from "#/react/consent-banner";
|
|
14
|
-
export {
|
|
15
|
-
ConsentBanner,
|
|
16
|
-
ConsentBannerAccept,
|
|
17
|
-
ConsentBannerActions,
|
|
18
|
-
ConsentBannerCategory,
|
|
19
|
-
ConsentBannerCustomize,
|
|
20
|
-
ConsentBannerDescription,
|
|
21
|
-
ConsentBannerPreferences,
|
|
22
|
-
ConsentBannerReject,
|
|
23
|
-
ConsentBannerSave,
|
|
24
|
-
ConsentBannerTitle,
|
|
25
|
-
ConsentToggle,
|
|
26
|
-
} from "#/react/consent-banner";
|
|
27
|
-
|
|
28
|
-
export type { UseConsentOptions, UseConsentResult } from "#/react/use-consent";
|
|
29
|
-
export { useConsent } from "#/react/use-consent";
|
|
@@ -1,128 +0,0 @@
|
|
|
1
|
-
import type { z } from "zod";
|
|
2
|
-
|
|
3
|
-
import type { Destination } from "#/core/destination";
|
|
4
|
-
import type { EventCatalog, EventsOf } from "#/core/event-catalog";
|
|
5
|
-
import { generateEventId } from "#/core/event-id";
|
|
6
|
-
import type { TrackedEvent } from "#/core/tracked-event";
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* @since 0.5.0-canary.4
|
|
10
|
-
*/
|
|
11
|
-
export interface ServerTrackContext {
|
|
12
|
-
anonymousId: string;
|
|
13
|
-
userId?: string | undefined;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @since 0.5.0-canary.4
|
|
18
|
-
*/
|
|
19
|
-
export interface ServerTrackerOptions<Catalog extends EventCatalog> {
|
|
20
|
-
catalog: Catalog;
|
|
21
|
-
destinations: Array<Destination>;
|
|
22
|
-
generateEventId?: () => string;
|
|
23
|
-
maxRetries?: number;
|
|
24
|
-
onDestinationError?: (error: unknown, destination: Destination, event: TrackedEvent) => void;
|
|
25
|
-
retryDelayMs?: number;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @since 0.5.0-canary.4
|
|
30
|
-
*/
|
|
31
|
-
export interface ServerTracker<Catalog extends EventCatalog> {
|
|
32
|
-
/** Explicit anonymous → known-user merge, for when `identify` timing can't do it. */
|
|
33
|
-
alias: (previousId: string, userId: string, context: ServerTrackContext) => Promise<void>;
|
|
34
|
-
group: (groupId: string, traits: Record<string, unknown> | undefined, context: ServerTrackContext) => Promise<void>;
|
|
35
|
-
track: <Name extends keyof EventsOf<Catalog, "server">>(
|
|
36
|
-
name: Name,
|
|
37
|
-
props: z.infer<EventsOf<Catalog, "server">[Name]["schema"]>,
|
|
38
|
-
context: ServerTrackContext,
|
|
39
|
-
) => Promise<void>;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function sleep(ms: number): Promise<void> {
|
|
43
|
-
return new Promise((resolve) => {
|
|
44
|
-
setTimeout(resolve, ms);
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async function sendWithRetry(
|
|
49
|
-
destination: Destination,
|
|
50
|
-
event: TrackedEvent,
|
|
51
|
-
maxRetries: number,
|
|
52
|
-
retryDelayMs: number,
|
|
53
|
-
onError: (error: unknown, destination: Destination, event: TrackedEvent) => void,
|
|
54
|
-
): Promise<void> {
|
|
55
|
-
for (let attempt = 0; attempt <= maxRetries; attempt += 1) {
|
|
56
|
-
try {
|
|
57
|
-
await destination.send(event);
|
|
58
|
-
|
|
59
|
-
return;
|
|
60
|
-
} catch (error) {
|
|
61
|
-
if (attempt === maxRetries) {
|
|
62
|
-
onError(error, destination, event);
|
|
63
|
-
|
|
64
|
-
return;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
await sleep(retryDelayMs * 2 ** attempt);
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* No dead-letter queue yet (self-host storage is Phase 2) — a destination that keeps
|
|
74
|
-
* failing after retries is only logged via `onDestinationError`, never re-queued.
|
|
75
|
-
*
|
|
76
|
-
* @since 0.5.0-canary.4
|
|
77
|
-
*/
|
|
78
|
-
export function createServerTracker<Catalog extends EventCatalog>(
|
|
79
|
-
options: ServerTrackerOptions<Catalog>,
|
|
80
|
-
): ServerTracker<Catalog> {
|
|
81
|
-
const maxRetries = options.maxRetries ?? 2;
|
|
82
|
-
const retryDelayMs = options.retryDelayMs ?? 200;
|
|
83
|
-
const createId = options.generateEventId ?? generateEventId;
|
|
84
|
-
const onError =
|
|
85
|
-
options.onDestinationError ??
|
|
86
|
-
((error: unknown, destination: Destination, event: TrackedEvent): void => {
|
|
87
|
-
console.error(`[tracking] destination "${destination.name}" failed for event "${event.name}"`, error);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
async function sendEvent(name: string, props: Record<string, unknown>, context: ServerTrackContext): Promise<void> {
|
|
91
|
-
const event: TrackedEvent = {
|
|
92
|
-
anonymousId: context.anonymousId,
|
|
93
|
-
eventId: createId(),
|
|
94
|
-
name,
|
|
95
|
-
owner: "server",
|
|
96
|
-
props,
|
|
97
|
-
timestamp: Date.now(),
|
|
98
|
-
...(context.userId === undefined ? {} : { userId: context.userId }),
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
await Promise.all(
|
|
102
|
-
options.destinations.map(async (destination) =>
|
|
103
|
-
sendWithRetry(destination, event, maxRetries, retryDelayMs, onError),
|
|
104
|
-
),
|
|
105
|
-
);
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
return {
|
|
109
|
-
async alias(previousId, userId, context) {
|
|
110
|
-
await sendEvent("$alias", { previousId, userId }, context);
|
|
111
|
-
},
|
|
112
|
-
async group(groupId, traits, context) {
|
|
113
|
-
await sendEvent("$group", { groupId, ...traits }, context);
|
|
114
|
-
},
|
|
115
|
-
async track(name, props, context) {
|
|
116
|
-
// noUncheckedIndexedAccess types this as possibly undefined; the owner check also
|
|
117
|
-
// guards callers who bypass the EventsOf filter with an `as` cast.
|
|
118
|
-
const definition = options.catalog[name];
|
|
119
|
-
|
|
120
|
-
if (!definition || definition.owner !== "server") {
|
|
121
|
-
throw new Error(`Unknown server-owned event: ${String(name)}`);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
definition.schema.parse(props);
|
|
125
|
-
await sendEvent(name as string, props as Record<string, unknown>, context);
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
}
|
package/src/server/index.ts
DELETED