PrivacyQuick

All documentation pages ↓

Install PrivacyQuick in Next.js

Load the banner in your root layout, wrap third-party scripts in a consent hook, add a cookie settings link and test. Works with the App Router and the Pages Router.

In a Next.js app the banner loads like any other script, but tracking code usually doesn't live in plain HTML tags. It's in components, next/script calls and third-party packages. So instead of tagging script elements, you ask the banner whether a category is allowed and render the script only then. This page is the short version; the full guide to cookie consent in React and Next.js covers embeds, server-side checks and edge cases.

What you need: a PrivacyQuick account (the Free plan is fine) and a Next.js 13.4+ app. Examples use TypeScript and the App Router; the Pages Router variant is shown where it differs.

1. Add your site and copy your site ID

In the dashboard, add the domain your app runs on. Copy the site ID from the snippet, the part that looks like pq_xxxxxxxxxx. The same site ID works on localhost and preview deployments, but page views and consent records from those environments count toward that site. If you test a lot, add your staging domain as a separate site and switch the ID with an environment variable.

2. Load the banner in the root layout

The Next.js documentation names consent managers as a use case for next/script's beforeInteractive strategy, which puts the script in the initial HTML ahead of your app code:

// app/layout.tsx
import Script from 'next/script'

export default function RootLayout({ children }: { children: React.ReactNode }) {
  return (
    <html lang="en">
      <body>
        {children}
        <Script
          src="https://cdn.privacyquick.com/v1/privacy.js"
          data-site-id="pq_xxxxxxxxxx"
          strategy="beforeInteractive"
        />
      </body>
    </html>
  )
}

Pages Router: put a plain tag in pages/_document.tsx instead:

<Head>
  <script src="https://cdn.privacyquick.com/v1/privacy.js" data-site-id="pq_xxxxxxxxxx" async />
</Head>

Load it once, in the root. The banner keeps working across client-side navigations, so don't add it to individual pages.

3. Gate scripts with a consent hook

This hook asks the banner whether a category is allowed, re-checks while the settings load, and updates when the visitor changes their mind. It uses only the public JavaScript API: PrivacyQuick.has() and the pq:consent event.

// app/consent/use-consent.ts
'use client'

import { useEffect, useState } from 'react'

export type ConsentCategory = 'analytics' | 'functional' | 'marketing'

declare global {
  interface Window {
    PrivacyQuick?: { open(): void; has(category: string): boolean }
  }
}

export function useConsent(category: ConsentCategory): boolean {
  // false on the server and on the first render, so nothing loads before we check
  const [allowed, setAllowed] = useState(false)

  useEffect(() => {
    const check = () => {
      const ok = window.PrivacyQuick?.has(category) ?? false
      setAllowed(ok)
      return ok
    }
    const onChoice = (e: Event) =>
      setAllowed((e as CustomEvent<{ categories: string[] }>).detail.categories.includes(category))

    check()
    let tries = 0
    const timer = window.setInterval(() => {
      if (check() || ++tries >= 10) window.clearInterval(timer)
    }, 500)
    document.addEventListener('pq:consent', onChoice)
    return () => {
      window.clearInterval(timer)
      document.removeEventListener('pq:consent', onChoice)
    }
  }, [category])

  return allowed
}

The polling covers visitors who are allowed a category without clicking anything (for example US visitors in opt-out mode, or a returning visitor), because pq:consent only fires when someone makes a choice.

Now render each third-party script only when its category is allowed:

// app/consent/analytics.tsx
'use client'

import Script from 'next/script'
import { useEffect, useRef } from 'react'
import { useConsent } from './use-consent'

export function Analytics() {
  const allowed = useConsent('analytics')
  const loaded = useRef(false)

  useEffect(() => {
    if (allowed) loaded.current = true
    else if (loaded.current) window.location.reload() // a script that ran can't be unloaded
  }, [allowed])

  if (!allowed) return null
  return <Script src="https://analytics.example.com/script.js" strategy="afterInteractive" />
}

Add <Analytics /> to the root layout. Do the same with 'marketing' for ad pixels and 'functional' for things like chat widgets.

4. Google tags

The banner sends Google Consent Mode v2 signals: everything denied before any other tag runs, then an update when the visitor chooses. You can either wrap the Google tag in the hook above (nothing from Google loads before consent), or load it for everyone, for example with <GoogleTagManager gtmId="GTM-XXXXXXX" /> from @next/third-parties/google, and let Consent Mode keep it cookieless until the visitor agrees. In the second case, check with Google Tag Assistant that the consent default is set before the first Google tag fires. Basic vs advanced Consent Mode explains the trade-off.

5. Add a "Cookie settings" link

// app/consent/cookie-settings-button.tsx
'use client'

export function CookieSettingsButton() {
  return (
    <button type="button" onClick={() => window.PrivacyQuick?.open()}>
      Cookie settings
    </button>
  )
}

Put it in your footer. The floating cookie button is on by default as well.

6. Test it

  1. Run a production build (next build && next start) or open your deployed site in a private window. The banner appears.
  2. In the Network tab, the gated scripts don't load before you choose.
  3. Click Accept all: they load without a page reload. Navigate between pages; nothing loads twice and the banner stays closed.
  4. Open the cookie settings and withdraw analytics: the page reloads and the script is gone.
  5. In the dashboard, the site shows Installed within a minute or two.

If you send a Content Security Policy, allow https://cdn.privacyquick.com in script-src and connect-src, and 'unsafe-inline' in style-src for the banner's styles.

Next steps

  • Run a cookie scan of your deployed site to catch tags you forgot.
  • Read Blocking scripts for plain HTML pages and iframes, such as a marketing site outside Next.js.
  • For other front-end frameworks, see our guides for Vue and Nuxt, Angular and SvelteKit.

Stuck? Email [email protected].