GA4 in BigQuery · Users, sessions and traffic
New vs returning users in GA4 BigQuery
In the GA4 export a new user is one who logged first_visit (first_open in apps) in the period; everyone else active in the period is returning. Flag first_visit per user and day with LOGICAL_OR, then split the daily user count.
The query
WITH daily AS (
SELECT
PARSE_DATE('%Y%m%d', event_date) AS day,
user_pseudo_id,
LOGICAL_OR(event_name = 'first_visit') AS is_new
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY day, user_pseudo_id
)
SELECT
day,
COUNT(*) AS users,
COUNTIF(is_new) AS new_users,
COUNTIF(NOT is_new) AS returning_users,
ROUND(COUNTIF(is_new) / COUNT(*), 3) AS new_user_share
FROM daily
GROUP BY day
ORDER BY day;How it works
GA4 marks a user's first visit with the first_visit event (web) or first_open (apps). Google's guide to matching the interface computes new users exactly that way, MAX(IF(event_name IN ('first_visit', 'first_open'), 1, 0)) per user, and returning users as the remaining users. The query does the same per day.
Returning, in this split, means "seen before this day", not "came back after a previous session in this range". Google's Returning users metric is defined as users who initiated at least one previous session, so a user whose first_visit was last month counts as returning today even if this is their first event in your range.
The split counts total users (any event). GA4 reports use active users; to match them, add the active-user flag from users, which needs event_params and costs more. The sample has no is_active_user column.
Per-day rows are fine to chart, but new and returning are not additive across days: a user can be new on Monday and returning on Tuesday. For a period total, group by user over the whole range instead of by day.
| Column | Meaning |
|---|---|
users | Distinct users with any event that day. |
new_users | Users whose first_visit happened that day. |
returning_users | Users with events that day and no first_visit that day. |
new_user_share | New users as a share of the day's users. |
Notes and cost
- Cost: only
event_date,event_nameanduser_pseudo_id, 67.3 MiB for one month. Noevent_params, so this stays cheap even on large exports. - If your site sends
first_visittwice for a user (cookies cleared, consent changes), that user counts as new again with a newuser_pseudo_id. The export cannot tell the difference. - On apps, add
'first_open'to the condition:event_name IN ('first_visit', 'first_open').
Variations
New vs returning sessions with ga_session_number
ga_session_number = 1 marks the user's first session. This splits sessions, not users, and needs event_params.
WITH sessions AS (
SELECT
user_pseudo_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS ga_session_id,
MAX((SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_number')) AS ga_session_number
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY user_pseudo_id, ga_session_id
)
SELECT
IF(ga_session_number = 1, 'new', 'returning') AS session_type,
COUNT(*) AS sessions
FROM sessions
GROUP BY session_type;Returning users by first-touch date
Uses user_first_touch_timestamp (microseconds, UTC) to separate users acquired before the month from users acquired during it.
WITH users AS (
SELECT
user_pseudo_id,
MIN(user_first_touch_timestamp) AS first_touch
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY user_pseudo_id
)
SELECT
CASE
WHEN first_touch IS NULL THEN 'unknown'
WHEN first_touch < UNIX_MICROS(TIMESTAMP '2020-12-01') THEN 'acquired before December'
ELSE 'acquired in December'
END AS user_type,
COUNT(*) AS users
FROM users
GROUP BY user_type;FAQ
How does GA4 define a new user in BigQuery?
A user who logged the first_visit event (web) or first_open (apps) in the date range. Count them with COUNT(DISTINCT IF(event_name = 'first_visit', user_pseudo_id, NULL)).
Why do new plus returning users not equal total users for a month?
If you sum daily rows, a user who is new on one day and returning on later days is counted on every day. Compute the split once per user over the whole month.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
