Ready-made integrations Google Analytics 4

Google Analytics 4

Markdown
Loading…

Load GA4 with one integration and let the SDK keep its Consent Mode signals up to date.

ga4() loads Google Analytics 4 and registers your measurement id. Consent reaches GA4 through Google Consent Mode: the SDK tells Google what the visitor allowed, on page load and on every change.

Set it up

First add the Consent Mode default, once, before any Google tag: Google Consent Mode v2 → Add the default.

Then install the integrations package and add ga4() to the initCookieYes() call you already have:

npm install @cookieyes/scripts
app/consent-manager.tsx
"use client";

import { initCookieYes } from "@cookieyes/nextjs";
import { ga4 } from "@cookieyes/scripts";

initCookieYes({
  mode: "cookie-only",
  regulation: "GDPR",
  integrations: [ga4({ measurementId: "G-XXXXXXX" })],
});

Send events with gtag("event", …) as usual. It is always safe to call: the Consent Mode default you added defines gtag and the dataLayer before anything else runs.

Options

OptionDefaultWhat it does
measurementIdrequiredYour GA4 id, "G-…"
category"analytics"The category the visitor must grant
consentMode"advanced""advanced" loads the tag at once and lets Consent Mode gate it. "basic" loads it only after consent and removes it, with the _ga cookies, on withdrawal. See Google Consent Mode v2 → Load the tag before or after consent
paramsnoneExtra gtag("config", …) settings, such as { send_page_view: false } when you track page views yourself, or { debug_mode: true } for DebugView
id"ga4"Only needed when you run more than one GA4 property

Common mistakes

Every event is counted twice. GA4 is also loaded by your Google Tag Manager container. Use googleTagManager() or ga4() for a property, not both. The SDK logs a warning when it sees both.

A returning visitor is treated as denied, although they accepted last time. The Consent Mode default is missing, so the update sent on page load was skipped and GA4 stays denied until the visitor changes their choice. Add the default as shown on Google Consent Mode v2 → Add the default.

Next steps

On this page