Track users with GA4
Set user_id for signed-in users with gtag.js or Google Tag Manager, send a sign_up event, then export GA4 events to BigQuery for Roadway to read.
Set the user ID with gtag.js
Use this section if Google Analytics 4 is installed directly on your site with gtag.js.
If you use Google Tag Manager, follow Set the user ID with Google Tag Manager instead.
GA4 already assigns every browser an anonymous ID (user_pseudo_id). Setting user_id adds your own ID to every event for signed-in users, which lets Roadway tie visits to your Users table.
You need:
- A GA4 property with a web data stream and the Google tag on your site.
- Access to your site's source code.
- The Editor role or higher on the GA4 account.
- A user ID that isn't PII, such as your database primary key.
Don't register
user_idas a custom dimension or user property. Google treatsuser_idas a reserved system parameter. Set it only as a configuration orsetparameter. Don't pass it inuser_properties, and don't send it as an event parameter on individual events.
Rules for the value
| User state | What to send |
|---|---|
| Never signed in | Don't send user_id |
| Signed in | The user's ID |
| Signed in, then signed out | null (the JavaScript value, not the string "null", an empty string, or a blank string) |
Set the user ID
-
Find where your page knows the signed-in user. Often this is a server-rendered variable or a call to your session endpoint.
-
Call
gtag('set', { user_id })on every page load for signed-in users, before the first event you want tied to the user. If your page loads the Google tag inline, place it beforegtag('config', ...):<script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXXXXXX"></script> <script> window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); // Set the user ID only when the visitor is signed in. var signedInUserId = window.APP_USER_ID; // your value, for example "usr_8f2c" if (signedInUserId) { gtag('set', { user_id: signedInUserId }); } gtag('config', 'G-XXXXXXXXXX'); </script> -
When a user signs in without a page reload, such as in a single-page app, set the ID at that moment:
function handleUserLogin(userId) { gtag('set', { user_id: userId }); gtag('event', 'login', { method: 'email' }); } -
When a user signs out, clear it:
function handleUserLogout() { gtag('set', { user_id: null }); gtag('event', 'logout'); } -
Send a
sign_upevent after callinggtag('set', { user_id }), so the sign-up event carries the ID.
Google's current guide recommends
gtag('set')overgtag('config')for settinguser_idafter initialization. Existingconfig-based implementations keep working.
Verify
-
Sign in to your site as a test user.
-
Open your browser's developer tools, go to Network, and filter for
collect. -
Reload a page. In the request to
google-analytics.com/g/collect, confirm the query string includesuid=<your user ID>. -
In GA4, open Admin > DebugView and confirm events for your test session appear.
-
After a day, open Explore > User Explorer and look for your test user under Effective user ID. Your property's Reporting Identity must be set to Blended or Observed for this to show.
-
Once the BigQuery export is on, run:
SELECT user_pseudo_id, user_id, event_name, TIMESTAMP_MICROS(event_timestamp) AS ts FROM `your_project.analytics_PROPERTY_ID.events_*` WHERE user_id = 'usr_8f2c' ORDER BY ts DESC LIMIT 20;Rows should have both
user_pseudo_idanduser_id. See GA4 BigQuery export.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
No uid in the collect request |
set ran after the event, or the user isn't signed in on that page |
Call set before the first event on every page |
| Different ID formats between GA4 and your database | Value transformed in the frontend | Send the exact database value as a string |
GA4 reports show too many (other) rows |
user_id registered as a custom dimension |
Remove the custom dimension. Don't register user_id |
| User ID is an email address | PII | Use a surrogate ID. Google's policies prohibit PII. |
Vendor docs: Send user IDs (Google), Google tag reference
Set the user ID with Google Tag Manager
Use this section if you deploy Google Analytics 4 through Google Tag Manager (GTM).
If GA4 is installed directly with gtag.js, see Set the user ID with gtag.js.
You'll push the signed-in user's ID to the data layer, read it with a Data Layer Variable, and add it to your Google tag's configuration settings.
You need:
- A GTM web container installed on your site and a GA4 Google tag (formerly the GA4 Configuration tag) that fires on Initialization - All Pages.
- Edit access to the container and Editor access to the GA4 property.
- A user ID that isn't PII.
Set
user_idas a configuration setting only. Don't add it as an event parameter or to the User Properties section of a tag, and don't register it as a custom dimension.
Steps
-
Push the user ID to the data layer before the GTM snippet. On pages where the visitor is signed in, add this above the container snippet:
<script> window.dataLayer = window.dataLayer || []; window.dataLayer.push({ user_id: 'usr_8f2c' }); // render your real value server-side </script> <!-- Google Tag Manager snippet goes here -->Don't push
user_idfor visitors who have never signed in. -
Create a Data Layer Variable. In GTM, go to Variables > User-Defined Variables > New. Choose Data Layer Variable, set Data Layer Variable Name to
user_id, set Data Layer Version to Version 2, and name itDLV - user_id. -
Add the variable to your Google tag. Open your Google tag, expand Configuration settings, click Add parameter, set Parameter Name to
user_idand Value to{{DLV - user_id}}. Save.If you still use the older GA4 Configuration tag, add
user_idunder Fields to Set. -
Handle logout. When a user signs out and the page doesn't reload, push
{ user_id: null }to the data layer:window.dataLayer.push({ user_id: null }); -
Handle login without a page reload. If users sign in without a reload, such as in a single-page app, the Google tag has already fired by the time the ID is known. Push the ID and a custom event, then re-fire a Google tag on that event, and verify in Preview mode that the later events carry
uid.window.dataLayer.push({ user_id: 'usr_8f2c', event: 'user_login' }); -
Test in Preview. Click Preview in GTM, load your site while signed in, and confirm the Google tag fired and
DLV - user_idshows the expected value in the Variables tab. -
Publish the container.
Verify
- In your browser's developer tools, open Network, filter for
collect, and confirm requests includeuid=<your user ID>. - Open Admin > DebugView in GA4 while Preview mode is on.
- After export, query
user_idin BigQuery. See GA4 BigQuery export.
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
Variable is undefined in Preview |
dataLayer.push happens after the container loads |
Push before the GTM snippet, or fire the Google tag on a later event |
uid missing on the first page view after login |
Login redirect doesn't re-render the data layer | Render the push on every authenticated page |
| User ID visible as an event parameter | user_id added to an event tag |
Remove it from event tags. Keep it in Configuration settings |
Vendor docs: Configure Google Analytics 4 in Tag Manager, Send user IDs (Google), Tag Manager data layer
Send a sign_up event
When GA4 is your users source, Roadway treats the sign_up event as the moment a visitor became a user.
By default Roadway looks for an event named sign_up. If you use a different name, tell Roadway the name you use.
You need:
- User ID is set in GA4 (or with GTM).
- You can run code at the moment sign-up succeeds, in the browser or on the server.
How Roadway uses the event
- The event timestamp becomes
signed_up_at. - If a user has several
sign_upevents, Roadway keeps the earliest. - The event must be tied to a user ID.
Roadway reads the top-level user_id field of the GA4 BigQuery export. Set User-ID through gtag('config'/'set', { user_id }) or your GTM Google tag. A user_id event parameter is not read. Set it on page views too, not only on the sign-up event. Sign-up rows with no user_id are dropped.
Send the event in the browser
-
Set the user ID first, then send the event. Both calls go in the same success handler:
function onSignUpSuccess(user) { gtag('set', { user_id: user.id }); // for example "usr_8f2c" gtag('event', 'sign_up', { method: 'email' }); }methodis optional. Use it to record how the person signed up, for exampleEmail,Google, orGitHub. -
With Google Tag Manager, push a data layer event after sign-up succeeds and create a Google Analytics: GA4 Event tag named
sign_upthat fires on it:window.dataLayer.push({ user_id: user.id, event: 'sign_up', method: 'email' });Your Google tag must read
user_idthrough its configuration settings. See Set user ID in GA4 with Google Tag Manager.
Send the event from your server
If sign-up is confirmed on your backend, such as after email verification, use the GA4 Measurement Protocol. Include your user_id (top-level in the payload). Include client_id too, for GA4's own reports.
Verify
-
Complete a test sign-up.
-
In GA4 DebugView, confirm a
sign_upevent appears. -
In the BigQuery export, confirm one row for the test user:
SELECT user_pseudo_id, user_id, event_name, TIMESTAMP_MICROS(event_timestamp) AS ts FROM `your_project.analytics_PROPERTY_ID.events_*` WHERE event_name = 'sign_up' AND user_id = 'usr_8f2c';
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
sign_up rows have empty user_id |
set ran after the event |
Call set first |
| Duplicate sign-ups per user | Event fires on every login | Fire it only when the account is created. Roadway keeps the earliest, but duplicates add noise |
Event name is Sign up or signup |
Non-standard name | Use sign_up, or tell Roadway your event name |
Vendor docs: Recommended events (Google), Event reference
Set up the BigQuery export
If you use GA4 as your sessions source in a warehouse-native implementation, Roadway reads page_view and sign_up events from the GA4 BigQuery export.
Link GA4 to BigQuery, then see Connect BigQuery to give Roadway access.
You need:
- A Google Cloud project with billing set up. The BigQuery sandbox works, with sandbox limits.
- The Editor role or higher on the GA4 property.
- Owner access to the Google Cloud project, using the same Google account.
- BigQuery API enabled on the project.
Steps
- In GA4, click Admin (the gear icon).
- Under Product links, click BigQuery links.
- Click Link.
- Click Choose a BigQuery project and select your project.
- Select a data location. Pick the region where the rest of your warehouse data lives. You can't change it later without deleting the link.
- Select the data streams to export. Include your web stream.
- Choose the export frequency: Daily, Streaming, or both. Daily exports one table per day. Streaming exports continuously.
- Review the options and click Submit.
Data begins flowing within 24 hours. The export starts from when you create the link, so plan for history to begin on that date.
What you get
GA4 creates a dataset named analytics_<PROPERTY_ID> in your project, with these tables:
| Table | Contents |
|---|---|
events_YYYYMMDD |
One table per day of complete events (daily export) |
events_intraday_YYYYMMDD |
Events as they arrive during the current day (streaming export). Deleted when the daily table completes. |
Fields Roadway cares about:
| Field | Meaning | Roadway column |
|---|---|---|
user_pseudo_id |
GA4's anonymous ID | visitor_id |
user_id |
Your user ID, when set | user_id |
event_timestamp |
Microseconds since epoch (UTC) | page_viewed_at |
event_name |
page_view, sign_up, and others |
n/a |
event_params (page_location, page_referrer) |
Landing page URL and referrer | landing_page_url, referring_page_url |
geo.city, geo.region, geo.country, geo.continent |
Location | city, region, country, continent |
Limits to know
- Standard GA4 properties have a daily export limit of 1 million events. If you consistently exceed it, Google pauses the daily export and doesn't reprocess earlier days. Use data-stream and event exclusion filters to reduce volume. Streaming export has no event limit.
- Property editors and administrators get an email when a property approaches or exceeds the limit.
- The link adds a Google-managed identity to your project's IAM policy with the
BigQuery Userrole. Don't remove it.
Verify
-
After 24 hours, in the BigQuery console, confirm that the dataset
analytics_<PROPERTY_ID>exists. -
Run a count of recent page views:
SELECT event_date, COUNT(*) AS page_views, COUNT(DISTINCT user_pseudo_id) AS visitors, COUNTIF(user_id IS NOT NULL) AS identified_page_views FROM `your_project.analytics_PROPERTY_ID.events_*` WHERE event_name = 'page_view' AND _TABLE_SUFFIX >= FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY)) GROUP BY event_date ORDER BY event_date DESC; -
Confirm
identified_page_viewsis greater than zero. If it's zero,user_idisn't being set. See Set user ID with gtag.js. -
Confirm
page_locationincludes query parameters on landing pages:SELECT (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS page_location FROM `your_project.analytics_PROPERTY_ID.events_*` WHERE event_name = 'page_view' AND _TABLE_SUFFIX = FORMAT_DATE('%Y%m%d', CURRENT_DATE()) LIMIT 20;
Troubleshooting
| Problem | Cause | Fix |
|---|---|---|
| Link fails | Your organization policy blocks service accounts from the export domain, or the BigQuery API is off | Ask your Cloud admin to adjust the policy and enable the API |
| Export stopped | Daily event limit exceeded, or the link's service identity was removed | Reduce volume, and restore the BigQuery User role for the link's identity |
| Wrong region | Chosen at link time | Delete and recreate the link, or replicate the dataset to another region. You may have a data gap. |
Vendor docs: Set up BigQuery Export, BigQuery Export schema