Manage Cookie Consent in Next.js 13 with Google Tag Manager
A guide on how to get Cookie Consent from the user in a Next.js 13 project using App router with server component and handle it with the Google Tag Manager built-in consent overview.

Note: if you are looking to implement this method in Next.js Page Router, check this tutorial https://javascript.plainenglish.io/manage-cookie-consent-in-next-js-with-google-tag-manager-4d58818266ea
Most websites today need a Cookie Consent System to comply with GDPR (and other) European regulation. Basically, this means that you can’t store cookies on a user’s computer if they don’t give you permission to do so. And you can’t allow other services that are active on your site (like Analytics or Advertising) to store cookies on your domain.
In this article, I will show how to get Cookie Consent from the user in a Next.js 13 project and handle it with the Google Tag Manager built-in consent overview.
Prerequisites
To follow along with this project you need:
- A Google Tag Manager account and its container with enabled consent overview (I’ll show you how to enable it)
- A service that a set cookie implemented using Google Tag Manager (we will use Google Analytics because most Google services have automatic consent checks, but other services can be implemented easily)
- A basic Next.js 13 project using App router where we will implement the banner for cookie consent
- A basic knowledge of Next.js 13, App router, server and client components
Limits
We will build a basic example where the user can accept or refuse the cookie with no granularity or choice about which cookies to accept and which to refuse. But it will be an easy task to extend this example to a more complete solution.
How will it work
Our Next.js 13 project will include in the pages (using layout.js) a Tag Manager Container, which will be the entry point for all our monitoring codes (analytics, advertising, remarketing…), Tag Manager will “run” the codes based on the user consent, so we will make a banner to let the user giving us (or denying) consent. Consent (consents to be precise, since there are more of it) is persisted and sent to Tag Manager on every page request so that tags are fired correctly and cookies are set based on user choice.
As we said there are more consents and more cookie types, Tag Manager will support:
- ad_storage (cookies that will track for advertising)
- analytics_storage (cookies tracking statistics)
- functionality_storage (cookies that store data for site use, like the language)
- personalization_storage (cookies that customize the user experience on the site based on their preferences)
- security_storage (cookies that store login information, keys, and so on)
There are also necessary cookies that are enabled by default, which are required to have the site working (for example cart functionality in e-commerce)
Google services (like Analytics and Ads) have the consent check built-in, for other services you need to specify the needed consent to work (more on this later)
For this example, we will implement Google Analytics with built-in consent.
Setting up Tag Manager
Login to your Google Tag Manager account and create a new account and a new container, give it a name and choose Web as Target platform.

Enable consent overview in Tag Manager heading to Admin -> Container Setting:

Create a new Tag to handle Google Analytics (you need a Google Analytics GA4 active account and its Measurement ID): go to Tags, click the New button, click Tag Configuration and add the Google Analytics: GA4 Configuration tag type. Configure it by adding the Measurement ID (you can get it from Google Analytics configuration), then scroll down to Consent Settings, and choose No additional consent required. As you can see GA4 got a built-in consent check enabled for ad_storage and analytics_storage. This means that you need to give Tag Manager these two consents to enable cookie storage and tracking from Google Analytics.

Then you need to configure the Firing Trigger, for Analytics to be fired on every page in Next.js you need to trigger both All Pages and on History Changes. Click Choose a trigger to make this tag fire… Select All Pages, then click the + button to add another trigger, and click the + sign again to create a custom trigger. Name it Page change in Nextjs click Choose a trigger type to begin setup… choose History Change and save it. In the end, your Tag configuration should look like this:

Do not forget to submit the Workspace to publish it.
Create a new Next.js 13 project
We can now create a new Next.js 13 project and install the dependencies we need, we will use cookies-next to handle cookies in client component.
Create the new project:
npx create-next-app@latestWhen requested give it the project a name, add the TailwindCSS support and App Router support.
Add the cookies-next module:
npm install --save cookies-nextAt this point, if you run the app with npm run devand head to

Add Tag Manager Code to the project
First of all, we need to add the Tag Manager Code to our project; to do so, we will use the next/script API (https://nextjs.org/docs/api-reference/next/script). Grab the JavaScript code (you need to get the one which goes in the head) from Tag Manager Admin -> Install Google Tag Manager:

The copied code will be added to our layout.jsso that it is executed on every page of our project:












