# Next.js cookie consent: what runs before your banner

Where you load Google's consent default in a Next.js App Router layout decides if it runs first. Of 8 ways we tested, 4 let Analytics set cookies first.

_Shaun Brown · October 21, 2026 · https://tagsentry.ai/blog/nextjs-cookie-consent_

> **The short answer:** Google's consent default has to run before any Google tag, and in a Next.js App Router site, where you load it decides whether it does. In our test of 8 loading patterns, a plain inline `<script>` first in the root layout's `<head>` ran first every time. A default loaded with `next/script`'s default strategy, a banner component that renders late or loads lazily, and `beforeInteractive` up against a Google tag pasted in the head all ran too late, and Google Analytics set its cookies before anyone clicked.

> **Not legal advice** This guide describes how Next.js loads scripts and what we measured in a test app. What your site needs depends on where your visitors are. Ask a lawyer if you need a definite answer.

## What has to run first

Google's Consent Mode is a set of four signals (analytics storage, ad storage, ad user data and ad personalisation) that tell Google's tags what a visitor has agreed to. Before a visitor chooses, your site sets a default, usually all denied. When they choose, your banner sends an update.

The default only counts if it comes first. Google says to set it "before any commands that send measurement data", and adds: "The order of the code here is vital."

That splits a Next.js cookie banner in two. The banner you see can render whenever it likes, but the default behind it has to run before any Google tag. If the terms are new to you, [Google Consent Mode v2, explained](/blog/google-consent-mode-v2-explained) covers each signal.

## How Next.js loads scripts

`next/script` has three strategies that matter here. `afterInteractive` is the default and loads "after some (or all) hydration occurs". `lazyOnload` waits for idle time. `beforeInteractive` is meant to "load before any Next.js code and before any page hydration occurs", and the Next.js docs list "cookie consent managers" as an example.

The `GoogleAnalytics` and `GoogleTagManager` components from `@next/third-parties` load their scripts "after hydration occurs on the page", per the same docs. The package is still marked experimental.

None of this tells you what runs first on a real page, so we built one and measured it.

## What we measured

We built a Next.js 16.3.1 App Router app with one root layout per pattern (8 ways to load the default, plus one with no default), ran `next build` and `next start`, and loaded each page 5 times in a fresh headless Chrome.

We then ran 8 of them again with the consent file (which two patterns load as a `src`) slowed by 800 ms, as if it came from a slow CDN.

That's 85 page loads, and every pattern gave the same result each time.

For each load we logged the order of everything pushed to `dataLayer` and read the first hit Google Analytics sent. A hit marked `gcs=G100` carried a denied state. A hit with no `gcs` went out with no consent state at all.

| How the default was loaded | Ran before Analytics? | `_ga` cookie before a click |
|---|---|---|
| Plain inline `<script>` in the root layout's `<head>` | Yes | No |
| `next/script`, `beforeInteractive`, inline | Yes | No |
| `next/script`, `beforeInteractive`, a `src` file (also slowed) | Yes | No |
| A banner component's `useEffect`, rendered above Analytics | Yes | No |
| `next/script` with no strategy (so `afterInteractive`), placed above Analytics | No | Yes |
| The same banner component, rendered below Analytics | No | Yes |
| The same banner component, loaded with `next/dynamic` and `ssr: false` | No | Yes |
| `beforeInteractive`, with Google's gtag snippet pasted as plain scripts in `<head>` | No | Yes |
| No default at all (for comparison) | None set | Yes |

Analytics came from `@next/third-parties` except in the eighth row. The `useEffect` result is the fragile one: the same component worked or failed depending only on where it sat in the layout.

## Why `beforeInteractive` isn't first

In the App Router, `beforeInteractive` doesn't put a script in `<head>`. The served HTML has a line like `self.__next_s.push(...)` in the body, and Next's own runtime runs the script once it starts.

That's early enough to beat `@next/third-parties`, which waits for hydration. A plain `<script>` in `<head>` runs as soon as the browser reads it, which is sooner. So if anything else on the page loads Google's tag the plain way, a `beforeInteractive` default arrives after it.

## A setup that works

Put the default in a plain inline script as the first thing in the root layout's `<head>`, and load Google's tag after it. This is the first row of our test, with one setting added:

```tsx
// app/layout.tsx

const consentDefault = `
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('consent', 'default', {
    ad_storage: 'denied',
    ad_user_data: 'denied',
    ad_personalization: 'denied',
    analytics_storage: 'denied',
    wait_for_update: 500
  });
`

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <head>
        <script dangerouslySetInnerHTML={{ __html: consentDefault }} />
      </head>
      <body>{children}</body>
      <GoogleAnalytics gaId="G-XXXXXXX" />
    </html>
  )
}
```

That setting, `wait_for_update`, tells Google's tags how many milliseconds to wait for an update before they send data, so a banner that loads a moment later can still pass on a choice made on an earlier visit.

Your banner can then be an ordinary client component. When the visitor chooses, it calls `gtag('consent', 'update', {...})` with the signals they agreed to. With Tag Manager the rule is the same: the default script first, then `<GoogleTagManager>` or Google's snippet.

A banner tool that asks you to install it with `beforeInteractive` will usually work if all your Google tags come through `@next/third-parties`. If any Google tag sits in `<head>` as a plain script, check the order with the steps below.

## Check your own Next.js site

1. Open your live site in a private window, from somewhere your banner shows, and don't click it.
2. Open developer tools (F12) › **Console** and run `dataLayer.map(e => e[0] ? e[0] + ' ' + e[1] : e.event)`.
3. `consent default` should come before `js` and `config` (or `gtm.js` with Tag Manager). If it comes after, the default was too late.
4. Look under **Application** › **Cookies**. A `_ga` cookie before you've clicked anything means Google Analytics ran without a choice.

[How to check Consent Mode v2 is working](/blog/check-consent-mode-v2-working) goes further, with the `gcs` and `gcd` values Google receives, and [the GDPR cookie banner guide](/blog/gdpr-cookie-banner) covers what the banner itself needs.

## Where TagSentry fits

Run `npx @tagsentry/cli` in your project folder. It signs you in through the browser, detects Next.js and writes a `<TagSentry />` component as the first thing in `<head>` in `app/layout.tsx` (or `pages/_document.tsx` on the Pages Router). It's plain inline scripts, the pattern that won our test. `npx @tagsentry/cli undo` puts your files back.

Before any tag runs, it sets every Consent Mode signal to denied, in the page itself. If our CDN can't be reached, Google's signals stay denied. Where visitors can opt out, it switches tracking on moments after the page loads, unless they opted out.

Our Install screen reads your homepage and names any Google tag that loads before our line, such as Google Analytics. Next.js steps are in the [install guide](/docs/install-by-platform), and [how consent works](/consent) covers the rules each place's law asks for. Consent is free up to 10,000 page views a month.

## Sources

- [Google for Developers: Set up consent mode on websites](https://developers.google.com/tag-platform/security/guides/consent)
- [Google for Developers: Consent mode overview](https://developers.google.com/tag-platform/security/concepts/consent-mode)
- [Next.js docs: Script component](https://nextjs.org/docs/app/api-reference/components/script)
- [Next.js docs: Third-party libraries (@next/third-parties)](https://nextjs.org/docs/app/guides/third-party-libraries)
- [Simo Ahava: Consent Mode V2 for Google tags (gcs and gcd decoding)](https://www.simoahava.com/analytics/consent-mode-v2-google-tags/)
