GA4 in BigQuery · Users, sessions and traffic
Users and sessions by device and browser in GA4 BigQuery
The device record in the GA4 export holds category (desktop, mobile, tablet), operating_system and web_info.browser. Group by those fields to count users and sessions. Reading only the subfields you need costs a fraction of the whole record.
The query
SELECT
device.category,
device.operating_system,
device.web_info.browser,
COUNT(DISTINCT user_pseudo_id) AS users,
COUNT(DISTINCT CONCAT(user_pseudo_id, '.', CAST(
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS STRING))) AS sessions
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY category, operating_system, browser
ORDER BY sessions DESC
LIMIT 20;How it works
Device information is a STRUCT on every event. device.category is desktop, mobile or tablet; device.operating_system and device.operating_system_version describe the OS; device.web_info.browser and browser_version describe the browser on web streams; device.mobile_brand_name and mobile_model_name describe the hardware; device.language is the browser or device language.
The query counts users and sessions per category, OS and browser. A user who switches from phone to laptop appears under both, so per-device users add up to more than total users.
STRUCT fields are stored as separate columns. Selecting device.category reads that one column, not the whole device record. Across the sample, device.category is 35.2 MiB while all of device is 295.8 MiB. Avoid SELECT device or device.* unless you need every field.
| Column | Meaning |
|---|---|
category / operating_system / browser | Device category, OS and browser from the device record. |
users | Distinct users seen on that combination. |
sessions | Distinct sessions on that combination. |
Notes and cost
- Cost: the session count needs
event_params, so this month costs 378.7 MiB. Users per device without sessions (first variation) costs 45.0 MiB. Decide whether the report really needs sessions. - Browser and OS versions have high cardinality. Group by
operating_systemorbrowseralone, or by the major version, for readable reports.
Variations
Users by device category only
No event_params: reads three small columns.
SELECT
device.category,
COUNT(DISTINCT user_pseudo_id) AS users,
COUNT(*) AS events
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
GROUP BY category
ORDER BY users DESC;Mobile models
SELECT
device.mobile_brand_name,
device.mobile_model_name,
COUNT(DISTINCT user_pseudo_id) AS users
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND device.category = 'mobile'
GROUP BY mobile_brand_name, mobile_model_name
ORDER BY users DESC
LIMIT 20;FAQ
Where is the browser in the GA4 BigQuery export?
device.web_info.browser, with the version in device.web_info.browser_version. Both are filled for web streams.
Why do users by device add up to more than total users?
A user who visits from several device categories or browsers is counted under each. Distinct counts are not additive across groups.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
