@codefast/tracking 0.5.0-canary.5 → 0.5.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 +467 -0
- package/README.md +176 -126
- package/dist/adapters/tanstack-start.d.ts +80 -0
- package/dist/adapters/tanstack-start.d.ts.map +1 -0
- package/dist/adapters/tanstack-start.js +74 -0
- package/dist/adapters/tanstack-start.js.map +1 -0
- package/dist/client/ad-framework-consent.d.ts +62 -0
- package/dist/client/ad-framework-consent.d.ts.map +1 -0
- package/dist/client/ad-framework-consent.js +40 -0
- package/dist/client/ad-framework-consent.js.map +1 -0
- package/dist/client/browser-cookie.d.ts +9 -0
- package/dist/client/browser-cookie.d.ts.map +1 -0
- package/dist/client/browser-cookie.js +15 -0
- package/dist/client/browser-cookie.js.map +1 -0
- package/dist/client/consent-runtime.d.ts +53 -0
- package/dist/client/consent-runtime.d.ts.map +1 -0
- package/dist/client/consent-runtime.js +33 -0
- package/dist/client/consent-runtime.js.map +1 -0
- package/dist/client/{consent-storage.d.mts → consent-storage.d.ts} +8 -6
- package/dist/client/consent-storage.d.ts.map +1 -0
- package/dist/client/consent-storage.js +109 -0
- package/dist/client/consent-storage.js.map +1 -0
- package/dist/client/consent-withdrawal.d.ts +18 -0
- package/dist/client/consent-withdrawal.d.ts.map +1 -0
- package/dist/client/consent-withdrawal.js +17 -0
- package/dist/client/consent-withdrawal.js.map +1 -0
- package/dist/client/cookie-anonymous-id.d.ts +41 -0
- package/dist/client/cookie-anonymous-id.d.ts.map +1 -0
- package/dist/client/cookie-anonymous-id.js +65 -0
- package/dist/client/cookie-anonymous-id.js.map +1 -0
- package/dist/client/create-client-tracker.d.ts +60 -0
- package/dist/client/create-client-tracker.d.ts.map +1 -0
- package/dist/client/create-client-tracker.js +91 -0
- package/dist/client/create-client-tracker.js.map +1 -0
- package/dist/client/gpc.d.ts +10 -0
- package/dist/client/gpc.d.ts.map +1 -0
- package/dist/client/gpc.js +12 -0
- package/dist/client/gpc.js.map +1 -0
- package/dist/client/initial-consent-store.d.ts +63 -0
- package/dist/client/initial-consent-store.d.ts.map +1 -0
- package/dist/client/initial-consent-store.js +138 -0
- package/dist/client/initial-consent-store.js.map +1 -0
- package/dist/client/is-analytics-allowed.d.ts +26 -0
- package/dist/client/is-analytics-allowed.d.ts.map +1 -0
- package/dist/client/is-analytics-allowed.js +22 -0
- package/dist/client/is-analytics-allowed.js.map +1 -0
- package/dist/client/server-persisted-anonymous-id.d.ts +30 -0
- package/dist/client/server-persisted-anonymous-id.d.ts.map +1 -0
- package/dist/client/server-persisted-anonymous-id.js +72 -0
- package/dist/client/server-persisted-anonymous-id.js.map +1 -0
- package/dist/core/consent-config.d.ts +24 -0
- package/dist/core/consent-config.d.ts.map +1 -0
- package/dist/core/consent-config.js +9 -0
- package/dist/core/consent-config.js.map +1 -0
- package/dist/core/consent-receipt.d.ts +71 -0
- package/dist/core/consent-receipt.d.ts.map +1 -0
- package/dist/core/consent-receipt.js +39 -0
- package/dist/core/consent-receipt.js.map +1 -0
- package/dist/core/consent.d.ts +182 -0
- package/dist/core/consent.d.ts.map +1 -0
- package/dist/core/consent.js +159 -0
- package/dist/core/consent.js.map +1 -0
- package/dist/core/cookie.d.ts +15 -0
- package/dist/core/cookie.d.ts.map +1 -0
- package/dist/core/cookie.js +26 -0
- package/dist/core/cookie.js.map +1 -0
- package/dist/core/destination.d.ts +29 -0
- package/dist/core/destination.d.ts.map +1 -0
- package/dist/core/destination.js +2 -0
- package/dist/core/destination.js.map +1 -0
- package/dist/core/event-catalog.d.ts +35 -0
- package/dist/core/event-catalog.d.ts.map +1 -0
- package/dist/core/event-catalog.js +30 -0
- package/dist/core/event-catalog.js.map +1 -0
- package/dist/core/event-id.d.ts +7 -0
- package/dist/core/event-id.d.ts.map +1 -0
- package/dist/core/event-id.js +9 -0
- package/dist/core/event-id.js.map +1 -0
- package/dist/core/tracked-event.d.ts +15 -0
- package/dist/core/tracked-event.d.ts.map +1 -0
- package/dist/core/tracked-event.js +2 -0
- package/dist/core/tracked-event.js.map +1 -0
- package/dist/destinations/ad-consent.d.ts +23 -0
- package/dist/destinations/ad-consent.d.ts.map +1 -0
- package/dist/destinations/ad-consent.js +12 -0
- package/dist/destinations/ad-consent.js.map +1 -0
- package/dist/destinations/google-analytics.d.ts +148 -0
- package/dist/destinations/google-analytics.d.ts.map +1 -0
- package/dist/destinations/google-analytics.js +156 -0
- package/dist/destinations/google-analytics.js.map +1 -0
- package/dist/destinations/google-consent.d.ts +62 -0
- package/dist/destinations/google-consent.d.ts.map +1 -0
- package/dist/destinations/google-consent.js +109 -0
- package/dist/destinations/google-consent.js.map +1 -0
- package/dist/destinations/meta.d.ts +63 -0
- package/dist/destinations/meta.d.ts.map +1 -0
- package/dist/destinations/meta.js +44 -0
- package/dist/destinations/meta.js.map +1 -0
- package/dist/destinations/microsoft-uet.d.ts +56 -0
- package/dist/destinations/microsoft-uet.d.ts.map +1 -0
- package/dist/destinations/microsoft-uet.js +41 -0
- package/dist/destinations/microsoft-uet.js.map +1 -0
- package/dist/destinations/shared.d.ts +18 -0
- package/dist/destinations/shared.d.ts.map +1 -0
- package/dist/destinations/shared.js +19 -0
- package/dist/destinations/shared.js.map +1 -0
- package/dist/destinations/tiktok.d.ts +55 -0
- package/dist/destinations/tiktok.d.ts.map +1 -0
- package/dist/destinations/tiktok.js +40 -0
- package/dist/destinations/tiktok.js.map +1 -0
- package/dist/destinations/vercel-analytics.d.ts +25 -0
- package/dist/destinations/vercel-analytics.d.ts.map +1 -0
- package/dist/destinations/vercel-analytics.js +23 -0
- package/dist/destinations/vercel-analytics.js.map +1 -0
- package/dist/index.d.ts +64 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +41 -0
- package/dist/index.js.map +1 -0
- package/dist/react/consent-banner.d.ts +155 -0
- package/dist/react/consent-banner.d.ts.map +1 -0
- package/dist/react/consent-banner.js +171 -0
- package/dist/react/consent-banner.js.map +1 -0
- package/dist/react/gtag-consent-bootstrap.d.ts +17 -0
- package/dist/react/gtag-consent-bootstrap.d.ts.map +1 -0
- package/dist/react/gtag-consent-bootstrap.js +22 -0
- package/dist/react/gtag-consent-bootstrap.js.map +1 -0
- package/dist/react/use-consent.d.ts +42 -0
- package/dist/react/use-consent.d.ts.map +1 -0
- package/dist/react/use-consent.js +60 -0
- package/dist/react/use-consent.js.map +1 -0
- package/dist/react/use-google-consent-sync.d.ts +20 -0
- package/dist/react/use-google-consent-sync.d.ts.map +1 -0
- package/dist/react/use-google-consent-sync.js +22 -0
- package/dist/react/use-google-consent-sync.js.map +1 -0
- package/dist/react/use-initial-consent.d.ts +11 -0
- package/dist/react/use-initial-consent.d.ts.map +1 -0
- package/dist/react/use-initial-consent.js +18 -0
- package/dist/react/use-initial-consent.js.map +1 -0
- package/dist/server/anonymous-id-cookie.d.ts +67 -0
- package/dist/server/anonymous-id-cookie.d.ts.map +1 -0
- package/dist/server/anonymous-id-cookie.js +71 -0
- package/dist/server/anonymous-id-cookie.js.map +1 -0
- package/dist/server/consent-receipt-store.d.ts +56 -0
- package/dist/server/consent-receipt-store.d.ts.map +1 -0
- package/dist/server/consent-receipt-store.js +44 -0
- package/dist/server/consent-receipt-store.js.map +1 -0
- package/dist/server/consent-receipt.d.ts +41 -0
- package/dist/server/consent-receipt.d.ts.map +1 -0
- package/dist/server/consent-receipt.js +60 -0
- package/dist/server/consent-receipt.js.map +1 -0
- package/dist/server/initial-consent.d.ts +26 -0
- package/dist/server/initial-consent.d.ts.map +1 -0
- package/dist/server/initial-consent.js +31 -0
- package/dist/server/initial-consent.js.map +1 -0
- package/dist/server/measurement-protocol.d.ts +69 -0
- package/dist/server/measurement-protocol.d.ts.map +1 -0
- package/dist/server/measurement-protocol.js +61 -0
- package/dist/server/measurement-protocol.js.map +1 -0
- package/dist/server/region.d.ts +27 -0
- package/dist/server/region.d.ts.map +1 -0
- package/dist/server/region.js +69 -0
- package/dist/server/region.js.map +1 -0
- package/dist/server/user-deletion.d.ts +64 -0
- package/dist/server/user-deletion.d.ts.map +1 -0
- package/dist/server/user-deletion.js +54 -0
- package/dist/server/user-deletion.js.map +1 -0
- package/dist/tooling/import-protection.d.ts +19 -0
- package/dist/tooling/import-protection.d.ts.map +1 -0
- package/dist/tooling/import-protection.js +23 -0
- package/dist/tooling/import-protection.js.map +1 -0
- package/package.json +201 -107
- package/src/adapters/tanstack-start.ts +135 -0
- package/src/client/ad-framework-consent.ts +86 -0
- package/src/client/browser-cookie.ts +16 -0
- package/src/client/consent-runtime.ts +80 -0
- package/src/client/consent-storage.ts +31 -1
- package/src/client/consent-withdrawal.ts +32 -0
- package/src/client/cookie-anonymous-id.ts +112 -0
- package/src/client/create-client-tracker.ts +128 -86
- package/src/client/gpc.ts +4 -2
- package/src/client/initial-consent-store.ts +218 -0
- package/src/client/is-analytics-allowed.ts +43 -0
- package/src/client/server-persisted-anonymous-id.ts +102 -0
- package/src/core/consent-config.ts +27 -0
- package/src/core/consent-receipt.ts +115 -0
- package/src/core/consent.ts +164 -10
- package/src/core/cookie.ts +29 -0
- package/src/core/destination.ts +17 -8
- package/src/core/event-catalog.ts +29 -15
- package/src/core/event-id.ts +1 -2
- package/src/core/tracked-event.ts +4 -4
- package/src/destinations/ad-consent.ts +29 -0
- package/src/destinations/google-analytics.ts +196 -133
- package/src/destinations/google-consent.ts +164 -0
- package/src/destinations/meta.ts +96 -0
- package/src/destinations/microsoft-uet.ts +86 -0
- package/src/destinations/shared.ts +45 -0
- package/src/destinations/tiktok.ts +85 -0
- package/src/destinations/vercel-analytics.ts +23 -24
- package/src/index.ts +165 -8
- package/src/react/consent-banner.tsx +40 -14
- package/src/react/gtag-consent-bootstrap.tsx +48 -0
- package/src/react/use-consent.ts +53 -45
- package/src/react/use-google-consent-sync.ts +38 -0
- package/src/react/use-initial-consent.ts +22 -0
- package/src/server/anonymous-id-cookie.ts +103 -0
- package/src/server/consent-receipt-store.ts +79 -0
- package/src/server/consent-receipt.ts +89 -0
- package/src/server/initial-consent.ts +46 -0
- package/src/server/measurement-protocol.ts +114 -0
- package/src/server/region.ts +22 -15
- package/src/server/user-deletion.ts +93 -0
- package/src/tooling/import-protection.ts +22 -0
- package/dist/client/consent-storage.mjs +0 -63
- package/dist/client/create-client-tracker.d.mts +0 -37
- package/dist/client/create-client-tracker.mjs +0 -66
- package/dist/client/gpc.d.mts +0 -10
- package/dist/client/gpc.mjs +0 -12
- package/dist/client/index.d.mts +0 -8
- package/dist/client/index.mjs +0 -8
- package/dist/client/lifecycle.d.mts +0 -23
- package/dist/client/lifecycle.mjs +0 -30
- package/dist/client/local-storage.d.mts +0 -13
- package/dist/client/local-storage.mjs +0 -31
- package/dist/client/queue.d.mts +0 -49
- package/dist/client/queue.mjs +0 -71
- package/dist/client/router.d.mts +0 -30
- package/dist/client/router.mjs +0 -17
- package/dist/core/consent.d.mts +0 -89
- package/dist/core/consent.mjs +0 -57
- package/dist/core/destination.d.mts +0 -23
- package/dist/core/destination.mjs +0 -1
- package/dist/core/event-catalog.d.mts +0 -36
- package/dist/core/event-catalog.mjs +0 -12
- package/dist/core/event-id.d.mts +0 -10
- package/dist/core/event-id.mjs +0 -12
- package/dist/core/index.d.mts +0 -6
- package/dist/core/index.mjs +0 -4
- package/dist/core/tracked-event.d.mts +0 -17
- package/dist/core/tracked-event.mjs +0 -1
- package/dist/destinations/ga4-measurement-protocol.d.mts +0 -54
- package/dist/destinations/ga4-measurement-protocol.mjs +0 -89
- package/dist/destinations/google-analytics.d.mts +0 -98
- package/dist/destinations/google-analytics.mjs +0 -126
- package/dist/destinations/http-destination.d.mts +0 -20
- package/dist/destinations/http-destination.mjs +0 -25
- package/dist/destinations/index.d.mts +0 -5
- package/dist/destinations/index.mjs +0 -5
- package/dist/destinations/vercel-analytics.d.mts +0 -13
- package/dist/destinations/vercel-analytics.mjs +0 -30
- package/dist/index.d.mts +0 -6
- package/dist/index.mjs +0 -4
- package/dist/react/consent-banner.d.mts +0 -163
- package/dist/react/consent-banner.mjs +0 -209
- package/dist/react/index.d.mts +0 -3
- package/dist/react/index.mjs +0 -3
- package/dist/react/use-consent.d.mts +0 -49
- package/dist/react/use-consent.mjs +0 -54
- package/dist/server/create-server-tracker.d.mts +0 -42
- package/dist/server/create-server-tracker.mjs +0 -67
- package/dist/server/index.d.mts +0 -3
- package/dist/server/index.mjs +0 -3
- package/dist/server/region.d.mts +0 -20
- package/dist/server/region.mjs +0 -56
- package/src/client/index.ts +0 -17
- package/src/client/lifecycle.ts +0 -50
- package/src/client/local-storage.ts +0 -44
- package/src/client/queue.ts +0 -117
- package/src/client/router.ts +0 -37
- package/src/core/index.ts +0 -24
- package/src/destinations/ga4-measurement-protocol.ts +0 -166
- package/src/destinations/http-destination.ts +0 -33
- package/src/destinations/index.ts +0 -20
- package/src/react/index.ts +0 -29
- package/src/server/create-server-tracker.ts +0 -128
- package/src/server/index.ts +0 -4
|
@@ -1,209 +0,0 @@
|
|
|
1
|
-
import { CONSENT_CATEGORIES } from "../core/consent.mjs";
|
|
2
|
-
import { createContext, useContext, useEffect, useState } from "react";
|
|
3
|
-
import { jsx, jsxs } from "react/jsx-runtime";
|
|
4
|
-
//#region src/react/consent-banner.tsx
|
|
5
|
-
const ConsentBannerContext = createContext(null);
|
|
6
|
-
function useConsentBannerContext(part) {
|
|
7
|
-
const context = useContext(ConsentBannerContext);
|
|
8
|
-
if (context === null) throw new Error(`\`${part}\` must be rendered inside \`ConsentBanner\``);
|
|
9
|
-
return context;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Non-blocking opt-in prompt for GDPR/NĐ 13 regions, as composable parts — the root owns
|
|
13
|
-
* the visibility gating and the preferences-layer state; the `Accept`/`Reject`/`Customize`/
|
|
14
|
-
* `Save` parts wire their own clicks to the consent hook, so any markup (including a
|
|
15
|
-
* design system's button styles via `className`) slots in. Renders nothing once a decision
|
|
16
|
-
* exists or the region defaults to opt-out (`consent.needsPrompt`), unless `open` says
|
|
17
|
-
* otherwise. Rendered as a labeled region, not a `<dialog>`: an open non-modal dialog
|
|
18
|
-
* neither traps focus nor blocks the page, so the dialog semantics would over-promise.
|
|
19
|
-
* Unstyled by default — style via `className`/`data-slot` selectors, or import the
|
|
20
|
-
* optional `@codefast/tracking/css/consent.css` theme. `data-state` on the root flips
|
|
21
|
-
* between `prompt` and `preferences` for state-dependent styling.
|
|
22
|
-
*
|
|
23
|
-
* @since 0.5.0-canary.4
|
|
24
|
-
*/
|
|
25
|
-
function ConsentBanner({ children, consent, open, ...props }) {
|
|
26
|
-
const [pending, setPending] = useState(void 0);
|
|
27
|
-
const isVisible = open ?? consent.needsPrompt;
|
|
28
|
-
useEffect(() => {
|
|
29
|
-
if (!isVisible) setPending(void 0);
|
|
30
|
-
}, [isVisible]);
|
|
31
|
-
if (!isVisible) return null;
|
|
32
|
-
return /* @__PURE__ */ jsx(ConsentBannerContext.Provider, {
|
|
33
|
-
value: {
|
|
34
|
-
consent,
|
|
35
|
-
pending,
|
|
36
|
-
setPending
|
|
37
|
-
},
|
|
38
|
-
children: /* @__PURE__ */ jsx("section", {
|
|
39
|
-
"aria-label": "Cookie consent",
|
|
40
|
-
"data-slot": "consent-banner",
|
|
41
|
-
"data-state": pending === void 0 ? "prompt" : "preferences",
|
|
42
|
-
...props,
|
|
43
|
-
children
|
|
44
|
-
})
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* @since 0.5.0-canary.5
|
|
49
|
-
*/
|
|
50
|
-
function ConsentBannerTitle({ children, ...props }) {
|
|
51
|
-
return /* @__PURE__ */ jsx("h2", {
|
|
52
|
-
"data-slot": "consent-title",
|
|
53
|
-
...props,
|
|
54
|
-
children
|
|
55
|
-
});
|
|
56
|
-
}
|
|
57
|
-
/**
|
|
58
|
-
* Hosts the message and the privacy-policy link informed consent requires.
|
|
59
|
-
*
|
|
60
|
-
* @since 0.5.0-canary.5
|
|
61
|
-
*/
|
|
62
|
-
function ConsentBannerDescription(props) {
|
|
63
|
-
return /* @__PURE__ */ jsx("p", {
|
|
64
|
-
"data-slot": "consent-description",
|
|
65
|
-
...props
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* @since 0.5.0-canary.5
|
|
70
|
-
*/
|
|
71
|
-
function ConsentBannerActions(props) {
|
|
72
|
-
return /* @__PURE__ */ jsx("div", {
|
|
73
|
-
"data-slot": "consent-actions",
|
|
74
|
-
...props
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
/**
|
|
78
|
-
* Grants the categories the app requested (`useConsent`'s `categories`) — never more.
|
|
79
|
-
*
|
|
80
|
-
* @since 0.5.0-canary.5
|
|
81
|
-
*/
|
|
82
|
-
function ConsentBannerAccept({ onClick, ...props }) {
|
|
83
|
-
const { consent, setPending } = useConsentBannerContext("ConsentBannerAccept");
|
|
84
|
-
return /* @__PURE__ */ jsx("button", {
|
|
85
|
-
"data-action": "accept",
|
|
86
|
-
"data-slot": "consent-action",
|
|
87
|
-
...props,
|
|
88
|
-
onClick: (event) => {
|
|
89
|
-
onClick?.(event);
|
|
90
|
-
consent.grantAll();
|
|
91
|
-
setPending(void 0);
|
|
92
|
-
},
|
|
93
|
-
type: "button"
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* @since 0.5.0-canary.5
|
|
98
|
-
*/
|
|
99
|
-
function ConsentBannerReject({ onClick, ...props }) {
|
|
100
|
-
const { consent, setPending } = useConsentBannerContext("ConsentBannerReject");
|
|
101
|
-
return /* @__PURE__ */ jsx("button", {
|
|
102
|
-
"data-action": "reject",
|
|
103
|
-
"data-slot": "consent-action",
|
|
104
|
-
...props,
|
|
105
|
-
onClick: (event) => {
|
|
106
|
-
onClick?.(event);
|
|
107
|
-
consent.denyAll();
|
|
108
|
-
setPending(void 0);
|
|
109
|
-
},
|
|
110
|
-
type: "button"
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Opens the per-category preferences layer, starting from the current effective state so
|
|
115
|
-
* nothing arrives pre-ticked in an opt-in region (pre-ticked consent is invalid under
|
|
116
|
-
* GDPR). Hidden while the layer is open — `ConsentBannerSave` takes over.
|
|
117
|
-
*
|
|
118
|
-
* @since 0.5.0-canary.5
|
|
119
|
-
*/
|
|
120
|
-
function ConsentBannerCustomize({ onClick, ...props }) {
|
|
121
|
-
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerCustomize");
|
|
122
|
-
if (pending !== void 0) return null;
|
|
123
|
-
return /* @__PURE__ */ jsx("button", {
|
|
124
|
-
"data-action": "customize",
|
|
125
|
-
"data-slot": "consent-action",
|
|
126
|
-
...props,
|
|
127
|
-
onClick: (event) => {
|
|
128
|
-
onClick?.(event);
|
|
129
|
-
setPending(consent.effectiveConsent);
|
|
130
|
-
},
|
|
131
|
-
type: "button"
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Container for the per-category rows — renders only while the preferences layer is open.
|
|
136
|
-
*
|
|
137
|
-
* @since 0.5.0-canary.5
|
|
138
|
-
*/
|
|
139
|
-
function ConsentBannerPreferences(props) {
|
|
140
|
-
const { pending } = useConsentBannerContext("ConsentBannerPreferences");
|
|
141
|
-
if (pending === void 0) return null;
|
|
142
|
-
return /* @__PURE__ */ jsx("div", {
|
|
143
|
-
"data-slot": "consent-preferences",
|
|
144
|
-
...props
|
|
145
|
-
});
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* One checkbox row of the preferences layer — `children` is the visitor-facing wording.
|
|
149
|
-
*
|
|
150
|
-
* @since 0.5.0-canary.5
|
|
151
|
-
*/
|
|
152
|
-
function ConsentBannerCategory({ category, children, ...props }) {
|
|
153
|
-
const { pending, setPending } = useConsentBannerContext("ConsentBannerCategory");
|
|
154
|
-
if (pending === void 0) return null;
|
|
155
|
-
return /* @__PURE__ */ jsxs("label", {
|
|
156
|
-
"data-slot": "consent-category",
|
|
157
|
-
...props,
|
|
158
|
-
children: [/* @__PURE__ */ jsx("input", {
|
|
159
|
-
checked: pending[category],
|
|
160
|
-
"data-slot": "consent-category-checkbox",
|
|
161
|
-
onChange: (event) => {
|
|
162
|
-
setPending({
|
|
163
|
-
...pending,
|
|
164
|
-
[category]: event.target.checked
|
|
165
|
-
});
|
|
166
|
-
},
|
|
167
|
-
type: "checkbox"
|
|
168
|
-
}), children]
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Persists the pending per-category selection — renders only while the preferences layer is open.
|
|
173
|
-
*
|
|
174
|
-
* @since 0.5.0-canary.5
|
|
175
|
-
*/
|
|
176
|
-
function ConsentBannerSave({ onClick, ...props }) {
|
|
177
|
-
const { consent, pending, setPending } = useConsentBannerContext("ConsentBannerSave");
|
|
178
|
-
if (pending === void 0) return null;
|
|
179
|
-
return /* @__PURE__ */ jsx("button", {
|
|
180
|
-
"data-action": "save",
|
|
181
|
-
"data-slot": "consent-action",
|
|
182
|
-
...props,
|
|
183
|
-
onClick: (event) => {
|
|
184
|
-
onClick?.(event);
|
|
185
|
-
consent.save(pending);
|
|
186
|
-
setPending(void 0);
|
|
187
|
-
},
|
|
188
|
-
type: "button"
|
|
189
|
-
});
|
|
190
|
-
}
|
|
191
|
-
/**
|
|
192
|
-
* Always-visible control for opt-out regions — CCPA/CPRA requires a persistent
|
|
193
|
-
* "Do Not Sell or Share My Personal Information" mechanism, not just a one-time prompt.
|
|
194
|
-
* Flips between denying everything and re-granting the app's requested categories.
|
|
195
|
-
*
|
|
196
|
-
* @since 0.5.0-canary.4
|
|
197
|
-
*/
|
|
198
|
-
function ConsentToggle({ allowLabel = "Allow tracking", consent, denyLabel = "Do Not Sell or Share My Personal Information", ...props }) {
|
|
199
|
-
const anyAllowed = CONSENT_CATEGORIES.some((category) => consent.effectiveConsent[category]);
|
|
200
|
-
return /* @__PURE__ */ jsx("button", {
|
|
201
|
-
"data-slot": "consent-toggle",
|
|
202
|
-
...props,
|
|
203
|
-
onClick: anyAllowed ? consent.denyAll : consent.grantAll,
|
|
204
|
-
type: "button",
|
|
205
|
-
children: anyAllowed ? denyLabel : allowLabel
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
|
-
//#endregion
|
|
209
|
-
export { ConsentBanner, ConsentBannerAccept, ConsentBannerActions, ConsentBannerCategory, ConsentBannerCustomize, ConsentBannerDescription, ConsentBannerPreferences, ConsentBannerReject, ConsentBannerSave, ConsentBannerTitle, ConsentToggle };
|
package/dist/react/index.d.mts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { UseConsentOptions, UseConsentResult, useConsent } from "./use-consent.mjs";
|
|
2
|
-
import { ConsentBanner, ConsentBannerAccept, ConsentBannerAcceptProps, ConsentBannerActions, ConsentBannerActionsProps, ConsentBannerCategory, ConsentBannerCategoryProps, ConsentBannerCustomize, ConsentBannerCustomizeProps, ConsentBannerDescription, ConsentBannerDescriptionProps, ConsentBannerPreferences, ConsentBannerPreferencesProps, ConsentBannerProps, ConsentBannerReject, ConsentBannerRejectProps, ConsentBannerSave, ConsentBannerSaveProps, ConsentBannerTitle, ConsentBannerTitleProps, ConsentToggle, ConsentToggleProps } from "./consent-banner.mjs";
|
|
3
|
-
export { ConsentBanner, ConsentBannerAccept, type ConsentBannerAcceptProps, ConsentBannerActions, type ConsentBannerActionsProps, ConsentBannerCategory, type ConsentBannerCategoryProps, ConsentBannerCustomize, type ConsentBannerCustomizeProps, ConsentBannerDescription, type ConsentBannerDescriptionProps, ConsentBannerPreferences, type ConsentBannerPreferencesProps, type ConsentBannerProps, ConsentBannerReject, type ConsentBannerRejectProps, ConsentBannerSave, type ConsentBannerSaveProps, ConsentBannerTitle, type ConsentBannerTitleProps, ConsentToggle, type ConsentToggleProps, type UseConsentOptions, type UseConsentResult, useConsent };
|
package/dist/react/index.mjs
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { ConsentBanner, ConsentBannerAccept, ConsentBannerActions, ConsentBannerCategory, ConsentBannerCustomize, ConsentBannerDescription, ConsentBannerPreferences, ConsentBannerReject, ConsentBannerSave, ConsentBannerTitle, ConsentToggle } from "./consent-banner.mjs";
|
|
2
|
-
import { useConsent } from "./use-consent.mjs";
|
|
3
|
-
export { ConsentBanner, ConsentBannerAccept, ConsentBannerActions, ConsentBannerCategory, ConsentBannerCustomize, ConsentBannerDescription, ConsentBannerPreferences, ConsentBannerReject, ConsentBannerSave, ConsentBannerTitle, ConsentToggle, useConsent };
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { ConsentCategory, ConsentDecision, ConsentMode, ConsentStorage } from "../core/consent.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/react/use-consent.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* @since 0.5.0-canary.4
|
|
6
|
-
*/
|
|
7
|
-
interface UseConsentOptions {
|
|
8
|
-
/**
|
|
9
|
-
* Categories the app's prompt actually asks about — `grantAll` grants exactly these,
|
|
10
|
-
* so an analytics-only banner can never grant ads consent it never asked for.
|
|
11
|
-
* Defaults to `["analytics"]`.
|
|
12
|
-
*/
|
|
13
|
-
categories?: ReadonlyArray<ConsentCategory>;
|
|
14
|
-
hasGlobalPrivacyControlSignal?: boolean;
|
|
15
|
-
mode: ConsentMode;
|
|
16
|
-
onDecision?: (decision: ConsentDecision) => void;
|
|
17
|
-
policyVersion: string;
|
|
18
|
-
/** Must be a stable reference (module-level or memoized) — a new object per render resubscribes every render. */
|
|
19
|
-
storage: ConsentStorage;
|
|
20
|
-
}
|
|
21
|
-
/**
|
|
22
|
-
* @since 0.5.0-canary.4
|
|
23
|
-
*/
|
|
24
|
-
interface UseConsentResult {
|
|
25
|
-
/** The stored decision under the current policy version — `undefined` until the visitor makes one. */
|
|
26
|
-
decision: ConsentDecision | undefined;
|
|
27
|
-
denyAll: () => void;
|
|
28
|
-
/** What tags must obey right now — the stored decision, or the region default before one exists. */
|
|
29
|
-
effectiveConsent: ConsentDecision;
|
|
30
|
-
grantAll: () => void;
|
|
31
|
-
/** Effective `analytics` consent — gates this package's own tracker pipeline. */
|
|
32
|
-
isTrackingAllowed: boolean;
|
|
33
|
-
/** True only for opt-in regions with no stored decision yet — drives whether to render the banner. */
|
|
34
|
-
needsPrompt: boolean;
|
|
35
|
-
/** Persist a granular per-category choice, e.g. from a preferences panel. */
|
|
36
|
-
save: (decision: ConsentDecision) => void;
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Bridges `resolveConsentMode`/`resolveDefaultConsent` (core, region-aware) to React via
|
|
40
|
-
* `useSyncExternalStore`: the stored record is the single source of truth, hydration is
|
|
41
|
-
* safe by construction (the server snapshot is always "no decision yet", matching what
|
|
42
|
-
* prerendered HTML could know), and a decision made in another tab syncs through the
|
|
43
|
-
* storage subscription.
|
|
44
|
-
*
|
|
45
|
-
* @since 0.5.0-canary.4
|
|
46
|
-
*/
|
|
47
|
-
declare function useConsent(options: UseConsentOptions): UseConsentResult;
|
|
48
|
-
//#endregion
|
|
49
|
-
export { UseConsentOptions, UseConsentResult, useConsent };
|
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import { CONSENT_CATEGORIES, createConsentDecision, isConsentDecision, resolveDefaultConsent } from "../core/consent.mjs";
|
|
2
|
-
import { useRef, useSyncExternalStore } from "react";
|
|
3
|
-
//#region src/react/use-consent.ts
|
|
4
|
-
const DEFAULT_CATEGORIES = ["analytics"];
|
|
5
|
-
/**
|
|
6
|
-
* Bridges `resolveConsentMode`/`resolveDefaultConsent` (core, region-aware) to React via
|
|
7
|
-
* `useSyncExternalStore`: the stored record is the single source of truth, hydration is
|
|
8
|
-
* safe by construction (the server snapshot is always "no decision yet", matching what
|
|
9
|
-
* prerendered HTML could know), and a decision made in another tab syncs through the
|
|
10
|
-
* storage subscription.
|
|
11
|
-
*
|
|
12
|
-
* @since 0.5.0-canary.4
|
|
13
|
-
*/
|
|
14
|
-
function useConsent(options) {
|
|
15
|
-
const { storage } = options;
|
|
16
|
-
const categories = options.categories ?? DEFAULT_CATEGORIES;
|
|
17
|
-
const cachedDecision = useRef(void 0);
|
|
18
|
-
const decision = useSyncExternalStore(storage.subscribe, () => {
|
|
19
|
-
const record = storage.load();
|
|
20
|
-
if (record?.policyVersion !== options.policyVersion || !isConsentDecision(record.decision)) {
|
|
21
|
-
cachedDecision.current = void 0;
|
|
22
|
-
return;
|
|
23
|
-
}
|
|
24
|
-
const stored = record.decision;
|
|
25
|
-
const cached = cachedDecision.current;
|
|
26
|
-
if (cached !== void 0 && CONSENT_CATEGORIES.every((category) => cached[category] === stored[category])) return cached;
|
|
27
|
-
cachedDecision.current = createConsentDecision(CONSENT_CATEGORIES.filter((category) => stored[category]));
|
|
28
|
-
return cachedDecision.current;
|
|
29
|
-
}, () => void 0);
|
|
30
|
-
function save(next) {
|
|
31
|
-
storage.save({
|
|
32
|
-
decision: next,
|
|
33
|
-
policyVersion: options.policyVersion,
|
|
34
|
-
timestamp: Date.now()
|
|
35
|
-
});
|
|
36
|
-
options.onDecision?.(next);
|
|
37
|
-
}
|
|
38
|
-
const effectiveConsent = decision ?? resolveDefaultConsent(options.mode, categories, options.hasGlobalPrivacyControlSignal ?? false);
|
|
39
|
-
return {
|
|
40
|
-
decision,
|
|
41
|
-
denyAll: () => {
|
|
42
|
-
save(createConsentDecision([]));
|
|
43
|
-
},
|
|
44
|
-
effectiveConsent,
|
|
45
|
-
grantAll: () => {
|
|
46
|
-
save(createConsentDecision(categories));
|
|
47
|
-
},
|
|
48
|
-
isTrackingAllowed: effectiveConsent.analytics,
|
|
49
|
-
needsPrompt: options.mode === "opt-in" && decision === void 0,
|
|
50
|
-
save
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
//#endregion
|
|
54
|
-
export { useConsent };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { TrackedEvent } from "../core/tracked-event.mjs";
|
|
2
|
-
import { Destination } from "../core/destination.mjs";
|
|
3
|
-
import { EventCatalog, EventsOf } from "../core/event-catalog.mjs";
|
|
4
|
-
import { z } from "zod";
|
|
5
|
-
|
|
6
|
-
//#region src/server/create-server-tracker.d.ts
|
|
7
|
-
/**
|
|
8
|
-
* @since 0.5.0-canary.4
|
|
9
|
-
*/
|
|
10
|
-
interface ServerTrackContext {
|
|
11
|
-
anonymousId: string;
|
|
12
|
-
userId?: string | undefined;
|
|
13
|
-
}
|
|
14
|
-
/**
|
|
15
|
-
* @since 0.5.0-canary.4
|
|
16
|
-
*/
|
|
17
|
-
interface ServerTrackerOptions<Catalog extends EventCatalog> {
|
|
18
|
-
catalog: Catalog;
|
|
19
|
-
destinations: Array<Destination>;
|
|
20
|
-
generateEventId?: () => string;
|
|
21
|
-
maxRetries?: number;
|
|
22
|
-
onDestinationError?: (error: unknown, destination: Destination, event: TrackedEvent) => void;
|
|
23
|
-
retryDelayMs?: number;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* @since 0.5.0-canary.4
|
|
27
|
-
*/
|
|
28
|
-
interface ServerTracker<Catalog extends EventCatalog> {
|
|
29
|
-
/** Explicit anonymous → known-user merge, for when `identify` timing can't do it. */
|
|
30
|
-
alias: (previousId: string, userId: string, context: ServerTrackContext) => Promise<void>;
|
|
31
|
-
group: (groupId: string, traits: Record<string, unknown> | undefined, context: ServerTrackContext) => Promise<void>;
|
|
32
|
-
track: <Name extends keyof EventsOf<Catalog, "server">>(name: Name, props: z.infer<EventsOf<Catalog, "server">[Name]["schema"]>, context: ServerTrackContext) => Promise<void>;
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* No dead-letter queue yet (self-host storage is Phase 2) — a destination that keeps
|
|
36
|
-
* failing after retries is only logged via `onDestinationError`, never re-queued.
|
|
37
|
-
*
|
|
38
|
-
* @since 0.5.0-canary.4
|
|
39
|
-
*/
|
|
40
|
-
declare function createServerTracker<Catalog extends EventCatalog>(options: ServerTrackerOptions<Catalog>): ServerTracker<Catalog>;
|
|
41
|
-
//#endregion
|
|
42
|
-
export { ServerTrackContext, ServerTracker, ServerTrackerOptions, createServerTracker };
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { generateEventId } from "../core/event-id.mjs";
|
|
2
|
-
//#region src/server/create-server-tracker.ts
|
|
3
|
-
function sleep(ms) {
|
|
4
|
-
return new Promise((resolve) => {
|
|
5
|
-
setTimeout(resolve, ms);
|
|
6
|
-
});
|
|
7
|
-
}
|
|
8
|
-
async function sendWithRetry(destination, event, maxRetries, retryDelayMs, onError) {
|
|
9
|
-
for (let attempt = 0; attempt <= maxRetries; attempt += 1) try {
|
|
10
|
-
await destination.send(event);
|
|
11
|
-
return;
|
|
12
|
-
} catch (error) {
|
|
13
|
-
if (attempt === maxRetries) {
|
|
14
|
-
onError(error, destination, event);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
await sleep(retryDelayMs * 2 ** attempt);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* No dead-letter queue yet (self-host storage is Phase 2) — a destination that keeps
|
|
22
|
-
* failing after retries is only logged via `onDestinationError`, never re-queued.
|
|
23
|
-
*
|
|
24
|
-
* @since 0.5.0-canary.4
|
|
25
|
-
*/
|
|
26
|
-
function createServerTracker(options) {
|
|
27
|
-
const maxRetries = options.maxRetries ?? 2;
|
|
28
|
-
const retryDelayMs = options.retryDelayMs ?? 200;
|
|
29
|
-
const createId = options.generateEventId ?? generateEventId;
|
|
30
|
-
const onError = options.onDestinationError ?? ((error, destination, event) => {
|
|
31
|
-
console.error(`[tracking] destination "${destination.name}" failed for event "${event.name}"`, error);
|
|
32
|
-
});
|
|
33
|
-
async function sendEvent(name, props, context) {
|
|
34
|
-
const event = {
|
|
35
|
-
anonymousId: context.anonymousId,
|
|
36
|
-
eventId: createId(),
|
|
37
|
-
name,
|
|
38
|
-
owner: "server",
|
|
39
|
-
props,
|
|
40
|
-
timestamp: Date.now(),
|
|
41
|
-
...context.userId === void 0 ? {} : { userId: context.userId }
|
|
42
|
-
};
|
|
43
|
-
await Promise.all(options.destinations.map(async (destination) => sendWithRetry(destination, event, maxRetries, retryDelayMs, onError)));
|
|
44
|
-
}
|
|
45
|
-
return {
|
|
46
|
-
async alias(previousId, userId, context) {
|
|
47
|
-
await sendEvent("$alias", {
|
|
48
|
-
previousId,
|
|
49
|
-
userId
|
|
50
|
-
}, context);
|
|
51
|
-
},
|
|
52
|
-
async group(groupId, traits, context) {
|
|
53
|
-
await sendEvent("$group", {
|
|
54
|
-
groupId,
|
|
55
|
-
...traits
|
|
56
|
-
}, context);
|
|
57
|
-
},
|
|
58
|
-
async track(name, props, context) {
|
|
59
|
-
const definition = options.catalog[name];
|
|
60
|
-
if (!definition || definition.owner !== "server") throw new Error(`Unknown server-owned event: ${String(name)}`);
|
|
61
|
-
definition.schema.parse(props);
|
|
62
|
-
await sendEvent(name, props, context);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
//#endregion
|
|
67
|
-
export { createServerTracker };
|
package/dist/server/index.d.mts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { ServerTrackContext, ServerTracker, ServerTrackerOptions, createServerTracker } from "./create-server-tracker.mjs";
|
|
2
|
-
import { resolveRegion, resolveRegionFromCountryCode } from "./region.mjs";
|
|
3
|
-
export { type ServerTrackContext, type ServerTracker, type ServerTrackerOptions, createServerTracker, resolveRegion, resolveRegionFromCountryCode };
|
package/dist/server/index.mjs
DELETED
package/dist/server/region.d.mts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { ConsentRegion } from "../core/consent.mjs";
|
|
2
|
-
|
|
3
|
-
//#region src/server/region.d.ts
|
|
4
|
-
/**
|
|
5
|
-
* Missing/unrecognized codes fall back to "other", which `resolveConsentMode` treats
|
|
6
|
-
* as opt-out — the least restrictive default, safe only because opt-in regions (EU/VN)
|
|
7
|
-
* are matched explicitly above it.
|
|
8
|
-
*
|
|
9
|
-
* @since 0.5.0-canary.4
|
|
10
|
-
*/
|
|
11
|
-
declare function resolveRegionFromCountryCode(countryCode: string | undefined): ConsentRegion;
|
|
12
|
-
/**
|
|
13
|
-
* Reads the hosting platform's geo header (e.g. Vercel's `x-vercel-ip-country`) — pass
|
|
14
|
-
* the incoming request's `Headers` from server-function/middleware context.
|
|
15
|
-
*
|
|
16
|
-
* @since 0.5.0-canary.4
|
|
17
|
-
*/
|
|
18
|
-
declare function resolveRegion(headers: Headers, headerName?: string): ConsentRegion;
|
|
19
|
-
//#endregion
|
|
20
|
-
export { resolveRegion, resolveRegionFromCountryCode };
|
package/dist/server/region.mjs
DELETED
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
//#region src/server/region.ts
|
|
2
|
-
const EU_COUNTRY_CODES = /* @__PURE__ */ new Set([
|
|
3
|
-
"AT",
|
|
4
|
-
"BE",
|
|
5
|
-
"BG",
|
|
6
|
-
"HR",
|
|
7
|
-
"CY",
|
|
8
|
-
"CZ",
|
|
9
|
-
"DK",
|
|
10
|
-
"EE",
|
|
11
|
-
"FI",
|
|
12
|
-
"FR",
|
|
13
|
-
"DE",
|
|
14
|
-
"GR",
|
|
15
|
-
"HU",
|
|
16
|
-
"IE",
|
|
17
|
-
"IT",
|
|
18
|
-
"LV",
|
|
19
|
-
"LT",
|
|
20
|
-
"LU",
|
|
21
|
-
"MT",
|
|
22
|
-
"NL",
|
|
23
|
-
"PL",
|
|
24
|
-
"PT",
|
|
25
|
-
"RO",
|
|
26
|
-
"SK",
|
|
27
|
-
"SI",
|
|
28
|
-
"ES",
|
|
29
|
-
"SE"
|
|
30
|
-
]);
|
|
31
|
-
/**
|
|
32
|
-
* Missing/unrecognized codes fall back to "other", which `resolveConsentMode` treats
|
|
33
|
-
* as opt-out — the least restrictive default, safe only because opt-in regions (EU/VN)
|
|
34
|
-
* are matched explicitly above it.
|
|
35
|
-
*
|
|
36
|
-
* @since 0.5.0-canary.4
|
|
37
|
-
*/
|
|
38
|
-
function resolveRegionFromCountryCode(countryCode) {
|
|
39
|
-
if (!countryCode) return "other";
|
|
40
|
-
const normalized = countryCode.toUpperCase();
|
|
41
|
-
if (EU_COUNTRY_CODES.has(normalized)) return "eu";
|
|
42
|
-
if (normalized === "VN") return "vn";
|
|
43
|
-
if (normalized === "US") return "us";
|
|
44
|
-
return "other";
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Reads the hosting platform's geo header (e.g. Vercel's `x-vercel-ip-country`) — pass
|
|
48
|
-
* the incoming request's `Headers` from server-function/middleware context.
|
|
49
|
-
*
|
|
50
|
-
* @since 0.5.0-canary.4
|
|
51
|
-
*/
|
|
52
|
-
function resolveRegion(headers, headerName = "x-vercel-ip-country") {
|
|
53
|
-
return resolveRegionFromCountryCode(headers.get(headerName) ?? void 0);
|
|
54
|
-
}
|
|
55
|
-
//#endregion
|
|
56
|
-
export { resolveRegion, resolveRegionFromCountryCode };
|
package/src/client/index.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export type { ClientTracker, ClientTrackerOptions } from "#/client/create-client-tracker";
|
|
2
|
-
export { createClientTracker } from "#/client/create-client-tracker";
|
|
3
|
-
|
|
4
|
-
export { createLocalStorageConsentStorage } from "#/client/consent-storage";
|
|
5
|
-
|
|
6
|
-
export { hasGlobalPrivacyControlSignal } from "#/client/gpc";
|
|
7
|
-
|
|
8
|
-
export type { ClientLifecycleOptions } from "#/client/lifecycle";
|
|
9
|
-
export { attachClientLifecycle } from "#/client/lifecycle";
|
|
10
|
-
|
|
11
|
-
export { createLocalStorageQueueStorage } from "#/client/local-storage";
|
|
12
|
-
|
|
13
|
-
export type { EventQueueOptions, EventQueueStorage } from "#/client/queue";
|
|
14
|
-
export { EventQueue } from "#/client/queue";
|
|
15
|
-
|
|
16
|
-
export type { RouterLike } from "#/client/router";
|
|
17
|
-
export { attachRouterPageTracking } from "#/client/router";
|
package/src/client/lifecycle.ts
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { ClientTracker } from "#/client/create-client-tracker";
|
|
2
|
-
import type { EventCatalog } from "#/core/event-catalog";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @since 0.5.0-canary.4
|
|
6
|
-
*/
|
|
7
|
-
export interface ClientLifecycleOptions {
|
|
8
|
-
/** `sendBeacon` target used on unload — omit to skip the unload flush entirely. */
|
|
9
|
-
beaconEndpoint?: string;
|
|
10
|
-
flushIntervalMs?: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Wires the two flush triggers the queue itself can't own (batch-size is handled inside
|
|
15
|
-
* `EventQueue`): a periodic interval flush, and a best-effort `sendBeacon` flush on
|
|
16
|
-
* unload — `fetch` isn't reliable once the page starts unloading, `sendBeacon` is.
|
|
17
|
-
* Returns a cleanup function to remove all listeners/timers.
|
|
18
|
-
*
|
|
19
|
-
* @since 0.5.0-canary.4
|
|
20
|
-
*/
|
|
21
|
-
export function attachClientLifecycle<Catalog extends EventCatalog>(
|
|
22
|
-
tracker: Pick<ClientTracker<Catalog>, "flush" | "flushWithBeacon">,
|
|
23
|
-
options: ClientLifecycleOptions = {},
|
|
24
|
-
): () => void {
|
|
25
|
-
const flushIntervalMs = options.flushIntervalMs ?? 10_000;
|
|
26
|
-
const intervalId = setInterval(() => {
|
|
27
|
-
void tracker.flush();
|
|
28
|
-
}, flushIntervalMs);
|
|
29
|
-
|
|
30
|
-
function flushOnUnload(): void {
|
|
31
|
-
if (options.beaconEndpoint !== undefined) {
|
|
32
|
-
tracker.flushWithBeacon(options.beaconEndpoint);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
function handleVisibilityChange(): void {
|
|
37
|
-
if (document.visibilityState === "hidden") {
|
|
38
|
-
flushOnUnload();
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
document.addEventListener("visibilitychange", handleVisibilityChange);
|
|
43
|
-
window.addEventListener("pagehide", flushOnUnload);
|
|
44
|
-
|
|
45
|
-
return () => {
|
|
46
|
-
clearInterval(intervalId);
|
|
47
|
-
document.removeEventListener("visibilitychange", handleVisibilityChange);
|
|
48
|
-
window.removeEventListener("pagehide", flushOnUnload);
|
|
49
|
-
};
|
|
50
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { EventQueueStorage } from "#/client/queue";
|
|
2
|
-
import type { TrackedEvent } from "#/core/tracked-event";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* `localStorage`-backed queue storage — corrupt/missing state, a private-mode/quota
|
|
6
|
-
* error, or no `window` (SSR) are all treated as empty rather than thrown, since a lost
|
|
7
|
-
* offline queue is preferable to a crashing app.
|
|
8
|
-
*
|
|
9
|
-
* @since 0.5.0-canary.4
|
|
10
|
-
*/
|
|
11
|
-
export function createLocalStorageQueueStorage(storageKey: string): EventQueueStorage {
|
|
12
|
-
return {
|
|
13
|
-
load(): Array<TrackedEvent> {
|
|
14
|
-
if (typeof globalThis.window === "undefined") {
|
|
15
|
-
return [];
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
try {
|
|
19
|
-
const raw = globalThis.window.localStorage.getItem(storageKey);
|
|
20
|
-
|
|
21
|
-
if (!raw) {
|
|
22
|
-
return [];
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const parsed: unknown = JSON.parse(raw);
|
|
26
|
-
|
|
27
|
-
return Array.isArray(parsed) ? (parsed as Array<TrackedEvent>) : [];
|
|
28
|
-
} catch {
|
|
29
|
-
return [];
|
|
30
|
-
}
|
|
31
|
-
},
|
|
32
|
-
save(events: Array<TrackedEvent>): void {
|
|
33
|
-
if (typeof globalThis.window === "undefined") {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
try {
|
|
38
|
-
globalThis.window.localStorage.setItem(storageKey, JSON.stringify(events));
|
|
39
|
-
} catch {
|
|
40
|
-
/* storage blocked (private mode / quota) */
|
|
41
|
-
}
|
|
42
|
-
},
|
|
43
|
-
};
|
|
44
|
-
}
|