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} + diff --git a/src/app/privacy/page.tsx b/src/app/privacy/page.tsx new file mode 100644 index 0000000..cd237b6 --- /dev/null +++ b/src/app/privacy/page.tsx @@ -0,0 +1,131 @@ +import type { Metadata } from "next" +import type { ReactNode } from "react" + +import { CookiePreferences } from "@/components/cookie-preferences" +import { Container, PageHeader } from "@/components/page-header" +import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" +import { siteConfig } from "@/lib/site" + +export const metadata: Metadata = { + title: "Privacy", + description: "What napi-bindings.dev measures, which cookies it sets and how to change your choice.", + alternates: { canonical: "/privacy/" }, +} + +const LAST_UPDATED = "November 21, 2026" + +const COOKIES = [ + { name: "_ga", purpose: "Tells visitors apart, so that visits can be counted.", duration: "2 years" }, + { name: "_ga_", purpose: "Keeps the state of the current visit (session).", duration: "2 years" }, +] + +function Section({ title, children }: { title: string; children: ReactNode }) { + return ( +
+

{title}

+
{children}
+
+ ) +} + +export default function PrivacyPage() { + const issues = `${siteConfig.github}/website/issues` + + return ( + + + +
+

+ If you accept, Google Analytics 4 records the pages you open: the page address and title, the page you came + from, the approximate country, the device and browser type, the language and the screen size. The site sends + no other events, uses no advertising features and has Google signals turned off. Google Analytics 4 does not + store IP addresses. +

+

+ If you decline, or make no choice, the Google tag is not loaded and no data is sent to Google. The site works + the same either way. +

+
+ +
+

Google Analytics sets these cookies, only after you accept:

+
+ + + + Cookie + Purpose + Duration + + + + {COOKIES.map((cookie) => ( + + {cookie.name} + {cookie.purpose} + {cookie.duration} + + ))} + +
+
+

+ Your choice itself is kept in your browser's local storage ( + napi-bindings:analytics-consent) for six months, + after which the site asks again. It is never sent anywhere. The light or dark theme you pick is kept the same + way. +

+
+ +
+

+ Google Analytics is provided by Google Ireland Limited. Google may process the data in the United States, + under the EU-U.S. Data Privacy Framework. The analytics data is kept for two months and then deleted; + only aggregated reports remain. +

+

+ The site is hosted on GitHub Pages. GitHub may log technical data, such as IP addresses, to deliver and + secure the pages, as described in the{" "} + + GitHub privacy statement + + . +

+
+ +
+

+ You can withdraw or change your choice at any time: the Google Analytics cookies are deleted and the banner + is shown again. The same link is in the footer of every page. +

+
+ +
+

+ For questions about this page, or to exercise your rights under the GDPR, contact the maintainers of the + napi-bindings organization by{" "} + + opening an issue + + . You can also lodge a complaint with your data protection authority. +

+
+ +

Last updated: {LAST_UPDATED}.

+
+ ) +} diff --git a/src/app/sitemap.ts b/src/app/sitemap.ts index dc4e901..7f502d0 100644 --- a/src/app/sitemap.ts +++ b/src/app/sitemap.ts @@ -14,6 +14,7 @@ export default function sitemap(): MetadataRoute.Sitemap { "/projects/", "/ecosystem/", "/activity/", + "/privacy/", ...guideSections.map((s) => routes.section(s.id)), ...nodeApi.symbols.map((s) => routes.symbol(s.name)), ...projects.flatMap((p) => [ diff --git a/src/components/analytics.tsx b/src/components/analytics.tsx new file mode 100644 index 0000000..a56f4c5 --- /dev/null +++ b/src/components/analytics.tsx @@ -0,0 +1,22 @@ +"use client" + +import { GoogleAnalytics } from "@next/third-parties/google" + +import { CookieBanner } from "@/components/cookie-banner" +import { GA_ID, useConsent } from "@/lib/analytics-consent" + +/** + * Google Analytics 4, page views only. The Google tag is mounted after the + * visitor accepts; navigations between pages are measured by GA4 itself + * ("Page changes based on browser history events" in Enhanced measurement). + */ +export function Analytics() { + const consent = useConsent() + + if (!GA_ID) return null + if (consent === "granted") { + return + } + if (consent === "unset") return + return null +} diff --git a/src/components/cookie-banner.tsx b/src/components/cookie-banner.tsx new file mode 100644 index 0000000..d00ee87 --- /dev/null +++ b/src/components/cookie-banner.tsx @@ -0,0 +1,31 @@ +"use client" + +import Link from "next/link" + +import { Button } from "@/components/ui/button" +import { setConsent } from "@/lib/analytics-consent" + +/** A small, non-blocking notice: both choices have the same weight. */ +export function CookieBanner() { + return ( +
+

+ We use Google Analytics cookies to count page views, only if you agree.{" "} + + Privacy + +

+
+ + +
+
+ ) +} diff --git a/src/components/cookie-preferences.tsx b/src/components/cookie-preferences.tsx new file mode 100644 index 0000000..44908d6 --- /dev/null +++ b/src/components/cookie-preferences.tsx @@ -0,0 +1,14 @@ +"use client" + +import { GA_ID, revokeConsent } from "@/lib/analytics-consent" +import { cn } from "@/lib/utils" + +/** Withdraws the analytics choice and shows the banner again. */ +export function CookiePreferences({ className, label = "Cookie preferences" }: { className?: string; label?: string }) { + if (!GA_ID) return null + return ( + + ) +} diff --git a/src/components/site-footer.tsx b/src/components/site-footer.tsx index be2b0bc..6105543 100644 --- a/src/components/site-footer.tsx +++ b/src/components/site-footer.tsx @@ -1,5 +1,6 @@ import Link from "next/link" +import { CookiePreferences } from "@/components/cookie-preferences" import { Logo } from "@/components/logo" import { PROJECTS } from "@/config/projects" import { formatDate } from "@/lib/utils" @@ -70,9 +71,15 @@ export function SiteFooter({ generatedAt }: { generatedAt: string }) { © napi-bindings contributors. Code licensed under Apache-2.0. Node-API reference content © Node.js contributors, MIT license.

- - Edit this site on GitHub - +
+ + Privacy + + + + Edit this site on GitHub + +
diff --git a/src/lib/analytics-consent.ts b/src/lib/analytics-consent.ts new file mode 100644 index 0000000..fad7795 --- /dev/null +++ b/src/lib/analytics-consent.ts @@ -0,0 +1,110 @@ +import { useSyncExternalStore } from "react" + +/** + * Consent for Google Analytics. + * + * Google Analytics is loaded only after the visitor accepts (Google's "basic" + * consent mode): before that, no request reaches Google. The choice is kept in + * localStorage for six months, then the banner asks again. The measurement ID + * is inlined at build time; when it is empty (local development, pull request + * builds) there is neither banner nor tracking. + */ +export const GA_ID = process.env.NEXT_PUBLIC_GA_ID ?? "" + +/** `unknown` is the value during the static render, before the browser is read. */ +export type ConsentState = "unknown" | "unset" | "granted" | "denied" + +interface StoredConsent { + v: 1 + granted: boolean + at: number +} + +const STORAGE_KEY = "napi-bindings:analytics-consent" +const CHANGE_EVENT = "napi-bindings:analytics-consent-change" +const MAX_AGE_MS = 182 * 24 * 60 * 60 * 1000 + +/** Used when localStorage is unavailable, so the choice holds for the current page view. */ +let memoryConsent: StoredConsent | null = null + +function isStoredConsent(value: unknown): value is StoredConsent { + if (typeof value !== "object" || value === null) return false + const record = value as Record + return record.v === 1 && typeof record.granted === "boolean" && typeof record.at === "number" +} + +function toState(consent: StoredConsent | null): ConsentState { + if (!consent || Date.now() - consent.at > MAX_AGE_MS) return "unset" + return consent.granted ? "granted" : "denied" +} + +function readConsent(): ConsentState { + if (memoryConsent) return toState(memoryConsent) + try { + const raw = window.localStorage.getItem(STORAGE_KEY) + if (!raw) return "unset" + const parsed: unknown = JSON.parse(raw) + return isStoredConsent(parsed) ? toState(parsed) : "unset" + } catch { + return "unset" + } +} + +function subscribe(onChange: () => void): () => void { + const onStorage = (event: StorageEvent) => { + if (event.key === null || event.key === STORAGE_KEY) onChange() + } + window.addEventListener(CHANGE_EVENT, onChange) + window.addEventListener("storage", onStorage) + return () => { + window.removeEventListener(CHANGE_EVENT, onChange) + window.removeEventListener("storage", onStorage) + } +} + +export function useConsent(): ConsentState { + return useSyncExternalStore(subscribe, readConsent, () => "unknown") +} + +export function setConsent(granted: boolean): void { + const consent: StoredConsent = { v: 1, granted, at: Date.now() } + try { + window.localStorage.setItem(STORAGE_KEY, JSON.stringify(consent)) + memoryConsent = null + } catch { + memoryConsent = consent + } + window.dispatchEvent(new Event(CHANGE_EVENT)) +} + +/** Deletes `_ga` and `_ga_` on the current host and on every parent domain. */ +function deleteAnalyticsCookies(): void { + const names = document.cookie + .split(";") + .map((cookie) => cookie.split("=")[0]?.trim() ?? "") + .filter((name) => name === "_ga" || name.startsWith("_ga_")) + const labels = window.location.hostname.split(".") + const domains = [""] + for (let i = 0; i < labels.length - 1; i++) domains.push(`; domain=.${labels.slice(i).join(".")}`) + for (const name of names) { + for (const domain of domains) { + document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/${domain}` + } + } +} + +/** + * Withdraws the choice: stops Google Analytics, deletes its cookies and + * reloads, so the page comes back without the Google tag and with the banner. + */ +export function revokeConsent(): void { + if (GA_ID) (window as unknown as Record)[`ga-disable-${GA_ID}`] = true + deleteAnalyticsCookies() + memoryConsent = null + try { + window.localStorage.removeItem(STORAGE_KEY) + } catch { + // Nothing stored: the reload alone shows the banner again. + } + window.location.reload() +}