@codefast/tracking 1.0.0-canary.6 → 1.0.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 +55 -0
- package/README.md +72 -17
- 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.mts → browser-cookie.d.ts} +2 -4
- 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} +3 -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.mts → gpc.d.ts} +2 -4
- 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.mts → consent.d.ts} +49 -51
- 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.mts → cookie.d.ts} +3 -5
- 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.mts → event-id.d.ts} +2 -4
- 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.mts → use-initial-consent.d.ts} +3 -6
- 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.mts → region.d.ts} +5 -15
- 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 +238 -59
- package/src/adapters/tanstack-start.ts +135 -0
- package/src/client/ad-framework-consent.ts +86 -0
- package/src/client/cookie-anonymous-id.ts +19 -0
- package/src/client/create-client-tracker.ts +81 -10
- package/src/client/server-persisted-anonymous-id.ts +4 -1
- package/src/core/consent-receipt.ts +115 -0
- package/src/core/consent.ts +2 -2
- package/src/core/destination.ts +7 -0
- package/src/core/event-catalog.ts +5 -4
- package/src/core/tracked-event.ts +5 -20
- package/src/destinations/ad-consent.ts +29 -0
- package/src/destinations/meta.ts +96 -0
- package/src/destinations/microsoft-uet.ts +86 -0
- package/src/destinations/tiktok.ts +85 -0
- package/src/index.ts +138 -19
- package/src/server/anonymous-id-cookie.ts +49 -22
- package/src/server/consent-receipt-store.ts +79 -0
- package/src/server/consent-receipt.ts +89 -0
- package/src/server/initial-consent.ts +0 -2
- package/src/server/measurement-protocol.ts +114 -0
- package/src/server/region.ts +0 -10
- package/src/server/user-deletion.ts +93 -0
- package/src/tooling/import-protection.ts +22 -0
- package/dist/client/browser-cookie.mjs +0 -15
- package/dist/client/consent-runtime.d.mts +0 -56
- package/dist/client/consent-runtime.mjs +0 -35
- package/dist/client/consent-storage.mjs +0 -82
- package/dist/client/consent-withdrawal.d.mts +0 -21
- package/dist/client/consent-withdrawal.mjs +0 -17
- package/dist/client/cookie-anonymous-id.d.mts +0 -37
- package/dist/client/cookie-anonymous-id.mjs +0 -46
- package/dist/client/create-client-tracker.d.mts +0 -32
- package/dist/client/create-client-tracker.mjs +0 -40
- package/dist/client/gpc.mjs +0 -14
- package/dist/client/index.d.mts +0 -10
- package/dist/client/index.mjs +0 -10
- package/dist/client/initial-consent-store.d.mts +0 -66
- package/dist/client/initial-consent-store.mjs +0 -119
- package/dist/client/is-analytics-allowed.d.mts +0 -29
- package/dist/client/is-analytics-allowed.mjs +0 -23
- package/dist/client/server-persisted-anonymous-id.d.mts +0 -33
- package/dist/client/server-persisted-anonymous-id.mjs +0 -50
- package/dist/core/consent-config.d.mts +0 -27
- package/dist/core/consent-config.mjs +0 -11
- package/dist/core/consent.mjs +0 -152
- package/dist/core/cookie.mjs +0 -23
- package/dist/core/destination.d.mts +0 -25
- package/dist/core/destination.mjs +0 -1
- package/dist/core/event-catalog.d.mts +0 -39
- package/dist/core/event-catalog.mjs +0 -30
- package/dist/core/event-id.mjs +0 -11
- package/dist/core/index.d.mts +0 -8
- package/dist/core/index.mjs +0 -6
- package/dist/core/tracked-event.d.mts +0 -30
- package/dist/core/tracked-event.mjs +0 -1
- package/dist/destinations/google-analytics.d.mts +0 -150
- package/dist/destinations/google-analytics.mjs +0 -130
- package/dist/destinations/google-consent.d.mts +0 -64
- package/dist/destinations/google-consent.mjs +0 -102
- package/dist/destinations/index.d.mts +0 -2
- package/dist/destinations/index.mjs +0 -2
- package/dist/destinations/shared.d.mts +0 -20
- package/dist/destinations/shared.mjs +0 -13
- package/dist/destinations/vercel-analytics.d.mts +0 -28
- package/dist/destinations/vercel-analytics.mjs +0 -23
- package/dist/import-protection.d.mts +0 -19
- package/dist/import-protection.mjs +0 -23
- package/dist/index.d.mts +0 -8
- package/dist/index.mjs +0 -6
- package/dist/react/consent-banner.d.mts +0 -180
- package/dist/react/consent-banner.mjs +0 -214
- package/dist/react/gtag-consent-bootstrap.d.mts +0 -27
- package/dist/react/gtag-consent-bootstrap.mjs +0 -29
- package/dist/react/index.d.mts +0 -6
- package/dist/react/index.mjs +0 -6
- package/dist/react/use-consent.d.mts +0 -45
- package/dist/react/use-consent.mjs +0 -71
- package/dist/react/use-google-consent-sync.d.mts +0 -23
- package/dist/react/use-google-consent-sync.mjs +0 -24
- package/dist/react/use-initial-consent.mjs +0 -19
- package/dist/server/anonymous-id-cookie.d.mts +0 -46
- package/dist/server/anonymous-id-cookie.mjs +0 -51
- package/dist/server/index.d.mts +0 -5
- package/dist/server/index.mjs +0 -4
- package/dist/server/initial-consent.d.mts +0 -29
- package/dist/server/initial-consent.mjs +0 -31
- package/dist/server/region.mjs +0 -77
- package/dist/tanstack-start/index.d.mts +0 -58
- package/dist/tanstack-start/index.mjs +0 -45
- package/src/client/index.ts +0 -28
- package/src/core/index.ts +0 -40
- package/src/destinations/index.ts +0 -19
- package/src/import-protection.ts +0 -20
- package/src/react/index.ts +0 -37
- package/src/server/index.ts +0 -16
- package/src/tanstack-start/index.ts +0 -83
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* DSR delegation for GA4 (spec-data-subject-rights §3): the platform ships its own
|
|
3
|
+
* per-visitor deletion, so the system **delegates** rather than reimplementing a deletion
|
|
4
|
+
* store. Targets the current **Analytics Admin API** `properties.submitUserDeletion` — the
|
|
5
|
+
* legacy v3 `userDeletionRequests:upsert` was sunset with Universal Analytics, so the spec's
|
|
6
|
+
* DSR-V3 `{ id: { type: "CLIENT_ID", userId } }` shape is stale; the current request keys the
|
|
7
|
+
* subject with a flat `clientId` (a oneof with `userId`/`appInstanceId`/`userProvidedData`).
|
|
8
|
+
*
|
|
9
|
+
* @see https://developers.google.com/analytics/devguides/config/userdeletion/migration
|
|
10
|
+
*/
|
|
11
|
+
const ADMIN_API_ORIGIN = "https://analyticsadmin.googleapis.com";
|
|
12
|
+
/**
|
|
13
|
+
* Builds the Admin API `submitUserDeletion` request that erases one GA4 subject by
|
|
14
|
+
* `clientId` (the `_ga`-cookie id — see `extractGaClientId`). Pure and network-free so the
|
|
15
|
+
* request shape can be snapshotted/verified; pair it with {@link submitGa4UserDeletion} or
|
|
16
|
+
* any authenticated transport.
|
|
17
|
+
*
|
|
18
|
+
* @param options - `propertyId` is the numeric GA4 property (no `properties/` prefix).
|
|
19
|
+
*
|
|
20
|
+
* @since 1.0.0-canary.7
|
|
21
|
+
*/
|
|
22
|
+
export function buildGa4UserDeletionRequest(options) {
|
|
23
|
+
return {
|
|
24
|
+
body: { clientId: options.clientId },
|
|
25
|
+
url: `${ADMIN_API_ORIGIN}/v1alpha/properties/${options.propertyId}:submitUserDeletion`,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
async function defaultTransport(request) {
|
|
29
|
+
const response = await fetch(request.url, { body: request.body, headers: request.headers, method: "POST" });
|
|
30
|
+
if (!response.ok) {
|
|
31
|
+
throw new Error(`GA4 user deletion responded ${String(response.status)}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Submits a GA4 user-deletion request for one `clientId`. Authorization is the caller's:
|
|
36
|
+
* pass a bearer token minted from a service account (never baked into the package). The
|
|
37
|
+
* deletion removes the subject from the Individual User report within ~72h and purges at the
|
|
38
|
+
* next run; it does **not** reach previously-aggregated reports or BigQuery exports
|
|
39
|
+
* (spec-data-subject-rights §3).
|
|
40
|
+
*
|
|
41
|
+
* @since 1.0.0-canary.7
|
|
42
|
+
*/
|
|
43
|
+
export async function submitGa4UserDeletion(options) {
|
|
44
|
+
const request = buildGa4UserDeletionRequest({ clientId: options.clientId, propertyId: options.propertyId });
|
|
45
|
+
await (options.transport ?? defaultTransport)({
|
|
46
|
+
body: JSON.stringify(request.body),
|
|
47
|
+
headers: {
|
|
48
|
+
authorization: `Bearer ${options.accessToken}`,
|
|
49
|
+
"content-type": "application/json",
|
|
50
|
+
},
|
|
51
|
+
url: request.url,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=user-deletion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"user-deletion.js","sourceRoot":"","sources":["../../src/server/user-deletion.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,MAAM,gBAAgB,GAAG,uCAAuC,CAAC;AAUjE;;;;;;;;;GASG;AACH,MAAM,UAAU,2BAA2B,CAAC,OAAiD;IAC3F,OAAO;QACL,IAAI,EAAE,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE;QACpC,GAAG,EAAE,GAAG,gBAAgB,uBAAuB,OAAO,CAAC,UAAU,qBAAqB;KACvF,CAAC;AACJ,CAAC;AAaD,KAAK,UAAU,gBAAgB,CAAC,OAI/B;IACC,MAAM,QAAQ,GAAG,MAAM,KAAK,CAAC,OAAO,CAAC,GAAG,EAAE,EAAE,IAAI,EAAE,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IAE5G,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;QACjB,MAAM,IAAI,KAAK,CAAC,+BAA+B,MAAM,CAAC,QAAQ,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAC5E,CAAC;AACH,CAAC;AAaD;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,qBAAqB,CAAC,OAAqC;IAC/E,MAAM,OAAO,GAAG,2BAA2B,CAAC,EAAE,QAAQ,EAAE,OAAO,CAAC,QAAQ,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC;IAE5G,MAAM,CAAC,OAAO,CAAC,SAAS,IAAI,gBAAgB,CAAC,CAAC;QAC5C,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,IAAI,CAAC;QAClC,OAAO,EAAE;YACP,aAAa,EAAE,UAAU,OAAO,CAAC,WAAW,EAAE;YAC9C,cAAc,EAAE,kBAAkB;SACnC;QACD,GAAG,EAAE,OAAO,CAAC,GAAG;KACjB,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `@codefast/tracking` subpaths every client bundle must deny (picomatch patterns) —
|
|
3
|
+
* framework-neutral data each build tool maps to its own guard: TanStack Start's
|
|
4
|
+
* `importProtection.client.specifiers`, Next's `server-only`, Remix's `.server` convention.
|
|
5
|
+
* Ships and versions with the package instead of going stale in each consumer's config.
|
|
6
|
+
* Nothing in this package imports it — its consumers are app build configs (see apps/ui
|
|
7
|
+
* vite.config.ts).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* tanstackStart({
|
|
12
|
+
* importProtection: { client: { specifiers: [...SERVER_ONLY_SUBPATHS] } },
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @since 1.0.0-canary.6
|
|
17
|
+
*/
|
|
18
|
+
export declare const SERVER_ONLY_SUBPATHS: ReadonlyArray<string>;
|
|
19
|
+
//# sourceMappingURL=import-protection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import-protection.d.ts","sourceRoot":"","sources":["../../src/tooling/import-protection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,eAAO,MAAM,oBAAoB,EAAE,aAAa,CAAC,MAAM,CAItD,CAAC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `@codefast/tracking` subpaths every client bundle must deny (picomatch patterns) —
|
|
3
|
+
* framework-neutral data each build tool maps to its own guard: TanStack Start's
|
|
4
|
+
* `importProtection.client.specifiers`, Next's `server-only`, Remix's `.server` convention.
|
|
5
|
+
* Ships and versions with the package instead of going stale in each consumer's config.
|
|
6
|
+
* Nothing in this package imports it — its consumers are app build configs (see apps/ui
|
|
7
|
+
* vite.config.ts).
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* tanstackStart({
|
|
12
|
+
* importProtection: { client: { specifiers: [...SERVER_ONLY_SUBPATHS] } },
|
|
13
|
+
* });
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @since 1.0.0-canary.6
|
|
17
|
+
*/
|
|
18
|
+
export const SERVER_ONLY_SUBPATHS = [
|
|
19
|
+
"@codefast/tracking/server",
|
|
20
|
+
"@codefast/tracking/server/**",
|
|
21
|
+
"@codefast/tracking/adapters/**",
|
|
22
|
+
];
|
|
23
|
+
//# sourceMappingURL=import-protection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"import-protection.js","sourceRoot":"","sources":["../../src/tooling/import-protection.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAA0B;IACzD,2BAA2B;IAC3B,8BAA8B;IAC9B,gCAAgC;CACjC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codefast/tracking",
|
|
3
|
-
"version": "1.0.0-canary.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "1.0.0-canary.7",
|
|
4
|
+
"description": "Consent-gated, type-safe event tracking for TanStack Start — client tracker and server consent helpers over a per-app Standard Schema event catalog",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"analytics",
|
|
7
7
|
"codefast",
|
|
8
8
|
"consent",
|
|
9
|
+
"standard-schema",
|
|
9
10
|
"tanstack-start",
|
|
10
11
|
"tracking",
|
|
11
|
-
"typescript"
|
|
12
|
-
"zod"
|
|
12
|
+
"typescript"
|
|
13
13
|
],
|
|
14
14
|
"homepage": "https://github.com/codefastlabs/codefast/tree/main/packages/tracking#readme",
|
|
15
15
|
"bugs": {
|
|
@@ -31,9 +31,9 @@
|
|
|
31
31
|
],
|
|
32
32
|
"type": "module",
|
|
33
33
|
"sideEffects": false,
|
|
34
|
-
"main": "./dist/index.
|
|
35
|
-
"module": "./dist/index.
|
|
36
|
-
"types": "./dist/index.d.
|
|
34
|
+
"main": "./dist/index.js",
|
|
35
|
+
"module": "./dist/index.js",
|
|
36
|
+
"types": "./dist/index.d.ts",
|
|
37
37
|
"imports": {
|
|
38
38
|
"#/tests/*": [
|
|
39
39
|
"./tests/*",
|
|
@@ -42,54 +42,234 @@
|
|
|
42
42
|
"./tests/*/index.ts",
|
|
43
43
|
"./tests/*/index.tsx"
|
|
44
44
|
],
|
|
45
|
-
"#/*":
|
|
46
|
-
"
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
"#/*": {
|
|
46
|
+
"source": [
|
|
47
|
+
"./src/*",
|
|
48
|
+
"./src/*.ts",
|
|
49
|
+
"./src/*.tsx",
|
|
50
|
+
"./src/*/index.ts",
|
|
51
|
+
"./src/*/index.tsx"
|
|
52
|
+
],
|
|
53
|
+
"types": [
|
|
54
|
+
"./dist/*.d.ts",
|
|
55
|
+
"./dist/*/index.d.ts"
|
|
56
|
+
],
|
|
57
|
+
"default": [
|
|
58
|
+
"./dist/*.js",
|
|
59
|
+
"./dist/*/index.js"
|
|
60
|
+
]
|
|
61
|
+
}
|
|
52
62
|
},
|
|
53
63
|
"exports": {
|
|
54
64
|
".": {
|
|
55
65
|
"source": "./src/index.ts",
|
|
56
|
-
"types": "./dist/index.d.
|
|
57
|
-
"import": "./dist/index.
|
|
58
|
-
},
|
|
59
|
-
"./
|
|
60
|
-
"source": "./src/
|
|
61
|
-
"types": "./dist/
|
|
62
|
-
"import": "./dist/
|
|
63
|
-
},
|
|
64
|
-
"./client": {
|
|
65
|
-
"source": "./src/client/
|
|
66
|
-
"types": "./dist/client/
|
|
67
|
-
"import": "./dist/client/
|
|
68
|
-
},
|
|
69
|
-
"./
|
|
70
|
-
"source": "./src/
|
|
71
|
-
"types": "./dist/
|
|
72
|
-
"import": "./dist/
|
|
73
|
-
},
|
|
74
|
-
"./
|
|
75
|
-
"source": "./src/
|
|
76
|
-
"types": "./dist/
|
|
77
|
-
"import": "./dist/
|
|
78
|
-
},
|
|
79
|
-
"./
|
|
80
|
-
"source": "./src/
|
|
81
|
-
"types": "./dist/
|
|
82
|
-
"import": "./dist/
|
|
83
|
-
},
|
|
84
|
-
"./
|
|
85
|
-
"source": "./src/
|
|
86
|
-
"types": "./dist/
|
|
87
|
-
"import": "./dist/
|
|
66
|
+
"types": "./dist/index.d.ts",
|
|
67
|
+
"import": "./dist/index.js"
|
|
68
|
+
},
|
|
69
|
+
"./adapters/tanstack-start": {
|
|
70
|
+
"source": "./src/adapters/tanstack-start.ts",
|
|
71
|
+
"types": "./dist/adapters/tanstack-start.d.ts",
|
|
72
|
+
"import": "./dist/adapters/tanstack-start.js"
|
|
73
|
+
},
|
|
74
|
+
"./client/ad-framework-consent": {
|
|
75
|
+
"source": "./src/client/ad-framework-consent.ts",
|
|
76
|
+
"types": "./dist/client/ad-framework-consent.d.ts",
|
|
77
|
+
"import": "./dist/client/ad-framework-consent.js"
|
|
78
|
+
},
|
|
79
|
+
"./client/browser-cookie": {
|
|
80
|
+
"source": "./src/client/browser-cookie.ts",
|
|
81
|
+
"types": "./dist/client/browser-cookie.d.ts",
|
|
82
|
+
"import": "./dist/client/browser-cookie.js"
|
|
83
|
+
},
|
|
84
|
+
"./client/consent-runtime": {
|
|
85
|
+
"source": "./src/client/consent-runtime.ts",
|
|
86
|
+
"types": "./dist/client/consent-runtime.d.ts",
|
|
87
|
+
"import": "./dist/client/consent-runtime.js"
|
|
88
|
+
},
|
|
89
|
+
"./client/consent-storage": {
|
|
90
|
+
"source": "./src/client/consent-storage.ts",
|
|
91
|
+
"types": "./dist/client/consent-storage.d.ts",
|
|
92
|
+
"import": "./dist/client/consent-storage.js"
|
|
93
|
+
},
|
|
94
|
+
"./client/consent-withdrawal": {
|
|
95
|
+
"source": "./src/client/consent-withdrawal.ts",
|
|
96
|
+
"types": "./dist/client/consent-withdrawal.d.ts",
|
|
97
|
+
"import": "./dist/client/consent-withdrawal.js"
|
|
98
|
+
},
|
|
99
|
+
"./client/cookie-anonymous-id": {
|
|
100
|
+
"source": "./src/client/cookie-anonymous-id.ts",
|
|
101
|
+
"types": "./dist/client/cookie-anonymous-id.d.ts",
|
|
102
|
+
"import": "./dist/client/cookie-anonymous-id.js"
|
|
103
|
+
},
|
|
104
|
+
"./client/create-client-tracker": {
|
|
105
|
+
"source": "./src/client/create-client-tracker.ts",
|
|
106
|
+
"types": "./dist/client/create-client-tracker.d.ts",
|
|
107
|
+
"import": "./dist/client/create-client-tracker.js"
|
|
108
|
+
},
|
|
109
|
+
"./client/gpc": {
|
|
110
|
+
"source": "./src/client/gpc.ts",
|
|
111
|
+
"types": "./dist/client/gpc.d.ts",
|
|
112
|
+
"import": "./dist/client/gpc.js"
|
|
113
|
+
},
|
|
114
|
+
"./client/initial-consent-store": {
|
|
115
|
+
"source": "./src/client/initial-consent-store.ts",
|
|
116
|
+
"types": "./dist/client/initial-consent-store.d.ts",
|
|
117
|
+
"import": "./dist/client/initial-consent-store.js"
|
|
118
|
+
},
|
|
119
|
+
"./client/is-analytics-allowed": {
|
|
120
|
+
"source": "./src/client/is-analytics-allowed.ts",
|
|
121
|
+
"types": "./dist/client/is-analytics-allowed.d.ts",
|
|
122
|
+
"import": "./dist/client/is-analytics-allowed.js"
|
|
123
|
+
},
|
|
124
|
+
"./client/server-persisted-anonymous-id": {
|
|
125
|
+
"source": "./src/client/server-persisted-anonymous-id.ts",
|
|
126
|
+
"types": "./dist/client/server-persisted-anonymous-id.d.ts",
|
|
127
|
+
"import": "./dist/client/server-persisted-anonymous-id.js"
|
|
128
|
+
},
|
|
129
|
+
"./core/consent": {
|
|
130
|
+
"source": "./src/core/consent.ts",
|
|
131
|
+
"types": "./dist/core/consent.d.ts",
|
|
132
|
+
"import": "./dist/core/consent.js"
|
|
133
|
+
},
|
|
134
|
+
"./core/consent-config": {
|
|
135
|
+
"source": "./src/core/consent-config.ts",
|
|
136
|
+
"types": "./dist/core/consent-config.d.ts",
|
|
137
|
+
"import": "./dist/core/consent-config.js"
|
|
138
|
+
},
|
|
139
|
+
"./core/consent-receipt": {
|
|
140
|
+
"source": "./src/core/consent-receipt.ts",
|
|
141
|
+
"types": "./dist/core/consent-receipt.d.ts",
|
|
142
|
+
"import": "./dist/core/consent-receipt.js"
|
|
143
|
+
},
|
|
144
|
+
"./core/cookie": {
|
|
145
|
+
"source": "./src/core/cookie.ts",
|
|
146
|
+
"types": "./dist/core/cookie.d.ts",
|
|
147
|
+
"import": "./dist/core/cookie.js"
|
|
148
|
+
},
|
|
149
|
+
"./core/destination": {
|
|
150
|
+
"source": "./src/core/destination.ts",
|
|
151
|
+
"types": "./dist/core/destination.d.ts",
|
|
152
|
+
"import": "./dist/core/destination.js"
|
|
153
|
+
},
|
|
154
|
+
"./core/event-catalog": {
|
|
155
|
+
"source": "./src/core/event-catalog.ts",
|
|
156
|
+
"types": "./dist/core/event-catalog.d.ts",
|
|
157
|
+
"import": "./dist/core/event-catalog.js"
|
|
158
|
+
},
|
|
159
|
+
"./core/event-id": {
|
|
160
|
+
"source": "./src/core/event-id.ts",
|
|
161
|
+
"types": "./dist/core/event-id.d.ts",
|
|
162
|
+
"import": "./dist/core/event-id.js"
|
|
163
|
+
},
|
|
164
|
+
"./core/tracked-event": {
|
|
165
|
+
"source": "./src/core/tracked-event.ts",
|
|
166
|
+
"types": "./dist/core/tracked-event.d.ts",
|
|
167
|
+
"import": "./dist/core/tracked-event.js"
|
|
168
|
+
},
|
|
169
|
+
"./destinations/ad-consent": {
|
|
170
|
+
"source": "./src/destinations/ad-consent.ts",
|
|
171
|
+
"types": "./dist/destinations/ad-consent.d.ts",
|
|
172
|
+
"import": "./dist/destinations/ad-consent.js"
|
|
173
|
+
},
|
|
174
|
+
"./destinations/google-analytics": {
|
|
175
|
+
"source": "./src/destinations/google-analytics.ts",
|
|
176
|
+
"types": "./dist/destinations/google-analytics.d.ts",
|
|
177
|
+
"import": "./dist/destinations/google-analytics.js"
|
|
178
|
+
},
|
|
179
|
+
"./destinations/google-consent": {
|
|
180
|
+
"source": "./src/destinations/google-consent.ts",
|
|
181
|
+
"types": "./dist/destinations/google-consent.d.ts",
|
|
182
|
+
"import": "./dist/destinations/google-consent.js"
|
|
183
|
+
},
|
|
184
|
+
"./destinations/meta": {
|
|
185
|
+
"source": "./src/destinations/meta.ts",
|
|
186
|
+
"types": "./dist/destinations/meta.d.ts",
|
|
187
|
+
"import": "./dist/destinations/meta.js"
|
|
188
|
+
},
|
|
189
|
+
"./destinations/microsoft-uet": {
|
|
190
|
+
"source": "./src/destinations/microsoft-uet.ts",
|
|
191
|
+
"types": "./dist/destinations/microsoft-uet.d.ts",
|
|
192
|
+
"import": "./dist/destinations/microsoft-uet.js"
|
|
193
|
+
},
|
|
194
|
+
"./destinations/shared": {
|
|
195
|
+
"source": "./src/destinations/shared.ts",
|
|
196
|
+
"types": "./dist/destinations/shared.d.ts",
|
|
197
|
+
"import": "./dist/destinations/shared.js"
|
|
198
|
+
},
|
|
199
|
+
"./destinations/tiktok": {
|
|
200
|
+
"source": "./src/destinations/tiktok.ts",
|
|
201
|
+
"types": "./dist/destinations/tiktok.d.ts",
|
|
202
|
+
"import": "./dist/destinations/tiktok.js"
|
|
88
203
|
},
|
|
89
204
|
"./destinations/vercel-analytics": {
|
|
90
205
|
"source": "./src/destinations/vercel-analytics.ts",
|
|
91
|
-
"types": "./dist/destinations/vercel-analytics.d.
|
|
92
|
-
"import": "./dist/destinations/vercel-analytics.
|
|
206
|
+
"types": "./dist/destinations/vercel-analytics.d.ts",
|
|
207
|
+
"import": "./dist/destinations/vercel-analytics.js"
|
|
208
|
+
},
|
|
209
|
+
"./react/consent-banner": {
|
|
210
|
+
"source": "./src/react/consent-banner.tsx",
|
|
211
|
+
"types": "./dist/react/consent-banner.d.ts",
|
|
212
|
+
"import": "./dist/react/consent-banner.js"
|
|
213
|
+
},
|
|
214
|
+
"./react/gtag-consent-bootstrap": {
|
|
215
|
+
"source": "./src/react/gtag-consent-bootstrap.tsx",
|
|
216
|
+
"types": "./dist/react/gtag-consent-bootstrap.d.ts",
|
|
217
|
+
"import": "./dist/react/gtag-consent-bootstrap.js"
|
|
218
|
+
},
|
|
219
|
+
"./react/use-consent": {
|
|
220
|
+
"source": "./src/react/use-consent.ts",
|
|
221
|
+
"types": "./dist/react/use-consent.d.ts",
|
|
222
|
+
"import": "./dist/react/use-consent.js"
|
|
223
|
+
},
|
|
224
|
+
"./react/use-google-consent-sync": {
|
|
225
|
+
"source": "./src/react/use-google-consent-sync.ts",
|
|
226
|
+
"types": "./dist/react/use-google-consent-sync.d.ts",
|
|
227
|
+
"import": "./dist/react/use-google-consent-sync.js"
|
|
228
|
+
},
|
|
229
|
+
"./react/use-initial-consent": {
|
|
230
|
+
"source": "./src/react/use-initial-consent.ts",
|
|
231
|
+
"types": "./dist/react/use-initial-consent.d.ts",
|
|
232
|
+
"import": "./dist/react/use-initial-consent.js"
|
|
233
|
+
},
|
|
234
|
+
"./server/anonymous-id-cookie": {
|
|
235
|
+
"source": "./src/server/anonymous-id-cookie.ts",
|
|
236
|
+
"types": "./dist/server/anonymous-id-cookie.d.ts",
|
|
237
|
+
"import": "./dist/server/anonymous-id-cookie.js"
|
|
238
|
+
},
|
|
239
|
+
"./server/consent-receipt": {
|
|
240
|
+
"source": "./src/server/consent-receipt.ts",
|
|
241
|
+
"types": "./dist/server/consent-receipt.d.ts",
|
|
242
|
+
"import": "./dist/server/consent-receipt.js"
|
|
243
|
+
},
|
|
244
|
+
"./server/consent-receipt-store": {
|
|
245
|
+
"source": "./src/server/consent-receipt-store.ts",
|
|
246
|
+
"types": "./dist/server/consent-receipt-store.d.ts",
|
|
247
|
+
"import": "./dist/server/consent-receipt-store.js"
|
|
248
|
+
},
|
|
249
|
+
"./server/initial-consent": {
|
|
250
|
+
"source": "./src/server/initial-consent.ts",
|
|
251
|
+
"types": "./dist/server/initial-consent.d.ts",
|
|
252
|
+
"import": "./dist/server/initial-consent.js"
|
|
253
|
+
},
|
|
254
|
+
"./server/measurement-protocol": {
|
|
255
|
+
"source": "./src/server/measurement-protocol.ts",
|
|
256
|
+
"types": "./dist/server/measurement-protocol.d.ts",
|
|
257
|
+
"import": "./dist/server/measurement-protocol.js"
|
|
258
|
+
},
|
|
259
|
+
"./server/region": {
|
|
260
|
+
"source": "./src/server/region.ts",
|
|
261
|
+
"types": "./dist/server/region.d.ts",
|
|
262
|
+
"import": "./dist/server/region.js"
|
|
263
|
+
},
|
|
264
|
+
"./server/user-deletion": {
|
|
265
|
+
"source": "./src/server/user-deletion.ts",
|
|
266
|
+
"types": "./dist/server/user-deletion.d.ts",
|
|
267
|
+
"import": "./dist/server/user-deletion.js"
|
|
268
|
+
},
|
|
269
|
+
"./tooling/import-protection": {
|
|
270
|
+
"source": "./src/tooling/import-protection.ts",
|
|
271
|
+
"types": "./dist/tooling/import-protection.d.ts",
|
|
272
|
+
"import": "./dist/tooling/import-protection.js"
|
|
93
273
|
},
|
|
94
274
|
"./css/*": "./src/css/*",
|
|
95
275
|
"./package.json": "./package.json"
|
|
@@ -101,27 +281,25 @@
|
|
|
101
281
|
"@standard-schema/spec": "^1.1.0"
|
|
102
282
|
},
|
|
103
283
|
"devDependencies": {
|
|
104
|
-
"@tanstack/react-start": "^1.168.
|
|
284
|
+
"@tanstack/react-start": "^1.168.32",
|
|
105
285
|
"@testing-library/dom": "^10.4.1",
|
|
106
286
|
"@testing-library/jest-dom": "^6.9.1",
|
|
107
287
|
"@testing-library/react": "^16.3.2",
|
|
108
288
|
"@testing-library/user-event": "^14.6.1",
|
|
109
|
-
"@types/node": "^26.1.
|
|
289
|
+
"@types/node": "^26.1.1",
|
|
110
290
|
"@types/react": "^19.2.17",
|
|
111
291
|
"@types/react-dom": "^19.2.3",
|
|
112
|
-
"@typescript/native-preview": "7.0.0-dev.20260704.1",
|
|
113
292
|
"@vercel/analytics": "^2.0.1",
|
|
114
293
|
"@vitejs/plugin-react": "^6.0.3",
|
|
115
|
-
"@vitest/coverage-v8": "^4.1.
|
|
294
|
+
"@vitest/coverage-v8": "^4.1.10",
|
|
116
295
|
"expect-type": "^1.4.0",
|
|
117
296
|
"jsdom": "^29.1.1",
|
|
118
297
|
"react": "^19.2.7",
|
|
119
298
|
"react-dom": "^19.2.7",
|
|
120
|
-
"
|
|
121
|
-
"
|
|
122
|
-
"vitest": "^4.1.9",
|
|
299
|
+
"typescript": "^7.0.2",
|
|
300
|
+
"vitest": "^4.1.10",
|
|
123
301
|
"zod": "^4.4.3",
|
|
124
|
-
"@codefast/typescript-config": "1.0.0-canary.
|
|
302
|
+
"@codefast/typescript-config": "1.0.0-canary.7"
|
|
125
303
|
},
|
|
126
304
|
"peerDependencies": {
|
|
127
305
|
"@tanstack/react-start": ">=1.168.0",
|
|
@@ -155,8 +333,8 @@
|
|
|
155
333
|
"node": ">=24.0.0"
|
|
156
334
|
},
|
|
157
335
|
"scripts": {
|
|
158
|
-
"build": "
|
|
159
|
-
"check-types": "
|
|
336
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
337
|
+
"check-types": "tsc --noEmit",
|
|
160
338
|
"clean": "rm -rf dist",
|
|
161
339
|
"test": "vitest run",
|
|
162
340
|
"test:coverage": "vitest run --coverage",
|
|
@@ -164,6 +342,7 @@
|
|
|
164
342
|
"test:integration": "vitest run tests/integration",
|
|
165
343
|
"test:type": "vitest run tests/types",
|
|
166
344
|
"test:unit": "vitest run tests/unit",
|
|
167
|
-
"test:watch": "vitest"
|
|
345
|
+
"test:watch": "vitest",
|
|
346
|
+
"dev": "tsc -p tsconfig.build.json --watch --preserveWatchOutput"
|
|
168
347
|
}
|
|
169
348
|
}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import {
|
|
2
|
+
deleteCookie,
|
|
3
|
+
getRequestHeader,
|
|
4
|
+
getRequestIP,
|
|
5
|
+
setCookie,
|
|
6
|
+
setResponseHeader,
|
|
7
|
+
} from "@tanstack/react-start/server";
|
|
8
|
+
|
|
9
|
+
import type { ConsentCategory, InitialConsent } from "#/core/consent";
|
|
10
|
+
import type { ConsentReceipt, ConsentReceiptInput } from "#/core/consent-receipt";
|
|
11
|
+
import { isConsentReceiptInput } from "#/core/consent-receipt";
|
|
12
|
+
import type { AnonymousIdCookieOptions } from "#/server/anonymous-id-cookie";
|
|
13
|
+
import { resolveAnonymousIdCookie, resolveClearAnonymousIdCookie } from "#/server/anonymous-id-cookie";
|
|
14
|
+
import { buildConsentReceipt } from "#/server/consent-receipt";
|
|
15
|
+
import type { ReceiptStore } from "#/server/consent-receipt-store";
|
|
16
|
+
import { resolveInitialConsent } from "#/server/initial-consent";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @since 1.0.0-canary.7
|
|
20
|
+
*/
|
|
21
|
+
export interface InitialConsentFromRequestOptions {
|
|
22
|
+
/**
|
|
23
|
+
* Header carrying the ISO 3166-1 alpha-2 country code.
|
|
24
|
+
*
|
|
25
|
+
* @defaultValue "x-vercel-ip-country"
|
|
26
|
+
*/
|
|
27
|
+
countryHeaderName?: string | undefined;
|
|
28
|
+
/** Categories the app's prompt asks about — opt-out regions grant exactly these by default. */
|
|
29
|
+
requestedCategories: ReadonlyArray<ConsentCategory>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Region-correct `InitialConsent` for the current request: reads the platform geo header
|
|
34
|
+
* and `sec-gpc`, fails closed when geo is missing, and stamps
|
|
35
|
+
* `cache-control: private, no-store` — the value is per-visitor by definition, so no
|
|
36
|
+
* shared cache may ever store the response carrying it.
|
|
37
|
+
*
|
|
38
|
+
* Server-only: deny this subpath in the client environment via Start's `importProtection`
|
|
39
|
+
* — the compiler already strips it with stubbed handler bodies.
|
|
40
|
+
*
|
|
41
|
+
* @since 1.0.0-canary.6
|
|
42
|
+
*/
|
|
43
|
+
export function resolveInitialConsentFromRequest(options: InitialConsentFromRequestOptions): InitialConsent {
|
|
44
|
+
setResponseHeader("cache-control", "private, no-store");
|
|
45
|
+
|
|
46
|
+
return resolveInitialConsent({
|
|
47
|
+
countryCode: getRequestHeader(options.countryHeaderName ?? "x-vercel-ip-country"),
|
|
48
|
+
hasGlobalPrivacyControlSignal: getRequestHeader("sec-gpc") === "1",
|
|
49
|
+
requestedCategories: options.requestedCategories,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Persists (or prolongs) a client-minted anonymous id via the framework's `setCookie` (which
|
|
55
|
+
* appends, so it never clobbers another `Set-Cookie` on the response) — the server re-issue
|
|
56
|
+
* that escapes Safari ITP's 7-day cap on script-written cookies. Validation lives in
|
|
57
|
+
* `resolveAnonymousIdCookie`: a non-UUID id throws, so this can safely back a public server
|
|
58
|
+
* function without echoing attacker input into a header.
|
|
59
|
+
*
|
|
60
|
+
* @since 1.0.0-canary.6
|
|
61
|
+
*/
|
|
62
|
+
export function setAnonymousIdResponseCookie(options: AnonymousIdCookieOptions): void {
|
|
63
|
+
const { name, value, ...attributes } = resolveAnonymousIdCookie({
|
|
64
|
+
cookieName: options.cookieName,
|
|
65
|
+
id: options.id,
|
|
66
|
+
maxAgeSeconds: options.maxAgeSeconds,
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
setCookie(name, value, attributes);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Expires the anonymous-id cookie on the current response — the server half of a consent withdrawal.
|
|
74
|
+
*
|
|
75
|
+
* @since 1.0.0-canary.6
|
|
76
|
+
*/
|
|
77
|
+
export function clearAnonymousIdResponseCookie(cookieName: string): void {
|
|
78
|
+
const { name, ...attributes } = resolveClearAnonymousIdCookie(cookieName);
|
|
79
|
+
|
|
80
|
+
deleteCookie(name, attributes);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* The client-visible acknowledgement of a recorded receipt — deliberately minimal so the
|
|
85
|
+
* stored PII (`subjectId`, `ipCoarse`) is never echoed back over the wire.
|
|
86
|
+
*
|
|
87
|
+
* @since 1.0.0-canary.7
|
|
88
|
+
*/
|
|
89
|
+
export interface ConsentReceiptAck {
|
|
90
|
+
receiptId: string;
|
|
91
|
+
timestamp: number;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @since 1.0.0-canary.7
|
|
96
|
+
*/
|
|
97
|
+
export interface RecordConsentReceiptFromRequestOptions {
|
|
98
|
+
input: ConsentReceiptInput;
|
|
99
|
+
/**
|
|
100
|
+
* Optional tamper-evidence signer — supply an HMAC/signature over the receipt to stamp an
|
|
101
|
+
* `integrityKey`. Omit it and the stored receipt carries none.
|
|
102
|
+
*/
|
|
103
|
+
sign?: ((receipt: Omit<ConsentReceipt, "integrityKey">) => string) | undefined;
|
|
104
|
+
store: ReceiptStore;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Records a consent receipt for the current request: rejects a body-supplied IP, derives a
|
|
109
|
+
* coarsened one from the connection, builds the server-stamped receipt, appends it to the
|
|
110
|
+
* store, and stamps `cache-control: no-store` — the response carries PII, so no cache
|
|
111
|
+
* (shared or private) may store it. Returns a minimal ack, never the stored record.
|
|
112
|
+
*
|
|
113
|
+
* @throws Error when `input` fails {@link isConsentReceiptInput} (including a body that
|
|
114
|
+
* carries an `ip`/`ipCoarse` field — the server owns IP derivation).
|
|
115
|
+
*
|
|
116
|
+
* @since 1.0.0-canary.7
|
|
117
|
+
*/
|
|
118
|
+
export async function recordConsentReceiptFromRequest(
|
|
119
|
+
options: RecordConsentReceiptFromRequestOptions,
|
|
120
|
+
): Promise<ConsentReceiptAck> {
|
|
121
|
+
setResponseHeader("cache-control", "no-store");
|
|
122
|
+
|
|
123
|
+
if (!isConsentReceiptInput(options.input)) {
|
|
124
|
+
throw new Error("Invalid consent receipt input");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// getRequestIP reads the platform/socket IP, trusting X-Forwarded-For's first hop — safe
|
|
128
|
+
// behind the CDN/proxy this app deploys to, and the maintained path over hand-parsing headers.
|
|
129
|
+
const rawIp = getRequestIP({ xForwardedFor: true });
|
|
130
|
+
const receipt = buildConsentReceipt({ input: options.input, rawIp, sign: options.sign });
|
|
131
|
+
|
|
132
|
+
await options.store.append(receipt);
|
|
133
|
+
|
|
134
|
+
return { receiptId: receipt.receiptId, timestamp: receipt.timestamp };
|
|
135
|
+
}
|