@vulcantech/analytics 0.1.1 → 0.2.0
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 +113 -2
- package/dist/index.d.mts +145 -9
- package/dist/index.d.ts +145 -9
- package/dist/index.js +132 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +133 -6
- package/dist/index.mjs.map +1 -1
- package/dist/route.d.mts +27 -0
- package/dist/route.d.ts +27 -0
- package/dist/route.js +4 -1
- package/dist/route.js.map +1 -1
- package/dist/route.mjs +4 -1
- package/dist/route.mjs.map +1 -1
- package/dist/tags.d.mts +100 -16
- package/dist/tags.d.ts +100 -16
- package/dist/tags.js +42 -3
- package/dist/tags.js.map +1 -1
- package/dist/tags.mjs +42 -3
- package/dist/tags.mjs.map +1 -1
- package/package.json +8 -7
- package/src/AnalyticsTracker.tsx +13 -3
- package/src/PhoneLink.tsx +69 -0
- package/src/index.tsx +2 -0
- package/src/route.ts +30 -0
- package/src/tags.tsx +179 -16
- package/src/tracker.ts +345 -17
package/README.md
CHANGED
|
@@ -10,6 +10,113 @@ Exports:
|
|
|
10
10
|
- `@vulcantech/analytics/route` — App Router proxy handler (`POST`, `OPTIONS`) for `/api/a`.
|
|
11
11
|
- `@vulcantech/analytics/tags` — `CMSTags` server component + `fetchTrackingTags` + the `TrackingTags` type.
|
|
12
12
|
|
|
13
|
+
## Event shape (`AnalyticsEvent`)
|
|
14
|
+
|
|
15
|
+
Every tracker call builds one of these client-side and beacons it to `/api/a`, which forwards it to the CMS. You rarely construct one by hand, but the field limits below are enforced **server-side on ingest** — anything over them is silently truncated or dropped, not rejected.
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
interface AnalyticsEvent {
|
|
19
|
+
type: "pageview" | "event"; // the only two valid values
|
|
20
|
+
url: string; // pageview: pathname + search. event: pathname only
|
|
21
|
+
ref: string; // document.referrer on pageviews; "" on custom events
|
|
22
|
+
sid: string; // session id — see Session identity below
|
|
23
|
+
vw: number; // window.innerWidth — the CMS derives device from this
|
|
24
|
+
vh: number; // window.innerHeight
|
|
25
|
+
ts: number; // Date.now() on the client (clock is not trusted)
|
|
26
|
+
utm?: Record<string, string>; // pageviews only, and only when present in the URL
|
|
27
|
+
name?: string; // custom events only — the event name
|
|
28
|
+
props?: Record<string, string | number | boolean>; // custom events only
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Field notes — all verified against the CMS ingest path (`/api/analytics` → `lib/analytics/ingest`):
|
|
33
|
+
|
|
34
|
+
- **`props` must be flat primitives.** Nested objects and arrays are **dropped** on ingest, so `{ items: [...] }` or `{ user: { id } }` silently vanish — flatten to `string | number | boolean` yourself. At most **32 keys** survive; keys are truncated to 64 chars, string values to 256, and HTML tags are stripped from both.
|
|
35
|
+
- **Length caps:** `url` and `ref` 2048, `name` 128, `sid` 64. Over-length values are truncated, not rejected.
|
|
36
|
+
- **`vw` drives the device breakdown** in the CMS dashboard: `<768` mobile, `<1024` tablet, else desktop. It is not merely diagnostic.
|
|
37
|
+
- **`ref` + `utm` drive traffic-source classification.** Custom events send `ref: ""`, so only pageviews carry attribution — don't expect a source on `trackEvent` data.
|
|
38
|
+
- **Reserved names.** Events with `name === "tracker_test"` or `url === "/__analytics_test__"` are deliberately discarded by the CMS (they exist for the connection self-test) and will never appear in stats. Don't use those values.
|
|
39
|
+
- **`type`, `url`, `sid`, `ts` are required** — an event missing any of them is rejected with a 400 at ingest.
|
|
40
|
+
|
|
41
|
+
### Session identity
|
|
42
|
+
|
|
43
|
+
`sid` is a random ID in `sessionStorage` under `__vt_sid` — **no cookies, so nothing survives a tab close** and there is no cross-tab or cross-visit identity. When `sessionStorage` throws (private mode, blocked storage), the tracker falls back to a fresh random ID **per event**, which inflates session counts for those visitors. This is by design; the package never fingerprints.
|
|
44
|
+
|
|
45
|
+
### Conversion tracking is automatic — do not double-wire
|
|
46
|
+
|
|
47
|
+
Importing this package (which `AnalyticsTracker` does) has two side effects at module load, on the client only:
|
|
48
|
+
|
|
49
|
+
1. **A delegated `document` click listener** (capture phase, installed once) fires `trackPhoneClick` / `trackEmailClick` for **every** `a[href^="tel:"]` and `a[href^="mailto:"]` on the site. Adding your own `onClick={() => trackPhoneClick(...)}` **double-counts the conversion** — the listener already caught it. Only call those functions manually for a non-anchor trigger (e.g. a button that dials via JS).
|
|
50
|
+
2. **`window.__vtTrack`** is registered with the four conversion trackers. `@vulcantech/forms` picks it up, so `form_view` / `form_submit` conversions track automatically on any site running `AnalyticsTracker`, with no per-form wiring. Passing `onFormView` / `onFormSubmit` props to `DynamicForm` **overrides** this rather than adding to it.
|
|
51
|
+
|
|
52
|
+
### Dev-host skip
|
|
53
|
+
|
|
54
|
+
`send()` no-ops when the hostname is `localhost`, `127.0.0.1`, `0.0.0.0`, ends in `.local`, or contains `"localhost"`. The tracker functions are still safe to call — they just do nothing — so there is no dev-only guard to write. Test conversions against a deployed URL.
|
|
55
|
+
|
|
56
|
+
### `AnalyticsTracker` firing rules
|
|
57
|
+
|
|
58
|
+
Client component (`"use client"`), renders `null`, and wraps itself in `Suspense` (Next 16 requires `useSearchParams` inside one) — so it is safe to drop straight into a server layout. It fires **exactly one** `trackPageView()` on mount and one per `pathname` **or** `searchParams` change. Because it watches search params, `/x?page=2` counts as a **separate pageview** from `/x`.
|
|
59
|
+
|
|
60
|
+
## Tag config shape (`TrackingTags`)
|
|
61
|
+
|
|
62
|
+
`fetchTrackingTags()` resolves the project's third-party tag IDs; `CMSTags` renders them. The type carries only public IDs — the CMS strips its internal `enabled` / `updatedAt` fields before returning them to a consumer site.
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
interface TrackingTags {
|
|
66
|
+
ga4Id?: string; // GA4 measurement ID — CMS validates /^G-[A-Z0-9]{4,14}$/
|
|
67
|
+
googleAdsId?: string; // Google Ads conversion ID — CMS validates /^AW-\d{6,12}$/
|
|
68
|
+
metaPixelId?: string; // Meta Pixel ID — CMS validates /^\d{6,20}$/ (numeric string)
|
|
69
|
+
// Google Ads form-conversion send_to — CMS validates /^AW-\d{6,12}\/[A-Za-z0-9_-]{6,64}$/
|
|
70
|
+
formConversionSendTo?: string;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
- **`fetchTrackingTags()` returns `null`, not `{}`,** when all three tag IDs resolve empty (`formConversionSendTo` does not count — see below). `CMSTags` renders `null` in that case.
|
|
75
|
+
- **Format validation is CMS-side only.** Those patterns are enforced when a staff user saves the config in the dashboard. Values coming from the `NEXT_PUBLIC_*` env fallbacks are **not** validated by this package — a typo'd env ID is injected verbatim.
|
|
76
|
+
- **`CMSTags` is an async React Server Component.** Do not add `"use client"` to a wrapper around it and do not call `fetchTrackingTags()` from client code — it reads `VULCANTECH_API_KEY`.
|
|
77
|
+
- **What it renders:** one shared `next/script` `gtag.js` loader with `strategy="afterInteractive"`, keyed to the **first** present Google ID, plus an inline init that `gtag('config', …)`s every present Google ID; and, for Meta, the standard inline `fbq` bootstrap with `fbq('track','PageView')` and a `<noscript>` tracking-pixel `<img>`. Because it uses `next/script`, mounting it in `<body>` is correct — it does not need to be in `<head>`.
|
|
78
|
+
- **No consent gating.** Tags load as soon as the page hydrates. If the site needs a CMP / GDPR consent gate, `CMSTags` does not provide one and must not be mounted unconditionally on a site that requires consent.
|
|
79
|
+
|
|
80
|
+
### `formConversionSendTo` — the form-submit Ads conversion
|
|
81
|
+
|
|
82
|
+
`formConversionSendTo` is **not a tag loader**. It is the Google Ads `send_to` value (`AW-<conversion id>/<label>`, e.g. `AW-18321257894/AbC-D3fGhIjKlmNoP`) that `@vulcantech/forms` fires when one of its forms submits successfully:
|
|
83
|
+
|
|
84
|
+
```js
|
|
85
|
+
gtag("event", "conversion", { send_to: "AW-18321257894/AbC-D3fGhIjKlmNoP" });
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The two packages have no dependency on each other, so `CMSTags` hands the value over through a global: the inline `vt-gtag-init` script assigns it to **`window.__vtFormConversionSendTo`**, and `DynamicForm` reads it there on success. A form's own `submitConfig.conversion.sendTo` takes precedence.
|
|
89
|
+
|
|
90
|
+
Consequences worth knowing:
|
|
91
|
+
|
|
92
|
+
- **It needs a Google ID to ride on.** The label is only published inside the `gtag` init block, which renders only when `ga4Id` or `googleAdsId` is set. A `formConversionSendTo` on its own renders nothing and does not count towards "something is configured" — `fetchTrackingTags()` still returns `null`.
|
|
93
|
+
- **Set it once per project, not per form.** Nothing in a consumer app should hardcode the label.
|
|
94
|
+
- **The `form_submit_success` dataLayer event fires regardless of this field** — see the `@vulcantech/forms` README. This field only controls the *direct* `gtag` conversion, which sites without a GTM container need.
|
|
95
|
+
|
|
96
|
+
**Where to get the value:** Google Ads → Goals → Summary → the conversion action → Tag setup → "Install the tag yourself"; the `send_to` in the snippet is the whole value, ID and label together.
|
|
97
|
+
|
|
98
|
+
### The `enabled` toggle does not override env fallbacks
|
|
99
|
+
|
|
100
|
+
Turning tracking tags **off** in the CMS makes `/api/analytics/config` return `config: null` — which is the *same* response as "never configured". `fetchTrackingTags` then fills each field from `NEXT_PUBLIC_GA_ID` / `NEXT_PUBLIC_GOOGLE_ADS_ID` / `NEXT_PUBLIC_META_PIXEL_ID`. **So a site with those env vars set keeps firing its tags after the CMS switch is off.** To actually disable tags on such a site you must unset the env vars and redeploy. Sites that rely purely on the CMS (no `NEXT_PUBLIC_*` set) honor the toggle as expected.
|
|
101
|
+
|
|
102
|
+
## Failure mode
|
|
103
|
+
|
|
104
|
+
**Nothing in this package throws, and nothing surfaces an error to your code.** There is no error state to render and no `catch` block worth writing — but that also means a fully broken pipeline looks identical to a working one from the site's side.
|
|
105
|
+
|
|
106
|
+
- **Trackers** (`trackEvent` and friends) swallow every send failure and return `void`. They also no-op entirely on SSR and on dev hostnames.
|
|
107
|
+
- **The `/api/a` proxy route** returns `503` when `VULCANTECH_PROJECT_ID` / `VULCANTECH_API_KEY` are unset and `400` on unparseable JSON — otherwise it **always returns `204`, including when the CMS was unreachable or rejected the event.** A 204 from `/api/a` is *not* evidence that anything was ingested.
|
|
108
|
+
- **The CMS drops events silently** when the request is bot-flagged by user agent, or rate limited — **100 events/min per `sid` and 600/min per IP**. Neither is reported back to the browser.
|
|
109
|
+
- **`fetchTrackingTags` / `CMSTags`** swallow CMS errors and fall through to the env fallbacks, then render `null` if those are empty too. A page build can never fail on the CMS being down.
|
|
110
|
+
|
|
111
|
+
**Two different API-key scopes are required, and the common misconfiguration is having only one.** Getting this wrong is invisible: events vanish while `/api/a` still answers 204.
|
|
112
|
+
|
|
113
|
+
| Surface | CMS endpoint | Required scope |
|
|
114
|
+
|---|---|---|
|
|
115
|
+
| `AnalyticsTracker` / trackers, via the `/api/a` proxy | `POST /api/analytics` | **`analytics:write`** |
|
|
116
|
+
| `CMSTags` / `fetchTrackingTags` | `GET /api/analytics/config` | **`analytics:read`** |
|
|
117
|
+
|
|
118
|
+
A site doing both needs **both scopes on the one key**. Verify ingestion in the CMS dashboard (Analytics → realtime), not by watching the browser's network tab for a 204.
|
|
119
|
+
|
|
13
120
|
## Integration
|
|
14
121
|
|
|
15
122
|
Wire analytics into a consumer site (Next.js App Router):
|
|
@@ -54,6 +161,8 @@ import { CMSTags } from "@vulcantech/analytics/tags";
|
|
|
54
161
|
|
|
55
162
|
The API key must be granted the **`analytics:read`** scope in the CMS for `CMSTags` to resolve tag IDs (the `/api/analytics/config` endpoint). Without it (or with tags disabled) the CMS returns `null` and only the env fallbacks apply.
|
|
56
163
|
|
|
164
|
+
Event ingestion needs a **separate** scope — **`analytics:write`** — on the same key. See the scope table under [Failure mode](#failure-mode); a key with only `analytics:read` drops every event silently.
|
|
165
|
+
|
|
57
166
|
### Env fallbacks (tags)
|
|
58
167
|
When the CMS omits a field (unreachable, unconfigured, or key lacks scope), each tag ID falls back to a public env var — **CMS value wins when present**:
|
|
59
168
|
|
|
@@ -62,8 +171,9 @@ When the CMS omits a field (unreachable, unconfigured, or key lacks scope), each
|
|
|
62
171
|
| GA4 | `ga4Id` | `NEXT_PUBLIC_GA_ID` |
|
|
63
172
|
| Google Ads | `googleAdsId` | `NEXT_PUBLIC_GOOGLE_ADS_ID` |
|
|
64
173
|
| Meta Pixel | `metaPixelId` | `NEXT_PUBLIC_META_PIXEL_ID` |
|
|
174
|
+
| Form conversion `send_to` | `formConversionSendTo` | `NEXT_PUBLIC_FORM_CONVERSION_SEND_TO` |
|
|
65
175
|
|
|
66
|
-
If all three end up empty, `CMSTags` renders nothing.
|
|
176
|
+
If all three **tag IDs** end up empty, `CMSTags` renders nothing — a `formConversionSendTo` alone is not enough, since it has no tag to attach to.
|
|
67
177
|
|
|
68
178
|
### Per-app values
|
|
69
179
|
None beyond env — analytics and tags are fully config-driven from the CMS.
|
|
@@ -74,10 +184,11 @@ None beyond env — analytics and tags are fully config-driven from the CMS.
|
|
|
74
184
|
```js
|
|
75
185
|
Object.keys(window.google_tag_manager) // includes both G-… and AW-…
|
|
76
186
|
window.dataLayer.filter(a => a[0] === "config")
|
|
187
|
+
window.__vtFormConversionSendTo // the form-conversion label, when set
|
|
77
188
|
```
|
|
78
189
|
|
|
79
190
|
### Notes
|
|
80
|
-
- The tracker skips all sends on localhost/dev hostnames; test conversions against a deployed URL.
|
|
191
|
+
- The tracker skips all sends on localhost/dev hostnames; test conversions against a deployed URL. (Exact host list under [Dev-host skip](#dev-host-skip).)
|
|
81
192
|
- `CMSTags` never throws — tag resolution failures are swallowed so a page build can never break on the CMS being down.
|
|
82
193
|
- ISR invalidation: the CMS purges the `tracking-tags:{projectId}` tag when the project's analytics config changes. No consumer redeploy is needed for CMS value changes — ISR picks them up within ~60s (or instantly if the site is registered as a revalidation consumer).
|
|
83
194
|
- **Google Ads shares the GA4 loader.** There is one `gtag.js` request (keyed to the GA4 ID); the Ads ID rides on it via an inline `gtag('config','AW-…')`. Don't expect a separate `gtag/js?id=AW-…` request or a standalone DOM element for it.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { AnchorHTMLAttributes, ReactNode } from 'react';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* VulcanTech Analytics — lightweight first-party tracker.
|
|
@@ -7,33 +8,168 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
|
7
8
|
* Sends page views and custom events to /api/a on the same domain
|
|
8
9
|
* via navigator.sendBeacon (falls back to fetch).
|
|
9
10
|
*/
|
|
11
|
+
/**
|
|
12
|
+
* One analytics event as sent to `/api/a` and forwarded to the CMS.
|
|
13
|
+
*
|
|
14
|
+
* The limits noted per field are enforced SERVER-SIDE on ingest
|
|
15
|
+
* (`/api/analytics` → `lib/analytics/ingest` in vulcantech-cms). Over-length
|
|
16
|
+
* values are truncated and unsupported shapes are dropped — never rejected —
|
|
17
|
+
* so an oversized event is stored lossily rather than failing loudly.
|
|
18
|
+
*
|
|
19
|
+
* `type`, `url`, `sid` and `ts` are required; an event missing any of them is
|
|
20
|
+
* rejected at ingest with a 400.
|
|
21
|
+
*/
|
|
10
22
|
interface AnalyticsEvent {
|
|
23
|
+
/** The only two valid values. */
|
|
11
24
|
type: "pageview" | "event";
|
|
25
|
+
/**
|
|
26
|
+
* Pageviews send `pathname + search`; custom events send `pathname` only.
|
|
27
|
+
* Truncated to 2048 chars on ingest, with HTML tags stripped.
|
|
28
|
+
*/
|
|
12
29
|
url: string;
|
|
30
|
+
/**
|
|
31
|
+
* `document.referrer` on pageviews, `""` on custom events. Truncated to
|
|
32
|
+
* 2048 chars. Together with {@link AnalyticsEvent.utm} this drives the CMS
|
|
33
|
+
* traffic-source classification; custom events have no referrer, so their
|
|
34
|
+
* attribution rides entirely on {@link AnalyticsEvent.utm}.
|
|
35
|
+
*/
|
|
13
36
|
ref: string;
|
|
37
|
+
/** Session id from `sessionStorage` — see {@link getSessionId}. Capped at 64 chars. */
|
|
14
38
|
sid: string;
|
|
39
|
+
/**
|
|
40
|
+
* `window.innerWidth`. Not merely diagnostic: the CMS derives the device
|
|
41
|
+
* breakdown from it (<768 mobile, <1024 tablet, else desktop).
|
|
42
|
+
*/
|
|
15
43
|
vw: number;
|
|
44
|
+
/** `window.innerHeight`. */
|
|
16
45
|
vh: number;
|
|
46
|
+
/** `Date.now()` on the client. The CMS stores its own `serverTs` alongside it. */
|
|
17
47
|
ts: number;
|
|
48
|
+
/**
|
|
49
|
+
* Campaign context. On pageviews: the `utm_*` params and click ids present
|
|
50
|
+
* in the URL, or — on later pages of the same visit — the ones the visit
|
|
51
|
+
* landed with (see {@link getSessionCampaign}). The CMS tells a landing from
|
|
52
|
+
* a later page by whether `url` itself carries them. On custom events:
|
|
53
|
+
* `utm_source`/`utm_medium`/`utm_campaign` from the 30-day ad-referral
|
|
54
|
+
* cookie, else the session's landing params.
|
|
55
|
+
*/
|
|
18
56
|
utm?: Record<string, string>;
|
|
57
|
+
/**
|
|
58
|
+
* Custom events only. Truncated to 128 chars.
|
|
59
|
+
*
|
|
60
|
+
* `"tracker_test"` is RESERVED — the CMS discards events with that name
|
|
61
|
+
* (and events with url `/__analytics_test__`) so the connection self-test
|
|
62
|
+
* never pollutes stats. They will never appear in the dashboard.
|
|
63
|
+
*/
|
|
19
64
|
name?: string;
|
|
65
|
+
/**
|
|
66
|
+
* Custom events only. Must be FLAT primitives: nested objects and arrays
|
|
67
|
+
* are dropped on ingest, so flatten before sending. At most 32 keys
|
|
68
|
+
* survive; keys are truncated to 64 chars, string values to 256, and HTML
|
|
69
|
+
* tags are stripped from both.
|
|
70
|
+
*/
|
|
20
71
|
props?: Record<string, string | number | boolean>;
|
|
21
72
|
}
|
|
22
73
|
/** Track a page view. */
|
|
23
74
|
declare function trackPageView(): void;
|
|
24
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* Track a custom event.
|
|
77
|
+
*
|
|
78
|
+
* `props` must be FLAT primitives — nested objects and arrays are dropped by
|
|
79
|
+
* the CMS on ingest, along with anything past the 32nd key. See
|
|
80
|
+
* {@link AnalyticsEvent.props} for the full set of caps.
|
|
81
|
+
*
|
|
82
|
+
* Avoid the reserved name `"tracker_test"`; the CMS discards it.
|
|
83
|
+
*
|
|
84
|
+
* No-ops during SSR and on dev hostnames, and never throws.
|
|
85
|
+
*/
|
|
25
86
|
declare function trackEvent(name: string, props?: Record<string, string | number | boolean>): void;
|
|
26
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* Track when a form comes into view.
|
|
89
|
+
*
|
|
90
|
+
* Usually you do NOT call this yourself: `@vulcantech/forms` finds it on
|
|
91
|
+
* `window.__vtTrack` and fires it automatically for every rendered form. Only
|
|
92
|
+
* call it manually for a form this package's global hook can't see.
|
|
93
|
+
*/
|
|
27
94
|
declare function trackFormView(formId: string, formName?: string): void;
|
|
28
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* Track when a form is submitted successfully.
|
|
97
|
+
*
|
|
98
|
+
* Like {@link trackFormView}, `@vulcantech/forms` fires this automatically via
|
|
99
|
+
* `window.__vtTrack`. `submissionType` defaults to `"contact"`.
|
|
100
|
+
*/
|
|
29
101
|
declare function trackFormSubmit(formId: string, formName?: string, submissionType?: string): void;
|
|
30
|
-
/**
|
|
102
|
+
/**
|
|
103
|
+
* Track when a phone number link is clicked.
|
|
104
|
+
*
|
|
105
|
+
* ⚠ Do NOT wire this into an `onClick` on an `<a href="tel:…">` — importing
|
|
106
|
+
* this module installs a delegated document listener that already fires it for
|
|
107
|
+
* every such anchor, so adding your own handler DOUBLE-COUNTS the conversion.
|
|
108
|
+
* Call it manually only for a non-anchor trigger (e.g. a button that dials via
|
|
109
|
+
* JS).
|
|
110
|
+
*/
|
|
31
111
|
declare function trackPhoneClick(phoneNumber?: string): void;
|
|
32
|
-
/**
|
|
112
|
+
/**
|
|
113
|
+
* Record that Google's forwarding-number swap showed this visitor a call
|
|
114
|
+
* tracking number — i.e. an ad visitor was eligible to place a tracked call.
|
|
115
|
+
*
|
|
116
|
+
* Fired automatically (see the registration block below) when `CMSTags`'
|
|
117
|
+
* swap callback dispatches `vt-phone`; once per session, since gtag runs the
|
|
118
|
+
* callback on every full page load. The calls themselves never reach us —
|
|
119
|
+
* they go Google → shop — so this is the first-party half of call tracking.
|
|
120
|
+
*
|
|
121
|
+
* @param formatted The forwarding number as Google formatted it.
|
|
122
|
+
*/
|
|
123
|
+
declare function trackPhoneSwap(formatted?: string): void;
|
|
124
|
+
/**
|
|
125
|
+
* Track when an email link is clicked.
|
|
126
|
+
*
|
|
127
|
+
* ⚠ Same double-count caveat as {@link trackPhoneClick}: `<a href="mailto:…">`
|
|
128
|
+
* clicks are already captured by the delegated listener installed on import.
|
|
129
|
+
*/
|
|
33
130
|
declare function trackEmailClick(email?: string): void;
|
|
34
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* Track a 404 page.
|
|
133
|
+
*
|
|
134
|
+
* `url` defaults to the current pathname. No-ops during SSR and on dev
|
|
135
|
+
* hostnames, and never throws — like every other tracker in this module.
|
|
136
|
+
*
|
|
137
|
+
* @param url Optional path to report instead of the current pathname.
|
|
138
|
+
*/
|
|
35
139
|
declare function track404(url?: string): void;
|
|
36
140
|
|
|
37
|
-
declare function AnalyticsTracker():
|
|
141
|
+
declare function AnalyticsTracker(): react.JSX.Element;
|
|
142
|
+
|
|
143
|
+
interface PhoneLinkProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "href" | "children" | "prefix"> {
|
|
144
|
+
/** The number as displayed, e.g. `"602.904.9109"`. */
|
|
145
|
+
display: string;
|
|
146
|
+
/** E.164 dial string without `tel:`, e.g. `"+16029049109"`. */
|
|
147
|
+
tel: string;
|
|
148
|
+
/** Rendered before the number, e.g. `"Or call · "` or an icon. */
|
|
149
|
+
prefix?: ReactNode;
|
|
150
|
+
/** Rendered after the number. */
|
|
151
|
+
suffix?: ReactNode;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* A `tel:` link that shows Google's forwarding number to visitors who arrived
|
|
155
|
+
* from an ad.
|
|
156
|
+
*
|
|
157
|
+
* `CMSTags` configures Google's number swap (when the project has a
|
|
158
|
+
* `callConversionSendTo` + `callConversionNumber`) with a callback that stores
|
|
159
|
+
* the forwarding number on `window.__vtPhone` and fires `vt-phone`; this
|
|
160
|
+
* component re-renders on that event. Doing it in React rather than letting
|
|
161
|
+
* gtag rewrite the DOM matters: React would put the original number back on
|
|
162
|
+
* the next render or client-side navigation.
|
|
163
|
+
*
|
|
164
|
+
* Everyone else — and the server render — sees `display` / `tel`. Taps are
|
|
165
|
+
* tracked by the delegated listener `AnalyticsTracker` installs; don't add an
|
|
166
|
+
* `onClick` tracker here or the tap double-counts.
|
|
167
|
+
*
|
|
168
|
+
* @param props.display Number as shown on the page (must match `callConversionNumber`).
|
|
169
|
+
* @param props.tel E.164 dial string, without the `tel:` scheme.
|
|
170
|
+
* @param props.prefix Optional content before the number.
|
|
171
|
+
* @param props.suffix Optional content after the number.
|
|
172
|
+
*/
|
|
173
|
+
declare function PhoneLink({ display, tel, prefix, suffix, ...rest }: PhoneLinkProps): react.JSX.Element;
|
|
38
174
|
|
|
39
|
-
export { type AnalyticsEvent, AnalyticsTracker, track404, trackEmailClick, trackEvent, trackFormSubmit, trackFormView, trackPageView, trackPhoneClick };
|
|
175
|
+
export { type AnalyticsEvent, AnalyticsTracker, PhoneLink, type PhoneLinkProps, track404, trackEmailClick, trackEvent, trackFormSubmit, trackFormView, trackPageView, trackPhoneClick, trackPhoneSwap };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as react from 'react';
|
|
2
|
+
import { AnchorHTMLAttributes, ReactNode } from 'react';
|
|
2
3
|
|
|
3
4
|
/**
|
|
4
5
|
* VulcanTech Analytics — lightweight first-party tracker.
|
|
@@ -7,33 +8,168 @@ import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
|
7
8
|
* Sends page views and custom events to /api/a on the same domain
|
|
8
9
|
* via navigator.sendBeacon (falls back to fetch).
|
|
9
10
|
*/
|
|
11
|
+
/**
|
|
12
|
+
* One analytics event as sent to `/api/a` and forwarded to the CMS.
|
|
13
|
+
*
|
|
14
|
+
* The limits noted per field are enforced SERVER-SIDE on ingest
|
|
15
|
+
* (`/api/analytics` → `lib/analytics/ingest` in vulcantech-cms). Over-length
|
|
16
|
+
* values are truncated and unsupported shapes are dropped — never rejected —
|
|
17
|
+
* so an oversized event is stored lossily rather than failing loudly.
|
|
18
|
+
*
|
|
19
|
+
* `type`, `url`, `sid` and `ts` are required; an event missing any of them is
|
|
20
|
+
* rejected at ingest with a 400.
|
|
21
|
+
*/
|
|
10
22
|
interface AnalyticsEvent {
|
|
23
|
+
/** The only two valid values. */
|
|
11
24
|
type: "pageview" | "event";
|
|
25
|
+
/**
|
|
26
|
+
* Pageviews send `pathname + search`; custom events send `pathname` only.
|
|
27
|
+
* Truncated to 2048 chars on ingest, with HTML tags stripped.
|
|
28
|
+
*/
|
|
12
29
|
url: string;
|
|
30
|
+
/**
|
|
31
|
+
* `document.referrer` on pageviews, `""` on custom events. Truncated to
|
|
32
|
+
* 2048 chars. Together with {@link AnalyticsEvent.utm} this drives the CMS
|
|
33
|
+
* traffic-source classification; custom events have no referrer, so their
|
|
34
|
+
* attribution rides entirely on {@link AnalyticsEvent.utm}.
|
|
35
|
+
*/
|
|
13
36
|
ref: string;
|
|
37
|
+
/** Session id from `sessionStorage` — see {@link getSessionId}. Capped at 64 chars. */
|
|
14
38
|
sid: string;
|
|
39
|
+
/**
|
|
40
|
+
* `window.innerWidth`. Not merely diagnostic: the CMS derives the device
|
|
41
|
+
* breakdown from it (<768 mobile, <1024 tablet, else desktop).
|
|
42
|
+
*/
|
|
15
43
|
vw: number;
|
|
44
|
+
/** `window.innerHeight`. */
|
|
16
45
|
vh: number;
|
|
46
|
+
/** `Date.now()` on the client. The CMS stores its own `serverTs` alongside it. */
|
|
17
47
|
ts: number;
|
|
48
|
+
/**
|
|
49
|
+
* Campaign context. On pageviews: the `utm_*` params and click ids present
|
|
50
|
+
* in the URL, or — on later pages of the same visit — the ones the visit
|
|
51
|
+
* landed with (see {@link getSessionCampaign}). The CMS tells a landing from
|
|
52
|
+
* a later page by whether `url` itself carries them. On custom events:
|
|
53
|
+
* `utm_source`/`utm_medium`/`utm_campaign` from the 30-day ad-referral
|
|
54
|
+
* cookie, else the session's landing params.
|
|
55
|
+
*/
|
|
18
56
|
utm?: Record<string, string>;
|
|
57
|
+
/**
|
|
58
|
+
* Custom events only. Truncated to 128 chars.
|
|
59
|
+
*
|
|
60
|
+
* `"tracker_test"` is RESERVED — the CMS discards events with that name
|
|
61
|
+
* (and events with url `/__analytics_test__`) so the connection self-test
|
|
62
|
+
* never pollutes stats. They will never appear in the dashboard.
|
|
63
|
+
*/
|
|
19
64
|
name?: string;
|
|
65
|
+
/**
|
|
66
|
+
* Custom events only. Must be FLAT primitives: nested objects and arrays
|
|
67
|
+
* are dropped on ingest, so flatten before sending. At most 32 keys
|
|
68
|
+
* survive; keys are truncated to 64 chars, string values to 256, and HTML
|
|
69
|
+
* tags are stripped from both.
|
|
70
|
+
*/
|
|
20
71
|
props?: Record<string, string | number | boolean>;
|
|
21
72
|
}
|
|
22
73
|
/** Track a page view. */
|
|
23
74
|
declare function trackPageView(): void;
|
|
24
|
-
/**
|
|
75
|
+
/**
|
|
76
|
+
* Track a custom event.
|
|
77
|
+
*
|
|
78
|
+
* `props` must be FLAT primitives — nested objects and arrays are dropped by
|
|
79
|
+
* the CMS on ingest, along with anything past the 32nd key. See
|
|
80
|
+
* {@link AnalyticsEvent.props} for the full set of caps.
|
|
81
|
+
*
|
|
82
|
+
* Avoid the reserved name `"tracker_test"`; the CMS discards it.
|
|
83
|
+
*
|
|
84
|
+
* No-ops during SSR and on dev hostnames, and never throws.
|
|
85
|
+
*/
|
|
25
86
|
declare function trackEvent(name: string, props?: Record<string, string | number | boolean>): void;
|
|
26
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* Track when a form comes into view.
|
|
89
|
+
*
|
|
90
|
+
* Usually you do NOT call this yourself: `@vulcantech/forms` finds it on
|
|
91
|
+
* `window.__vtTrack` and fires it automatically for every rendered form. Only
|
|
92
|
+
* call it manually for a form this package's global hook can't see.
|
|
93
|
+
*/
|
|
27
94
|
declare function trackFormView(formId: string, formName?: string): void;
|
|
28
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* Track when a form is submitted successfully.
|
|
97
|
+
*
|
|
98
|
+
* Like {@link trackFormView}, `@vulcantech/forms` fires this automatically via
|
|
99
|
+
* `window.__vtTrack`. `submissionType` defaults to `"contact"`.
|
|
100
|
+
*/
|
|
29
101
|
declare function trackFormSubmit(formId: string, formName?: string, submissionType?: string): void;
|
|
30
|
-
/**
|
|
102
|
+
/**
|
|
103
|
+
* Track when a phone number link is clicked.
|
|
104
|
+
*
|
|
105
|
+
* ⚠ Do NOT wire this into an `onClick` on an `<a href="tel:…">` — importing
|
|
106
|
+
* this module installs a delegated document listener that already fires it for
|
|
107
|
+
* every such anchor, so adding your own handler DOUBLE-COUNTS the conversion.
|
|
108
|
+
* Call it manually only for a non-anchor trigger (e.g. a button that dials via
|
|
109
|
+
* JS).
|
|
110
|
+
*/
|
|
31
111
|
declare function trackPhoneClick(phoneNumber?: string): void;
|
|
32
|
-
/**
|
|
112
|
+
/**
|
|
113
|
+
* Record that Google's forwarding-number swap showed this visitor a call
|
|
114
|
+
* tracking number — i.e. an ad visitor was eligible to place a tracked call.
|
|
115
|
+
*
|
|
116
|
+
* Fired automatically (see the registration block below) when `CMSTags`'
|
|
117
|
+
* swap callback dispatches `vt-phone`; once per session, since gtag runs the
|
|
118
|
+
* callback on every full page load. The calls themselves never reach us —
|
|
119
|
+
* they go Google → shop — so this is the first-party half of call tracking.
|
|
120
|
+
*
|
|
121
|
+
* @param formatted The forwarding number as Google formatted it.
|
|
122
|
+
*/
|
|
123
|
+
declare function trackPhoneSwap(formatted?: string): void;
|
|
124
|
+
/**
|
|
125
|
+
* Track when an email link is clicked.
|
|
126
|
+
*
|
|
127
|
+
* ⚠ Same double-count caveat as {@link trackPhoneClick}: `<a href="mailto:…">`
|
|
128
|
+
* clicks are already captured by the delegated listener installed on import.
|
|
129
|
+
*/
|
|
33
130
|
declare function trackEmailClick(email?: string): void;
|
|
34
|
-
/**
|
|
131
|
+
/**
|
|
132
|
+
* Track a 404 page.
|
|
133
|
+
*
|
|
134
|
+
* `url` defaults to the current pathname. No-ops during SSR and on dev
|
|
135
|
+
* hostnames, and never throws — like every other tracker in this module.
|
|
136
|
+
*
|
|
137
|
+
* @param url Optional path to report instead of the current pathname.
|
|
138
|
+
*/
|
|
35
139
|
declare function track404(url?: string): void;
|
|
36
140
|
|
|
37
|
-
declare function AnalyticsTracker():
|
|
141
|
+
declare function AnalyticsTracker(): react.JSX.Element;
|
|
142
|
+
|
|
143
|
+
interface PhoneLinkProps extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "href" | "children" | "prefix"> {
|
|
144
|
+
/** The number as displayed, e.g. `"602.904.9109"`. */
|
|
145
|
+
display: string;
|
|
146
|
+
/** E.164 dial string without `tel:`, e.g. `"+16029049109"`. */
|
|
147
|
+
tel: string;
|
|
148
|
+
/** Rendered before the number, e.g. `"Or call · "` or an icon. */
|
|
149
|
+
prefix?: ReactNode;
|
|
150
|
+
/** Rendered after the number. */
|
|
151
|
+
suffix?: ReactNode;
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* A `tel:` link that shows Google's forwarding number to visitors who arrived
|
|
155
|
+
* from an ad.
|
|
156
|
+
*
|
|
157
|
+
* `CMSTags` configures Google's number swap (when the project has a
|
|
158
|
+
* `callConversionSendTo` + `callConversionNumber`) with a callback that stores
|
|
159
|
+
* the forwarding number on `window.__vtPhone` and fires `vt-phone`; this
|
|
160
|
+
* component re-renders on that event. Doing it in React rather than letting
|
|
161
|
+
* gtag rewrite the DOM matters: React would put the original number back on
|
|
162
|
+
* the next render or client-side navigation.
|
|
163
|
+
*
|
|
164
|
+
* Everyone else — and the server render — sees `display` / `tel`. Taps are
|
|
165
|
+
* tracked by the delegated listener `AnalyticsTracker` installs; don't add an
|
|
166
|
+
* `onClick` tracker here or the tap double-counts.
|
|
167
|
+
*
|
|
168
|
+
* @param props.display Number as shown on the page (must match `callConversionNumber`).
|
|
169
|
+
* @param props.tel E.164 dial string, without the `tel:` scheme.
|
|
170
|
+
* @param props.prefix Optional content before the number.
|
|
171
|
+
* @param props.suffix Optional content after the number.
|
|
172
|
+
*/
|
|
173
|
+
declare function PhoneLink({ display, tel, prefix, suffix, ...rest }: PhoneLinkProps): react.JSX.Element;
|
|
38
174
|
|
|
39
|
-
export { type AnalyticsEvent, AnalyticsTracker, track404, trackEmailClick, trackEvent, trackFormSubmit, trackFormView, trackPageView, trackPhoneClick };
|
|
175
|
+
export { type AnalyticsEvent, AnalyticsTracker, PhoneLink, type PhoneLinkProps, track404, trackEmailClick, trackEvent, trackFormSubmit, trackFormView, trackPageView, trackPhoneClick, trackPhoneSwap };
|