@codefast/tracking 0.5.0-canary.4 → 1.0.0-canary.6
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 +171 -0
- package/README.md +123 -96
- package/dist/client/browser-cookie.d.mts +11 -0
- package/dist/client/browser-cookie.mjs +15 -0
- package/dist/client/consent-runtime.d.mts +56 -0
- package/dist/client/consent-runtime.mjs +35 -0
- package/dist/client/consent-storage.d.mts +5 -0
- package/dist/client/consent-storage.mjs +20 -1
- package/dist/client/consent-withdrawal.d.mts +21 -0
- package/dist/client/consent-withdrawal.mjs +17 -0
- package/dist/client/cookie-anonymous-id.d.mts +37 -0
- package/dist/client/cookie-anonymous-id.mjs +46 -0
- package/dist/client/create-client-tracker.d.mts +11 -16
- package/dist/client/create-client-tracker.mjs +27 -53
- package/dist/client/gpc.d.mts +2 -0
- package/dist/client/gpc.mjs +3 -1
- package/dist/client/index.d.mts +7 -5
- package/dist/client/index.mjs +8 -6
- package/dist/client/initial-consent-store.d.mts +66 -0
- package/dist/client/initial-consent-store.mjs +119 -0
- package/dist/client/is-analytics-allowed.d.mts +29 -0
- package/dist/client/is-analytics-allowed.mjs +23 -0
- package/dist/client/server-persisted-anonymous-id.d.mts +33 -0
- package/dist/client/server-persisted-anonymous-id.mjs +50 -0
- package/dist/core/consent-config.d.mts +27 -0
- package/dist/core/consent-config.mjs +11 -0
- package/dist/core/consent.d.mts +99 -4
- package/dist/core/consent.mjs +99 -4
- package/dist/core/cookie.d.mts +17 -0
- package/dist/core/cookie.mjs +23 -0
- package/dist/core/destination.d.mts +10 -8
- package/dist/core/event-catalog.d.mts +16 -13
- package/dist/core/event-catalog.mjs +19 -1
- package/dist/core/event-id.d.mts +1 -2
- package/dist/core/event-id.mjs +1 -2
- package/dist/core/index.d.mts +6 -4
- package/dist/core/index.mjs +5 -3
- package/dist/core/tracked-event.d.mts +21 -8
- package/dist/destinations/google-analytics.d.mts +100 -48
- package/dist/destinations/google-analytics.mjs +89 -85
- package/dist/destinations/google-consent.d.mts +64 -0
- package/dist/destinations/google-consent.mjs +102 -0
- package/dist/destinations/index.d.mts +2 -5
- package/dist/destinations/index.mjs +2 -5
- package/dist/destinations/shared.d.mts +20 -0
- package/dist/destinations/shared.mjs +13 -0
- package/dist/destinations/vercel-analytics.d.mts +19 -4
- package/dist/destinations/vercel-analytics.mjs +11 -18
- package/dist/import-protection.d.mts +19 -0
- package/dist/import-protection.mjs +23 -0
- package/dist/index.d.mts +6 -4
- package/dist/index.mjs +5 -3
- package/dist/react/consent-banner.d.mts +143 -38
- package/dist/react/consent-banner.mjs +192 -92
- package/dist/react/gtag-consent-bootstrap.d.mts +27 -0
- package/dist/react/gtag-consent-bootstrap.mjs +29 -0
- package/dist/react/index.d.mts +5 -2
- package/dist/react/index.mjs +5 -2
- package/dist/react/use-consent.d.mts +9 -13
- package/dist/react/use-consent.mjs +42 -25
- package/dist/react/use-google-consent-sync.d.mts +23 -0
- package/dist/react/use-google-consent-sync.mjs +24 -0
- package/dist/react/use-initial-consent.d.mts +14 -0
- package/dist/react/use-initial-consent.mjs +19 -0
- package/dist/server/anonymous-id-cookie.d.mts +46 -0
- package/dist/server/anonymous-id-cookie.mjs +51 -0
- package/dist/server/index.d.mts +5 -3
- package/dist/server/index.mjs +4 -3
- package/dist/server/initial-consent.d.mts +29 -0
- package/dist/server/initial-consent.mjs +31 -0
- package/dist/server/region.d.mts +21 -4
- package/dist/server/region.mjs +26 -5
- package/dist/tanstack-start/index.d.mts +58 -0
- package/dist/tanstack-start/index.mjs +45 -0
- package/package.json +27 -110
- 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 +93 -0
- package/src/client/create-client-tracker.ts +56 -85
- package/src/client/gpc.ts +4 -2
- package/src/client/index.ts +19 -8
- 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 +99 -0
- package/src/core/consent-config.ts +27 -0
- package/src/core/consent.ts +164 -10
- package/src/core/cookie.ts +29 -0
- package/src/core/destination.ts +10 -8
- package/src/core/event-catalog.ts +28 -15
- package/src/core/event-id.ts +1 -2
- package/src/core/index.ts +19 -3
- package/src/core/tracked-event.ts +22 -7
- package/src/css/consent.css +119 -0
- package/src/destinations/google-analytics.ts +196 -133
- package/src/destinations/google-consent.ts +164 -0
- package/src/destinations/index.ts +14 -15
- package/src/destinations/shared.ts +45 -0
- package/src/destinations/vercel-analytics.ts +23 -24
- package/src/import-protection.ts +20 -0
- package/src/index.ts +46 -8
- package/src/react/consent-banner.tsx +308 -111
- package/src/react/gtag-consent-bootstrap.tsx +48 -0
- package/src/react/index.ts +34 -2
- 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 +76 -0
- package/src/server/index.ts +15 -3
- package/src/server/initial-consent.ts +48 -0
- package/src/server/region.ts +22 -5
- package/src/tanstack-start/index.ts +83 -0
- 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/destinations/ga4-measurement-protocol.d.mts +0 -54
- package/dist/destinations/ga4-measurement-protocol.mjs +0 -89
- package/dist/destinations/http-destination.d.mts +0 -20
- package/dist/destinations/http-destination.mjs +0 -25
- package/dist/server/create-server-tracker.d.mts +0 -42
- package/dist/server/create-server-tracker.mjs +0 -67
- 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/destinations/ga4-measurement-protocol.ts +0 -166
- package/src/destinations/http-destination.ts +0 -33
- package/src/server/create-server-tracker.ts +0 -128
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import type { ConsentCategory, ConsentDecision } from "#/core/consent";
|
|
2
|
+
import { CONSENT_CATEGORIES } from "#/core/consent";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* gtag.js/gtm.js's default queue-array name — shared so the gtag and GTM helpers cannot disagree.
|
|
6
|
+
*
|
|
7
|
+
* @since 1.0.0-canary.6
|
|
8
|
+
*/
|
|
9
|
+
export const DEFAULT_DATA_LAYER_NAME = "dataLayer";
|
|
10
|
+
|
|
11
|
+
type GoogleConsentState = "denied" | "granted";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Consent Mode v2 params issued via `gtag('consent', …)` — shared by the gtag and GTM
|
|
15
|
+
* bootstraps so signal mapping cannot drift between them.
|
|
16
|
+
*
|
|
17
|
+
* @since 1.0.0-canary.6
|
|
18
|
+
*/
|
|
19
|
+
export interface GoogleConsentParams {
|
|
20
|
+
ad_personalization: GoogleConsentState;
|
|
21
|
+
ad_storage: GoogleConsentState;
|
|
22
|
+
ad_user_data: GoogleConsentState;
|
|
23
|
+
analytics_storage: GoogleConsentState;
|
|
24
|
+
region?: ReadonlyArray<string> | undefined;
|
|
25
|
+
wait_for_update?: number | undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Which per-category decision each Consent Mode v2 signal follows: `analytics` drives
|
|
30
|
+
* `analytics_storage`; `ads` drives all three ads signals together.
|
|
31
|
+
*/
|
|
32
|
+
const GOOGLE_CONSENT_SIGNAL_CATEGORIES = {
|
|
33
|
+
ad_personalization: "ads",
|
|
34
|
+
ad_storage: "ads",
|
|
35
|
+
ad_user_data: "ads",
|
|
36
|
+
analytics_storage: "analytics",
|
|
37
|
+
} as const satisfies Record<
|
|
38
|
+
"ad_personalization" | "ad_storage" | "ad_user_data" | "analytics_storage",
|
|
39
|
+
ConsentCategory
|
|
40
|
+
>;
|
|
41
|
+
|
|
42
|
+
type GoogleConsentSignal = keyof typeof GOOGLE_CONSENT_SIGNAL_CATEGORIES;
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Maps a package `ConsentDecision` onto Consent Mode v2's four storage signals.
|
|
46
|
+
*
|
|
47
|
+
* @since 1.0.0-canary.6
|
|
48
|
+
*/
|
|
49
|
+
export function toGoogleConsentParams(decision: ConsentDecision): GoogleConsentParams {
|
|
50
|
+
// Derived from the signal map (not hand-written per signal) so the runtime params and
|
|
51
|
+
// the bootstrap fragment below can never disagree.
|
|
52
|
+
return {
|
|
53
|
+
ad_personalization: decision[GOOGLE_CONSENT_SIGNAL_CATEGORIES.ad_personalization] ? "granted" : "denied",
|
|
54
|
+
ad_storage: decision[GOOGLE_CONSENT_SIGNAL_CATEGORIES.ad_storage] ? "granted" : "denied",
|
|
55
|
+
ad_user_data: decision[GOOGLE_CONSENT_SIGNAL_CATEGORIES.ad_user_data] ? "granted" : "denied",
|
|
56
|
+
analytics_storage: decision[GOOGLE_CONSENT_SIGNAL_CATEGORIES.analytics_storage] ? "granted" : "denied",
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** JS fragment that validates a stored `ConsentRecord.decision` shape inside a bootstrap. */
|
|
61
|
+
function consentDecisionShapeCheckExpression(): string {
|
|
62
|
+
return CONSENT_CATEGORIES.map((category) => `typeof record.decision[${JSON.stringify(category)}] === "boolean"`).join(
|
|
63
|
+
" && ",
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* JS object-literal fragment for `gtag("consent", "default", { … })` — generated from
|
|
69
|
+
* {@link GOOGLE_CONSENT_SIGNAL_CATEGORIES} so runtime and bootstrap stay aligned.
|
|
70
|
+
*/
|
|
71
|
+
function consentSignalAssignmentsExpression(): string {
|
|
72
|
+
return (Object.keys(GOOGLE_CONSENT_SIGNAL_CATEGORIES) as Array<GoogleConsentSignal>)
|
|
73
|
+
.map((signal) => {
|
|
74
|
+
const category = GOOGLE_CONSENT_SIGNAL_CATEGORIES[signal];
|
|
75
|
+
|
|
76
|
+
return `${signal}: consent.${category} ? "granted" : "denied"`;
|
|
77
|
+
})
|
|
78
|
+
.join(",\n ");
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @since 1.0.0-canary.6
|
|
83
|
+
*/
|
|
84
|
+
export interface GoogleConsentBootstrapPreambleOptions {
|
|
85
|
+
/** localStorage key holding the package's `ConsentRecord`. */
|
|
86
|
+
consentStorageKey: string;
|
|
87
|
+
dataLayerName: string;
|
|
88
|
+
/** Literal fallback when nothing valid is stored — baked into the script as JSON. */
|
|
89
|
+
defaultConsent: ConsentDecision;
|
|
90
|
+
policyVersion: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Shared head of the gtag and GTM consent bootstraps: dataLayer init, `gtag()` stub,
|
|
95
|
+
* stored-decision read (policy-version + shape checked), and the Consent Mode v2
|
|
96
|
+
* "default" signal. The builders append only their tag-specific tail, so the consent
|
|
97
|
+
* logic cannot drift between them.
|
|
98
|
+
*
|
|
99
|
+
* @since 1.0.0-canary.6
|
|
100
|
+
*/
|
|
101
|
+
export function buildGoogleConsentBootstrapPreamble(options: GoogleConsentBootstrapPreambleOptions): string {
|
|
102
|
+
const dataLayerAccess = `window[${JSON.stringify(options.dataLayerName)}]`;
|
|
103
|
+
|
|
104
|
+
return `
|
|
105
|
+
${dataLayerAccess} = ${dataLayerAccess} || [];
|
|
106
|
+
function gtag(){${dataLayerAccess}.push(arguments);}
|
|
107
|
+
var storedConsent = null;
|
|
108
|
+
try {
|
|
109
|
+
var record = JSON.parse(window.localStorage.getItem(${JSON.stringify(options.consentStorageKey)}));
|
|
110
|
+
if (record && record.policyVersion === ${JSON.stringify(options.policyVersion)} && record.decision && ${consentDecisionShapeCheckExpression()}) {
|
|
111
|
+
storedConsent = record.decision;
|
|
112
|
+
}
|
|
113
|
+
} catch (e) {}
|
|
114
|
+
var consent = storedConsent || (${JSON.stringify(options.defaultConsent)});
|
|
115
|
+
gtag("consent", "default", {
|
|
116
|
+
${consentSignalAssignmentsExpression()}
|
|
117
|
+
});`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Reads or creates a named `window[dataLayerName]` queue array.
|
|
122
|
+
*
|
|
123
|
+
* @since 1.0.0-canary.6
|
|
124
|
+
*/
|
|
125
|
+
export function ensureDataLayer(dataLayerName: string): Array<unknown> | undefined {
|
|
126
|
+
if (typeof window === "undefined") {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const existing = Reflect.get(window, dataLayerName);
|
|
131
|
+
const layer: Array<unknown> = Array.isArray(existing) ? existing : [];
|
|
132
|
+
|
|
133
|
+
Reflect.set(window, dataLayerName, layer);
|
|
134
|
+
|
|
135
|
+
return layer;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* GA4's event-name rules (letter start, letters/digits/underscores only, ≤40 chars).
|
|
140
|
+
* Catalog `track` names that fail this are accepted by gtag/MP HTTP but dropped at GA4
|
|
141
|
+
* processing with no client error — destinations warn and skip instead of sending nowhere.
|
|
142
|
+
*/
|
|
143
|
+
const GA4_EVENT_NAME_PATTERN = /^[A-Za-z][\w]{0,39}$/;
|
|
144
|
+
|
|
145
|
+
function isGa4EventName(name: string): boolean {
|
|
146
|
+
return GA4_EVENT_NAME_PATTERN.test(name);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Warns and returns `false` when `eventName` would be rejected by GA4.
|
|
151
|
+
*
|
|
152
|
+
* @since 1.0.0-canary.6
|
|
153
|
+
*/
|
|
154
|
+
export function warnUnlessGa4EventName(destinationName: string, eventName: string): boolean {
|
|
155
|
+
if (isGa4EventName(eventName)) {
|
|
156
|
+
return true;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
console.warn(
|
|
160
|
+
`[tracking] "${destinationName}" dropped event "${eventName}" — GA4 event names must start with a letter and contain only letters, digits, or underscores (max 40 chars)`,
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
return false;
|
|
164
|
+
}
|
|
@@ -1,20 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
extractGa4ClientId,
|
|
5
|
-
extractGa4SessionId,
|
|
6
|
-
} from "#/destinations/ga4-measurement-protocol";
|
|
1
|
+
// Browser-lane barrel. Deliberately NOT re-exported here:
|
|
2
|
+
// - `#/destinations/vercel-analytics` — its top-level `@vercel/analytics` import would make
|
|
3
|
+
// the optional peer mandatory for every barrel consumer; import it via its own subpath.
|
|
7
4
|
|
|
8
|
-
export type {
|
|
5
|
+
export type {
|
|
6
|
+
EnsureGtagOptions,
|
|
7
|
+
GoogleAnalyticsDestinationOptions,
|
|
8
|
+
GtagConsentBootstrapOptions,
|
|
9
|
+
GtagFunction,
|
|
10
|
+
LoadGtagScriptOptions,
|
|
11
|
+
} from "#/destinations/google-analytics";
|
|
9
12
|
export {
|
|
13
|
+
buildGtagConsentBootstrapScript,
|
|
14
|
+
clearGoogleAnalyticsCookies,
|
|
10
15
|
createGoogleAnalyticsDestination,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
setGoogleUrlPassthrough,
|
|
16
|
+
ensureGtag,
|
|
17
|
+
loadGtagScript,
|
|
14
18
|
updateGoogleConsent,
|
|
15
19
|
} 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";
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @since 1.0.0-canary.6
|
|
3
|
+
*/
|
|
4
|
+
export type FlatPropertyValue = boolean | number | string;
|
|
5
|
+
|
|
6
|
+
interface FlattenEventPropsOptions {
|
|
7
|
+
/** Vercel Analytics accepts `null` directly; GA4 (gtag.js/Measurement Protocol) does not. */
|
|
8
|
+
allowNull?: boolean | undefined;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Event params must be flat string/number/boolean (optionally `null`) — stringify
|
|
13
|
+
* anything else instead of letting the destination silently drop it.
|
|
14
|
+
*
|
|
15
|
+
* @since 1.0.0-canary.6
|
|
16
|
+
*/
|
|
17
|
+
export function flattenEventProps(properties: Record<string, unknown>): Record<string, FlatPropertyValue>;
|
|
18
|
+
/**
|
|
19
|
+
* @since 1.0.0-canary.6
|
|
20
|
+
*/
|
|
21
|
+
export function flattenEventProps(
|
|
22
|
+
properties: Record<string, unknown>,
|
|
23
|
+
options: { allowNull: true },
|
|
24
|
+
): Record<string, FlatPropertyValue | null>;
|
|
25
|
+
/**
|
|
26
|
+
* @since 1.0.0-canary.6
|
|
27
|
+
*/
|
|
28
|
+
export function flattenEventProps(
|
|
29
|
+
properties: Record<string, unknown>,
|
|
30
|
+
options: FlattenEventPropsOptions = {},
|
|
31
|
+
): Record<string, FlatPropertyValue | null> {
|
|
32
|
+
const result: Record<string, FlatPropertyValue | null> = {};
|
|
33
|
+
|
|
34
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
35
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
36
|
+
result[key] = value;
|
|
37
|
+
} else if (options.allowNull === true && value === null) {
|
|
38
|
+
result[key] = null;
|
|
39
|
+
} else if (value !== undefined && value !== null) {
|
|
40
|
+
result[key] = JSON.stringify(value);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
@@ -1,40 +1,39 @@
|
|
|
1
|
-
import { track } from "@vercel/analytics
|
|
1
|
+
import { track } from "@vercel/analytics";
|
|
2
2
|
|
|
3
3
|
import type { Destination } from "#/core/destination";
|
|
4
|
-
|
|
5
|
-
type VercelAnalyticsPropertyValue = boolean | number | string | null;
|
|
4
|
+
import { flattenEventProps } from "#/destinations/shared";
|
|
6
5
|
|
|
7
6
|
/**
|
|
8
|
-
*
|
|
9
|
-
* to a string instead of silently dropping it.
|
|
7
|
+
* @since 1.0.0-canary.6
|
|
10
8
|
*/
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return result;
|
|
9
|
+
export interface VercelAnalyticsDestinationOptions {
|
|
10
|
+
/**
|
|
11
|
+
* Vercel Analytics is cookieless and its events carry no identifier from this
|
|
12
|
+
* package, so it qualifies for `"exempt"` — it then keeps receiving events while the
|
|
13
|
+
* tracker's analytics gate is closed. Defaults to `"required"` so pre-consent delivery
|
|
14
|
+
* stays an explicit opt-in.
|
|
15
|
+
*/
|
|
16
|
+
consentRequirement?: "exempt" | "required" | undefined;
|
|
17
|
+
name?: string | undefined;
|
|
23
18
|
}
|
|
24
19
|
|
|
25
20
|
/**
|
|
26
21
|
* `track()` only pushes onto Vercel's own in-page queue (`window.va`/`window.vaq`), which
|
|
27
|
-
* handles batching and unload delivery itself
|
|
28
|
-
* `<Analytics />` from `@vercel/analytics/react
|
|
22
|
+
* handles batching and unload delivery itself. Requires the Vercel Analytics script to be
|
|
23
|
+
* loaded once elsewhere in the app (`<Analytics />` from `@vercel/analytics/react`, or the
|
|
24
|
+
* framework-agnostic `inject()`) — this destination only queues events, so it imports the
|
|
25
|
+
* base `@vercel/analytics` package and has no React dependency of its own.
|
|
29
26
|
*
|
|
30
27
|
* @since 0.5.0-canary.4
|
|
31
28
|
*/
|
|
32
|
-
export function createVercelAnalyticsDestination(
|
|
29
|
+
export function createVercelAnalyticsDestination(options: VercelAnalyticsDestinationOptions = {}): Destination {
|
|
33
30
|
return {
|
|
34
|
-
|
|
35
|
-
name,
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
consentRequirement: options.consentRequirement ?? "required",
|
|
32
|
+
name: options.name ?? "vercel-analytics",
|
|
33
|
+
// Synchronous today, but declared async so a throw here rejects the returned Promise
|
|
34
|
+
// instead of escaping as a synchronous throw — matches Destination.send's contract.
|
|
35
|
+
async send(event) {
|
|
36
|
+
track(event.name, flattenEventProps(event.properties, { allowNull: true }));
|
|
38
37
|
},
|
|
39
38
|
};
|
|
40
39
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Import specifiers (picomatch patterns) every client bundle must deny — spread into
|
|
3
|
+
* TanStack Start's `importProtection.client.specifiers` so the deny-list ships and
|
|
4
|
+
* versions with the package instead of going stale in each consumer's config. Nothing in
|
|
5
|
+
* this package imports it — its consumers are app build configs (see apps/ui vite.config.ts).
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* tanstackStart({
|
|
10
|
+
* importProtection: { client: { specifiers: [...SERVER_ONLY_IMPORT_SPECIFIERS] } },
|
|
11
|
+
* });
|
|
12
|
+
* ```
|
|
13
|
+
*
|
|
14
|
+
* @since 1.0.0-canary.6
|
|
15
|
+
*/
|
|
16
|
+
export const SERVER_ONLY_IMPORT_SPECIFIERS: ReadonlyArray<string> = [
|
|
17
|
+
"@codefast/tracking/server",
|
|
18
|
+
"@codefast/tracking/server/**",
|
|
19
|
+
"@codefast/tracking/tanstack-start",
|
|
20
|
+
];
|
package/src/index.ts
CHANGED
|
@@ -1,13 +1,51 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* \@codefast/tracking
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
4
|
+
* Consent-gated, type-safe event tracking for TanStack Start apps: define an event
|
|
5
|
+
* catalog over any Standard Schema library, wire a `ConsentConfig` + `createConsentRuntime`,
|
|
6
|
+
* and fan tracked events out to gtag / Vercel Analytics destinations.
|
|
7
7
|
*
|
|
8
|
-
* - **Root** (this module): isomorphic catalog types, `Destination`, consent
|
|
9
|
-
* - **`@codefast/tracking/client`**: `createClientTracker`
|
|
10
|
-
* - **`@codefast/tracking/server`**: `
|
|
11
|
-
* - **`@codefast/tracking/
|
|
8
|
+
* - **Root** (this module): isomorphic catalog types, `Destination`, consent model + config — safe to import from both client and server code.
|
|
9
|
+
* - **`@codefast/tracking/client`**: `createClientTracker`, `createConsentRuntime`, anonymous-id helpers.
|
|
10
|
+
* - **`@codefast/tracking/server`**: region → initial-consent resolution, anonymous-id `Set-Cookie` builders. Server-only — never import from client code.
|
|
11
|
+
* - **`@codefast/tracking/tanstack-start`**: request/response glue over TanStack Start's server context. Server-only — deny it in the client environment via Start's `importProtection`.
|
|
12
|
+
* - **`@codefast/tracking/destinations`**: the gtag destination, script loader, and Consent Mode bootstrap; Vercel Analytics lives on its own subpath.
|
|
12
13
|
*/
|
|
13
|
-
export
|
|
14
|
+
export type {
|
|
15
|
+
ConsentCategory,
|
|
16
|
+
ConsentConfig,
|
|
17
|
+
ConsentDecision,
|
|
18
|
+
ConsentMode,
|
|
19
|
+
ConsentRecord,
|
|
20
|
+
ConsentRegion,
|
|
21
|
+
ConsentStorage,
|
|
22
|
+
Destination,
|
|
23
|
+
EventCatalog,
|
|
24
|
+
EventDefinition,
|
|
25
|
+
InitialConsent,
|
|
26
|
+
ResolveDefaultConsentOptions,
|
|
27
|
+
ResolveEffectiveConsentOptions,
|
|
28
|
+
TrackedEvent,
|
|
29
|
+
TrackedEventBase,
|
|
30
|
+
TrackEvent,
|
|
31
|
+
} from "#/core";
|
|
32
|
+
export {
|
|
33
|
+
assertValidEventProperties,
|
|
34
|
+
CONSENT_CATEGORIES,
|
|
35
|
+
CONSENT_REGIONS,
|
|
36
|
+
createConsentDecision,
|
|
37
|
+
defineConsentConfig,
|
|
38
|
+
defineEventCatalog,
|
|
39
|
+
generateEventId,
|
|
40
|
+
isConsentDecision,
|
|
41
|
+
isConsentRecord,
|
|
42
|
+
isConsentRegion,
|
|
43
|
+
isInitialConsent,
|
|
44
|
+
normalizeConsentDecision,
|
|
45
|
+
readCookieValue,
|
|
46
|
+
readStoredDecision,
|
|
47
|
+
resolveConsentMode,
|
|
48
|
+
resolveDefaultConsent,
|
|
49
|
+
resolveEffectiveConsent,
|
|
50
|
+
STRICTEST_INITIAL_CONSENT,
|
|
51
|
+
} from "#/core";
|