tracker.getClientId() / getSessionId() / getConsent()
Three read-only accessors for the SDK’s view of the user’s identity and consent state. All three are synchronous and never throw.
tracker.getClientId()
Section titled “tracker.getClientId()”tracker.getClientId(): string | undefined;Returns the canonical GA4 client ID — the substring after GA<v>.<count>. in the _ga cookie, e.g. '1234567890.1700000000'. Returns undefined if the _ga cookie is missing or malformed.
The server tracker requires clientId on trackEvent and on the helpers (trackPurchase, etc.) to stitch GA4 sessions across browser and server. Read it here on the browser, forward it with your order data, and pass it to serverTracker.trackEvent({ clientId, ... }) on the server.
A fresh page that has not yet fired any GA4 hit may return undefined because gtag hasn’t set the cookie yet — getClientId does not await gtag init.
tracker.getSessionId()
Section titled “tracker.getSessionId()”tracker.getSessionId(): string | undefined;Returns the GA4 session ID — the third dot-segment of the _ga_<containerId> cookie. Returns undefined if ga4MeasurementId was not configured at init, the cookie is absent, or the value is malformed.
Use this when you need to attach session_id to server-side GA4 events for session continuity beyond what clientId alone gives you.
tracker.getConsent()
Section titled “tracker.getConsent()”tracker.getConsent(): ConsentState;Returns a defensive copy of the SDK’s view of consent across all four signals:
type ConsentState = { ad_storage: 'granted' | 'denied' | 'unknown'; ad_user_data: 'granted' | 'denied' | 'unknown'; ad_personalization: 'granted' | 'denied' | 'unknown'; analytics_storage: 'granted' | 'denied' | 'unknown';};Under consentMode: 'off', all four start as 'granted'. Under consentMode: 'v2', all four start as 'unknown' until your CMP calls tracker.updateConsent({ ... }).
The returned object is a fresh copy — mutating it does not affect tracker state. The round-trip tracker.updateConsent(tracker.getConsent()) typechecks because ConsentUpdate includes 'unknown' as a valid value.
Example — forward identity to the server
Section titled “Example — forward identity to the server”'use client';import { tracker } from '@/lib/tracker.client';
async function placeOrder(cart: Cart) { const clientId = tracker.getClientId(); const sessionId = tracker.getSessionId(); const clickIds = tracker.getClickIdentifiers();
await fetch('/api/orders', { method: 'POST', body: JSON.stringify({ cart, clientId, sessionId, clickIds }), });}For the more common case — capturing all of this plus consent and (optional) PII in one call — use exportContext.
See also
Section titled “See also”tracker.exportContext()— packages all of these into a single envelope.tracker.updateConsent()— the writable counterpart ofgetConsent.tracker.getClickIdentifiers()— the click-ID accessor.