@codefast/tracking 0.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/LICENSE +21 -0
- package/README.md +90 -0
- package/dist/client/consent-storage.d.mts +11 -0
- package/dist/client/consent-storage.mjs +27 -0
- package/dist/client/create-client-tracker.d.mts +27 -0
- package/dist/client/create-client-tracker.mjs +53 -0
- package/dist/client/gpc.d.mts +8 -0
- package/dist/client/gpc.mjs +10 -0
- package/dist/client/index.d.mts +8 -0
- package/dist/client/index.mjs +8 -0
- package/dist/client/lifecycle.d.mts +18 -0
- package/dist/client/lifecycle.mjs +28 -0
- package/dist/client/local-storage.d.mts +10 -0
- package/dist/client/local-storage.mjs +24 -0
- package/dist/client/queue.d.mts +42 -0
- package/dist/client/queue.mjs +69 -0
- package/dist/client/router.d.mts +26 -0
- package/dist/client/router.mjs +15 -0
- package/dist/core/consent.d.mts +37 -0
- package/dist/core/consent.mjs +25 -0
- package/dist/core/destination.d.mts +13 -0
- package/dist/core/destination.mjs +1 -0
- package/dist/core/event-catalog.d.mts +28 -0
- package/dist/core/event-catalog.mjs +10 -0
- package/dist/core/event-id.d.mts +8 -0
- package/dist/core/event-id.mjs +10 -0
- package/dist/core/index.d.mts +6 -0
- package/dist/core/index.mjs +4 -0
- package/dist/core/tracked-event.d.mts +15 -0
- package/dist/core/tracked-event.mjs +1 -0
- package/dist/destinations/http-destination.d.mts +15 -0
- package/dist/destinations/http-destination.mjs +23 -0
- package/dist/destinations/index.d.mts +3 -0
- package/dist/destinations/index.mjs +3 -0
- package/dist/destinations/vercel-analytics.d.mts +11 -0
- package/dist/destinations/vercel-analytics.mjs +27 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +4 -0
- package/dist/react/consent-banner.d.mts +41 -0
- package/dist/react/consent-banner.mjs +42 -0
- package/dist/react/index.d.mts +3 -0
- package/dist/react/index.mjs +3 -0
- package/dist/react/use-consent.d.mts +24 -0
- package/dist/react/use-consent.mjs +31 -0
- package/dist/server/create-server-tracker.d.mts +28 -0
- package/dist/server/create-server-tracker.mjs +48 -0
- package/dist/server/index.d.mts +3 -0
- package/dist/server/index.mjs +3 -0
- package/dist/server/region.d.mts +16 -0
- package/dist/server/region.mjs +52 -0
- package/package.json +242 -0
- package/src/client/consent-storage.ts +30 -0
- package/src/client/create-client-tracker.ts +97 -0
- package/src/client/gpc.ts +11 -0
- package/src/client/index.ts +17 -0
- package/src/client/lifecycle.ts +45 -0
- package/src/client/local-storage.ts +29 -0
- package/src/client/queue.ts +110 -0
- package/src/client/router.ts +33 -0
- package/src/core/consent.ts +58 -0
- package/src/core/destination.ts +10 -0
- package/src/core/event-catalog.ts +32 -0
- package/src/core/event-id.ts +7 -0
- package/src/core/index.ts +11 -0
- package/src/core/tracked-event.ts +12 -0
- package/src/destinations/http-destination.ts +28 -0
- package/src/destinations/index.ts +4 -0
- package/src/destinations/vercel-analytics.ts +37 -0
- package/src/index.ts +13 -0
- package/src/react/consent-banner.tsx +64 -0
- package/src/react/index.ts +5 -0
- package/src/react/use-consent.ts +53 -0
- package/src/server/create-server-tracker.ts +105 -0
- package/src/server/index.ts +4 -0
- package/src/server/region.ts +66 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ConsentDecision, ConsentMode, ConsentRecord, ConsentRegion, ConsentStorage, resolveConsentMode, shouldTrackByDefault } from "./consent.mjs";
|
|
2
|
+
import { TrackedEvent } from "./tracked-event.mjs";
|
|
3
|
+
import { Destination } from "./destination.mjs";
|
|
4
|
+
import { EventCatalog, EventDefinition, EventsOf, defineEventCatalog } from "./event-catalog.mjs";
|
|
5
|
+
import { generateEventId } from "./event-id.mjs";
|
|
6
|
+
export { type ConsentDecision, type ConsentMode, type ConsentRecord, type ConsentRegion, type ConsentStorage, type Destination, type EventCatalog, type EventDefinition, type EventsOf, type TrackedEvent, defineEventCatalog, generateEventId, resolveConsentMode, shouldTrackByDefault };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { resolveConsentMode, shouldTrackByDefault } from "./consent.mjs";
|
|
2
|
+
import { defineEventCatalog } from "./event-catalog.mjs";
|
|
3
|
+
import { generateEventId } from "./event-id.mjs";
|
|
4
|
+
export { defineEventCatalog, generateEventId, resolveConsentMode, shouldTrackByDefault };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/core/tracked-event.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Envelope every tracker (client/server) builds before handing an event to a destination.
|
|
4
|
+
*/
|
|
5
|
+
interface TrackedEvent {
|
|
6
|
+
anonymousId: string;
|
|
7
|
+
eventId: string;
|
|
8
|
+
name: string;
|
|
9
|
+
owner: "client" | "server";
|
|
10
|
+
props: Record<string, unknown>;
|
|
11
|
+
timestamp: number;
|
|
12
|
+
userId?: string | undefined;
|
|
13
|
+
}
|
|
14
|
+
//#endregion
|
|
15
|
+
export { TrackedEvent };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Destination } from "../core/destination.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/destinations/http-destination.d.ts
|
|
4
|
+
interface HttpDestinationOptions {
|
|
5
|
+
endpoint: string;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
name: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Generic fetch-based `Destination` — the building block for a real provider adapter
|
|
11
|
+
* (PostHog, GA4, ...); those ship their own payload shape and aren't implemented here.
|
|
12
|
+
*/
|
|
13
|
+
declare function createHttpDestination(options: HttpDestinationOptions): Destination;
|
|
14
|
+
//#endregion
|
|
15
|
+
export { HttpDestinationOptions, createHttpDestination };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
//#region src/destinations/http-destination.ts
|
|
2
|
+
/**
|
|
3
|
+
* Generic fetch-based `Destination` — the building block for a real provider adapter
|
|
4
|
+
* (PostHog, GA4, ...); those ship their own payload shape and aren't implemented here.
|
|
5
|
+
*/
|
|
6
|
+
function createHttpDestination(options) {
|
|
7
|
+
return {
|
|
8
|
+
name: options.name,
|
|
9
|
+
async send(event) {
|
|
10
|
+
const response = await fetch(options.endpoint, {
|
|
11
|
+
body: JSON.stringify(event),
|
|
12
|
+
headers: {
|
|
13
|
+
"content-type": "application/json",
|
|
14
|
+
...options.headers
|
|
15
|
+
},
|
|
16
|
+
method: "POST"
|
|
17
|
+
});
|
|
18
|
+
if (!response.ok) throw new Error(`"${options.name}" destination responded with ${String(response.status)}`);
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { createHttpDestination };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Destination } from "../core/destination.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/destinations/vercel-analytics.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* `track()` only pushes onto Vercel's own in-page queue (`window.va`/`window.vaq`) — it
|
|
6
|
+
* never rejects, so this destination never triggers `EventQueue`'s retry path. Requires
|
|
7
|
+
* `<Analytics />` from `@vercel/analytics/react` to be mounted once in the app.
|
|
8
|
+
*/
|
|
9
|
+
declare function createVercelAnalyticsDestination(name?: string): Destination;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { createVercelAnalyticsDestination };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { track } from "@vercel/analytics/react";
|
|
2
|
+
//#region src/destinations/vercel-analytics.ts
|
|
3
|
+
/**
|
|
4
|
+
* Vercel's `track()` rejects nested objects/arrays outright — flatten anything else
|
|
5
|
+
* to a string instead of silently dropping it.
|
|
6
|
+
*/
|
|
7
|
+
function toVercelAnalyticsProps(props) {
|
|
8
|
+
const result = {};
|
|
9
|
+
for (const [key, value] of Object.entries(props)) if (value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean") result[key] = value;
|
|
10
|
+
else if (value !== void 0) result[key] = JSON.stringify(value);
|
|
11
|
+
return result;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* `track()` only pushes onto Vercel's own in-page queue (`window.va`/`window.vaq`) — it
|
|
15
|
+
* never rejects, so this destination never triggers `EventQueue`'s retry path. Requires
|
|
16
|
+
* `<Analytics />` from `@vercel/analytics/react` to be mounted once in the app.
|
|
17
|
+
*/
|
|
18
|
+
function createVercelAnalyticsDestination(name = "vercel-analytics") {
|
|
19
|
+
return {
|
|
20
|
+
name,
|
|
21
|
+
send(event) {
|
|
22
|
+
track(event.name, toVercelAnalyticsProps(event.props));
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//#endregion
|
|
27
|
+
export { createVercelAnalyticsDestination };
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { ConsentDecision, ConsentMode, ConsentRecord, ConsentRegion, ConsentStorage, resolveConsentMode, shouldTrackByDefault } from "./core/consent.mjs";
|
|
2
|
+
import { TrackedEvent } from "./core/tracked-event.mjs";
|
|
3
|
+
import { Destination } from "./core/destination.mjs";
|
|
4
|
+
import { EventCatalog, EventDefinition, EventsOf, defineEventCatalog } from "./core/event-catalog.mjs";
|
|
5
|
+
import { generateEventId } from "./core/event-id.mjs";
|
|
6
|
+
export { type ConsentDecision, type ConsentMode, type ConsentRecord, type ConsentRegion, type ConsentStorage, type Destination, type EventCatalog, type EventDefinition, type EventsOf, type TrackedEvent, defineEventCatalog, generateEventId, resolveConsentMode, shouldTrackByDefault };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { resolveConsentMode, shouldTrackByDefault } from "./core/consent.mjs";
|
|
2
|
+
import { defineEventCatalog } from "./core/event-catalog.mjs";
|
|
3
|
+
import { generateEventId } from "./core/event-id.mjs";
|
|
4
|
+
export { defineEventCatalog, generateEventId, resolveConsentMode, shouldTrackByDefault };
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { UseConsentResult } from "./use-consent.mjs";
|
|
2
|
+
import { ReactNode } from "react";
|
|
3
|
+
|
|
4
|
+
//#region src/react/consent-banner.d.ts
|
|
5
|
+
interface ConsentBannerProps {
|
|
6
|
+
acceptLabel?: string;
|
|
7
|
+
className?: string;
|
|
8
|
+
consent: UseConsentResult;
|
|
9
|
+
message?: string;
|
|
10
|
+
rejectLabel?: string;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Blocking opt-in prompt for GDPR/NĐ 13 regions — renders nothing once a decision
|
|
14
|
+
* exists or the region defaults to opt-out (`consent.needsPrompt` covers both). No
|
|
15
|
+
* styling is baked in; pass `className` to match the host app's design system.
|
|
16
|
+
*/
|
|
17
|
+
declare function ConsentBanner({
|
|
18
|
+
acceptLabel,
|
|
19
|
+
className,
|
|
20
|
+
consent,
|
|
21
|
+
message,
|
|
22
|
+
rejectLabel
|
|
23
|
+
}: ConsentBannerProps): ReactNode;
|
|
24
|
+
interface ConsentToggleProps {
|
|
25
|
+
allowLabel?: string;
|
|
26
|
+
className?: string;
|
|
27
|
+
consent: UseConsentResult;
|
|
28
|
+
denyLabel?: string;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Always-visible control for opt-out regions — CCPA/CPRA requires a persistent
|
|
32
|
+
* "Do Not Sell or Share My Personal Information" mechanism, not just a one-time prompt.
|
|
33
|
+
*/
|
|
34
|
+
declare function ConsentToggle({
|
|
35
|
+
allowLabel,
|
|
36
|
+
className,
|
|
37
|
+
consent,
|
|
38
|
+
denyLabel
|
|
39
|
+
}: ConsentToggleProps): ReactNode;
|
|
40
|
+
//#endregion
|
|
41
|
+
export { ConsentBanner, ConsentBannerProps, ConsentToggle, ConsentToggleProps };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { jsx, jsxs } from "react/jsx-runtime";
|
|
2
|
+
//#region src/react/consent-banner.tsx
|
|
3
|
+
/**
|
|
4
|
+
* Blocking opt-in prompt for GDPR/NĐ 13 regions — renders nothing once a decision
|
|
5
|
+
* exists or the region defaults to opt-out (`consent.needsPrompt` covers both). No
|
|
6
|
+
* styling is baked in; pass `className` to match the host app's design system.
|
|
7
|
+
*/
|
|
8
|
+
function ConsentBanner({ acceptLabel = "Accept", className, consent, message = "We use cookies to understand how you use this site.", rejectLabel = "Reject" }) {
|
|
9
|
+
if (!consent.needsPrompt) return null;
|
|
10
|
+
return /* @__PURE__ */ jsxs("dialog", {
|
|
11
|
+
"aria-live": "polite",
|
|
12
|
+
className,
|
|
13
|
+
open: true,
|
|
14
|
+
children: [
|
|
15
|
+
/* @__PURE__ */ jsx("p", { children: message }),
|
|
16
|
+
/* @__PURE__ */ jsx("button", {
|
|
17
|
+
onClick: consent.grant,
|
|
18
|
+
type: "button",
|
|
19
|
+
children: acceptLabel
|
|
20
|
+
}),
|
|
21
|
+
/* @__PURE__ */ jsx("button", {
|
|
22
|
+
onClick: consent.deny,
|
|
23
|
+
type: "button",
|
|
24
|
+
children: rejectLabel
|
|
25
|
+
})
|
|
26
|
+
]
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Always-visible control for opt-out regions — CCPA/CPRA requires a persistent
|
|
31
|
+
* "Do Not Sell or Share My Personal Information" mechanism, not just a one-time prompt.
|
|
32
|
+
*/
|
|
33
|
+
function ConsentToggle({ allowLabel = "Allow tracking", className, consent, denyLabel = "Do Not Sell or Share My Personal Information" }) {
|
|
34
|
+
return /* @__PURE__ */ jsx("button", {
|
|
35
|
+
className,
|
|
36
|
+
onClick: consent.isTrackingAllowed ? consent.deny : consent.grant,
|
|
37
|
+
type: "button",
|
|
38
|
+
children: consent.isTrackingAllowed ? denyLabel : allowLabel
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
//#endregion
|
|
42
|
+
export { ConsentBanner, ConsentToggle };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { UseConsentOptions, UseConsentResult, useConsent } from "./use-consent.mjs";
|
|
2
|
+
import { ConsentBanner, ConsentBannerProps, ConsentToggle, ConsentToggleProps } from "./consent-banner.mjs";
|
|
3
|
+
export { ConsentBanner, type ConsentBannerProps, ConsentToggle, type ConsentToggleProps, type UseConsentOptions, type UseConsentResult, useConsent };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ConsentDecision, ConsentMode, ConsentStorage } from "../core/consent.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/react/use-consent.d.ts
|
|
4
|
+
interface UseConsentOptions {
|
|
5
|
+
hasGlobalPrivacyControlSignal?: boolean;
|
|
6
|
+
mode: ConsentMode;
|
|
7
|
+
onDecision?: (decision: ConsentDecision) => void;
|
|
8
|
+
policyVersion: string;
|
|
9
|
+
storage: ConsentStorage;
|
|
10
|
+
}
|
|
11
|
+
interface UseConsentResult {
|
|
12
|
+
deny: () => void;
|
|
13
|
+
grant: () => void;
|
|
14
|
+
isTrackingAllowed: boolean;
|
|
15
|
+
/** True only for opt-in regions with no stored decision yet — drives whether to render the banner. */
|
|
16
|
+
needsPrompt: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Bridges `resolveConsentMode`/`shouldTrackByDefault` (core, region-aware) to React state,
|
|
20
|
+
* persisting the visitor's decision via the given `ConsentStorage`.
|
|
21
|
+
*/
|
|
22
|
+
declare function useConsent(options: UseConsentOptions): UseConsentResult;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { UseConsentOptions, UseConsentResult, useConsent };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { shouldTrackByDefault } from "../core/consent.mjs";
|
|
2
|
+
import { useCallback, useState } from "react";
|
|
3
|
+
//#region src/react/use-consent.ts
|
|
4
|
+
/**
|
|
5
|
+
* Bridges `resolveConsentMode`/`shouldTrackByDefault` (core, region-aware) to React state,
|
|
6
|
+
* persisting the visitor's decision via the given `ConsentStorage`.
|
|
7
|
+
*/
|
|
8
|
+
function useConsent(options) {
|
|
9
|
+
const [decision, setDecision] = useState(() => options.storage.load()?.decision);
|
|
10
|
+
const decide = useCallback((next) => {
|
|
11
|
+
options.storage.save({
|
|
12
|
+
decision: next,
|
|
13
|
+
policyVersion: options.policyVersion,
|
|
14
|
+
timestamp: Date.now()
|
|
15
|
+
});
|
|
16
|
+
setDecision(next);
|
|
17
|
+
options.onDecision?.(next);
|
|
18
|
+
}, [options]);
|
|
19
|
+
return {
|
|
20
|
+
deny: () => {
|
|
21
|
+
decide("denied");
|
|
22
|
+
},
|
|
23
|
+
grant: () => {
|
|
24
|
+
decide("granted");
|
|
25
|
+
},
|
|
26
|
+
isTrackingAllowed: decision === void 0 ? shouldTrackByDefault(options.mode, options.hasGlobalPrivacyControlSignal ?? false) : decision === "granted",
|
|
27
|
+
needsPrompt: options.mode === "opt-in" && decision === void 0
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//#endregion
|
|
31
|
+
export { useConsent };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { TrackedEvent } from "../core/tracked-event.mjs";
|
|
2
|
+
import { Destination } from "../core/destination.mjs";
|
|
3
|
+
import { EventCatalog, EventsOf } from "../core/event-catalog.mjs";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
|
|
6
|
+
//#region src/server/create-server-tracker.d.ts
|
|
7
|
+
interface ServerTrackContext {
|
|
8
|
+
anonymousId: string;
|
|
9
|
+
userId?: string | undefined;
|
|
10
|
+
}
|
|
11
|
+
interface ServerTrackerOptions<Catalog extends EventCatalog> {
|
|
12
|
+
catalog: Catalog;
|
|
13
|
+
destinations: Array<Destination>;
|
|
14
|
+
generateEventId?: () => string;
|
|
15
|
+
maxRetries?: number;
|
|
16
|
+
onDestinationError?: (error: unknown, destination: Destination, event: TrackedEvent) => void;
|
|
17
|
+
retryDelayMs?: number;
|
|
18
|
+
}
|
|
19
|
+
interface ServerTracker<Catalog extends EventCatalog> {
|
|
20
|
+
track: <Name extends keyof EventsOf<Catalog, "server">>(name: Name, props: z.infer<EventsOf<Catalog, "server">[Name]["schema"]>, context: ServerTrackContext) => Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* No dead-letter queue yet (self-host storage is Phase 2) — a destination that keeps
|
|
24
|
+
* failing after retries is only logged via `onDestinationError`, never re-queued.
|
|
25
|
+
*/
|
|
26
|
+
declare function createServerTracker<Catalog extends EventCatalog>(options: ServerTrackerOptions<Catalog>): ServerTracker<Catalog>;
|
|
27
|
+
//#endregion
|
|
28
|
+
export { ServerTrackContext, ServerTracker, ServerTrackerOptions, createServerTracker };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { generateEventId } from "../core/event-id.mjs";
|
|
2
|
+
//#region src/server/create-server-tracker.ts
|
|
3
|
+
function sleep(ms) {
|
|
4
|
+
return new Promise((resolve) => {
|
|
5
|
+
setTimeout(resolve, ms);
|
|
6
|
+
});
|
|
7
|
+
}
|
|
8
|
+
async function sendWithRetry(destination, event, maxRetries, retryDelayMs, onError) {
|
|
9
|
+
for (let attempt = 0; attempt <= maxRetries; attempt += 1) try {
|
|
10
|
+
await destination.send(event);
|
|
11
|
+
return;
|
|
12
|
+
} catch (error) {
|
|
13
|
+
if (attempt === maxRetries) {
|
|
14
|
+
onError(error, destination, event);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
await sleep(retryDelayMs * 2 ** attempt);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* No dead-letter queue yet (self-host storage is Phase 2) — a destination that keeps
|
|
22
|
+
* failing after retries is only logged via `onDestinationError`, never re-queued.
|
|
23
|
+
*/
|
|
24
|
+
function createServerTracker(options) {
|
|
25
|
+
const maxRetries = options.maxRetries ?? 2;
|
|
26
|
+
const retryDelayMs = options.retryDelayMs ?? 200;
|
|
27
|
+
const createId = options.generateEventId ?? generateEventId;
|
|
28
|
+
const onError = options.onDestinationError ?? ((error, destination, event) => {
|
|
29
|
+
console.error(`[tracking] destination "${destination.name}" failed for event "${event.name}"`, error);
|
|
30
|
+
});
|
|
31
|
+
return { async track(name, props, context) {
|
|
32
|
+
const definition = options.catalog[name];
|
|
33
|
+
if (!definition || definition.owner !== "server") throw new Error(`Unknown server-owned event: ${String(name)}`);
|
|
34
|
+
definition.schema.parse(props);
|
|
35
|
+
const event = {
|
|
36
|
+
anonymousId: context.anonymousId,
|
|
37
|
+
eventId: createId(),
|
|
38
|
+
name,
|
|
39
|
+
owner: "server",
|
|
40
|
+
props,
|
|
41
|
+
timestamp: Date.now(),
|
|
42
|
+
...context.userId === void 0 ? {} : { userId: context.userId }
|
|
43
|
+
};
|
|
44
|
+
await Promise.all(options.destinations.map(async (destination) => sendWithRetry(destination, event, maxRetries, retryDelayMs, onError)));
|
|
45
|
+
} };
|
|
46
|
+
}
|
|
47
|
+
//#endregion
|
|
48
|
+
export { createServerTracker };
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { ServerTrackContext, ServerTracker, ServerTrackerOptions, createServerTracker } from "./create-server-tracker.mjs";
|
|
2
|
+
import { resolveRegion, resolveRegionFromCountryCode } from "./region.mjs";
|
|
3
|
+
export { type ServerTrackContext, type ServerTracker, type ServerTrackerOptions, createServerTracker, resolveRegion, resolveRegionFromCountryCode };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { ConsentRegion } from "../core/consent.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/server/region.d.ts
|
|
4
|
+
/**
|
|
5
|
+
* Missing/unrecognized codes fall back to "other", which `resolveConsentMode` treats
|
|
6
|
+
* as opt-out — the least restrictive default, safe only because opt-in regions (EU/VN)
|
|
7
|
+
* are matched explicitly above it.
|
|
8
|
+
*/
|
|
9
|
+
declare function resolveRegionFromCountryCode(countryCode: string | undefined): ConsentRegion;
|
|
10
|
+
/**
|
|
11
|
+
* Reads the hosting platform's geo header (e.g. Vercel's `x-vercel-ip-country`) — pass
|
|
12
|
+
* the incoming request's `Headers` from server-function/middleware context.
|
|
13
|
+
*/
|
|
14
|
+
declare function resolveRegion(headers: Headers, headerName?: string): ConsentRegion;
|
|
15
|
+
//#endregion
|
|
16
|
+
export { resolveRegion, resolveRegionFromCountryCode };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
//#region src/server/region.ts
|
|
2
|
+
const EU_COUNTRY_CODES = /* @__PURE__ */ new Set([
|
|
3
|
+
"AT",
|
|
4
|
+
"BE",
|
|
5
|
+
"BG",
|
|
6
|
+
"HR",
|
|
7
|
+
"CY",
|
|
8
|
+
"CZ",
|
|
9
|
+
"DK",
|
|
10
|
+
"EE",
|
|
11
|
+
"FI",
|
|
12
|
+
"FR",
|
|
13
|
+
"DE",
|
|
14
|
+
"GR",
|
|
15
|
+
"HU",
|
|
16
|
+
"IE",
|
|
17
|
+
"IT",
|
|
18
|
+
"LV",
|
|
19
|
+
"LT",
|
|
20
|
+
"LU",
|
|
21
|
+
"MT",
|
|
22
|
+
"NL",
|
|
23
|
+
"PL",
|
|
24
|
+
"PT",
|
|
25
|
+
"RO",
|
|
26
|
+
"SK",
|
|
27
|
+
"SI",
|
|
28
|
+
"ES",
|
|
29
|
+
"SE"
|
|
30
|
+
]);
|
|
31
|
+
/**
|
|
32
|
+
* Missing/unrecognized codes fall back to "other", which `resolveConsentMode` treats
|
|
33
|
+
* as opt-out — the least restrictive default, safe only because opt-in regions (EU/VN)
|
|
34
|
+
* are matched explicitly above it.
|
|
35
|
+
*/
|
|
36
|
+
function resolveRegionFromCountryCode(countryCode) {
|
|
37
|
+
if (!countryCode) return "other";
|
|
38
|
+
const normalized = countryCode.toUpperCase();
|
|
39
|
+
if (EU_COUNTRY_CODES.has(normalized)) return "eu";
|
|
40
|
+
if (normalized === "VN") return "vn";
|
|
41
|
+
if (normalized === "US") return "us";
|
|
42
|
+
return "other";
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Reads the hosting platform's geo header (e.g. Vercel's `x-vercel-ip-country`) — pass
|
|
46
|
+
* the incoming request's `Headers` from server-function/middleware context.
|
|
47
|
+
*/
|
|
48
|
+
function resolveRegion(headers, headerName = "x-vercel-ip-country") {
|
|
49
|
+
return resolveRegionFromCountryCode(headers.get(headerName) ?? void 0);
|
|
50
|
+
}
|
|
51
|
+
//#endregion
|
|
52
|
+
export { resolveRegion, resolveRegionFromCountryCode };
|