diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml
index c84da25..c667f0f 100644
--- a/.github/workflows/deploy.yml
+++ b/.github/workflows/deploy.yml
@@ -54,6 +54,10 @@ jobs:
# Empty with the custom domain; set to /website to serve from
# https://napi-bindings.github.io/website instead.
NEXT_PUBLIC_BASE_PATH: ""
+ # Google Analytics 4 measurement ID, from the repository variable
+ # GA_MEASUREMENT_ID. Left empty for pull requests, so their builds
+ # contain neither the consent banner nor the Google tag.
+ NEXT_PUBLIC_GA_ID: ${{ github.event_name != 'pull_request' && vars.GA_MEASUREMENT_ID || '' }}
- uses: actions/upload-artifact@v4
with:
diff --git a/README.md b/README.md
index 6cbbd0c..43348f4 100644
--- a/README.md
+++ b/README.md
@@ -94,6 +94,25 @@ and a `.nojekyll` file so that `_next/` is served.
To serve the site from `https://napi-bindings.github.io/website` instead of the custom domain,
set `NEXT_PUBLIC_BASE_PATH: /website` in the workflow and remove the `cname` input.
+## Analytics
+
+The site counts page views with Google Analytics 4, through `GoogleAnalytics` from
+[`@next/third-parties`](https://nextjs.org/docs/app/guides/third-party-libraries#google-analytics).
+No custom events are sent.
+
+- The Google tag is loaded **only after the visitor accepts** the banner (`src/components/cookie-banner.tsx`):
+ before that, no request reaches Google. The choice is kept in `localStorage` for six months
+ (`src/lib/analytics-consent.ts`); "Cookie preferences" in the footer withdraws it, deletes the `_ga*`
+ cookies and shows the banner again. `/privacy/` describes all of this to visitors.
+- Navigations between pages are measured by GA4 itself: in the web data stream, Enhanced measurement must
+ have Page views on, with "Page changes based on browser history events" checked.
+- The measurement ID comes from the repository variable `GA_MEASUREMENT_ID`
+ (Settings → Secrets and variables → Actions → Variables) and is inlined at build time as
+ `NEXT_PUBLIC_GA_ID`. Pull request builds and local builds leave it empty: no banner, no tag.
+
+To try it locally, create `.env.local` (ignored by git) with the line `NEXT_PUBLIC_GA_ID=G-XXXXXXXXXX`
+and run `npm run dev`; in development the tag runs in debug mode, so the events show up in GA4 DebugView.
+
## License
Code: [Apache-2.0](LICENSE). Node-API documentation content: © Node.js contributors, MIT license.
diff --git a/package-lock.json b/package-lock.json
index 839857b..9b5aba6 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -10,6 +10,7 @@
"license": "Apache-2.0",
"dependencies": {
"@base-ui/react": "^1.9.0",
+ "@next/third-parties": "16.4.6",
"@shikijs/rehype": "^4.6.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
@@ -2370,6 +2371,19 @@
"node": ">= 10"
}
},
+ "node_modules/@next/third-parties": {
+ "version": "16.4.6",
+ "resolved": "https://registry.npmjs.org/@next/third-parties/-/third-parties-16.4.6.tgz",
+ "integrity": "sha512-Pp2FLFGkiy6FwLcuVTK1U/TNoZuC/+MK31wcp+MAM4dwjoGKufF3RxvgtnTlkBI14Oq1HSyeBqEw0KVoSsZlmg==",
+ "license": "MIT",
+ "dependencies": {
+ "third-party-capital": "1.0.20"
+ },
+ "peerDependencies": {
+ "next": "^13.0.0 || ^14.0.0 || ^15.0.0 || ^16.0.0",
+ "react": "^18.2.0 || 19.0.0-rc-de68d2f4-20241204 || ^19.0.0"
+ }
+ },
"node_modules/@nodelib/fs.scandir": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
@@ -12172,6 +12186,12 @@
"url": "https://opencollective.com/webpack"
}
},
+ "node_modules/third-party-capital": {
+ "version": "1.0.20",
+ "resolved": "https://registry.npmjs.org/third-party-capital/-/third-party-capital-1.0.20.tgz",
+ "integrity": "sha512-oB7yIimd8SuGptespDAZnNkzIz+NWaJCu2RMsbs4Wmp9zSDUM8Nhi3s2OOcqYuv3mN4hitXc8DVx+LyUmbUDiA==",
+ "license": "ISC"
+ },
"node_modules/tiny-invariant": {
"version": "1.3.3",
"resolved": "https://registry.npmjs.org/tiny-invariant/-/tiny-invariant-1.3.3.tgz",
diff --git a/package.json b/package.json
index e2874df..90746de 100644
--- a/package.json
+++ b/package.json
@@ -14,6 +14,7 @@
},
"dependencies": {
"@base-ui/react": "^1.9.0",
+ "@next/third-parties": "16.4.6",
"@shikijs/rehype": "^4.6.0",
"class-variance-authority": "^0.7.1",
"clsx": "^2.1.1",
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index aa7a7ef..48d9e2a 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -2,6 +2,7 @@ import type { Metadata, Viewport } from "next"
import { GeistMono } from "geist/font/mono"
import { GeistSans } from "geist/font/sans"
+import { Analytics } from "@/components/analytics"
import { SiteFooter } from "@/components/site-footer"
import { SiteHeader } from "@/components/site-header"
import { ThemeProvider } from "@/components/theme-provider"
@@ -52,6 +53,7 @@ export default function RootLayout({ children }: LayoutProps<"/">) {
{children}
+