GA4 in BigQuery
GA4 BigQuery export: the SQL cookbook
Ready-to-run queries for the Google Analytics 4 export, from unnesting event_params to sessions, attribution, funnels and cohorts. Each one runs on Google's public GA4 sample, shows what it scans, and how to scan less as your own export grows.
No recipe matches. Try the UNNEST reference.
Working with the export
How the GA4 export is laid out, and how to query it without scanning everything.
- Build GA4 sessions in BigQuery from ga_session_idA GA4 session is identified by user_pseudo_id plus the ga_session_id event parameter.
- GA4 BigQuery export schema: tables, nested fields and value typesThe GA4 export writes one table per day (events_YYYYMMDD) and, with streaming on, events_intraday_YYYYMMDD.
- GA4 user_id vs user_pseudo_id in BigQueryuser_pseudo_id identifies a browser or app instance; user_id is the login ID you set yourself, NULL until you send it.
- Query GA4 intraday tables together with daily tablesWith streaming export on, GA4 writes today's events to events_intraday_YYYYMMDD and deletes it once the daily table is complete.
- Query GA4 user properties in BigQueryGA4 exports user properties in the repeated user_properties record on each event, with key, value slots and set_timestamp_micros.
- Unnest GA4 event_params in BigQueryRead a GA4 event parameter with a scalar subquery over UNNEST(event_params): (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location').
- Why GA4 BigQuery numbers differ from the GA4 interfaceBigQuery holds raw events, while GA4 reports estimate users and sessions with HyperLogLog++, may apply thresholds, sampling and consent-mode modeling, use the reporting identity and time zone, and show active users.
Users, sessions and traffic
Users, sessions, page views and where visitors come from.
- Count GA4 sessions in BigQueryCount GA4 sessions as distinct user_pseudo_id plus ga_session_id, the way GA4 does.
- Count total, active and new users in GA4 BigQueryTotal users in GA4 BigQuery is COUNT(DISTINCT user_pseudo_id).
- First user source, medium and campaign in GA4 BigQueryThe traffic_source record in the GA4 export is first-touch acquisition: the source, medium and campaign that first brought the user, the same on every later event.
- Landing pages in GA4 BigQueryA landing page is the first page_view of a session.
- New vs returning users in GA4 BigQueryIn 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.
- Page views by page in GA4 BigQueryPage views in the GA4 export are page_view events.
- Session source / medium in GA4 BigQueryOlder GA4 exports, including the public sample, carry source, medium and campaign as event parameters.
- Users and sessions by country and city in GA4 BigQueryThe geo record in the GA4 export holds continent, sub_continent, country, region, city and metro, derived from the user's IP address.
- Users and sessions by device and browser in GA4 BigQueryThe device record in the GA4 export holds category (desktop, mobile, tablet), operating_system and web_info.browser.
Engagement and events
Engagement time, engaged sessions, events and their parameters.
- Bounce rate by landing page in GA4 BigQueryIn GA4, bounce rate is the share of sessions that were not engaged: 1 minus engagement rate.
- Count GA4 events by name in BigQueryCount GA4 events with COUNT() grouped by event_name.
- Engagement rate and engaged sessions in GA4 BigQueryEngagement rate in GA4 is engaged sessions divided by sessions.
- Engagement time from engagement_time_msec in GA4 BigQueryGA4 engagement time is the sum of the engagement_time_msec parameter across events.
- Scroll depth by page in GA4 BigQueryGA4 enhanced measurement sends one scroll event per page the first time 90% of its height becomes visible.
Ecommerce and conversions
Purchases, revenue, items and funnels.
- Ecommerce funnel from view_item to purchase in GA4 BigQueryBuild a GA4 checkout funnel by flagging, per user, which of view_item, add_to_cart, begin_checkout, add_shipping_info, add_payment_info and purchase they reached, each step requiring the previous ones.
- Item revenue and units sold in GA4 BigQueryItem-level ecommerce data in the GA4 export is the repeated items record.
- Key events (conversions) and conversion rate in GA4 BigQueryThe GA4 export has no flag for key events (formerly conversions): you list your key event names in SQL.
- Purchases, revenue and average order value in GA4 BigQueryGA4 purchase revenue is in ecommerce.purchase_revenue_in_usd (and purchase_revenue in your currency) on purchase events.
Retention and cohorts
Who comes back, and when.
Cost and performance
Keep GA4 queries cheap as the export grows.
- Make GA4 BigQuery queries cheaper: _TABLE_SUFFIX, columns, maximum bytes billedOn-demand BigQuery bills the columns a query reads in every table it touches.
- Materialize GA4 sessions into a partitioned daily tableTurn the GA4 export into a session table partitioned by date and clustered by source and medium, then refresh only the last three days every morning.
