tracker.identifyUser() / clearUser()
Set or clear the GA4 user_id so subsequent GA4 events tie back to a specific user across sessions and devices.
Signatures
Section titled “Signatures”tracker.identifyUser(userId: string): void;tracker.clearUser(): void;Behavior
Section titled “Behavior”Both methods push a gtag('config', ga4MeasurementId, { user_id, send_page_view: false }) call. The send_page_view: false flag is required — without it gtag fires a page_view every time config is called, double-counting.
identifyUser(userId)setsuser_idto the given string.clearUser()setsuser_idtoundefined(typically called on logout).
Both are no-ops if ga4MeasurementId was not configured on createBrowserTracker. Under debug: true, the no-op debug-warns.
The user_id value also lands in any envelope you exportContext afterwards.
Example
Section titled “Example”'use client';import { tracker } from '@/lib/tracker.client';
async function onLogin(user: User) { tracker.identifyUser(user.id); // Subsequent trackEvent / trackPurchase calls carry the user_id.}
function onLogout() { tracker.clearUser();}For sign-up specifically, call identifyUser before trackSignUp so the sign_up event itself carries the new user_id:
const user = await api.signUp(/* ... */);tracker.identifyUser(user.id);await tracker.trackSignUp({ transactionId: user.id, method: 'email', userData: { email: user.email } });See also
Section titled “See also”tracker.trackSignUp()— common pairing.tracker.exportContext()—userIdis included in the envelope.