Skip to content

tracker.identifyUser() / clearUser()

Set or clear the GA4 user_id so subsequent GA4 events tie back to a specific user across sessions and devices.

tracker.identifyUser(userId: string): void;
tracker.clearUser(): void;

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) sets user_id to the given string.
  • clearUser() sets user_id to undefined (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.

'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 } });