Page dimensions & metrics (GA4)
How to query the most important page tracking dimensions and metrics from the GA4 BigQuery export, like hostname, page, previous page path, page path level, page title, landing page, entrances and (unique) page views.
A lot of Google Analytics 4 dimensions and metrics can be derived straight from the tables without performing calculations on the data. With the help of unnest
and parse
for dates or timestamps we can do the trick.
Other dimensions and metrics that you might need are a bit harder to access. You will have to (re)calculate them, which can require some serious SQL-skills.
While designing the course Query GA4 Data In Google BigQuery I've learned a lot about best practices to calculate dimensions and metrics. To share this knowledge with you I will provide a combined query for default dimensions and metrics, and a single example query for every non-default dimension or metric.
As always, drop a line in the comments if you have any questions, feedback or suggestions related to this article.
Default page tracking dimensions
- hostname
select
device.web_info.hostname,
countif(event_name = 'page_view') as page_views
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
group by
hostname
order by
page_views
Calculated page tracking dimensions
- page (location)
select
(select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location') as page,
countif(event_name = 'page_view') as page_views
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
group by
page
order by
page_views desc
- previous page (location)
select
lag((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location'), 1) over (partition by user_pseudo_id,(select value.int_value from unnest(event_params) where event_name = 'page_view' and key = 'ga_session_id') order by event_timestamp asc) as previous_page
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
- page path level 1/2/3
select
case when split(split((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location'),'/')[safe_ordinal(4)],'?')[safe_ordinal(1)] = '' then null else concat('/',split(split((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location'),'/')[safe_ordinal(4)],'?')[safe_ordinal(1)]) end as pagepath_level_1,
case when split(split((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location'),'/')[safe_ordinal(5)],'?')[safe_ordinal(1)] = '' then null else concat('/',split(split((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location'),'/')[safe_ordinal(5)],'?')[safe_ordinal(1)]) end as pagepath_level_2,
case when split(split((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location'),'/')[safe_ordinal(6)],'?')[safe_ordinal(1)] = '' then null else concat('/',split(split((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location'),'/')[safe_ordinal(6)],'?')[safe_ordinal(1)]) end as pagepath_level_3,
countif(event_name = 'page_view') as page_views
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
group by
pagepath_level_1,
pagepath_level_2,
pagepath_level_3
order by
page_views desc
- page title
select
(select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_title') as page_title,
countif(event_name = 'page_view') as page_views
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
group by
page_title
order by
page_views desc
- landing page
select
case when (select value.int_value from unnest(event_params) where event_name = 'page_view' and key = 'entrances') = 1 then (select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location') end as landing_page,
countif(event_name = 'page_view') as page_views
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
group by
landing_page
having
landing_page is not null
order by
page_views desc
- second page
select
case when (select value.int_value from unnest(event_params) where event_name = 'page_view' and key = 'entrances') = 1 then lead((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location'), 1) over (partition by user_pseudo_id,(select value.int_value from unnest(event_params) where event_name = 'page_view' and key = 'ga_session_id') order by event_timestamp asc) else null end as second_page
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
- exit page
select
case when (select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location') = first_value((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location')) over (partition by user_pseudo_id,(select value.int_value from unnest(event_params) where event_name = 'page_view' and key = 'ga_session_id') order by event_timestamp desc) then ( select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location') else null end as exit_page
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
Calculated page tracking metrics
- entrances
select
count(case when (select value.int_value from unnest(event_params) where event_name = 'page_view' and key = 'entrances') = 1 then concat(user_pseudo_id,(select value.int_value from unnest(event_params) where key = 'ga_session_id')) end) as entrances
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
- page views
select
countif(event_name = 'page_view') as page_views
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
- unique pageviews
select
count(distinct case when event_name = 'page_view' then concat(user_pseudo_id,(select value.int_value from unnest(event_params) where key = 'ga_session_id')) end) as unique_pageviews
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
- pages per session
select
countif(event_name = 'page_view') / count(distinct concat(user_pseudo_id,(select value.int_value from unnest(event_params) where key = 'ga_session_id'))) as pages_per_session
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
- pages per user
select
countif(event_name = 'page_view') / count(distinct concat(user_pseudo_id)) as pages_per_session
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
- exits
with prep as (
select
user_pseudo_id,
(select value.int_value from unnest(event_params) where event_name = 'page_view' and key = 'ga_session_id') as session_id,
event_timestamp,
first_value((select value.string_value from unnest(event_params) where event_name = 'page_view' and key = 'page_location')) over (partition by user_pseudo_id,(select value.int_value from unnest(event_params) where event_name = 'page_view' and key = 'ga_session_id') order by event_timestamp desc) as exit_page
from
-- change this to your google analytics 4 export location in bigquery
`ga4bigquery.analytics_250794857.events_*`
where
-- define static and/or dynamic start and end date
_table_suffix between '20240701'
and format_date('%Y%m%d',date_sub(current_date(), interval 1 day))
and event_name = 'page_view')
select
exit_page,
count(distinct concat(user_pseudo_id,session_id)) as exits
from
prep
group by
exit_page
having
exit_page is not null
order by
exits desc