@bmnova/web-analytics 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +153 -2
- package/bin/setup.mjs +173 -0
- package/dist/bots-BLlaamUX.d.ts +11 -0
- package/dist/chunk-4B5GWKPD.js +308 -0
- package/dist/chunk-4B5GWKPD.js.map +1 -0
- package/dist/chunk-PD4SX2FD.js +19 -0
- package/dist/chunk-PD4SX2FD.js.map +1 -0
- package/dist/client/index.d.ts +39 -0
- package/dist/client/index.js +318 -0
- package/dist/client/index.js.map +1 -0
- package/dist/react/index.d.ts +30 -0
- package/dist/react/index.js +106 -0
- package/dist/react/index.js.map +1 -0
- package/dist/schema-1Re1Bw-P.d.ts +80 -0
- package/dist/server/index.d.ts +123 -0
- package/dist/server/index.js +484 -0
- package/dist/server/index.js.map +1 -0
- package/dist/shared/index.d.ts +42 -0
- package/dist/shared/index.js +25 -0
- package/dist/shared/index.js.map +1 -0
- package/package.json +47 -4
- package/sql/01_events.sql +40 -0
- package/sql/02_functions.sql +112 -0
- package/sql/03_daily_aggregates.sql +108 -0
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/** Consent level of the visitor when the event was recorded. */
|
|
2
|
+
type Consent = "basic" | "full";
|
|
3
|
+
/** Where a row was produced. */
|
|
4
|
+
type Via = "client" | "server" | "proxy";
|
|
5
|
+
interface Utm {
|
|
6
|
+
source?: string;
|
|
7
|
+
medium?: string;
|
|
8
|
+
campaign?: string;
|
|
9
|
+
term?: string;
|
|
10
|
+
content?: string;
|
|
11
|
+
}
|
|
12
|
+
type PropValue = string | number | boolean | null;
|
|
13
|
+
type Props = Record<string, PropValue>;
|
|
14
|
+
/** One event as the browser tracker sends it to the collector. */
|
|
15
|
+
interface ClientEvent {
|
|
16
|
+
event: string;
|
|
17
|
+
ts?: number;
|
|
18
|
+
path: string;
|
|
19
|
+
title?: string;
|
|
20
|
+
referrer?: string;
|
|
21
|
+
utm?: Utm;
|
|
22
|
+
props?: Props;
|
|
23
|
+
consent: Consent;
|
|
24
|
+
visitor_id?: string;
|
|
25
|
+
session_id?: string;
|
|
26
|
+
user_id?: string;
|
|
27
|
+
}
|
|
28
|
+
interface ClientBatch {
|
|
29
|
+
site: string;
|
|
30
|
+
events: ClientEvent[];
|
|
31
|
+
}
|
|
32
|
+
/** One row of the BigQuery `events` table. Keep in sync with sql/01_events.sql. */
|
|
33
|
+
interface EventRow {
|
|
34
|
+
ts: string;
|
|
35
|
+
site: string;
|
|
36
|
+
event: string;
|
|
37
|
+
via: Via;
|
|
38
|
+
visitor_hash: string | null;
|
|
39
|
+
visitor_id: string | null;
|
|
40
|
+
session_id: string | null;
|
|
41
|
+
user_id: string | null;
|
|
42
|
+
consent: Consent | null;
|
|
43
|
+
host: string | null;
|
|
44
|
+
path: string | null;
|
|
45
|
+
title: string | null;
|
|
46
|
+
referrer_host: string | null;
|
|
47
|
+
channel: string | null;
|
|
48
|
+
source: string | null;
|
|
49
|
+
utm_source: string | null;
|
|
50
|
+
utm_medium: string | null;
|
|
51
|
+
utm_campaign: string | null;
|
|
52
|
+
utm_term: string | null;
|
|
53
|
+
utm_content: string | null;
|
|
54
|
+
country: string | null;
|
|
55
|
+
region: string | null;
|
|
56
|
+
device: string | null;
|
|
57
|
+
browser: string | null;
|
|
58
|
+
os: string | null;
|
|
59
|
+
is_bot: boolean;
|
|
60
|
+
bot_name: string | null;
|
|
61
|
+
bot_kind: string | null;
|
|
62
|
+
user_agent: string | null;
|
|
63
|
+
props: string | null;
|
|
64
|
+
}
|
|
65
|
+
declare const LIMITS: {
|
|
66
|
+
readonly bodyBytes: 8192;
|
|
67
|
+
readonly eventsPerBatch: 25;
|
|
68
|
+
readonly eventName: RegExp;
|
|
69
|
+
readonly site: RegExp;
|
|
70
|
+
readonly id: RegExp;
|
|
71
|
+
readonly pathLength: 512;
|
|
72
|
+
readonly titleLength: 200;
|
|
73
|
+
readonly utmLength: 150;
|
|
74
|
+
readonly propKeys: 20;
|
|
75
|
+
readonly propKey: RegExp;
|
|
76
|
+
readonly propStringLength: 200;
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
export { LIMITS as L };
|
|
80
|
+
export type { Consent as C, EventRow as E, Props as P, Utm as U, Via as V, ClientBatch as a, ClientEvent as b, PropValue as c };
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { Props, Consent } from '../client/index.js';
|
|
2
|
+
import { EventRow, Via, Utm } from '../shared/index.js';
|
|
3
|
+
import { Bot } from '../shared/index.js';
|
|
4
|
+
|
|
5
|
+
interface ServiceAccount {
|
|
6
|
+
client_email: string;
|
|
7
|
+
private_key: string;
|
|
8
|
+
}
|
|
9
|
+
interface WaConfig {
|
|
10
|
+
site: string | null;
|
|
11
|
+
project: string | null;
|
|
12
|
+
dataset: string;
|
|
13
|
+
table: string;
|
|
14
|
+
serviceAccount: ServiceAccount | null;
|
|
15
|
+
saltSecret: string | null;
|
|
16
|
+
allowedOrigins: string[];
|
|
17
|
+
/** True when credentials are missing: rows are logged instead of written. */
|
|
18
|
+
dryRun: boolean;
|
|
19
|
+
}
|
|
20
|
+
type WaEnv = Partial<Record<"WA_SITE" | "WA_BQ_PROJECT" | "WA_BQ_DATASET" | "WA_BQ_TABLE" | "WA_GCP_SA_KEY" | "WA_SALT_SECRET" | "WA_ALLOWED_ORIGINS" | "WA_DRY_RUN", string | undefined>>;
|
|
21
|
+
/** Read configuration from env (overrides win). Never logs secret values. */
|
|
22
|
+
declare function getConfig(overrides?: WaEnv): WaConfig;
|
|
23
|
+
|
|
24
|
+
interface CollectOptions {
|
|
25
|
+
/** Site label; defaults to WA_SITE, then the request host. */
|
|
26
|
+
site?: string;
|
|
27
|
+
/** Env overrides (e.g. Cloudflare bindings). Defaults to process.env. */
|
|
28
|
+
env?: WaEnv;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Route handler for the browser tracker. Usage in app/api/collect/route.ts:
|
|
32
|
+
* export const { POST } = createCollectHandler();
|
|
33
|
+
* Do not set `runtime = "edge"`; it works on the default runtime everywhere.
|
|
34
|
+
*/
|
|
35
|
+
declare function createCollectHandler(options?: CollectOptions): {
|
|
36
|
+
POST: (req: Request) => Promise<Response>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
40
|
+
type RequestLike = Request & {
|
|
41
|
+
nextUrl?: URL;
|
|
42
|
+
};
|
|
43
|
+
type InnerHandler<Req extends RequestLike = RequestLike, Ev = unknown> = (req: Req, event: Ev) => MaybePromise<Response | null | undefined | void>;
|
|
44
|
+
interface AnalyticsProxyOptions {
|
|
45
|
+
site?: string;
|
|
46
|
+
env?: WaEnv;
|
|
47
|
+
/** Log bot and AI crawler requests (default true). */
|
|
48
|
+
logBots?: boolean;
|
|
49
|
+
/** Skip paths, e.g. health checks. The collector endpoint is always skipped. */
|
|
50
|
+
ignore?: (pathname: string) => boolean;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Wrap a Next middleware (Next 14/15) or proxy (Next 16) to log bot and AI crawler
|
|
54
|
+
* requests. Human page views come from the browser tracker instead.
|
|
55
|
+
*
|
|
56
|
+
* // middleware.ts (Next 14/15) // proxy.ts (Next 16)
|
|
57
|
+
* export default withAnalytics(mine); export const proxy = withAnalytics(mine);
|
|
58
|
+
*/
|
|
59
|
+
declare function withAnalytics<Req extends RequestLike, Ev>(inner?: InnerHandler<Req, Ev>, options?: AnalyticsProxyOptions): (req: Req, event: Ev) => Promise<void | Response | null | undefined>;
|
|
60
|
+
|
|
61
|
+
interface TrackServerOptions {
|
|
62
|
+
userId?: string | null;
|
|
63
|
+
site?: string;
|
|
64
|
+
/** Pass the incoming request to attach country/device; omit for webhooks. */
|
|
65
|
+
request?: Request | null;
|
|
66
|
+
path?: string;
|
|
67
|
+
env?: WaEnv;
|
|
68
|
+
/** Wait for the write to finish instead of deferring it (e.g. in scripts). */
|
|
69
|
+
await?: boolean;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Record a server-side event (sign-up, payment webhook, import...). Ad blockers
|
|
73
|
+
* cannot drop these. Props pass the same deny-list as browser events.
|
|
74
|
+
*/
|
|
75
|
+
declare function trackServer(event: string, props?: Props, options?: TrackServerOptions): Promise<void>;
|
|
76
|
+
|
|
77
|
+
interface RequestInfo {
|
|
78
|
+
path: string;
|
|
79
|
+
method: string;
|
|
80
|
+
headers: Record<string, string | string[] | undefined> | Headers;
|
|
81
|
+
}
|
|
82
|
+
interface ErrorContext {
|
|
83
|
+
routerKind?: string;
|
|
84
|
+
routePath?: string;
|
|
85
|
+
routeType?: string;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Next.js instrumentation hook. In instrumentation.ts:
|
|
89
|
+
* export const onRequestError = createOnRequestError();
|
|
90
|
+
* Stores only the message (truncated), digest and route, never stack traces or request bodies.
|
|
91
|
+
*/
|
|
92
|
+
declare function createOnRequestError(options?: {
|
|
93
|
+
site?: string;
|
|
94
|
+
env?: WaEnv;
|
|
95
|
+
}): (error: unknown, request: RequestInfo, context?: ErrorContext) => Promise<void>;
|
|
96
|
+
|
|
97
|
+
/** Stream rows into BigQuery. Never throws: analytics must not break the site. */
|
|
98
|
+
declare function writeRows(config: WaConfig, rows: EventRow[]): Promise<void>;
|
|
99
|
+
|
|
100
|
+
interface RowInput {
|
|
101
|
+
site: string;
|
|
102
|
+
event: string;
|
|
103
|
+
via: Via;
|
|
104
|
+
ts?: number;
|
|
105
|
+
path?: string | null;
|
|
106
|
+
host?: string | null;
|
|
107
|
+
title?: string | null;
|
|
108
|
+
referrer?: string | null;
|
|
109
|
+
utm?: Utm | null;
|
|
110
|
+
props?: Props | Record<string, unknown> | null;
|
|
111
|
+
consent?: Consent | null;
|
|
112
|
+
visitorId?: string | null;
|
|
113
|
+
sessionId?: string | null;
|
|
114
|
+
userId?: string | null;
|
|
115
|
+
/** Request headers, used for geo, UA and the cookieless hash. */
|
|
116
|
+
headers?: Headers | null;
|
|
117
|
+
/** Pre-detected bot, to avoid re-parsing. */
|
|
118
|
+
bot?: Bot | null;
|
|
119
|
+
}
|
|
120
|
+
declare function buildRow(config: WaConfig, input: RowInput): Promise<EventRow>;
|
|
121
|
+
|
|
122
|
+
export { buildRow, createCollectHandler, createOnRequestError, getConfig, trackServer, withAnalytics, writeRows };
|
|
123
|
+
export type { AnalyticsProxyOptions, CollectOptions, InnerHandler, TrackServerOptions, WaConfig, WaEnv };
|
|
@@ -0,0 +1,484 @@
|
|
|
1
|
+
import {
|
|
2
|
+
classifyChannel,
|
|
3
|
+
cleanPath,
|
|
4
|
+
detectBot,
|
|
5
|
+
hostOf,
|
|
6
|
+
parseUa,
|
|
7
|
+
sanitizeProps
|
|
8
|
+
} from "../chunk-4B5GWKPD.js";
|
|
9
|
+
import {
|
|
10
|
+
LIMITS
|
|
11
|
+
} from "../chunk-PD4SX2FD.js";
|
|
12
|
+
|
|
13
|
+
// src/server/env.ts
|
|
14
|
+
var g = globalThis;
|
|
15
|
+
var WARNED = /* @__PURE__ */ Symbol.for("@bmnova/web-analytics/warned");
|
|
16
|
+
var warned = g[WARNED] ??= /* @__PURE__ */ new Set();
|
|
17
|
+
function warnOnce(key, message) {
|
|
18
|
+
if (warned.has(key)) return;
|
|
19
|
+
warned.add(key);
|
|
20
|
+
console.warn(`[web-analytics] ${message}`);
|
|
21
|
+
}
|
|
22
|
+
function readProcessEnv() {
|
|
23
|
+
const p = globalThis.process;
|
|
24
|
+
return p?.env ?? {};
|
|
25
|
+
}
|
|
26
|
+
function parseServiceAccount(raw) {
|
|
27
|
+
if (!raw) return null;
|
|
28
|
+
try {
|
|
29
|
+
const json = JSON.parse(raw.trim().startsWith("{") ? raw : atob(raw));
|
|
30
|
+
if (typeof json.client_email === "string" && typeof json.private_key === "string") {
|
|
31
|
+
return { client_email: json.client_email, private_key: json.private_key };
|
|
32
|
+
}
|
|
33
|
+
} catch {
|
|
34
|
+
}
|
|
35
|
+
warnOnce("sa", "WA_GCP_SA_KEY is set but is not a valid service account JSON (raw or base64). Running in dry-run mode.");
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
var cached = null;
|
|
39
|
+
function getConfig(overrides = {}) {
|
|
40
|
+
const env = { ...readProcessEnv(), ...overrides };
|
|
41
|
+
const key = JSON.stringify([env.WA_SITE, env.WA_BQ_PROJECT, env.WA_BQ_DATASET, env.WA_BQ_TABLE, env.WA_GCP_SA_KEY?.length, env.WA_SALT_SECRET?.length, env.WA_ALLOWED_ORIGINS, env.WA_DRY_RUN]);
|
|
42
|
+
if (cached && cached.key === key) return cached.config;
|
|
43
|
+
const serviceAccount = parseServiceAccount(env.WA_GCP_SA_KEY);
|
|
44
|
+
const project = env.WA_BQ_PROJECT?.trim() || null;
|
|
45
|
+
const config = {
|
|
46
|
+
site: env.WA_SITE?.trim() || null,
|
|
47
|
+
project,
|
|
48
|
+
dataset: env.WA_BQ_DATASET?.trim() || "web_analytics",
|
|
49
|
+
table: env.WA_BQ_TABLE?.trim() || "events",
|
|
50
|
+
serviceAccount,
|
|
51
|
+
saltSecret: env.WA_SALT_SECRET || null,
|
|
52
|
+
allowedOrigins: (env.WA_ALLOWED_ORIGINS ?? "").split(",").map((s) => s.trim().replace(/\/$/, "").toLowerCase()).filter(Boolean),
|
|
53
|
+
dryRun: env.WA_DRY_RUN === "1" || env.WA_DRY_RUN === "true" || !serviceAccount || !project
|
|
54
|
+
};
|
|
55
|
+
if (config.dryRun) warnOnce("dry", "dry-run mode: rows are printed to the log, not written to BigQuery (set WA_GCP_SA_KEY and WA_BQ_PROJECT).");
|
|
56
|
+
if (!config.saltSecret) warnOnce("salt", "WA_SALT_SECRET is not set: visitor_hash will be empty, so visitors without consent cannot be counted.");
|
|
57
|
+
cached = { key, config };
|
|
58
|
+
return config;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// src/server/bigquery.ts
|
|
62
|
+
var TOKEN_URL = "https://oauth2.googleapis.com/token";
|
|
63
|
+
var SCOPE = "https://www.googleapis.com/auth/bigquery.insertdata";
|
|
64
|
+
var keyCache = null;
|
|
65
|
+
var tokenCache = null;
|
|
66
|
+
function b64url(data) {
|
|
67
|
+
const bytes = typeof data === "string" ? new TextEncoder().encode(data) : new Uint8Array(data);
|
|
68
|
+
let bin = "";
|
|
69
|
+
for (let i = 0; i < bytes.length; i++) bin += String.fromCharCode(bytes[i]);
|
|
70
|
+
return btoa(bin).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
71
|
+
}
|
|
72
|
+
function pemToDer(pem) {
|
|
73
|
+
const body = pem.replace(/\\n/g, "\n").replace(/-----[^-]+-----/g, "").replace(/\s+/g, "");
|
|
74
|
+
const bin = atob(body);
|
|
75
|
+
const out = new Uint8Array(bin.length);
|
|
76
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
77
|
+
return out.buffer;
|
|
78
|
+
}
|
|
79
|
+
async function signingKey(sa) {
|
|
80
|
+
if (keyCache?.email === sa.client_email) return keyCache.key;
|
|
81
|
+
const key = await crypto.subtle.importKey(
|
|
82
|
+
"pkcs8",
|
|
83
|
+
pemToDer(sa.private_key),
|
|
84
|
+
{ name: "RSASSA-PKCS1-v1_5", hash: "SHA-256" },
|
|
85
|
+
false,
|
|
86
|
+
["sign"]
|
|
87
|
+
);
|
|
88
|
+
keyCache = { email: sa.client_email, key };
|
|
89
|
+
return key;
|
|
90
|
+
}
|
|
91
|
+
async function buildAssertion(sa, nowSec = Math.floor(Date.now() / 1e3)) {
|
|
92
|
+
const header = b64url(JSON.stringify({ alg: "RS256", typ: "JWT" }));
|
|
93
|
+
const claims = b64url(JSON.stringify({ iss: sa.client_email, scope: SCOPE, aud: TOKEN_URL, iat: nowSec, exp: nowSec + 3600 }));
|
|
94
|
+
const input = `${header}.${claims}`;
|
|
95
|
+
const sig = await crypto.subtle.sign("RSASSA-PKCS1-v1_5", await signingKey(sa), new TextEncoder().encode(input));
|
|
96
|
+
return `${input}.${b64url(sig)}`;
|
|
97
|
+
}
|
|
98
|
+
async function accessToken(sa) {
|
|
99
|
+
const now = Date.now();
|
|
100
|
+
if (tokenCache && tokenCache.email === sa.client_email && tokenCache.exp - 6e4 > now) return tokenCache.token;
|
|
101
|
+
const res = await fetch(TOKEN_URL, {
|
|
102
|
+
method: "POST",
|
|
103
|
+
headers: { "content-type": "application/x-www-form-urlencoded" },
|
|
104
|
+
body: new URLSearchParams({
|
|
105
|
+
grant_type: "urn:ietf:params:oauth:grant-type:jwt-bearer",
|
|
106
|
+
assertion: await buildAssertion(sa)
|
|
107
|
+
})
|
|
108
|
+
});
|
|
109
|
+
if (!res.ok) throw new Error(`token exchange failed: ${res.status} ${await res.text().catch(() => "")}`.slice(0, 300));
|
|
110
|
+
const json = await res.json();
|
|
111
|
+
tokenCache = { email: sa.client_email, token: json.access_token, exp: now + json.expires_in * 1e3 };
|
|
112
|
+
return json.access_token;
|
|
113
|
+
}
|
|
114
|
+
function insertId(row) {
|
|
115
|
+
return crypto.randomUUID ? crypto.randomUUID() : `${row.ts}-${Math.random().toString(36).slice(2)}`;
|
|
116
|
+
}
|
|
117
|
+
async function writeRows(config, rows) {
|
|
118
|
+
if (!rows.length) return;
|
|
119
|
+
if (config.dryRun || !config.serviceAccount || !config.project) {
|
|
120
|
+
for (const row of rows) console.log("[web-analytics:dry-run]", JSON.stringify(row));
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
try {
|
|
124
|
+
const token = await accessToken(config.serviceAccount);
|
|
125
|
+
const url = `https://bigquery.googleapis.com/bigquery/v2/projects/${encodeURIComponent(config.project)}/datasets/${encodeURIComponent(config.dataset)}/tables/${encodeURIComponent(config.table)}/insertAll`;
|
|
126
|
+
const res = await fetch(url, {
|
|
127
|
+
method: "POST",
|
|
128
|
+
headers: { authorization: `Bearer ${token}`, "content-type": "application/json" },
|
|
129
|
+
body: JSON.stringify({
|
|
130
|
+
skipInvalidRows: true,
|
|
131
|
+
ignoreUnknownValues: true,
|
|
132
|
+
rows: rows.map((json) => ({ insertId: insertId(json), json }))
|
|
133
|
+
})
|
|
134
|
+
});
|
|
135
|
+
if (!res.ok) {
|
|
136
|
+
warnOnce(`bq-${res.status}`, `BigQuery insertAll failed with ${res.status}: ${(await res.text().catch(() => "")).slice(0, 300)}`);
|
|
137
|
+
if (res.status === 401) tokenCache = null;
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
const body = await res.json();
|
|
141
|
+
if (body.insertErrors?.length) {
|
|
142
|
+
warnOnce("bq-row", `BigQuery rejected ${body.insertErrors.length} row(s): ${body.insertErrors[0]?.errors[0]?.message ?? "unknown"}`);
|
|
143
|
+
}
|
|
144
|
+
} catch (err) {
|
|
145
|
+
warnOnce("bq-ex", `BigQuery write error: ${err.message}`);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/server/defer.ts
|
|
150
|
+
var afterFn;
|
|
151
|
+
async function loadAfter() {
|
|
152
|
+
if (afterFn !== void 0) return afterFn;
|
|
153
|
+
try {
|
|
154
|
+
const mod = await import("next/server");
|
|
155
|
+
const fn = mod["after"];
|
|
156
|
+
afterFn = typeof fn === "function" ? fn : null;
|
|
157
|
+
} catch {
|
|
158
|
+
afterFn = null;
|
|
159
|
+
}
|
|
160
|
+
return afterFn;
|
|
161
|
+
}
|
|
162
|
+
function platformWaitUntil() {
|
|
163
|
+
const g2 = globalThis;
|
|
164
|
+
const vercel = g2[/* @__PURE__ */ Symbol.for("@vercel/request-context")];
|
|
165
|
+
const v = vercel?.get?.();
|
|
166
|
+
if (v?.waitUntil) return { waitUntil: v.waitUntil.bind(v) };
|
|
167
|
+
const cf = g2[/* @__PURE__ */ Symbol.for("__cloudflare-context__")];
|
|
168
|
+
if (cf?.ctx?.waitUntil) return { waitUntil: cf.ctx.waitUntil.bind(cf.ctx) };
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
async function defer(task, event) {
|
|
172
|
+
if (event?.waitUntil) {
|
|
173
|
+
event.waitUntil(task());
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
const after = await loadAfter();
|
|
177
|
+
if (after) {
|
|
178
|
+
try {
|
|
179
|
+
after(task);
|
|
180
|
+
return;
|
|
181
|
+
} catch {
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const platform = platformWaitUntil();
|
|
185
|
+
if (platform) {
|
|
186
|
+
platform.waitUntil(task());
|
|
187
|
+
return;
|
|
188
|
+
}
|
|
189
|
+
await task();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/server/geo.ts
|
|
193
|
+
var UNKNOWN = /* @__PURE__ */ new Set(["XX", "T1", ""]);
|
|
194
|
+
function readGeo(headers) {
|
|
195
|
+
const ip = headers.get("cf-connecting-ip") || headers.get("x-real-ip") || headers.get("x-forwarded-for")?.split(",")[0]?.trim() || null;
|
|
196
|
+
const country = (headers.get("x-vercel-ip-country") || headers.get("cf-ipcountry") || "").toUpperCase();
|
|
197
|
+
const region = headers.get("x-vercel-ip-country-region") || headers.get("cf-region-code") || null;
|
|
198
|
+
return {
|
|
199
|
+
ip,
|
|
200
|
+
country: UNKNOWN.has(country) ? null : country,
|
|
201
|
+
region: region ? decodeURIComponent(region).slice(0, 64) : null
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// src/server/hash.ts
|
|
206
|
+
var enc = new TextEncoder();
|
|
207
|
+
function hex(buf) {
|
|
208
|
+
return Array.from(new Uint8Array(buf), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
209
|
+
}
|
|
210
|
+
var saltCache = /* @__PURE__ */ new Map();
|
|
211
|
+
async function dailyKey(secret, day) {
|
|
212
|
+
const id2 = `${secret.length}:${day}`;
|
|
213
|
+
const hit = saltCache.get(id2);
|
|
214
|
+
if (hit) return hit;
|
|
215
|
+
const master = await crypto.subtle.importKey("raw", enc.encode(secret), { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
216
|
+
const salt = await crypto.subtle.sign("HMAC", master, enc.encode(`web-analytics:${day}`));
|
|
217
|
+
const key = await crypto.subtle.importKey("raw", salt, { name: "HMAC", hash: "SHA-256" }, false, ["sign"]);
|
|
218
|
+
saltCache.clear();
|
|
219
|
+
saltCache.set(id2, key);
|
|
220
|
+
return key;
|
|
221
|
+
}
|
|
222
|
+
async function visitorHash(opts) {
|
|
223
|
+
if (!opts.ip && !opts.ua) return null;
|
|
224
|
+
const day = (opts.date ?? /* @__PURE__ */ new Date()).toISOString().slice(0, 10);
|
|
225
|
+
const key = await dailyKey(opts.secret, day);
|
|
226
|
+
const sig = await crypto.subtle.sign("HMAC", key, enc.encode(`${opts.site}|${opts.ip ?? ""}|${opts.ua ?? ""}`));
|
|
227
|
+
return hex(sig).slice(0, 32);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// src/server/rows.ts
|
|
231
|
+
var str = (v, max) => v ? v.slice(0, max) : null;
|
|
232
|
+
var id = (v) => v && LIMITS.id.test(v) ? v : null;
|
|
233
|
+
async function buildRow(config, input) {
|
|
234
|
+
const headers = input.headers ?? null;
|
|
235
|
+
const ua = headers?.get("user-agent") ?? null;
|
|
236
|
+
const bot = input.bot !== void 0 ? input.bot : headers ? detectBot(ua) : null;
|
|
237
|
+
const geo = headers ? readGeo(headers) : { ip: null, country: null, region: null };
|
|
238
|
+
const info = parseUa(ua);
|
|
239
|
+
const host = hostOf(input.host ?? headers?.get("host") ?? null);
|
|
240
|
+
const { channel, source, referrerHost } = classifyChannel({
|
|
241
|
+
utmSource: input.utm?.source,
|
|
242
|
+
utmMedium: input.utm?.medium,
|
|
243
|
+
referrer: input.referrer,
|
|
244
|
+
selfHost: host
|
|
245
|
+
});
|
|
246
|
+
const hash = !bot && config.saltSecret && headers && (geo.ip || ua) ? await visitorHash({ secret: config.saltSecret, site: input.site, ip: geo.ip, ua }) : null;
|
|
247
|
+
const props = sanitizeProps(input.props);
|
|
248
|
+
const ts = input.ts && Math.abs(Date.now() - input.ts) < 10 * 6e4 ? input.ts : Date.now();
|
|
249
|
+
return {
|
|
250
|
+
ts: new Date(ts).toISOString(),
|
|
251
|
+
site: input.site,
|
|
252
|
+
event: input.event,
|
|
253
|
+
via: input.via,
|
|
254
|
+
visitor_hash: hash,
|
|
255
|
+
visitor_id: input.consent === "full" ? id(input.visitorId) : null,
|
|
256
|
+
session_id: input.consent === "full" ? id(input.sessionId) : null,
|
|
257
|
+
user_id: id(input.userId),
|
|
258
|
+
consent: input.consent ?? null,
|
|
259
|
+
host,
|
|
260
|
+
path: cleanPath(input.path),
|
|
261
|
+
title: str(input.title, LIMITS.titleLength),
|
|
262
|
+
referrer_host: referrerHost,
|
|
263
|
+
channel: input.via === "server" && !input.referrer && !input.utm ? null : channel,
|
|
264
|
+
source,
|
|
265
|
+
utm_source: str(input.utm?.source, LIMITS.utmLength),
|
|
266
|
+
utm_medium: str(input.utm?.medium, LIMITS.utmLength),
|
|
267
|
+
utm_campaign: str(input.utm?.campaign, LIMITS.utmLength),
|
|
268
|
+
utm_term: str(input.utm?.term, LIMITS.utmLength),
|
|
269
|
+
utm_content: str(input.utm?.content, LIMITS.utmLength),
|
|
270
|
+
country: geo.country,
|
|
271
|
+
region: geo.region,
|
|
272
|
+
device: bot ? null : info.device,
|
|
273
|
+
browser: bot ? null : info.browser,
|
|
274
|
+
os: bot ? null : info.os,
|
|
275
|
+
is_bot: !!bot,
|
|
276
|
+
bot_name: bot?.name ?? null,
|
|
277
|
+
bot_kind: bot?.kind ?? null,
|
|
278
|
+
// Raw user agents are personal data for humans; keep them only for bots.
|
|
279
|
+
user_agent: bot ? str(ua, 400) : null,
|
|
280
|
+
props: props ? JSON.stringify(props) : null
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
function resolveSite(config, explicit, host) {
|
|
284
|
+
const site = explicit || config.site || hostOf(host) || "unknown";
|
|
285
|
+
return LIMITS.site.test(site) ? site : "unknown";
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// src/server/collect.ts
|
|
289
|
+
var noContent = () => new Response(null, { status: 204 });
|
|
290
|
+
var reject = (status) => new Response(null, { status });
|
|
291
|
+
function originAllowed(req, allowed) {
|
|
292
|
+
const url = new URL(req.url);
|
|
293
|
+
const self = `${url.protocol}//${url.host}`.toLowerCase();
|
|
294
|
+
const forwardedHost = req.headers.get("x-forwarded-host");
|
|
295
|
+
const candidates = /* @__PURE__ */ new Set([self, ...allowed]);
|
|
296
|
+
if (forwardedHost) candidates.add(`https://${forwardedHost.toLowerCase()}`);
|
|
297
|
+
const origin = req.headers.get("origin") ?? (() => {
|
|
298
|
+
const ref = req.headers.get("referer");
|
|
299
|
+
try {
|
|
300
|
+
return ref ? new URL(ref).origin : null;
|
|
301
|
+
} catch {
|
|
302
|
+
return null;
|
|
303
|
+
}
|
|
304
|
+
})();
|
|
305
|
+
if (!origin) return false;
|
|
306
|
+
const o = origin.toLowerCase();
|
|
307
|
+
if (candidates.has(o)) return true;
|
|
308
|
+
const oh = (() => {
|
|
309
|
+
try {
|
|
310
|
+
return new URL(o).host;
|
|
311
|
+
} catch {
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
})();
|
|
315
|
+
return !!oh && (oh === url.host || oh === forwardedHost) && /^(localhost|127\.0\.0\.1)(:\d+)?$/.test(oh);
|
|
316
|
+
}
|
|
317
|
+
function validEvent(e) {
|
|
318
|
+
if (!e || typeof e !== "object") return false;
|
|
319
|
+
const ev = e;
|
|
320
|
+
return typeof ev.event === "string" && LIMITS.eventName.test(ev.event) && typeof ev.path === "string" && ev.path.length <= LIMITS.pathLength * 2 && (ev.consent === "basic" || ev.consent === "full") && (ev.ts === void 0 || typeof ev.ts === "number") && (ev.title === void 0 || typeof ev.title === "string") && (ev.referrer === void 0 || typeof ev.referrer === "string") && (ev.utm === void 0 || typeof ev.utm === "object" && ev.utm !== null) && (ev.props === void 0 || typeof ev.props === "object" && ev.props !== null);
|
|
321
|
+
}
|
|
322
|
+
function createCollectHandler(options = {}) {
|
|
323
|
+
async function POST(req) {
|
|
324
|
+
const config = getConfig(options.env);
|
|
325
|
+
if (!originAllowed(req, config.allowedOrigins)) return reject(403);
|
|
326
|
+
const declared = Number(req.headers.get("content-length") ?? "0");
|
|
327
|
+
if (declared > LIMITS.bodyBytes) return reject(413);
|
|
328
|
+
const text = await req.text();
|
|
329
|
+
if (text.length > LIMITS.bodyBytes) return reject(413);
|
|
330
|
+
let body;
|
|
331
|
+
try {
|
|
332
|
+
body = JSON.parse(text);
|
|
333
|
+
} catch {
|
|
334
|
+
return reject(400);
|
|
335
|
+
}
|
|
336
|
+
const batch = body;
|
|
337
|
+
if (!Array.isArray(batch.events) || batch.events.length === 0) return reject(400);
|
|
338
|
+
const host = req.headers.get("x-forwarded-host") ?? new URL(req.url).host;
|
|
339
|
+
const site = resolveSite(config, options.site ?? (typeof batch.site === "string" ? batch.site : null), host);
|
|
340
|
+
const bot = detectBot(req.headers.get("user-agent"));
|
|
341
|
+
const events = batch.events.slice(0, LIMITS.eventsPerBatch).filter(validEvent);
|
|
342
|
+
if (!events.length) return reject(400);
|
|
343
|
+
await defer(async () => {
|
|
344
|
+
const rows = await Promise.all(
|
|
345
|
+
events.map(
|
|
346
|
+
(e) => buildRow(config, {
|
|
347
|
+
site,
|
|
348
|
+
event: e.event,
|
|
349
|
+
via: "client",
|
|
350
|
+
ts: e.ts,
|
|
351
|
+
path: e.path,
|
|
352
|
+
host,
|
|
353
|
+
title: e.title,
|
|
354
|
+
referrer: e.referrer,
|
|
355
|
+
utm: e.utm,
|
|
356
|
+
props: e.props,
|
|
357
|
+
consent: e.consent,
|
|
358
|
+
visitorId: e.visitor_id,
|
|
359
|
+
sessionId: e.session_id,
|
|
360
|
+
userId: e.user_id,
|
|
361
|
+
headers: req.headers,
|
|
362
|
+
bot
|
|
363
|
+
})
|
|
364
|
+
)
|
|
365
|
+
);
|
|
366
|
+
await writeRows(config, rows);
|
|
367
|
+
});
|
|
368
|
+
return noContent();
|
|
369
|
+
}
|
|
370
|
+
return { POST };
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
// src/server/proxy.ts
|
|
374
|
+
var STATIC = /^\/(_next\/|favicon\.ico$)/;
|
|
375
|
+
var ASSET = /\.(?:png|jpe?g|gif|webp|avif|svg|ico|bmp|css|js|mjs|map|woff2?|ttf|otf|eot|mp4|webm|mp3|wav|pdf|zip)$/i;
|
|
376
|
+
function withAnalytics(inner, options = {}) {
|
|
377
|
+
return async function analytics(req, event) {
|
|
378
|
+
try {
|
|
379
|
+
if (options.logBots !== false) {
|
|
380
|
+
const url = req.nextUrl ?? new URL(req.url);
|
|
381
|
+
const path = url.pathname;
|
|
382
|
+
if (!STATIC.test(path) && !ASSET.test(path) && path !== "/api/collect" && !options.ignore?.(path)) {
|
|
383
|
+
const bot = detectBot(req.headers.get("user-agent"));
|
|
384
|
+
if (bot) {
|
|
385
|
+
const config = getConfig(options.env);
|
|
386
|
+
const host = req.headers.get("x-forwarded-host") ?? req.headers.get("host") ?? url.host;
|
|
387
|
+
const site = resolveSite(config, options.site, host);
|
|
388
|
+
const ev = event;
|
|
389
|
+
const waitable = ev && typeof ev.waitUntil === "function" ? ev : null;
|
|
390
|
+
await defer(async () => {
|
|
391
|
+
const row = await buildRow(config, {
|
|
392
|
+
site,
|
|
393
|
+
event: "bot_request",
|
|
394
|
+
via: "proxy",
|
|
395
|
+
path,
|
|
396
|
+
host,
|
|
397
|
+
referrer: req.headers.get("referer"),
|
|
398
|
+
headers: req.headers,
|
|
399
|
+
bot,
|
|
400
|
+
props: { method: req.method }
|
|
401
|
+
});
|
|
402
|
+
await writeRows(config, [row]);
|
|
403
|
+
}, waitable);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
} catch (err) {
|
|
408
|
+
console.warn("[web-analytics] proxy logging failed:", err.message);
|
|
409
|
+
}
|
|
410
|
+
return inner ? inner(req, event) : void 0;
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// src/server/track.ts
|
|
415
|
+
async function trackServer(event, props = {}, options = {}) {
|
|
416
|
+
const config = getConfig(options.env);
|
|
417
|
+
const host = options.request ? options.request.headers.get("x-forwarded-host") ?? new URL(options.request.url).host : null;
|
|
418
|
+
const site = resolveSite(config, options.site, host);
|
|
419
|
+
const task = async () => {
|
|
420
|
+
const row = await buildRow(config, {
|
|
421
|
+
site,
|
|
422
|
+
event,
|
|
423
|
+
via: "server",
|
|
424
|
+
path: options.path ?? (options.request ? new URL(options.request.url).pathname : null),
|
|
425
|
+
host,
|
|
426
|
+
props,
|
|
427
|
+
userId: options.userId,
|
|
428
|
+
headers: options.request?.headers ?? null,
|
|
429
|
+
bot: null
|
|
430
|
+
});
|
|
431
|
+
await writeRows(config, [row]);
|
|
432
|
+
};
|
|
433
|
+
if (options.await) await task();
|
|
434
|
+
else await defer(task);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// src/server/errors.ts
|
|
438
|
+
function toHeaders(h) {
|
|
439
|
+
if (h instanceof Headers) return h;
|
|
440
|
+
const out = new Headers();
|
|
441
|
+
for (const [k, v] of Object.entries(h)) if (typeof v === "string") out.set(k, v);
|
|
442
|
+
return out;
|
|
443
|
+
}
|
|
444
|
+
function createOnRequestError(options = {}) {
|
|
445
|
+
return async function onRequestError(error, request, context = {}) {
|
|
446
|
+
try {
|
|
447
|
+
const config = getConfig(options.env);
|
|
448
|
+
const headers = toHeaders(request.headers);
|
|
449
|
+
const err = error;
|
|
450
|
+
const site = resolveSite(config, options.site, headers.get("host"));
|
|
451
|
+
await defer(async () => {
|
|
452
|
+
const row = await buildRow(config, {
|
|
453
|
+
site,
|
|
454
|
+
event: "error",
|
|
455
|
+
via: "server",
|
|
456
|
+
path: request.path,
|
|
457
|
+
host: headers.get("host"),
|
|
458
|
+
headers,
|
|
459
|
+
props: {
|
|
460
|
+
message: (err?.message ?? String(error)).slice(0, 200),
|
|
461
|
+
error_name: err?.name ?? null,
|
|
462
|
+
digest: err?.digest ?? null,
|
|
463
|
+
route: context.routePath ?? null,
|
|
464
|
+
route_type: context.routeType ?? null,
|
|
465
|
+
router: context.routerKind ?? null,
|
|
466
|
+
method: request.method
|
|
467
|
+
}
|
|
468
|
+
});
|
|
469
|
+
await writeRows(config, [row]);
|
|
470
|
+
});
|
|
471
|
+
} catch {
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
export {
|
|
476
|
+
buildRow,
|
|
477
|
+
createCollectHandler,
|
|
478
|
+
createOnRequestError,
|
|
479
|
+
getConfig,
|
|
480
|
+
trackServer,
|
|
481
|
+
withAnalytics,
|
|
482
|
+
writeRows
|
|
483
|
+
};
|
|
484
|
+
//# sourceMappingURL=index.js.map
|