@vulcantech/analytics 0.1.0 → 0.1.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/README.md +92 -30
- package/dist/index.d.mts +90 -8
- package/dist/index.d.ts +90 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/route.d.mts +21 -0
- package/dist/route.d.ts +21 -0
- package/dist/route.js.map +1 -1
- package/dist/route.mjs.map +1 -1
- package/dist/tags.d.mts +47 -13
- package/dist/tags.d.ts +47 -13
- package/dist/tags.js.map +1 -1
- package/dist/tags.mjs.map +1 -1
- package/package.json +6 -6
- package/src/AnalyticsTracker.tsx +13 -3
- package/src/route.ts +21 -0
- package/src/tags.tsx +45 -11
- package/src/tracker.ts +127 -14
package/src/tracker.ts
CHANGED
|
@@ -9,7 +9,15 @@
|
|
|
9
9
|
const ENDPOINT = "/api/a";
|
|
10
10
|
const SESSION_KEY = "__vt_sid";
|
|
11
11
|
|
|
12
|
-
/**
|
|
12
|
+
/**
|
|
13
|
+
* Generate a random session ID (stored in sessionStorage, not cookies).
|
|
14
|
+
*
|
|
15
|
+
* No cookies means nothing survives a tab close, and there is no cross-tab or
|
|
16
|
+
* cross-visit identity by design. When `sessionStorage` throws (private mode,
|
|
17
|
+
* blocked storage) this returns a FRESH id per call, so each event from that
|
|
18
|
+
* visitor counts as its own session — inflating session counts rather than
|
|
19
|
+
* failing.
|
|
20
|
+
*/
|
|
13
21
|
function getSessionId(): string {
|
|
14
22
|
if (typeof window === "undefined") return "";
|
|
15
23
|
try {
|
|
@@ -42,20 +50,69 @@ function getUtmParams(): Record<string, string> {
|
|
|
42
50
|
return utms;
|
|
43
51
|
}
|
|
44
52
|
|
|
53
|
+
/**
|
|
54
|
+
* One analytics event as sent to `/api/a` and forwarded to the CMS.
|
|
55
|
+
*
|
|
56
|
+
* The limits noted per field are enforced SERVER-SIDE on ingest
|
|
57
|
+
* (`/api/analytics` → `lib/analytics/ingest` in vulcantech-cms). Over-length
|
|
58
|
+
* values are truncated and unsupported shapes are dropped — never rejected —
|
|
59
|
+
* so an oversized event is stored lossily rather than failing loudly.
|
|
60
|
+
*
|
|
61
|
+
* `type`, `url`, `sid` and `ts` are required; an event missing any of them is
|
|
62
|
+
* rejected at ingest with a 400.
|
|
63
|
+
*/
|
|
45
64
|
export interface AnalyticsEvent {
|
|
65
|
+
/** The only two valid values. */
|
|
46
66
|
type: "pageview" | "event";
|
|
67
|
+
/**
|
|
68
|
+
* Pageviews send `pathname + search`; custom events send `pathname` only.
|
|
69
|
+
* Truncated to 2048 chars on ingest, with HTML tags stripped.
|
|
70
|
+
*/
|
|
47
71
|
url: string;
|
|
72
|
+
/**
|
|
73
|
+
* `document.referrer` on pageviews, `""` on custom events. Truncated to
|
|
74
|
+
* 2048 chars. Together with {@link AnalyticsEvent.utm} this drives the CMS
|
|
75
|
+
* traffic-source classification — so custom events carry no attribution.
|
|
76
|
+
*/
|
|
48
77
|
ref: string;
|
|
78
|
+
/** Session id from `sessionStorage` — see {@link getSessionId}. Capped at 64 chars. */
|
|
49
79
|
sid: string;
|
|
80
|
+
/**
|
|
81
|
+
* `window.innerWidth`. Not merely diagnostic: the CMS derives the device
|
|
82
|
+
* breakdown from it (<768 mobile, <1024 tablet, else desktop).
|
|
83
|
+
*/
|
|
50
84
|
vw: number;
|
|
85
|
+
/** `window.innerHeight`. */
|
|
51
86
|
vh: number;
|
|
87
|
+
/** `Date.now()` on the client. The CMS stores its own `serverTs` alongside it. */
|
|
52
88
|
ts: number;
|
|
89
|
+
/** Pageviews only, and only for UTM params actually present in the URL. */
|
|
53
90
|
utm?: Record<string, string>;
|
|
91
|
+
/**
|
|
92
|
+
* Custom events only. Truncated to 128 chars.
|
|
93
|
+
*
|
|
94
|
+
* `"tracker_test"` is RESERVED — the CMS discards events with that name
|
|
95
|
+
* (and events with url `/__analytics_test__`) so the connection self-test
|
|
96
|
+
* never pollutes stats. They will never appear in the dashboard.
|
|
97
|
+
*/
|
|
54
98
|
name?: string;
|
|
99
|
+
/**
|
|
100
|
+
* Custom events only. Must be FLAT primitives: nested objects and arrays
|
|
101
|
+
* are dropped on ingest, so flatten before sending. At most 32 keys
|
|
102
|
+
* survive; keys are truncated to 64 chars, string values to 256, and HTML
|
|
103
|
+
* tags are stripped from both.
|
|
104
|
+
*/
|
|
55
105
|
props?: Record<string, string | number | boolean>;
|
|
56
106
|
}
|
|
57
107
|
|
|
58
|
-
/**
|
|
108
|
+
/**
|
|
109
|
+
* Check if we're in a dev environment — skip all tracking if so.
|
|
110
|
+
*
|
|
111
|
+
* Matches `localhost`, `127.0.0.1`, `0.0.0.0`, any `*.local` host, and any
|
|
112
|
+
* hostname CONTAINING "localhost". Tracker functions stay safe to call on
|
|
113
|
+
* those hosts; they simply do nothing, so no dev-only guard is needed at the
|
|
114
|
+
* call site. Test conversions against a deployed URL.
|
|
115
|
+
*/
|
|
59
116
|
function isDev(): boolean {
|
|
60
117
|
if (typeof window === "undefined") return true;
|
|
61
118
|
const host = window.location.hostname;
|
|
@@ -68,7 +125,15 @@ function isDev(): boolean {
|
|
|
68
125
|
);
|
|
69
126
|
}
|
|
70
127
|
|
|
71
|
-
/**
|
|
128
|
+
/**
|
|
129
|
+
* Send an analytics event via beacon (non-blocking).
|
|
130
|
+
*
|
|
131
|
+
* Never throws and never reports failure: send errors are swallowed, and the
|
|
132
|
+
* `/api/a` proxy answers 204 even when the CMS was unreachable or rejected
|
|
133
|
+
* the event. The CMS additionally drops events silently when the request is
|
|
134
|
+
* bot-flagged by user agent or rate limited (100/min per `sid`, 600/min per
|
|
135
|
+
* IP). Confirm ingestion in the CMS dashboard, not from the network tab.
|
|
136
|
+
*/
|
|
72
137
|
function send(event: AnalyticsEvent): void {
|
|
73
138
|
if (isDev()) return;
|
|
74
139
|
const payload = JSON.stringify(event);
|
|
@@ -104,7 +169,17 @@ export function trackPageView(): void {
|
|
|
104
169
|
});
|
|
105
170
|
}
|
|
106
171
|
|
|
107
|
-
/**
|
|
172
|
+
/**
|
|
173
|
+
* Track a custom event.
|
|
174
|
+
*
|
|
175
|
+
* `props` must be FLAT primitives — nested objects and arrays are dropped by
|
|
176
|
+
* the CMS on ingest, along with anything past the 32nd key. See
|
|
177
|
+
* {@link AnalyticsEvent.props} for the full set of caps.
|
|
178
|
+
*
|
|
179
|
+
* Avoid the reserved name `"tracker_test"`; the CMS discards it.
|
|
180
|
+
*
|
|
181
|
+
* No-ops during SSR and on dev hostnames, and never throws.
|
|
182
|
+
*/
|
|
108
183
|
export function trackEvent(
|
|
109
184
|
name: string,
|
|
110
185
|
props?: Record<string, string | number | boolean>,
|
|
@@ -123,12 +198,23 @@ export function trackEvent(
|
|
|
123
198
|
});
|
|
124
199
|
}
|
|
125
200
|
|
|
126
|
-
/**
|
|
201
|
+
/**
|
|
202
|
+
* Track when a form comes into view.
|
|
203
|
+
*
|
|
204
|
+
* Usually you do NOT call this yourself: `@vulcantech/forms` finds it on
|
|
205
|
+
* `window.__vtTrack` and fires it automatically for every rendered form. Only
|
|
206
|
+
* call it manually for a form this package's global hook can't see.
|
|
207
|
+
*/
|
|
127
208
|
export function trackFormView(formId: string, formName?: string): void {
|
|
128
209
|
trackEvent("form_view", { formId, formName: formName ?? "" });
|
|
129
210
|
}
|
|
130
211
|
|
|
131
|
-
/**
|
|
212
|
+
/**
|
|
213
|
+
* Track when a form is submitted successfully.
|
|
214
|
+
*
|
|
215
|
+
* Like {@link trackFormView}, `@vulcantech/forms` fires this automatically via
|
|
216
|
+
* `window.__vtTrack`. `submissionType` defaults to `"contact"`.
|
|
217
|
+
*/
|
|
132
218
|
export function trackFormSubmit(
|
|
133
219
|
formId: string,
|
|
134
220
|
formName?: string,
|
|
@@ -141,17 +227,36 @@ export function trackFormSubmit(
|
|
|
141
227
|
});
|
|
142
228
|
}
|
|
143
229
|
|
|
144
|
-
/**
|
|
230
|
+
/**
|
|
231
|
+
* Track when a phone number link is clicked.
|
|
232
|
+
*
|
|
233
|
+
* ⚠ Do NOT wire this into an `onClick` on an `<a href="tel:…">` — importing
|
|
234
|
+
* this module installs a delegated document listener that already fires it for
|
|
235
|
+
* every such anchor, so adding your own handler DOUBLE-COUNTS the conversion.
|
|
236
|
+
* Call it manually only for a non-anchor trigger (e.g. a button that dials via
|
|
237
|
+
* JS).
|
|
238
|
+
*/
|
|
145
239
|
export function trackPhoneClick(phoneNumber?: string): void {
|
|
146
240
|
trackEvent("phone_click", { phone: phoneNumber ?? "" });
|
|
147
241
|
}
|
|
148
242
|
|
|
149
|
-
/**
|
|
243
|
+
/**
|
|
244
|
+
* Track when an email link is clicked.
|
|
245
|
+
*
|
|
246
|
+
* ⚠ Same double-count caveat as {@link trackPhoneClick}: `<a href="mailto:…">`
|
|
247
|
+
* clicks are already captured by the delegated listener installed on import.
|
|
248
|
+
*/
|
|
150
249
|
export function trackEmailClick(email?: string): void {
|
|
151
250
|
trackEvent("email_click", { email: email ?? "" });
|
|
152
251
|
}
|
|
153
252
|
|
|
154
|
-
/**
|
|
253
|
+
/**
|
|
254
|
+
* Track a 404 page.
|
|
255
|
+
*
|
|
256
|
+
* Client-only. Unlike the other trackers, this dereferences `window` while
|
|
257
|
+
* building its argument, so calling it during SSR throws a ReferenceError —
|
|
258
|
+
* call it from a client component (or an effect), or pass `url` explicitly.
|
|
259
|
+
*/
|
|
155
260
|
export function track404(url?: string): void {
|
|
156
261
|
trackEvent("404", { url: url ?? window?.location?.pathname ?? "" });
|
|
157
262
|
}
|
|
@@ -160,11 +265,19 @@ export function track404(url?: string): void {
|
|
|
160
265
|
/* Global hook registration */
|
|
161
266
|
/* ------------------------------------------------------------------ */
|
|
162
267
|
|
|
163
|
-
//
|
|
164
|
-
//
|
|
165
|
-
//
|
|
166
|
-
//
|
|
167
|
-
//
|
|
268
|
+
// TWO IMPORT-TIME SIDE EFFECTS, client-only. Importing this module (which
|
|
269
|
+
// AnalyticsTracker does) is enough to activate both — integrators get them
|
|
270
|
+
// whether or not they ask.
|
|
271
|
+
//
|
|
272
|
+
// 1. `window.__vtTrack` exposes the conversion trackers so UI packages (e.g.
|
|
273
|
+
// @vulcantech/forms) can report form views/submits without a hard
|
|
274
|
+
// dependency on this module. Passing onFormView/onFormSubmit props to
|
|
275
|
+
// DynamicForm OVERRIDES these rather than adding to them.
|
|
276
|
+
//
|
|
277
|
+
// 2. ONE delegated capture-phase click listener makes every
|
|
278
|
+
// `a[href^="tel:"]` / `a[href^="mailto:"]` on the site track a phone/email
|
|
279
|
+
// conversion with zero per-component wiring. Consequently, adding a manual
|
|
280
|
+
// onClick tracker to such an anchor double-counts it.
|
|
168
281
|
if (typeof window !== "undefined") {
|
|
169
282
|
const w = window as unknown as Record<string, unknown>;
|
|
170
283
|
w.__vtTrack = {
|