Skip to content
Install guide · Next.js / React

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

1

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.

2

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.

3

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.

4

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>.

<script src="https://www.complymo.com/embed/v1.js" data-project="cw_your_key" data-autoblock="1"></script>

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>
  );
}

Cookie consent and accessibility, free for 14 days

Paste one line of code today. The banner and the menu go live on your next page load.

14-day free trial · card needed to start, cancel before day 14 and you pay nothing