GA4 in BigQuery · Users, sessions and traffic
Count total, active and new users in GA4 BigQuery
Total users in GA4 BigQuery is COUNT(DISTINCT user_pseudo_id). Active users, the number GA4 reports show, are users with an engaged session, a first_visit event or an engagement_time_msec parameter. New users logged first_visit. Compute all three over the whole range, not per day.
The query
WITH users AS (
SELECT
user_pseudo_id,
LOGICAL_OR(event_name = 'first_visit') AS is_new,
LOGICAL_OR(
event_name = 'first_visit'
OR (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'engagement_time_msec') IS NOT NULL
OR (SELECT COALESCE(value.string_value, CAST(value.int_value AS STRING))
FROM UNNEST(event_params) WHERE key = 'session_engaged') = '1'
) AS is_active
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY user_pseudo_id
)
SELECT
COUNT(*) AS total_users,
COUNTIF(is_active) AS active_users,
COUNTIF(is_new) AS new_users
FROM users;How it works
GA4 has three user metrics, and the export can produce all of them. Google defines Total users as unique users who triggered any event, which in BigQuery is COUNT(DISTINCT user_pseudo_id). Active users are users who engaged: Google counts a web user as active when they have an engaged session, or when Analytics collects a first_visit event or an engagement_time_msec parameter. New users logged first_visit (web) or first_open (apps) in the range.
The query reduces the events to one row per user with two flags, then counts. LOGICAL_OR is true if any event of the user meets the condition. Engaged sessions are marked by the session_engaged parameter; in this sample it is a string '1' on most events and an integer on others, so the subquery reads both slots.
Most GA4 reports label Active users simply as Users, so active_users is the number to compare with the interface, not total_users. Exports created after Google added the column also have a boolean is_active_user per event, populated in daily tables only. Where it exists, COUNT(DISTINCT IF(is_active_user, user_pseudo_id, NULL)) replaces the flag logic; the sample predates it, so the query rebuilds the definition from events.
Distinct counts do not add up across days. Monthly users are not the sum of daily users, because a user active on five days counts five times in the sum. Run one distinct count over the period you report.
| Column | Meaning |
|---|---|
total_users | Distinct user_pseudo_id with any event in the range. |
active_users | Users with an engaged session, a first_visit or an engagement_time_msec value. Comparable to Users in GA4 reports. |
new_users | Users whose first_visit happened in the range. |
Notes and cost
- Cost: the active flag needs
event_params, so one month costs 573.3 MiB. Total and new users alone need onlyuser_pseudo_idandevent_nameand cost 51.9 MiB (first variation). - BigQuery counts are exact; GA4 estimates users with HyperLogLog++ at precision 14, so expect a small gap (see GA4 vs BigQuery differences).
user_pseudo_idcounts browsers, not people. For logged-in users see user_id vs user_pseudo_id.APPROX_COUNT_DISTINCT(user_pseudo_id)is faster on large ranges but does not reduce bytes scanned; on-demand cost depends on bytes, not on the aggregation.
Variations
Total and new users without event_params
Skips the engagement flags, so the scan is a fraction of the main query.
SELECT
COUNT(DISTINCT user_pseudo_id) AS total_users,
COUNT(DISTINCT IF(event_name = 'first_visit', user_pseudo_id, NULL)) AS new_users
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231';Daily users
One distinct count per day. Do not sum the rows to get a monthly number.
SELECT
PARSE_DATE('%Y%m%d', event_date) AS day,
COUNT(DISTINCT user_pseudo_id) AS total_users,
COUNT(DISTINCT IF(event_name = 'first_visit', user_pseudo_id, NULL)) AS new_users
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY day
ORDER BY day;FAQ
How do I count users in GA4 BigQuery?
COUNT(DISTINCT user_pseudo_id) gives Total users for the date range you filter with _TABLE_SUFFIX. For the Users number in GA4 reports, count only active users.
What is the difference between total users and active users in GA4?
Total users triggered any event. Active users engaged: an engaged session, a first_visit, or an engagement_time_msec parameter on the web. GA4 reports show active users by default.
Why is my monthly user count lower than the sum of daily users?
Because returning users are counted once per day in the sum and once in the monthly distinct count. Always compute distinct users over the full period.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
