@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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 BMNova
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,154 @@
1
- # Temporary Holding Version
1
+ # @bmnova/web-analytics
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Privacy-first analytics for Next.js sites that writes straight into **your own BigQuery**.
4
+ One small package gives you:
5
+
6
+ - **Who came, from where:** channel and source, with AI assistants (ChatGPT, Perplexity, Claude, Gemini, Copilot…) as their own channel next to Google, social, email, paid and referral.
7
+ - **What they did:** pageviews (including SPA navigation), clicks on `data-track` elements, outbound links, custom events, scroll depth, engaged time and exit pages.
8
+ - **Funnels and journeys:** sessions and ordered funnels as BigQuery table functions. Signed-in users and server events (sign-ups, payments) join the same funnel.
9
+ - **Errors:** browser errors and Next.js `onRequestError`, stored next to the funnel.
10
+ - **AI crawlers and bots:** GPTBot, ChatGPT-User, ClaudeBot, PerplexityBot, Googlebot and others, logged from your middleware/proxy, including hits on `robots.txt` and `llms.txt`.
11
+ - **One panel for many sites:** each site writes to its own GCP project; an observer dataset unions them for Looker Studio.
12
+
13
+ It runs on **Vercel** (Node or Edge) and **Cloudflare Workers via OpenNext**: no Node-only SDKs; BigQuery is called with `fetch` + WebCrypto.
14
+
15
+ ## How it works
16
+
17
+ ```
18
+ browser tracker ──POST /api/collect──┐
19
+ middleware/proxy (bots) ─────────────┼──► BigQuery insertAll ──► <project>.web_analytics.events
20
+ server code (trackServer, errors) ───┘ │ hourly scheduled query
21
+ ▼
22
+ daily_* tables ──► observer views ──► Looker Studio
23
+ ```
24
+
25
+ ## Privacy model
26
+
27
+ | | `basic` (default, no banner needed) | `full` (after the visitor accepts) |
28
+ |---|---|---|
29
+ | Stored in the browser | nothing (only the choice itself, once made) | `wa_vid` visitor id, `wa_sid` session id |
30
+ | Visitor counting | daily-rotating HMAC of site + IP + UA, computed on the server; the salt is derived from a secret and the date and never stored | persistent visitor id |
31
+ | Signed-in user id | not sent from the browser | sent after `identify()` |
32
+ | Session replay (Clarity) | never | only if you pass `clarityId` |
33
+
34
+ Always:
35
+ - IP addresses are never stored.
36
+ - Query strings are stripped from paths.
37
+ - Raw user agents are kept for bots only.
38
+ - Event props drop keys such as `email`, `amount`, `revenue`, `token`, `password` and email-looking values.
39
+
40
+ Global Privacy Control and Do Not Track keep a visitor in `basic`. You are still responsible for your privacy policy; see [docs/consent-and-privacy.md](docs/consent-and-privacy.md).
41
+
42
+ ## Install
43
+
44
+ ```bash
45
+ npm i @bmnova/web-analytics
46
+ ```
47
+
48
+ ### 1. Collector route: `app/api/collect/route.ts`
49
+ ```ts
50
+ import { createCollectHandler } from "@bmnova/web-analytics/server";
51
+ export const { POST } = createCollectHandler();
52
+ // Do not add `export const runtime = "edge"`; the default runtime works on Vercel and OpenNext.
53
+ ```
54
+
55
+ ### 2. Tracker and banner in the root layout
56
+ ```tsx
57
+ import { Analytics, ConsentBanner } from "@bmnova/web-analytics/react";
58
+
59
+ <body>
60
+ {children}
61
+ <Analytics site="mysite" />
62
+ <ConsentBanner policyHref="/privacy" />
63
+ </body>
64
+ ```
65
+ Track anything else:
66
+ ```tsx
67
+ <button data-track="signup_started" data-track-plan="pro">Start</button>
68
+ ```
69
+ ```ts
70
+ import { track, identify } from "@bmnova/web-analytics/client";
71
+ track("file_parsed", { success: true, source: "csv" });
72
+ identify(user.id); // after sign-in; identify(null) on sign-out
73
+ ```
74
+
75
+ ### 3. Bot and AI-crawler logging
76
+ ```ts
77
+ // Next 14/15: middleware.ts
78
+ import { withAnalytics } from "@bmnova/web-analytics/server";
79
+ export default withAnalytics(myMiddleware); // or withAnalytics() if you have none
80
+ export const config = { matcher: ["/((?!_next/static|_next/image).*)"] };
81
+ ```
82
+ ```ts
83
+ // Next 16: proxy.ts
84
+ import { withAnalytics } from "@bmnova/web-analytics/server";
85
+ export const proxy = withAnalytics(myProxy);
86
+ ```
87
+ Make sure the matcher includes `robots.txt`, `sitemap.xml` and `llms.txt`. On Cloudflare, files in `public/` are served without running the worker, so serve them from route handlers (`app/robots.ts`, `app/llms.txt/route.ts`) instead.
88
+
89
+ ### 4. Server events and errors (optional)
90
+ ```ts
91
+ import { trackServer } from "@bmnova/web-analytics/server";
92
+ await trackServer("subscription_started", { plan: "pro" }, { userId });
93
+ ```
94
+ ```ts
95
+ // instrumentation.ts
96
+ import { createOnRequestError } from "@bmnova/web-analytics/server";
97
+ export const onRequestError = createOnRequestError();
98
+ ```
99
+
100
+ ### 5. Environment
101
+ See [.env.example](.env.example). Without `WA_GCP_SA_KEY` the package runs in **dry-run mode** and prints rows to the log, which is handy locally. Requests with bot user agents (including `curl`) are flagged `is_bot` and never get a `visitor_hash`; test with a browser user agent.
102
+
103
+ | Var | |
104
+ |---|---|
105
+ | `WA_SITE` | label on every row |
106
+ | `WA_BQ_PROJECT`, `WA_BQ_DATASET` | destination (dataset defaults to `web_analytics`) |
107
+ | `WA_GCP_SA_KEY` | service-account JSON (raw or base64); needs BigQuery Data Editor on the dataset only |
108
+ | `WA_SALT_SECRET` | random secret for the daily visitor hash (`openssl rand -hex 32`) |
109
+ | `WA_ALLOWED_ORIGINS` | extra origins allowed to post (the site's own origin is always allowed) |
110
+
111
+ ## BigQuery setup
112
+
113
+ ```bash
114
+ npx @bmnova/web-analytics setup --project my-site-project --location EU
115
+ ```
116
+ This creates:
117
+ - the `events` table (partitioned, clustered, 14-month retention by default)
118
+ - the `sessionized_events`, `sessions` and `funnel` table functions
119
+ - the `daily_*` aggregate tables
120
+ - an hourly scheduled query that refreshes the aggregates
121
+
122
+ Add `--print` to see the SQL without running it.
123
+
124
+ Funnel example:
125
+ ```sql
126
+ SELECT * FROM `my-site-project.web_analytics.funnel`(
127
+ ['pageview:/', 'pageview:/pricing', 'signup_started', 'signed_up'], '2026-01-01', CURRENT_DATE())
128
+ ```
129
+
130
+ ### One panel for several sites
131
+ ```bash
132
+ npx @bmnova/web-analytics observer --project my-observer --sites site-a.web_analytics,site-b.web_analytics
133
+ ```
134
+ Then follow [docs/dashboard.md](docs/dashboard.md).
135
+
136
+ ## Docs
137
+ - [Next.js on Vercel](docs/nextjs-vercel.md)
138
+ - [Next.js on Cloudflare (OpenNext)](docs/nextjs-cloudflare.md)
139
+ - [Consent and privacy](docs/consent-and-privacy.md)
140
+ - [Dashboard](docs/dashboard.md)
141
+ - [Rate limiting](docs/rate-limiting.md)
142
+
143
+ ## Development
144
+ ```bash
145
+ pnpm install
146
+ pnpm test
147
+ pnpm build
148
+ ```
149
+ - A pre-commit hook runs [gitleaks](https://github.com/gitleaks/gitleaks) on staged changes (`npx simple-git-hooks` once after install).
150
+ - CI scans the full history.
151
+ - Never commit credentials: configuration lives in env vars only.
152
+
153
+ ## License
154
+ MIT
package/bin/setup.mjs ADDED
@@ -0,0 +1,173 @@
1
+ #!/usr/bin/env node
2
+ // web-analytics CLI: creates the BigQuery dataset, table, functions and scheduled
3
+ // aggregation for one site, or the cross-site observer views. Uses the `bq` CLI with
4
+ // your own gcloud login; it never reads or writes service-account keys.
5
+ import { spawnSync } from "node:child_process";
6
+ import { readFileSync } from "node:fs";
7
+ import { dirname, join } from "node:path";
8
+ import { fileURLToPath } from "node:url";
9
+
10
+ const SQL_DIR = join(dirname(fileURLToPath(import.meta.url)), "..", "sql");
11
+ const PROJECT = /^[a-z][a-z0-9-]{4,28}[a-z0-9]$/;
12
+ const DATASET = /^[A-Za-z_][A-Za-z0-9_]{0,1023}$/;
13
+ const LOCATION = /^[A-Za-z0-9-]{2,40}$/;
14
+ const AGG_TABLES = ["daily_overview", "daily_channels", "daily_pages", "daily_events", "daily_errors", "daily_bots"];
15
+
16
+ const HELP = `Usage:
17
+ web-analytics setup --project <gcp-project> [--dataset web_analytics] [--location EU]
18
+ [--retention-days 425] [--lookback-days 2] [--schedule-sa <email>] [--no-schedule] [--print]
19
+ web-analytics observer --project <observer-project> --sites <proj.dataset,proj.dataset,...>
20
+ [--dataset all_sites] [--location EU] [--print]
21
+
22
+ --schedule-sa service account that runs the hourly aggregation (needs BigQuery Job User on the
23
+ project and Data Editor on the dataset). Without it the scheduled query is skipped,
24
+ because bq would need an interactive browser consent.
25
+ --print only print the SQL, do not run anything.
26
+ Requires the bq CLI (Google Cloud SDK) logged in as someone who can create datasets.`;
27
+
28
+ function args(argv) {
29
+ const out = { _: [] };
30
+ for (let i = 0; i < argv.length; i++) {
31
+ const a = argv[i];
32
+ if (a.startsWith("--no-")) out[a.slice(5)] = false;
33
+ else if (a.startsWith("--")) {
34
+ const next = argv[i + 1];
35
+ if (next === undefined || next.startsWith("--")) out[a.slice(2)] = true;
36
+ else (out[a.slice(2)] = next), i++;
37
+ } else out._.push(a);
38
+ }
39
+ return out;
40
+ }
41
+
42
+ function fail(msg) {
43
+ console.error(`error: ${msg}\n\n${HELP}`);
44
+ process.exit(1);
45
+ }
46
+
47
+ function check(value, re, name) {
48
+ if (typeof value !== "string" || !re.test(value)) fail(`invalid or missing --${name}`);
49
+ return value;
50
+ }
51
+
52
+ function render(file, vars) {
53
+ return readFileSync(join(SQL_DIR, file), "utf8").replace(/\{\{(\w+)\}\}/g, (_, k) => {
54
+ if (!(k in vars)) throw new Error(`missing template var ${k}`);
55
+ return String(vars[k]);
56
+ });
57
+ }
58
+
59
+ function bq(argv, input) {
60
+ const res = spawnSync("bq", argv, { input, encoding: "utf8", stdio: [input ? "pipe" : "ignore", "pipe", "pipe"] });
61
+ if (res.error) fail(`could not run bq (${res.error.message}). Install the Google Cloud SDK and run \`gcloud auth login\`.`);
62
+ return res;
63
+ }
64
+
65
+ function runQuery(project, location, sql, label) {
66
+ process.stdout.write(`• ${label} … `);
67
+ const res = bq(["--quiet", `--project_id=${project}`, `--location=${location}`, "query", "--use_legacy_sql=false", "--format=none"], sql);
68
+ if (res.status !== 0) {
69
+ console.log("failed");
70
+ console.error(res.stderr || res.stdout);
71
+ process.exit(1);
72
+ }
73
+ console.log("ok");
74
+ }
75
+
76
+ function setup(o) {
77
+ const project = check(o.project, PROJECT, "project");
78
+ const dataset = check(o.dataset ?? "web_analytics", DATASET, "dataset");
79
+ const location = check(o.location ?? "EU", LOCATION, "location");
80
+ const retention_days = Number(o["retention-days"] ?? 425);
81
+ const lookback_days = Number(o["lookback-days"] ?? 2);
82
+ if (!Number.isInteger(retention_days) || retention_days < 1) fail("--retention-days must be a positive integer");
83
+ if (!Number.isInteger(lookback_days) || lookback_days < 0 || lookback_days > 30) fail("--lookback-days must be 0-30");
84
+
85
+ const vars = { project, dataset, retention_days, lookback_days };
86
+ const ddl = `CREATE SCHEMA IF NOT EXISTS \`${project}.${dataset}\` OPTIONS (location = '${location}');\n${render("01_events.sql", vars)}`;
87
+ const functions = render("02_functions.sql", vars);
88
+ const aggregates = render("03_daily_aggregates.sql", vars);
89
+
90
+ if (o.print) {
91
+ console.log([ddl, functions, aggregates].join("\n\n"));
92
+ return;
93
+ }
94
+ runQuery(project, location, ddl, `dataset ${project}.${dataset} and events table`);
95
+ runQuery(project, location, functions, "session and funnel functions");
96
+ runQuery(project, location, aggregates, "daily aggregate tables (first run)");
97
+
98
+ if (o.schedule !== false && typeof o["schedule-sa"] !== "string") {
99
+ console.log("• hourly scheduled query … skipped (pass --schedule-sa <service-account-email> to create it)");
100
+ } else if (o.schedule !== false) {
101
+ const sa = check(o["schedule-sa"], /^[a-z0-9-]+@[a-z0-9-]+\.iam\.gserviceaccount\.com$/, "schedule-sa");
102
+ process.stdout.write("• hourly scheduled query … ");
103
+ const res = bq([
104
+ `--project_id=${project}`,
105
+ `--location=${location}`,
106
+ "mk",
107
+ "--transfer_config",
108
+ "--data_source=scheduled_query",
109
+ `--display_name=web-analytics aggregates (${dataset})`,
110
+ "--schedule=every 1 hours",
111
+ `--service_account_name=${sa}`,
112
+ `--params=${JSON.stringify({ query: aggregates })}`,
113
+ ]);
114
+ if (res.status !== 0) {
115
+ console.log("skipped");
116
+ console.error((res.stderr || res.stdout).trim());
117
+ console.error("Check that the BigQuery Data Transfer API is enabled and that you can act as the service account (roles/iam.serviceAccountUser), or create the scheduled query in the console from `--print` output.");
118
+ } else console.log("ok");
119
+ }
120
+
121
+ console.log(`
122
+ Next steps (run these yourself; this tool never creates or downloads keys):
123
+ 1. Create a writer service account:
124
+ gcloud iam service-accounts create web-analytics-writer --project=${project}
125
+ 2. Let it insert rows into this dataset only (BigQuery console → ${dataset} → Sharing → Permissions →
126
+ add web-analytics-writer@${project}.iam.gserviceaccount.com as "BigQuery Data Editor").
127
+ 3. Create a JSON key for it and store the file contents as the WA_GCP_SA_KEY secret on your host
128
+ (Vercel env var / \`wrangler secret put WA_GCP_SA_KEY\`). Do not commit the file; delete it afterwards.
129
+ 4. Set WA_BQ_PROJECT=${project}, WA_BQ_DATASET=${dataset}, WA_SITE, WA_SALT_SECRET (openssl rand -hex 32).`);
130
+ }
131
+
132
+ function observer(o) {
133
+ const project = check(o.project, PROJECT, "project");
134
+ const dataset = check(o.dataset ?? "all_sites", DATASET, "dataset");
135
+ const location = check(o.location ?? "EU", LOCATION, "location");
136
+ if (typeof o.sites !== "string") fail("missing --sites");
137
+ const sites = o.sites.split(",").map((s) => s.trim()).filter(Boolean).map((s) => {
138
+ const [p, d] = s.split(".");
139
+ check(p, PROJECT, "sites (project part)");
140
+ check(d, DATASET, "sites (dataset part)");
141
+ return `${p}.${d}`;
142
+ });
143
+ if (!sites.length) fail("--sites is empty");
144
+
145
+ const target = `${project}.${dataset}`;
146
+ const parts = [`CREATE SCHEMA IF NOT EXISTS \`${target}\` OPTIONS (location = '${location}');`];
147
+ for (const table of AGG_TABLES) {
148
+ parts.push(`CREATE OR REPLACE VIEW \`${target}.${table}\` AS\n${sites.map((s) => `SELECT * FROM \`${s}.${table}\``).join("\nUNION ALL\n")};`);
149
+ }
150
+ parts.push(
151
+ `CREATE OR REPLACE TABLE FUNCTION \`${target}.funnel\`(steps ARRAY<STRING>, from_date DATE, to_date DATE) AS (\n${sites
152
+ .map((s) => ` SELECT * FROM \`${s}.funnel\`(steps, from_date, to_date)`)
153
+ .join("\n UNION ALL\n")}\n);`,
154
+ `CREATE OR REPLACE TABLE FUNCTION \`${target}.sessions\`(from_date DATE, to_date DATE) AS (\n${sites
155
+ .map((s) => ` SELECT * FROM \`${s}.sessions\`(from_date, to_date)`)
156
+ .join("\n UNION ALL\n")}\n);`,
157
+ );
158
+ const sql = parts.join("\n\n");
159
+ if (o.print) return console.log(sql);
160
+ runQuery(project, location, sql, `observer views in ${target} over ${sites.length} site(s)`);
161
+ console.log(`
162
+ Give the observer (you, and Looker Studio's data credentials) "BigQuery Data Viewer" on each site dataset,
163
+ then connect Looker Studio to ${target}.daily_* — see docs/dashboard.md.`);
164
+ }
165
+
166
+ const o = args(process.argv.slice(2));
167
+ const cmd = o._[0];
168
+ if (cmd === "setup") setup(o);
169
+ else if (cmd === "observer") observer(o);
170
+ else {
171
+ console.log(HELP);
172
+ process.exit(cmd ? 1 : 0);
173
+ }
@@ -0,0 +1,11 @@
1
+ type BotKind = "ai_training" | "ai_user" | "ai_search" | "search" | "social" | "seo" | "monitor" | "other";
2
+ interface Bot {
3
+ name: string;
4
+ kind: BotKind;
5
+ }
6
+ /** Returns the bot behind a user agent, or null for a (probable) human browser. */
7
+ declare function detectBot(userAgent: string | null | undefined): Bot | null;
8
+ declare const AI_BOT_KINDS: ReadonlySet<BotKind>;
9
+
10
+ export { AI_BOT_KINDS as A, detectBot as d };
11
+ export type { Bot as B, BotKind as a };
@@ -0,0 +1,308 @@
1
+ import {
2
+ LIMITS
3
+ } from "./chunk-PD4SX2FD.js";
4
+
5
+ // src/shared/bots.ts
6
+ var BOTS = [
7
+ // OpenAI
8
+ [/ChatGPT-User/i, "ChatGPT-User", "ai_user"],
9
+ [/OAI-SearchBot/i, "OAI-SearchBot", "ai_search"],
10
+ [/GPTBot/i, "GPTBot", "ai_training"],
11
+ // Anthropic
12
+ [/Claude-User/i, "Claude-User", "ai_user"],
13
+ [/Claude-SearchBot/i, "Claude-SearchBot", "ai_search"],
14
+ [/Claude-Web/i, "Claude-Web", "ai_user"],
15
+ [/ClaudeBot/i, "ClaudeBot", "ai_training"],
16
+ [/anthropic-ai/i, "anthropic-ai", "ai_training"],
17
+ // Perplexity
18
+ [/Perplexity-User/i, "Perplexity-User", "ai_user"],
19
+ [/PerplexityBot/i, "PerplexityBot", "ai_search"],
20
+ // Others
21
+ [/MistralAI-User/i, "MistralAI-User", "ai_user"],
22
+ [/DuckAssistBot/i, "DuckAssistBot", "ai_user"],
23
+ [/Meta-ExternalFetcher/i, "Meta-ExternalFetcher", "ai_user"],
24
+ [/meta-externalagent/i, "meta-externalagent", "ai_training"],
25
+ [/Google-CloudVertexBot/i, "Google-CloudVertexBot", "ai_training"],
26
+ [/Bytespider/i, "Bytespider", "ai_training"],
27
+ [/CCBot/i, "CCBot", "ai_training"],
28
+ [/Amazonbot/i, "Amazonbot", "ai_training"],
29
+ [/cohere-ai|cohere-training-data-crawler/i, "cohere-ai", "ai_training"],
30
+ [/Diffbot/i, "Diffbot", "ai_training"],
31
+ [/YouBot/i, "YouBot", "ai_search"],
32
+ [/PhindBot/i, "PhindBot", "ai_search"],
33
+ [/Timpibot/i, "Timpibot", "ai_training"],
34
+ [/AI2Bot/i, "AI2Bot", "ai_training"],
35
+ [/omgili/i, "omgili", "ai_training"],
36
+ // Search engines
37
+ [/Googlebot|Google-InspectionTool|Storebot-Google/i, "Googlebot", "search"],
38
+ [/GoogleOther/i, "GoogleOther", "other"],
39
+ [/bingbot|BingPreview/i, "Bingbot", "search"],
40
+ [/Applebot/i, "Applebot", "search"],
41
+ [/YandexBot|YandexMobileBot/i, "YandexBot", "search"],
42
+ [/Baiduspider/i, "Baiduspider", "search"],
43
+ [/DuckDuckBot/i, "DuckDuckBot", "search"],
44
+ [/PetalBot/i, "PetalBot", "search"],
45
+ [/Yeti\//i, "NaverYeti", "search"],
46
+ [/SeznamBot/i, "SeznamBot", "search"],
47
+ // Link previews
48
+ [/facebookexternalhit|facebookcatalog/i, "Facebook", "social"],
49
+ [/Twitterbot/i, "Twitterbot", "social"],
50
+ [/LinkedInBot/i, "LinkedInBot", "social"],
51
+ [/Slackbot|Slack-ImgProxy/i, "Slackbot", "social"],
52
+ [/Discordbot/i, "Discordbot", "social"],
53
+ [/TelegramBot/i, "TelegramBot", "social"],
54
+ [/WhatsApp/i, "WhatsApp", "social"],
55
+ [/Pinterestbot/i, "Pinterestbot", "social"],
56
+ [/redditbot/i, "redditbot", "social"],
57
+ // SEO tools
58
+ [/AhrefsBot|AhrefsSiteAudit/i, "AhrefsBot", "seo"],
59
+ [/SemrushBot/i, "SemrushBot", "seo"],
60
+ [/MJ12bot/i, "MJ12bot", "seo"],
61
+ [/DotBot/i, "DotBot", "seo"],
62
+ [/DataForSeoBot/i, "DataForSeoBot", "seo"],
63
+ [/Screaming Frog/i, "ScreamingFrog", "seo"],
64
+ // Monitoring
65
+ [/UptimeRobot/i, "UptimeRobot", "monitor"],
66
+ [/Pingdom/i, "Pingdom", "monitor"],
67
+ [/Better ?Uptime|BetterStack/i, "BetterStack", "monitor"],
68
+ [/vercel-(screenshot|favicon)/i, "Vercel", "monitor"]
69
+ ];
70
+ var GENERIC = /bot\b|crawler|spider|crawl|slurp|headlesschrome|phantomjs|puppeteer|playwright|python-requests|python-urllib|aiohttp|httpx|go-http-client|curl\/|wget\/|axios\/|node-fetch|undici|okhttp|java\/|libwww|scrapy/i;
71
+ function detectBot(userAgent) {
72
+ if (!userAgent) return { name: "empty-ua", kind: "other" };
73
+ for (const [re, name, kind] of BOTS) {
74
+ if (re.test(userAgent)) return { name, kind };
75
+ }
76
+ if (GENERIC.test(userAgent)) return { name: "generic", kind: "other" };
77
+ return null;
78
+ }
79
+ var AI_BOT_KINDS = /* @__PURE__ */ new Set(["ai_training", "ai_user", "ai_search"]);
80
+
81
+ // src/shared/channel.ts
82
+ var AI_HOSTS = [
83
+ "chatgpt.com",
84
+ "chat.openai.com",
85
+ "openai.com",
86
+ "perplexity.ai",
87
+ "claude.ai",
88
+ "gemini.google.com",
89
+ "bard.google.com",
90
+ "aistudio.google.com",
91
+ "notebooklm.google.com",
92
+ "copilot.microsoft.com",
93
+ "copilot.cloud.microsoft",
94
+ "deepseek.com",
95
+ "grok.com",
96
+ "meta.ai",
97
+ "chat.mistral.ai",
98
+ "you.com",
99
+ "phind.com",
100
+ "poe.com",
101
+ "kimi.com",
102
+ "kimi.moonshot.cn",
103
+ "chat.qwen.ai",
104
+ "duck.ai"
105
+ ];
106
+ var AI_SOURCE_WORDS = /^(chatgpt|openai|perplexity|claude|gemini|copilot|deepseek|grok|mistral|le-chat|meta-ai|you|phind|poe)$/i;
107
+ var SEARCH_HOSTS = [
108
+ "bing.com",
109
+ "duckduckgo.com",
110
+ "search.yahoo.com",
111
+ "yahoo.com",
112
+ "yandex.ru",
113
+ "yandex.com",
114
+ "yandex.com.tr",
115
+ "baidu.com",
116
+ "ecosia.org",
117
+ "search.brave.com",
118
+ "startpage.com",
119
+ "qwant.com",
120
+ "naver.com",
121
+ "seznam.cz",
122
+ "kagi.com"
123
+ ];
124
+ var SOCIAL_HOSTS = [
125
+ "facebook.com",
126
+ "fb.com",
127
+ "instagram.com",
128
+ "t.co",
129
+ "x.com",
130
+ "twitter.com",
131
+ "linkedin.com",
132
+ "lnkd.in",
133
+ "reddit.com",
134
+ "youtube.com",
135
+ "youtu.be",
136
+ "tiktok.com",
137
+ "pinterest.com",
138
+ "threads.net",
139
+ "threads.com",
140
+ "bsky.app",
141
+ "mastodon.social",
142
+ "news.ycombinator.com",
143
+ "producthunt.com",
144
+ "medium.com",
145
+ "quora.com",
146
+ "discord.com",
147
+ "t.me",
148
+ "telegram.org",
149
+ "web.whatsapp.com",
150
+ "wa.me"
151
+ ];
152
+ var EMAIL_HOSTS = ["mail.google.com", "outlook.live.com", "outlook.office.com", "mail.yahoo.com", "mail.proton.me"];
153
+ var GOOGLE = /^(?:[a-z0-9-]+\.)*google\.[a-z]{2,3}(?:\.[a-z]{2})?$/;
154
+ function hostOf(value) {
155
+ if (!value) return null;
156
+ const raw = value.trim().toLowerCase();
157
+ if (!raw) return null;
158
+ let host;
159
+ try {
160
+ host = new URL(raw.includes("://") ? raw : `https://${raw}`).hostname;
161
+ } catch {
162
+ return null;
163
+ }
164
+ if (!host || !host.includes(".")) return host || null;
165
+ return host.replace(/^www\./, "");
166
+ }
167
+ function matches(host, list) {
168
+ for (const d of list) if (host === d || host.endsWith(`.${d}`)) return d;
169
+ return null;
170
+ }
171
+ function classifyHost(host) {
172
+ const ai = matches(host, AI_HOSTS);
173
+ if (ai) return { channel: "ai", source: ai };
174
+ const mail = matches(host, EMAIL_HOSTS);
175
+ if (mail) return { channel: "email", source: mail };
176
+ if (GOOGLE.test(host)) return { channel: "search", source: "google" };
177
+ const search = matches(host, SEARCH_HOSTS);
178
+ if (search) return { channel: "search", source: search };
179
+ const social = matches(host, SOCIAL_HOSTS);
180
+ if (social) return { channel: "social", source: social.replace(/^(m|l|lm)\./, "") };
181
+ return null;
182
+ }
183
+ var PAID = /^(cpc|ppc|paid|paidsearch|paid_search|paid-social|paid_social|paidsocial|display|cpm|banner|ads?)$/i;
184
+ var EMAIL_MEDIUM = /^(e-?mail|newsletter)$/i;
185
+ var SOCIAL_MEDIUM = /^(social|social-network|social-media|sm|organic_social)$/i;
186
+ function classifyChannel(input) {
187
+ const referrerHost = hostOf(input.referrer);
188
+ const selfHost = hostOf(input.selfHost);
189
+ const utmSource = input.utmSource?.trim().toLowerCase() || null;
190
+ const utmMedium = input.utmMedium?.trim().toLowerCase() || null;
191
+ if (utmSource || utmMedium) {
192
+ const srcHost = utmSource ? hostOf(utmSource) : null;
193
+ const byHost2 = srcHost && srcHost.includes(".") ? classifyHost(srcHost) : null;
194
+ const source = byHost2?.source ?? utmSource;
195
+ if (byHost2?.channel === "ai" || utmSource && AI_SOURCE_WORDS.test(utmSource)) {
196
+ return { channel: "ai", source, referrerHost };
197
+ }
198
+ if (utmMedium && PAID.test(utmMedium)) return { channel: "paid", source, referrerHost };
199
+ if (utmMedium && EMAIL_MEDIUM.test(utmMedium)) return { channel: "email", source, referrerHost };
200
+ if (utmMedium && SOCIAL_MEDIUM.test(utmMedium)) return { channel: "social", source, referrerHost };
201
+ if (byHost2) return { channel: byHost2.channel, source, referrerHost };
202
+ return { channel: "campaign", source, referrerHost };
203
+ }
204
+ if (!referrerHost) return { channel: "direct", source: null, referrerHost: null };
205
+ if (selfHost && (referrerHost === selfHost || referrerHost.endsWith(`.${selfHost}`) || selfHost.endsWith(`.${referrerHost}`))) {
206
+ return { channel: "internal", source: null, referrerHost };
207
+ }
208
+ const byHost = classifyHost(referrerHost);
209
+ if (byHost) return { ...byHost, referrerHost };
210
+ return { channel: "referral", source: referrerHost, referrerHost };
211
+ }
212
+
213
+ // src/shared/privacy.ts
214
+ var DENY = [
215
+ "email",
216
+ "e_mail",
217
+ "phone",
218
+ "password",
219
+ "passwd",
220
+ "secret",
221
+ "token",
222
+ "apikey",
223
+ "api_key",
224
+ "auth",
225
+ "cookie",
226
+ "session_token",
227
+ "card",
228
+ "iban",
229
+ "cvv",
230
+ "ssn",
231
+ "tckn",
232
+ "address",
233
+ "amount",
234
+ "revenue",
235
+ "income",
236
+ "salary",
237
+ "price",
238
+ "balance",
239
+ "file_content",
240
+ "content",
241
+ "ip"
242
+ ];
243
+ var DENY_EXACT = /* @__PURE__ */ new Set(["ip", "name", "first_name", "last_name", "full_name", "fullname"]);
244
+ function isDeniedKey(key) {
245
+ const k = key.toLowerCase();
246
+ if (DENY_EXACT.has(k)) return true;
247
+ return DENY.some((d) => d === "ip" ? /(^|_)ip(_|$)/.test(k) : k.includes(d));
248
+ }
249
+ var EMAIL_LIKE = /[^\s@]+@[^\s@]+\.[^\s@]+/;
250
+ function sanitizeProps(input) {
251
+ if (!input || typeof input !== "object" || Array.isArray(input)) return null;
252
+ const out = {};
253
+ let n = 0;
254
+ for (const [key, value] of Object.entries(input)) {
255
+ if (n >= LIMITS.propKeys) break;
256
+ if (!LIMITS.propKey.test(key) || isDeniedKey(key)) continue;
257
+ const v = cleanValue(value);
258
+ if (v === void 0) continue;
259
+ out[key] = v;
260
+ n++;
261
+ }
262
+ return n ? out : null;
263
+ }
264
+ function cleanValue(value) {
265
+ if (value === null) return null;
266
+ if (typeof value === "boolean") return value;
267
+ if (typeof value === "number") return Number.isFinite(value) ? value : void 0;
268
+ if (typeof value === "string") {
269
+ if (EMAIL_LIKE.test(value)) return void 0;
270
+ return value.slice(0, LIMITS.propStringLength);
271
+ }
272
+ return void 0;
273
+ }
274
+ function cleanPath(path) {
275
+ if (!path) return null;
276
+ let p = path.trim();
277
+ const cut = p.search(/[?#]/);
278
+ if (cut >= 0) p = p.slice(0, cut);
279
+ if (!p.startsWith("/")) {
280
+ try {
281
+ p = new URL(p).pathname;
282
+ } catch {
283
+ return null;
284
+ }
285
+ }
286
+ return p.slice(0, LIMITS.pathLength) || "/";
287
+ }
288
+
289
+ // src/shared/ua.ts
290
+ function parseUa(ua) {
291
+ if (!ua) return { device: null, browser: null, os: null };
292
+ const os = /iPhone|iPad|iPod/.test(ua) ? "iOS" : /Android/.test(ua) ? "Android" : /Windows/.test(ua) ? "Windows" : /CrOS/.test(ua) ? "ChromeOS" : /Mac OS X|Macintosh/.test(ua) ? "macOS" : /Linux/.test(ua) ? "Linux" : null;
293
+ const browser = /Edg(e|A|iOS)?\//.test(ua) ? "Edge" : /OPR\/|Opera/.test(ua) ? "Opera" : /SamsungBrowser/.test(ua) ? "Samsung Internet" : /YaBrowser/.test(ua) ? "Yandex" : /Firefox|FxiOS/.test(ua) ? "Firefox" : /Chrome|CriOS/.test(ua) ? "Chrome" : /Safari/.test(ua) ? "Safari" : null;
294
+ const device = /iPad|Tablet|(Android(?!.*Mobile))/.test(ua) ? "tablet" : /Mobi|iPhone|iPod|Android/.test(ua) ? "mobile" : "desktop";
295
+ return { device, browser, os };
296
+ }
297
+
298
+ export {
299
+ detectBot,
300
+ AI_BOT_KINDS,
301
+ hostOf,
302
+ classifyChannel,
303
+ isDeniedKey,
304
+ sanitizeProps,
305
+ cleanPath,
306
+ parseUa
307
+ };
308
+ //# sourceMappingURL=chunk-4B5GWKPD.js.map