WebmasterID on Next.js
WebmasterID installs on Next.js with the built-in next/script component — no special SDK required. This guide covers App Router and Pages Router placement and a consent-safe setup.
Recommended: next/script
Add the tracker once in app/layout.tsx (App Router) or pages/_app.tsx (Pages Router) so it loads once per route.
import Script from "next/script";
<Script
id="webmasterid-tracker"
src="https://webmasterid.com/tracker.iife.min.js"
strategy="afterInteractive"
data-wmid="wm_xxxxxxxxxxxxxxxx"
data-endpoint="https://webmasterid-ingest-api.vercel.app/api/events"
/>Consent-safe placement
For GDPR/ePrivacy, load the tracker only after analytics consent. The pattern below is the same consent-gated approach documented in Privacy & GDPR — inject it from your consent manager when the analytics category is granted, not unconditionally.
Render the <Script> only after analytics consent — for example from a consent-provider effect — rather than unconditionally in the layout.
Common mistakes
- Placing the Script inside a component that renders on every navigation (causes duplicates) — keep it in the layout/_app once.
- Loading before consent in EU/EEA — gate it.
- Missing
connect-srcfor the ingest endpoint in a strict CSP.
See the canonical WebmasterID Next.js doc for the optional component package (only if available in your project).
A consent-gated component (App Router)
Wrap the script in a small client component that renders it only once analytics consent has been granted, then place that component once in app/layout.tsx:
'use client';
import Script from "next/script";
import { useConsent } from "./consent"; // your consent provider
export function WebmasterIDScript() {
const { analytics } = useConsent();
if (!analytics) return null;
return (
<Script
id="webmasterid-tracker"
src="https://webmasterid.com/tracker.iife.min.js"
strategy="afterInteractive"
data-wmid="wm_xxxxxxxxxxxxxxxx"
data-endpoint="https://webmasterid-ingest-api.vercel.app/api/events"
/>
);
}Because the component lives in the root layout, it is mounted once and is not re-created when visitors move between pages. In the Pages Router, render the same component in pages/_app.tsx.
Content Security Policy
If next.config.js headers or middleware set a CSP, add https://webmasterid.com to script-src and the ingest endpoint to connect-src. With a nonce-based policy, pass the request nonce to the <Script> through its nonce prop, or the browser will block it.
Static export and hosting
The tracker runs entirely in the browser, so it works the same with output: "export", on a Node.js server or on any host. On Vercel, see the Vercel integration for platform-specific notes.
Check that it works
- Run a production build (
next buildandnext start); development mode can render components twice. - Accept analytics and confirm in the Network tab that
tracker.iife.min.jsloads once. - Decline analytics in a new private window and confirm that nothing loads.
FAQ
Do I need an SDK?
No. The universally-supported method is next/script. An optional package exists but is not required.
App Router or Pages Router?
Both work — add the Script once in the root layout or _app so it loads once per route.
Is it consent-gated?
It should be. Render or inject it only after the analytics consent category is granted.
Does it work with static export?
Yes. The tracker is client-side, so a site built with output: "export" loads it exactly like a server-rendered one.
Why do I see two script loads in development?
React Strict Mode renders components twice in development. Check a production build before assuming a duplicate.
Related
Related tools
Owned, lightweight,
privacy-conscious
WebmasterID is designed for website visibility you control. Explore the product, docs, and integrations.