Add a cookie banner & accessibility to Next.js
One <Script> in your root layout, app/layout.tsx. On another React setup (Vite, Create React App), the plain tag goes first in index.html’s <head>.
Installation steps
Add the snippet to your root layout
App Router: in app/layout.tsx, import Script from "next/script" and put the snippet inside <head>. Pages Router: the same in pages/_document.tsx.
Use strategy="beforeInteractive"
beforeInteractive makes Next.js load it before your other scripts without blocking first paint. Keep it the first script in <head>, above any analytics.
Hold Google Analytics and the Meta Pixel
Don’t add Google Analytics or the Meta Pixel with next/script (it preloads their files on every page, before consent and after a "no", whatever type you give it) or as plain async <script> tags (React moves those above the ComplyMo snippet, so they start before the widget and auto-block can’t stop them). Write them as raw <script type="text/plain" data-cookieconsent> tags instead.
Not on Next.js?
For another React setup (Vite, Create React App), paste the plain HTML snippet as the first tag in index.html’s <head>.
Copy your snippet from Install in your dashboard (Home, or a site's Widget tab) and put it first in the <head>, before any analytics tag.
app/layout.tsx
The ComplyMo <Script> first in <head>, then your Google and Meta tags as raw <script type="text/plain"> tags with their category. Nothing is fetched from Google or Meta until its category is allowed: by the visitor's choice, or by default for analytics in opt-out regions.
Content-Security-Policy with a nonce, tracker alerts and the JavaScript API are in the Custom code guide.
import Script from "next/script";
export default function RootLayout({ children }: { children: React.ReactNode }) {
return (
<html lang="en">
<head>
<Script src="https://www.complymo.com/embed/v1.js" data-project="cw_your_id"
data-autoblock="1" strategy="beforeInteractive" />
{/* Raw tags on purpose: next/script preloads its src even with type="text/plain". */}
<script type="text/plain" data-cookieconsent="analytics" async
src="https://www.googletagmanager.com/gtag/js?id=G-XXXX" />
<script type="text/plain" data-cookieconsent="analytics"
dangerouslySetInnerHTML={{ __html: `window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config','G-XXXX');` }} />
<script type="text/plain" data-cookieconsent="marketing"
dangerouslySetInnerHTML={{ __html: `/* your Meta Pixel base code */` }} />
</head>
<body>{children}</body>
</html>
);
}Hold them until consent
Write Google Analytics and the Meta Pixel as raw <script> tags in app/layout.tsx with type="text/plain" and data-cookieconsent="analytics" or "marketing". The widget runs them after the visitor agrees; nothing is fetched before that.
Or keep them in a client component that renders them only after consent: window.CookieConsent.onChange(fn), or the cw:consent event.
// app/layout.tsx, inside <head>, after the ComplyMo <Script>.
// Raw <script> tags on purpose: next/script preloads its src even with type="text/plain".
<script type="text/plain" data-cookieconsent="analytics" async
src="https://www.googletagmanager.com/gtag/js?id=G-XXXX" />
<script type="text/plain" data-cookieconsent="analytics"
dangerouslySetInnerHTML={{ __html: `window.dataLayer=window.dataLayer||[];function gtag(){dataLayer.push(arguments);}gtag('js',new Date());gtag('config','G-XXXX');` }} />
<script type="text/plain" data-cookieconsent="marketing"
dangerouslySetInnerHTML={{ __html: `/* your Meta Pixel base code */` }} />
