This is a minimal example of Google Analytics (GA4) in Next.JS 14 with an opt-in cookie banner. You can see it at the bottom of the screen :)
- Install the node modules with
npm install
. - Replace the Google Analytics ID
GA_MEASUREMENT_ID
in thelayout.tsx
file with your own. - Run the development server with
npm run dev
.
- The cookie key (i.e., name) is
cookie_consent
and the value is set totrue
orfalse
when you click Accept or Decline, respectively. - The cookie is stored in local storage. If you Accept or Decline the Cookie, you'll have to delete it from local storage to reset the banner.