This example query contains all following Google Analytics user dimensions and metrics. If you only need one dimension or metric, look at the
-- comments in the example query and copy the part you need from the
select clause. Make sure that you also add any additional conditions (in the
group by and
order by) that are necessary to calculate the results correctly.
- user type
- count of sessions
- new users
- % new sessions
- number of sessions per user
select -- user type (dimension) case when totals.newvisits = 1 then 'New visitor' else 'Returning visitor' end as user_type, -- count of sessions (dimension) visitnumber as count_of_sessions, -- users (metric) count(distinct fullvisitorid) as users, -- new users (metric) count(distinct(case when totals.newvisits = 1 then fullvisitorid else null end)) as new_users, -- % new sessions (metric) count(distinct(case when totals.newvisits = 1 then fullvisitorid else null end)) / count(distinct concat(fullvisitorid, cast(visitstarttime as string))) as percentage_new_sessions, -- number of sessions per user (metric) count(distinct concat(fullvisitorid, cast(visitstarttime as string))) / count(distinct fullvisitorid) as number_of_sessions_per_user, -- hits (metric) sum((select totals.hits from unnest(hits) group by totals.hits)) as hits from `bigquery-public-data.google_analytics_sample.ga_sessions_20160801` group by user_type, count_of_sessions order by count_of_sessions
Enjoying this post? Subscribe to the GA4BigQuery newsletter!
You will receive regular updates (max 1x/week) straight in your inbox. You can unsubscribe anytime.