@codefast/tracking 0.7.0 → 0.7.2
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 +21 -0
- package/README.md +235 -105
- package/dist/adapters/tanstack-start.d.ts +12 -9
- package/dist/adapters/tanstack-start.js +5 -6
- package/dist/client/ad-framework-consent.d.ts +9 -8
- package/dist/client/ad-framework-consent.js +4 -5
- package/dist/client/browser-cookie.d.ts +2 -3
- package/dist/client/browser-cookie.js +2 -3
- package/dist/client/consent-runtime.d.ts +6 -5
- package/dist/client/consent-runtime.js +2 -3
- package/dist/client/consent-storage.d.ts +1 -2
- package/dist/client/consent-storage.js +1 -2
- package/dist/client/consent-withdrawal.d.ts +5 -4
- package/dist/client/consent-withdrawal.js +2 -3
- package/dist/client/cookie-anonymous-id.d.ts +8 -5
- package/dist/client/cookie-anonymous-id.js +2 -3
- package/dist/client/create-client-tracker.d.ts +8 -3
- package/dist/client/create-client-tracker.js +3 -2
- package/dist/client/gpc.d.ts +1 -2
- package/dist/client/gpc.js +1 -2
- package/dist/client/initial-consent-store.d.ts +7 -6
- package/dist/client/initial-consent-store.js +2 -3
- package/dist/client/is-analytics-allowed.d.ts +5 -4
- package/dist/client/is-analytics-allowed.js +2 -3
- package/dist/client/server-persisted-anonymous-id.d.ts +5 -4
- package/dist/client/server-persisted-anonymous-id.js +2 -3
- package/dist/core/consent-config.d.ts +3 -4
- package/dist/core/consent-config.js +2 -3
- package/dist/core/consent-receipt.d.ts +7 -8
- package/dist/core/consent-receipt.js +2 -3
- package/dist/core/consent.d.ts +24 -13
- package/dist/core/consent.js +13 -10
- package/dist/core/cookie.d.ts +3 -4
- package/dist/core/cookie.js +3 -4
- package/dist/core/destination.d.ts +1 -2
- package/dist/core/destination.js +1 -2
- package/dist/core/event-catalog.d.ts +2 -3
- package/dist/core/event-catalog.js +2 -3
- package/dist/core/event-id.d.ts +1 -2
- package/dist/core/event-id.js +1 -2
- package/dist/core/tracked-event.d.ts +1 -2
- package/dist/core/tracked-event.js +1 -2
- package/dist/destinations/ad-consent.d.ts +3 -4
- package/dist/destinations/ad-consent.js +2 -3
- package/dist/destinations/google-analytics.d.ts +17 -10
- package/dist/destinations/google-analytics.js +5 -6
- package/dist/destinations/google-consent.d.ts +10 -9
- package/dist/destinations/google-consent.js +6 -7
- package/dist/destinations/meta.d.ts +8 -7
- package/dist/destinations/meta.js +3 -4
- package/dist/destinations/microsoft-uet.d.ts +8 -7
- package/dist/destinations/microsoft-uet.js +3 -4
- package/dist/destinations/shared.d.ts +8 -5
- package/dist/destinations/shared.js +4 -3
- package/dist/destinations/tiktok.d.ts +8 -7
- package/dist/destinations/tiktok.js +3 -4
- package/dist/destinations/vercel-analytics.d.ts +4 -3
- package/dist/destinations/vercel-analytics.js +1 -2
- package/dist/index.d.ts +1 -2
- package/dist/index.js +1 -2
- package/dist/react/consent-banner.d.ts +29 -2
- package/dist/react/consent-banner.js +12 -9
- package/dist/react/gtag-consent-bootstrap.d.ts +5 -4
- package/dist/react/gtag-consent-bootstrap.js +2 -3
- package/dist/react/use-consent.d.ts +5 -2
- package/dist/react/use-consent.js +1 -2
- package/dist/react/use-google-consent-sync.d.ts +5 -4
- package/dist/react/use-google-consent-sync.js +2 -3
- package/dist/react/use-initial-consent.d.ts +2 -3
- package/dist/react/use-initial-consent.js +2 -3
- package/dist/server/anonymous-id-cookie.d.ts +8 -7
- package/dist/server/anonymous-id-cookie.js +5 -6
- package/dist/server/consent-receipt-store.d.ts +5 -6
- package/dist/server/consent-receipt-store.js +3 -4
- package/dist/server/consent-receipt.d.ts +5 -6
- package/dist/server/consent-receipt.js +4 -5
- package/dist/server/initial-consent.d.ts +5 -4
- package/dist/server/initial-consent.js +2 -3
- package/dist/server/measurement-protocol.d.ts +10 -9
- package/dist/server/measurement-protocol.js +4 -5
- package/dist/server/region.d.ts +3 -4
- package/dist/server/region.js +3 -4
- package/dist/server/user-deletion.d.ts +10 -7
- package/dist/server/user-deletion.js +3 -4
- package/dist/tooling/import-protection.d.ts +2 -3
- package/dist/tooling/import-protection.js +2 -3
- package/package.json +11 -62
- package/dist/adapters/tanstack-start.d.ts.map +0 -1
- package/dist/adapters/tanstack-start.js.map +0 -1
- package/dist/client/ad-framework-consent.d.ts.map +0 -1
- package/dist/client/ad-framework-consent.js.map +0 -1
- package/dist/client/browser-cookie.d.ts.map +0 -1
- package/dist/client/browser-cookie.js.map +0 -1
- package/dist/client/consent-runtime.d.ts.map +0 -1
- package/dist/client/consent-runtime.js.map +0 -1
- package/dist/client/consent-storage.d.ts.map +0 -1
- package/dist/client/consent-storage.js.map +0 -1
- package/dist/client/consent-withdrawal.d.ts.map +0 -1
- package/dist/client/consent-withdrawal.js.map +0 -1
- package/dist/client/cookie-anonymous-id.d.ts.map +0 -1
- package/dist/client/cookie-anonymous-id.js.map +0 -1
- package/dist/client/create-client-tracker.d.ts.map +0 -1
- package/dist/client/create-client-tracker.js.map +0 -1
- package/dist/client/gpc.d.ts.map +0 -1
- package/dist/client/gpc.js.map +0 -1
- package/dist/client/initial-consent-store.d.ts.map +0 -1
- package/dist/client/initial-consent-store.js.map +0 -1
- package/dist/client/is-analytics-allowed.d.ts.map +0 -1
- package/dist/client/is-analytics-allowed.js.map +0 -1
- package/dist/client/server-persisted-anonymous-id.d.ts.map +0 -1
- package/dist/client/server-persisted-anonymous-id.js.map +0 -1
- package/dist/core/consent-config.d.ts.map +0 -1
- package/dist/core/consent-config.js.map +0 -1
- package/dist/core/consent-receipt.d.ts.map +0 -1
- package/dist/core/consent-receipt.js.map +0 -1
- package/dist/core/consent.d.ts.map +0 -1
- package/dist/core/consent.js.map +0 -1
- package/dist/core/cookie.d.ts.map +0 -1
- package/dist/core/cookie.js.map +0 -1
- package/dist/core/destination.d.ts.map +0 -1
- package/dist/core/destination.js.map +0 -1
- package/dist/core/event-catalog.d.ts.map +0 -1
- package/dist/core/event-catalog.js.map +0 -1
- package/dist/core/event-id.d.ts.map +0 -1
- package/dist/core/event-id.js.map +0 -1
- package/dist/core/tracked-event.d.ts.map +0 -1
- package/dist/core/tracked-event.js.map +0 -1
- package/dist/destinations/ad-consent.d.ts.map +0 -1
- package/dist/destinations/ad-consent.js.map +0 -1
- package/dist/destinations/google-analytics.d.ts.map +0 -1
- package/dist/destinations/google-analytics.js.map +0 -1
- package/dist/destinations/google-consent.d.ts.map +0 -1
- package/dist/destinations/google-consent.js.map +0 -1
- package/dist/destinations/meta.d.ts.map +0 -1
- package/dist/destinations/meta.js.map +0 -1
- package/dist/destinations/microsoft-uet.d.ts.map +0 -1
- package/dist/destinations/microsoft-uet.js.map +0 -1
- package/dist/destinations/shared.d.ts.map +0 -1
- package/dist/destinations/shared.js.map +0 -1
- package/dist/destinations/tiktok.d.ts.map +0 -1
- package/dist/destinations/tiktok.js.map +0 -1
- package/dist/destinations/vercel-analytics.d.ts.map +0 -1
- package/dist/destinations/vercel-analytics.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/react/consent-banner.d.ts.map +0 -1
- package/dist/react/consent-banner.js.map +0 -1
- package/dist/react/gtag-consent-bootstrap.d.ts.map +0 -1
- package/dist/react/gtag-consent-bootstrap.js.map +0 -1
- package/dist/react/use-consent.d.ts.map +0 -1
- package/dist/react/use-consent.js.map +0 -1
- package/dist/react/use-google-consent-sync.d.ts.map +0 -1
- package/dist/react/use-google-consent-sync.js.map +0 -1
- package/dist/react/use-initial-consent.d.ts.map +0 -1
- package/dist/react/use-initial-consent.js.map +0 -1
- package/dist/server/anonymous-id-cookie.d.ts.map +0 -1
- package/dist/server/anonymous-id-cookie.js.map +0 -1
- package/dist/server/consent-receipt-store.d.ts.map +0 -1
- package/dist/server/consent-receipt-store.js.map +0 -1
- package/dist/server/consent-receipt.d.ts.map +0 -1
- package/dist/server/consent-receipt.js.map +0 -1
- package/dist/server/initial-consent.d.ts.map +0 -1
- package/dist/server/initial-consent.js.map +0 -1
- package/dist/server/measurement-protocol.d.ts.map +0 -1
- package/dist/server/measurement-protocol.js.map +0 -1
- package/dist/server/region.d.ts.map +0 -1
- package/dist/server/region.js.map +0 -1
- package/dist/server/user-deletion.d.ts.map +0 -1
- package/dist/server/user-deletion.js.map +0 -1
- package/dist/tooling/import-protection.d.ts.map +0 -1
- package/dist/tooling/import-protection.js.map +0 -1
- package/src/adapters/tanstack-start.ts +0 -135
- package/src/client/ad-framework-consent.ts +0 -86
- package/src/client/browser-cookie.ts +0 -16
- package/src/client/consent-runtime.ts +0 -80
- package/src/client/consent-storage.ts +0 -125
- package/src/client/consent-withdrawal.ts +0 -32
- package/src/client/cookie-anonymous-id.ts +0 -112
- package/src/client/create-client-tracker.ts +0 -168
- package/src/client/gpc.ts +0 -15
- package/src/client/initial-consent-store.ts +0 -218
- package/src/client/is-analytics-allowed.ts +0 -43
- package/src/client/server-persisted-anonymous-id.ts +0 -102
- package/src/core/consent-config.ts +0 -27
- package/src/core/consent-receipt.ts +0 -115
- package/src/core/consent.ts +0 -286
- package/src/core/cookie.ts +0 -29
- package/src/core/destination.ts +0 -29
- package/src/core/event-catalog.ts +0 -54
- package/src/core/event-id.ts +0 -8
- package/src/core/tracked-event.ts +0 -14
- package/src/css/consent.css +0 -119
- package/src/destinations/ad-consent.ts +0 -29
- package/src/destinations/google-analytics.ts +0 -296
- package/src/destinations/google-consent.ts +0 -164
- package/src/destinations/meta.ts +0 -96
- package/src/destinations/microsoft-uet.ts +0 -86
- package/src/destinations/shared.ts +0 -45
- package/src/destinations/tiktok.ts +0 -85
- package/src/destinations/vercel-analytics.ts +0 -39
- package/src/index.ts +0 -171
- package/src/react/consent-banner.tsx +0 -360
- package/src/react/gtag-consent-bootstrap.tsx +0 -48
- package/src/react/use-consent.ts +0 -119
- package/src/react/use-google-consent-sync.ts +0 -38
- package/src/react/use-initial-consent.ts +0 -22
- package/src/server/anonymous-id-cookie.ts +0 -103
- package/src/server/consent-receipt-store.ts +0 -79
- package/src/server/consent-receipt.ts +0 -89
- package/src/server/initial-consent.ts +0 -46
- package/src/server/measurement-protocol.ts +0 -114
- package/src/server/region.ts +0 -77
- package/src/server/user-deletion.ts +0 -93
- package/src/tooling/import-protection.ts +0 -22
|
@@ -1,296 +0,0 @@
|
|
|
1
|
-
import type { ConsentDecision } from "#/core/consent";
|
|
2
|
-
import type { ConsentConfig } from "#/core/consent-config";
|
|
3
|
-
import type { Destination } from "#/core/destination";
|
|
4
|
-
import type { GoogleConsentParams } from "#/destinations/google-consent";
|
|
5
|
-
import {
|
|
6
|
-
buildGoogleConsentBootstrapPreamble,
|
|
7
|
-
DEFAULT_DATA_LAYER_NAME,
|
|
8
|
-
ensureDataLayer,
|
|
9
|
-
toGoogleConsentParams,
|
|
10
|
-
warnUnlessGa4EventName,
|
|
11
|
-
} from "#/destinations/google-consent";
|
|
12
|
-
import type { FlatPropertyValue } from "#/destinations/shared";
|
|
13
|
-
import { flattenEventProps } from "#/destinations/shared";
|
|
14
|
-
|
|
15
|
-
const GTAG_SCRIPT_BASE_URL = "https://www.googletagmanager.com/gtag/js";
|
|
16
|
-
|
|
17
|
-
type GtagPropertyValue = FlatPropertyValue;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* All signatures share one `gtag` global, so they must live in a single overloaded type —
|
|
21
|
-
* two separate `declare global` augmentations with different signatures for the same
|
|
22
|
-
* property is a TS error, not a merge.
|
|
23
|
-
*
|
|
24
|
-
* @since 1.0.0-canary.6
|
|
25
|
-
*/
|
|
26
|
-
export type GtagFunction = {
|
|
27
|
-
(command: "config", targetId: string, params?: Record<string, GtagPropertyValue>): void;
|
|
28
|
-
(command: "consent", action: "default" | "update", params: GoogleConsentParams): void;
|
|
29
|
-
(command: "event", eventName: string, params?: Record<string, GtagPropertyValue>): void;
|
|
30
|
-
(command: "js", startDate: Date): void;
|
|
31
|
-
(command: "set", params: Record<string, GtagPropertyValue>): void;
|
|
32
|
-
(command: "set", key: string, value: boolean): void;
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
declare global {
|
|
36
|
-
interface Window {
|
|
37
|
-
dataLayer?: Array<unknown>;
|
|
38
|
-
gtag?: GtagFunction;
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function gtagScriptSrc(gaMeasurementId: string, dataLayerName: string): string {
|
|
43
|
-
const url = new URL(GTAG_SCRIPT_BASE_URL);
|
|
44
|
-
|
|
45
|
-
url.searchParams.set("id", gaMeasurementId);
|
|
46
|
-
|
|
47
|
-
if (dataLayerName !== DEFAULT_DATA_LAYER_NAME) {
|
|
48
|
-
// gtag.js's `l` param names the queue array — must match the stub that pushes into it.
|
|
49
|
-
url.searchParams.set("l", dataLayerName);
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return url.toString();
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @since 1.0.0-canary.6
|
|
57
|
-
*/
|
|
58
|
-
export interface EnsureGtagOptions {
|
|
59
|
-
/**
|
|
60
|
-
* Name of the queue array on `window`. Defaults to `"dataLayer"`. Must match across
|
|
61
|
-
* `ensureGtag` / `loadGtagScript` / `buildGtagConsentBootstrapScript` for one page.
|
|
62
|
-
*/
|
|
63
|
-
dataLayerName?: string | undefined;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Ensures the standard gtag.js queueing stub exists so consent commands can be issued
|
|
68
|
-
* before the tag itself loads — gtag.js replays the queue in order once it boots.
|
|
69
|
-
*
|
|
70
|
-
* @since 1.0.0-canary.6
|
|
71
|
-
*/
|
|
72
|
-
export function ensureGtag(options: EnsureGtagOptions = {}): GtagFunction | undefined {
|
|
73
|
-
if (typeof window === "undefined") {
|
|
74
|
-
return undefined;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
const dataLayerName = options.dataLayerName ?? DEFAULT_DATA_LAYER_NAME;
|
|
78
|
-
|
|
79
|
-
ensureDataLayer(dataLayerName);
|
|
80
|
-
|
|
81
|
-
// First stub wins — recreating would orphan commands already queued on another layer.
|
|
82
|
-
// Callers must pass the same dataLayerName for every helper on the page.
|
|
83
|
-
window.gtag ??= function gtag() {
|
|
84
|
-
ensureDataLayer(dataLayerName)?.push(arguments);
|
|
85
|
-
} as GtagFunction;
|
|
86
|
-
|
|
87
|
-
return window.gtag;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* @since 1.0.0-canary.6
|
|
92
|
-
*/
|
|
93
|
-
export interface LoadGtagScriptOptions {
|
|
94
|
-
/**
|
|
95
|
-
* Name of the queue array on `window`. Defaults to `"dataLayer"` — must match the
|
|
96
|
-
* bootstrap / `ensureGtag` call that already applied Consent Mode.
|
|
97
|
-
*/
|
|
98
|
-
dataLayerName?: string | undefined;
|
|
99
|
-
/** Forwarded as `gtag('config', id, { debug_mode: true })` for GA4 DebugView. */
|
|
100
|
-
debugMode?: boolean | undefined;
|
|
101
|
-
/** Google Analytics 4 Measurement ID (e.g. `"G-XXXXXXX"`). */
|
|
102
|
-
gaMeasurementId: string;
|
|
103
|
-
/**
|
|
104
|
-
* CSP nonce applied to the injected gtag.js `<script>` element. The app must also put
|
|
105
|
-
* the same nonce on any inline bootstrap `<script>` that runs `buildGtagConsentBootstrapScript`.
|
|
106
|
-
*/
|
|
107
|
-
nonce?: string | undefined;
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Loads gtag.js on demand — idempotent (a second call is a no-op) and reuses any
|
|
112
|
-
* existing `dataLayer`/`gtag` stub instead of clobbering it. Queues `js`/`config` before
|
|
113
|
-
* appending the script tag, so gtag.js replays them under the caller's already-applied
|
|
114
|
-
* consent state once it boots. Prefer `buildGtagConsentBootstrapScript` for page load
|
|
115
|
-
* (advanced Consent Mode always injects the tag after the default); use this when the
|
|
116
|
-
* bootstrap did not run, or as an idempotent safety net after a runtime grant.
|
|
117
|
-
*
|
|
118
|
-
* @since 1.0.0-canary.6
|
|
119
|
-
*/
|
|
120
|
-
export function loadGtagScript(options: LoadGtagScriptOptions): void {
|
|
121
|
-
if (typeof document === "undefined" || document.querySelector(`script[src^="${GTAG_SCRIPT_BASE_URL}"]`) !== null) {
|
|
122
|
-
return;
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
const dataLayerName = options.dataLayerName ?? DEFAULT_DATA_LAYER_NAME;
|
|
126
|
-
const gtag = ensureGtag({ dataLayerName });
|
|
127
|
-
|
|
128
|
-
if (!gtag) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
gtag("js", new Date());
|
|
133
|
-
|
|
134
|
-
if (options.debugMode === true) {
|
|
135
|
-
gtag("config", options.gaMeasurementId, { debug_mode: true });
|
|
136
|
-
} else {
|
|
137
|
-
gtag("config", options.gaMeasurementId);
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
const script = document.createElement("script");
|
|
141
|
-
|
|
142
|
-
script.async = true;
|
|
143
|
-
script.src = gtagScriptSrc(options.gaMeasurementId, dataLayerName);
|
|
144
|
-
|
|
145
|
-
if (options.nonce !== undefined) {
|
|
146
|
-
script.nonce = options.nonce;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
document.head.append(script);
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Expires Google's `_ga` / `_ga_*` cookies on `path=/` and the current host's parent
|
|
154
|
-
* domain. Consent Mode stops using them once denied but never removes them — call from
|
|
155
|
-
* a consent-withdrawal handler so a revoke does not leave identifier cookies behind.
|
|
156
|
-
*
|
|
157
|
-
* @since 1.0.0-canary.6
|
|
158
|
-
*/
|
|
159
|
-
export function clearGoogleAnalyticsCookies(): void {
|
|
160
|
-
if (typeof document === "undefined") {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
const { hostname } = globalThis.location;
|
|
165
|
-
|
|
166
|
-
for (const cookie of document.cookie.split("; ")) {
|
|
167
|
-
const name = cookie.split("=")[0];
|
|
168
|
-
|
|
169
|
-
if (name !== undefined && (name === "_ga" || name.startsWith("_ga_"))) {
|
|
170
|
-
// GA sets its cookies on the broadest domain it can reach — expire both variants.
|
|
171
|
-
document.cookie = `${name}=; path=/; max-age=0`;
|
|
172
|
-
document.cookie = `${name}=; path=/; max-age=0; domain=.${hostname}`;
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Google Consent Mode v2's "update" signal — call whenever the visitor's effective
|
|
179
|
-
* consent changes (a banner decision, or one synced from another tab), so already-loaded
|
|
180
|
-
* GA4/Ads tags pick up the change without a page reload.
|
|
181
|
-
*
|
|
182
|
-
* @since 0.5.0-canary.4
|
|
183
|
-
*/
|
|
184
|
-
export function updateGoogleConsent(decision: ConsentDecision, options: EnsureGtagOptions = {}): void {
|
|
185
|
-
ensureGtag(options)?.("consent", "update", toGoogleConsentParams(decision));
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* @since 1.0.0-canary.6
|
|
190
|
-
*/
|
|
191
|
-
export interface GtagConsentBootstrapOptions {
|
|
192
|
-
/** The same object `useConsent` receives — the bootstrap reads its `storageKey` and `policyVersion`. */
|
|
193
|
-
config: ConsentConfig;
|
|
194
|
-
/**
|
|
195
|
-
* Name of the queue array on `window`. Defaults to `"dataLayer"`. Must match
|
|
196
|
-
* `loadGtagScript` / `ensureGtag` if those run later on the same page.
|
|
197
|
-
*/
|
|
198
|
-
dataLayerName?: string | undefined;
|
|
199
|
-
/**
|
|
200
|
-
* Consent to apply when nothing valid is stored yet. Embedded as a literal — bake the
|
|
201
|
-
* strictest default on cached/shared HTML; upgrade after hydration via the private
|
|
202
|
-
* server-fn lane + `updateGoogleConsent`.
|
|
203
|
-
*/
|
|
204
|
-
defaultConsent: ConsentDecision;
|
|
205
|
-
/** Forwarded as `gtag('config', id, { debug_mode: true })` after the consent default. */
|
|
206
|
-
debugMode?: boolean | undefined;
|
|
207
|
-
/** Google Analytics 4 Measurement ID (e.g. `"G-XXXXXXX"`). */
|
|
208
|
-
gaMeasurementId: string;
|
|
209
|
-
/**
|
|
210
|
-
* CSP nonce written onto the *injected* gtag.js `<script>` element inside the generated
|
|
211
|
-
* source. The host `<script dangerouslySetInnerHTML={…}>` that embeds this string must
|
|
212
|
-
* receive the same nonce from the app (this helper only returns JS text, not a React node).
|
|
213
|
-
*/
|
|
214
|
-
nonce?: string | undefined;
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Builds the literal JS source for a `<script dangerouslySetInnerHTML>` mounted in
|
|
219
|
-
* `<head>`, before hydration: applies Consent Mode v2's default signal from the visitor's
|
|
220
|
-
* stored decision (falling back to literal `defaultConsent` when none is stored yet),
|
|
221
|
-
* then always loads gtag.js (advanced Consent Mode) so cookieless pings / modeling can
|
|
222
|
-
* run even when storage is denied. A runtime decision change needs `updateGoogleConsent`
|
|
223
|
-
* so the already-loaded tag picks up the grant/deny — this only covers the page-load
|
|
224
|
-
* default + script injection.
|
|
225
|
-
*
|
|
226
|
-
* @since 1.0.0-canary.6
|
|
227
|
-
*/
|
|
228
|
-
export function buildGtagConsentBootstrapScript(options: GtagConsentBootstrapOptions): string {
|
|
229
|
-
const {
|
|
230
|
-
config,
|
|
231
|
-
dataLayerName = DEFAULT_DATA_LAYER_NAME,
|
|
232
|
-
defaultConsent,
|
|
233
|
-
debugMode,
|
|
234
|
-
gaMeasurementId,
|
|
235
|
-
nonce,
|
|
236
|
-
} = options;
|
|
237
|
-
|
|
238
|
-
const gtagScriptUrl = gtagScriptSrc(gaMeasurementId, dataLayerName);
|
|
239
|
-
const configArgs =
|
|
240
|
-
debugMode === true ? `${JSON.stringify(gaMeasurementId)}, { debug_mode: true }` : JSON.stringify(gaMeasurementId);
|
|
241
|
-
const nonceAssignment = nonce === undefined ? "" : `gtagScript.nonce = ${JSON.stringify(nonce)};`;
|
|
242
|
-
const preamble = buildGoogleConsentBootstrapPreamble({
|
|
243
|
-
consentStorageKey: config.storageKey,
|
|
244
|
-
dataLayerName,
|
|
245
|
-
defaultConsent,
|
|
246
|
-
policyVersion: config.policyVersion,
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
// Advanced Consent Mode: consent default first, then always load the tag (even when denied).
|
|
250
|
-
return `${preamble}
|
|
251
|
-
gtag("js", new Date());
|
|
252
|
-
gtag("config", ${configArgs});
|
|
253
|
-
var gtagScript = document.createElement("script");
|
|
254
|
-
gtagScript.async = true;
|
|
255
|
-
gtagScript.src = ${JSON.stringify(gtagScriptUrl)};
|
|
256
|
-
${nonceAssignment}
|
|
257
|
-
document.head.appendChild(gtagScript);
|
|
258
|
-
`;
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
/**
|
|
262
|
-
* @since 0.5.0-canary.4
|
|
263
|
-
*/
|
|
264
|
-
export interface GoogleAnalyticsDestinationOptions {
|
|
265
|
-
name?: string | undefined;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
/**
|
|
269
|
-
* Forwards catalog events to `window.gtag` as GA4 custom events. Page views are not this
|
|
270
|
-
* destination's job: `gtag('config', ...)` sends the initial page_view and GA4's Enhanced
|
|
271
|
-
* Measurement tracks SPA history changes. Requires the gtag.js snippet plus
|
|
272
|
-
* `gtag('config', measurementId)` mounted once by the app (see the consent bootstrap) —
|
|
273
|
-
* gtag.js owns its own batching and unload delivery.
|
|
274
|
-
*
|
|
275
|
-
* @since 0.5.0-canary.4
|
|
276
|
-
*/
|
|
277
|
-
export function createGoogleAnalyticsDestination(options: GoogleAnalyticsDestinationOptions = {}): Destination {
|
|
278
|
-
const name = options.name ?? "google-analytics";
|
|
279
|
-
|
|
280
|
-
return {
|
|
281
|
-
name,
|
|
282
|
-
// Synchronous today, but declared async so a throw here rejects the returned Promise
|
|
283
|
-
// instead of escaping as a synchronous throw — matches Destination.send's contract.
|
|
284
|
-
async send(event) {
|
|
285
|
-
if (typeof window === "undefined" || typeof window.gtag !== "function") {
|
|
286
|
-
return;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
if (!warnUnlessGa4EventName(name, event.name)) {
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
window.gtag("event", event.name, flattenEventProps(event.properties));
|
|
294
|
-
},
|
|
295
|
-
};
|
|
296
|
-
}
|
|
@@ -1,164 +0,0 @@
|
|
|
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
|
-
}
|
package/src/destinations/meta.ts
DELETED
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import type { ConsentDecision } from "#/core/consent";
|
|
2
|
-
import type { Destination } from "#/core/destination";
|
|
3
|
-
import { toAdConsentState } from "#/destinations/ad-consent";
|
|
4
|
-
import type { FlatPropertyValue } from "#/destinations/shared";
|
|
5
|
-
import { flattenEventProps } from "#/destinations/shared";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Meta's `dataProcessingOptions` restriction. `['LDU'], 0, 0` turns on Limited Data Use and
|
|
9
|
-
* lets Meta geolocate the user — the recommended default beyond California, where only
|
|
10
|
-
* country `1` (USA) / state `1000` (California) are documented codes (spec-destinations §5).
|
|
11
|
-
* `[]` applies no restriction.
|
|
12
|
-
*
|
|
13
|
-
* @since 1.0.0-canary.7
|
|
14
|
-
*/
|
|
15
|
-
export interface MetaDataProcessingOptions {
|
|
16
|
-
country: number;
|
|
17
|
-
options: ReadonlyArray<string>;
|
|
18
|
-
state: number;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Maps a `ConsentDecision` to Meta's `dataProcessingOptions`: LDU when `ads` is denied.
|
|
23
|
-
*
|
|
24
|
-
* @since 1.0.0-canary.7
|
|
25
|
-
*/
|
|
26
|
-
export function toMetaDataProcessingOptions(decision: ConsentDecision): MetaDataProcessingOptions {
|
|
27
|
-
return toAdConsentState(decision).limitedDataUse
|
|
28
|
-
? { country: 0, options: ["LDU"], state: 0 }
|
|
29
|
-
: { country: 0, options: [], state: 0 };
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* The mapped per-event payload handed to a Meta transport (Pixel `fbq` or a CAPI request).
|
|
34
|
-
*
|
|
35
|
-
* @since 1.0.0-canary.7
|
|
36
|
-
*/
|
|
37
|
-
export interface MetaEventPayload {
|
|
38
|
-
dataProcessingOptions: MetaDataProcessingOptions;
|
|
39
|
-
name: string;
|
|
40
|
-
properties: Record<string, FlatPropertyValue>;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @since 1.0.0-canary.7
|
|
45
|
-
*/
|
|
46
|
-
export interface MetaDestinationOptions {
|
|
47
|
-
/**
|
|
48
|
-
* Erasure cookie-clear seam — invoked by `onErasure` to drop Meta's `_fbp`/`_fbc` cookies
|
|
49
|
-
* (spec-data-subject-rights DSR-V4). Meta exposes no per-visitor deletion API, so erasure
|
|
50
|
-
* is cookie-clear + stop-send only. Omit if the integrator clears cookies elsewhere.
|
|
51
|
-
*/
|
|
52
|
-
clearCookies?: (() => void) | undefined;
|
|
53
|
-
/** Read per event so an `ads` change restricts data use without recreating the destination. */
|
|
54
|
-
getDecision: () => ConsentDecision;
|
|
55
|
-
name?: string | undefined;
|
|
56
|
-
/**
|
|
57
|
-
* Delivers one mapped event — the transport seam. Wire it to `fbq(...)` (Pixel) or a
|
|
58
|
-
* Conversions API request; this package ships the consent mapping, never the credentials
|
|
59
|
-
* or network client.
|
|
60
|
-
*/
|
|
61
|
-
transport: (payload: MetaEventPayload) => Promise<void> | void;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Reference ad destination: maps each event to Meta's shape and applies Limited Data Use
|
|
66
|
-
* from the live `ads` decision (spec-destinations §5), then hands it to an injected
|
|
67
|
-
* transport. Consent-restriction mapping only — the Pixel/CAPI transport and credentials
|
|
68
|
-
* are the integrator's to supply. `consentRequirement` stays `"required"`: an ad sink is
|
|
69
|
-
* never exempt.
|
|
70
|
-
*
|
|
71
|
-
* @since 1.0.0-canary.7
|
|
72
|
-
*/
|
|
73
|
-
export function createMetaDestination(options: MetaDestinationOptions): Destination {
|
|
74
|
-
// Erasure halts delivery for the rest of the session — Meta has no per-visitor deletion API.
|
|
75
|
-
let stopped = false;
|
|
76
|
-
|
|
77
|
-
return {
|
|
78
|
-
consentRequirement: "required",
|
|
79
|
-
name: options.name ?? "meta",
|
|
80
|
-
onErasure() {
|
|
81
|
-
stopped = true;
|
|
82
|
-
options.clearCookies?.();
|
|
83
|
-
},
|
|
84
|
-
async send(event) {
|
|
85
|
-
if (stopped) {
|
|
86
|
-
return;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
await options.transport({
|
|
90
|
-
dataProcessingOptions: toMetaDataProcessingOptions(options.getDecision()),
|
|
91
|
-
name: event.name,
|
|
92
|
-
properties: flattenEventProps(event.properties),
|
|
93
|
-
});
|
|
94
|
-
},
|
|
95
|
-
};
|
|
96
|
-
}
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import type { ConsentDecision } from "#/core/consent";
|
|
2
|
-
import type { Destination } from "#/core/destination";
|
|
3
|
-
import { toAdConsentState } from "#/destinations/ad-consent";
|
|
4
|
-
import type { FlatPropertyValue } from "#/destinations/shared";
|
|
5
|
-
import { flattenEventProps } from "#/destinations/shared";
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* Microsoft UET consent. UET enforces **only** `ad_storage` — it has no `analytics_storage`,
|
|
9
|
-
* and its `ad_user_data`/`ad_personalization` fields have no documented behavior, so this
|
|
10
|
-
* models the one enforced signal (spec-destinations §5). Pushed via
|
|
11
|
-
* `uetq.push('consent','update', …)`.
|
|
12
|
-
*
|
|
13
|
-
* @since 1.0.0-canary.7
|
|
14
|
-
*/
|
|
15
|
-
export interface MicrosoftUetConsent {
|
|
16
|
-
ad_storage: "denied" | "granted";
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Maps a `ConsentDecision` to UET's enforced `ad_storage` signal via the shared ad lever, so
|
|
21
|
-
* it can't drift from Meta/TikTok — `ad_storage` is the inverse of Limited Data Use.
|
|
22
|
-
*
|
|
23
|
-
* @since 1.0.0-canary.7
|
|
24
|
-
*/
|
|
25
|
-
export function toMicrosoftUetConsent(decision: ConsentDecision): MicrosoftUetConsent {
|
|
26
|
-
return { ad_storage: toAdConsentState(decision).limitedDataUse ? "denied" : "granted" };
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* The mapped per-event payload handed to a UET transport (`uetq.push`).
|
|
31
|
-
*
|
|
32
|
-
* @since 1.0.0-canary.7
|
|
33
|
-
*/
|
|
34
|
-
export interface MicrosoftUetEventPayload {
|
|
35
|
-
consent: MicrosoftUetConsent;
|
|
36
|
-
name: string;
|
|
37
|
-
properties: Record<string, FlatPropertyValue>;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* @since 1.0.0-canary.7
|
|
42
|
-
*/
|
|
43
|
-
export interface MicrosoftUetDestinationOptions {
|
|
44
|
-
/** Erasure cookie-clear seam — UET exposes no per-visitor deletion API, so erasure is cookie-clear + stop-send (DSR-V4). */
|
|
45
|
-
clearCookies?: (() => void) | undefined;
|
|
46
|
-
/** Read per event so an `ads` change updates `ad_storage` without recreating the destination. */
|
|
47
|
-
getDecision: () => ConsentDecision;
|
|
48
|
-
name?: string | undefined;
|
|
49
|
-
/**
|
|
50
|
-
* Delivers one mapped event — the transport seam. Wire it to `uetq.push(...)`; this package
|
|
51
|
-
* ships the consent mapping, never the tag id or network client.
|
|
52
|
-
*/
|
|
53
|
-
transport: (payload: MicrosoftUetEventPayload) => Promise<void> | void;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Reference ad destination for Microsoft UET: maps each event and the live `ads` decision to
|
|
58
|
-
* UET's `ad_storage` signal, then hands it to an injected transport. Consent-restriction
|
|
59
|
-
* mapping only — the tag id and `uetq` transport are the integrator's. `consentRequirement`
|
|
60
|
-
* stays `"required"`.
|
|
61
|
-
*
|
|
62
|
-
* @since 1.0.0-canary.7
|
|
63
|
-
*/
|
|
64
|
-
export function createMicrosoftUetDestination(options: MicrosoftUetDestinationOptions): Destination {
|
|
65
|
-
let stopped = false;
|
|
66
|
-
|
|
67
|
-
return {
|
|
68
|
-
consentRequirement: "required",
|
|
69
|
-
name: options.name ?? "microsoft-uet",
|
|
70
|
-
onErasure() {
|
|
71
|
-
stopped = true;
|
|
72
|
-
options.clearCookies?.();
|
|
73
|
-
},
|
|
74
|
-
async send(event) {
|
|
75
|
-
if (stopped) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
await options.transport({
|
|
80
|
-
consent: toMicrosoftUetConsent(options.getDecision()),
|
|
81
|
-
name: event.name,
|
|
82
|
-
properties: flattenEventProps(event.properties),
|
|
83
|
-
});
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
}
|
|
@@ -1,45 +0,0 @@
|
|
|
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
|
-
}
|