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_IDin thelayout.tsxfile with your own. - Run the development server with
npm run dev.
- The cookie key (i.e., name) is
cookie_consentand the value is set totrueorfalsewhen 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.
