GA4 in BigQuery · Working with the export
GA4 user_id vs user_pseudo_id in BigQuery
user_pseudo_id identifies a browser or app instance; user_id is the login ID you set yourself, NULL until you send it. Count people by mapping each user_pseudo_id to the user_id seen on any of its events, then counting COALESCE(user_id, user_pseudo_id).
The query
WITH ids AS (
-- One row per browser: the login ID seen on any of its events, if any
SELECT
user_pseudo_id,
MAX(user_id) AS user_id
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY user_pseudo_id
)
SELECT
COUNT(*) AS pseudo_ids,
COUNTIF(user_id IS NOT NULL) AS pseudo_ids_with_login,
COUNT(DISTINCT user_id) AS user_ids,
COUNT(DISTINCT COALESCE(user_id, user_pseudo_id)) AS people
FROM ids;How it works
user_pseudo_id is the pseudonymous ID GA4 assigns on first visit; on the web it comes from the Analytics cookie, so one person on a laptop and a phone is two user_pseudo_id values, and clearing cookies creates a new one. user_id is the ID you send when someone logs in. It is NULL on events where you did not set it, including the events before login in the same visit.
Counting DISTINCT user_pseudo_id gives browsers, not people. Counting DISTINCT user_id ignores everyone who never logged in. The query stitches the two: every browser that ever carried a user_id inherits it for all its events, and everything else falls back to the pseudo ID. A person who logged in on two devices then counts once.
The GA4 interface goes further. With Google signals and a blended reporting identity it can deduplicate devices that never logged in, and none of that is in the export. Google also states that only device-ID based data is exported, so user counts from BigQuery are usually higher than in reports (see GA4 vs BigQuery differences).
In the sample, user_id is NULL on every one of the 4,295,584 events, and there are 270,154 distinct user_pseudo_id values across the three months. The query runs, but pseudo_ids_with_login and user_ids come back as 0.
| Column | Meaning |
|---|---|
pseudo_ids | Distinct browsers or app instances in the range. |
pseudo_ids_with_login | Browsers that carried a user_id on at least one event. |
user_ids | Distinct logged-in people. |
people | Distinct people after stitching: user_id where known, otherwise the browser. |
Notes and cost
- Cost: only
user_pseudo_idanduser_idfor one month, 31.8 MiB. Identity questions rarely needevent_params; keep it out of these queries. MAX(user_id)picks one ID when a shared device saw several logins. If that matters, countCOUNT(DISTINCT user_id)peruser_pseudo_idfirst and decide how to treat shared devices.- The stitch is only as good as the date range. A browser that logged in last month but not this month stays anonymous here; for a stable mapping, keep a small
user_pseudo_idtouser_idtable that you update daily. - Under consent mode, Google's guidance notes that cookieless pings arrive with a different
user_pseudo_idper session, which inflates distinct counts.user_idis exported regardless of consent state.
Variations
Attach the stitched person ID to every event
WITH ids AS (
SELECT user_pseudo_id, MAX(user_id) AS user_id
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY user_pseudo_id
)
SELECT
e.event_timestamp,
e.event_name,
COALESCE(ids.user_id, e.user_pseudo_id) AS person_id
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*` AS e
JOIN ids USING (user_pseudo_id)
WHERE e._TABLE_SUFFIX BETWEEN '20201201' AND '20201231';Browsers per logged-in user
How many devices or browsers each user_id uses. Empty on the sample.
SELECT
user_id,
COUNT(DISTINCT user_pseudo_id) AS browsers
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND user_id IS NOT NULL
GROUP BY user_id
ORDER BY browsers DESC
LIMIT 100;FAQ
What is user_pseudo_id in GA4 BigQuery?
A pseudonymous ID for a browser or app instance, assigned on first visit or first open. On the web it is based on the Analytics cookie, so it changes when cookies are cleared and differs across devices.
Why is user_id NULL in my GA4 export?
GA4 only fills user_id on events where your site or app sent it, usually after login. Events before login, and every event in the public sample, have NULL.
Why does BigQuery show more users than the GA4 interface?
The interface can deduplicate users with Google signals and modeling, and reports active users by default. Google exports only device-ID based data, so distinct user_pseudo_id counts are usually higher.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
