Skip to content

GA4 User-ID: Test Anonymous, Signed-In and Signed-Out States

Check GA4 User-ID before login, after login and after logout. Use a test account and inspect the tag’s data-layer value.

Three paper slots with a lilac tag in the centre and empty slots on both sides
Jack Lee
3 Min Read
Share
On This Page

Someone signs out of a training portal, then another learner signs in on the same browser. Verify that the first account’s GA4 User-ID is cleared between those actions. The test covers the page before login, the authenticated session and the first action after logout.

Set User-ID from the application’s login state

Application stateGoogle tag should receive
Fresh visit before any loginOmit user_id.
Signed inSet the account’s stable non-personally-identifying ID.
Signed out after loginSet JavaScript null to clear the prior value.

Google documents User-ID as a tag configuration value. Use a unique, persistent identifier from your account system. Keep emails and phone numbers out of the value and keep user identifiers out of custom dimensions.

Check the data layer before the tag fires

This example shows the value a Tag Manager implementation can queue. Call it from your application after it has resolved login state. The data layer must exist first and the signed-in ID must already be available to the application.

function queueUserId(state, analyticsId) {
  if (state === 'signed-in') {
    window.dataLayer.push({ user_id: analyticsId });
  } else if (state === 'signed-out') {
    window.dataLayer.push({ user_id: null });
  }
}

In our local test, a probe after login saw qa-account-17, the first probe after logout saw null, and login under a second test account used qa-account-82. Before login, the queue had no ID. A local script checked message order and simulated the data layer’s current value at each probe. It sent no Analytics requests.

  1. Start with a fresh test browser and use Tag Assistant to inspect the data layer and Google tag. Check which tag fired and what the data-layer variable resolved to. Use a test property for the follow-up event.
  2. Sign in and check the first event after authentication. In Tag Manager, map the Data Layer Variable to the Google tag’s configuration. On a page load where the user is already signed in, put the ID into the data layer before the container snippet so the initial page view can use it.
  3. Sign out and inspect the first event after the state change. Verify that the logout update came before the event and used actual JavaScript null. Check the fired tag for a stale value. Sign in with a second test account and check that its ID replaces the first.

Use DebugView to identify the follow-up event. Separately review cross-domain tracking if the issue is a split session between sites.

Frequently asked questions

Can I send the text “null” on logout?

Use actual JavaScript null. The text “null” is a string and can be interpreted as a user ID.

Should visitors who never signed in share a guest ID?

Leave User-ID unset when a visitor has never signed in. A shared guest ID could make those visits look like one account.

Does clearing User-ID end the GA4 session?

It clears the account identifier used by the tag. Session and device-based measurement have separate behavior under your tag settings.

Share This Article