GA4 in BigQuery · Users, sessions and traffic
Page views by page in GA4 BigQuery
Page views in the GA4 export are page_view events. Read the URL from the page_location parameter, strip the query string, and count events and distinct users per page. The event_name filter does not reduce bytes; the date range and the columns do.
The query
SELECT
REGEXP_REPLACE(
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location'),
r'[?#].*$', '') AS page,
COUNT(*) AS views,
COUNT(DISTINCT user_pseudo_id) AS users
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND event_name = 'page_view'
GROUP BY page
ORDER BY views DESC
LIMIT 20;How it works
GA4 sends a page_view event each time a page loads or the site changes the browser history state. Its parameters include page_location (the full URL), page_referrer and, in this sample, page_title. The Views metric in GA4 is the count of these events, and Views per user divides by distinct users.
page_location is the full URL with query string and fragment, so /product?color=red and /product?color=blue would be two pages. REGEXP_REPLACE cuts everything from the first ? or #. If you need the path only, also remove the scheme and host: REGEXP_EXTRACT(page_location, r'^https?://[^/]+(/[^?#]*)').
The event_name = 'page_view' filter changes the result, not the bill. BigQuery stores data by column, so it reads event_name and the event_params columns for every event in the scanned tables, page views or not. LIMIT 20 does not reduce bytes either.
| Column | Meaning |
|---|---|
page | page_location without query string or fragment. |
views | page_view events for the page. |
users | Distinct users who viewed it. |
Notes and cost
- Cost: one month of
page_locationmeans readingevent_params.keyandstring_value, 516.3 MiB. Grouping bypage_titleinstead (first variation) reads the same columns. The cheapest page report is one you materialize daily and query from there. - Single-page apps fire
page_viewon history changes only if enhanced measurement or your tag is set up for it. If an app shows very few views per session, check the implementation before the SQL. - Lowercase URLs with
LOWER()if your site serves the same page under different cases; GA4 reports keep the URL as sent.
Variations
Views by page title
SELECT
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_title') AS page_title,
COUNT(*) AS views,
COUNT(DISTINCT user_pseudo_id) AS users
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND event_name = 'page_view'
GROUP BY page_title
ORDER BY views DESC
LIMIT 20;Views per day for one page
SELECT
PARSE_DATE('%Y%m%d', event_date) AS day,
COUNT(*) AS views
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201231'
AND event_name = 'page_view'
AND (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location')
LIKE 'https://shop.googlemerchandisestore.com/basket.html%'
GROUP BY day
ORDER BY day;FAQ
How do I count page views in GA4 BigQuery?
Count events with event_name = 'page_view', grouped by the page_location parameter. That is the Views metric in GA4.
Why does filtering on event_name not make my GA4 query cheaper?
BigQuery bills the columns you read for every row in the scanned tables. A WHERE on event_name filters rows after reading them. Only _TABLE_SUFFIX limits the tables, and only the column list limits the columns.
How do I remove query parameters from page_location?
REGEXP_REPLACE(page_location, r'[?#].*$', '') drops everything from the first ? or #.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
