Prerequisites
- Next.js (App Router) project (
app/
directory). - Your CookieChimp Site ID (replace
YOUR_SITE_ID
below).
What You’ll Build
- Load the CookieChimp script first in
<head>
- Add the required trigger container
- Listen for consent and log if a service is allowed
Step 1 - Load CookieChimp in <head>
first
Use Next.js <Script>
with strategy="beforeInteractive"
so CookieChimp runs before hydration.
Replace
YOUR_SITE_ID
with your actual CookieChimp Site ID from your dashboard.Step 2 - Add a consent logger
If you need to take actions based on user consent (such as loading analytics scripts, tracking pixels, or other third-party services), this is where you’ll implement that logic. If you’re using Google Tag Manager, you can implement the GTM setup following our Google Tag Manager integration guide for the same approach. This client component:- Checks if a single service is allowed (e.g., “Google Analytics” in “analytics”)
- Logs the result on initial load and on user updates
Replace
"Google Analytics"
and "analytics"
with the exact service and category names you configured in CookieChimp.Step 3 - Use the logger on your page
Drop the logger onto any page (or mount once inapp/layout.tsx
if you prefer a global listener).
Verify
- Start your app and open the homepage.
- Open DevTools → Console.
- Interact with the Cookie banner (Accept / Deny / Update preferences).
-
You should see logs:
✅ Google Analytics allowed
❌ Google Analytics blocked
Notes & Tips
- Service vs Category: use
acceptedService("Service", "Category")
for granular control. - Already Consented Users: the logger runs once on mount to catch prior consent, and again on
cc:onConsented
/cc:onUpdate
. - Production: replace
console.log
with your real actions (e.g., loading pixels/SDKs) only after consent.
Pages Router?
If you’re on the Pages Router, place the<Script>
tag in pages/_document.tsx
within <Head>
, keep the <div id="cookiechimp-container" />
in pages/_app.tsx
, and use the same ConsentLogger
.