GA4 in BigQuery · Engagement and events
Engagement rate and engaged sessions in GA4 BigQuery
Engagement rate in GA4 is engaged sessions divided by sessions. In the export a session is engaged when any of its events has session_engaged = '1'. Group events by user_pseudo_id and ga_session_id, flag engaged sessions with LOGICAL_OR, then divide.
The query
WITH events AS (
SELECT
event_date,
user_pseudo_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS ga_session_id,
(SELECT COALESCE(value.string_value, CAST(value.int_value AS STRING))
FROM UNNEST(event_params) WHERE key = 'session_engaged') AS session_engaged
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
),
sessions AS (
SELECT
user_pseudo_id,
ga_session_id,
MIN(event_date) AS session_date,
LOGICAL_OR(session_engaged = '1') AS is_engaged
FROM events
GROUP BY user_pseudo_id, ga_session_id
)
SELECT
PARSE_DATE('%Y%m%d', session_date) AS day,
COUNT(*) AS sessions,
COUNTIF(is_engaged) AS engaged_sessions,
ROUND(SAFE_DIVIDE(COUNTIF(is_engaged), COUNT(*)), 4) AS engagement_rate
FROM sessions
GROUP BY day
ORDER BY day;How it works
Google defines an engaged session as one that lasts longer than 10 seconds, has a key event, or has 2 or more page or screen views. Engagement rate is the percentage of sessions that were engaged, and bounce rate is the rest (see bounce rate).
You do not have to rebuild that rule from timestamps. The export carries a session_engaged parameter on events, set to 1 once the session counts as engaged, so a session is engaged if any of its events carries session_engaged = '1'. The query reads it per event, groups by session, and uses LOGICAL_OR to flag the session.
Watch the type. In this sample session_engaged is a string on 3,682,921 events and an integer on 313,391, and values include both '1' and '0'. Reading only string_value would miss engaged sessions whose flag arrived as an integer, so the query normalizes both slots to a string first. Check your own export with the inventory in export schema.
Each session is assigned to the day of its first event, so a session crossing midnight counts once. The sample also has an integer engaged_session_event parameter; it is a different flag and not needed for this metric.
| Column | Meaning |
|---|---|
sessions | Sessions whose first event falls on the day. |
engaged_sessions | Sessions with at least one event where session_engaged is 1. |
engagement_rate | Engaged sessions divided by sessions, as a fraction (0.65 means 65%). |
Notes and cost
- Cost: both parameters live in
event_params, andsession_engagedis mostly a string, so the query readskey,int_valueandstring_value: 155.5 MiB for one week. Engagement rate per day is a classic candidate for a daily summary table: compute it once, read a few kilobytes afterwards. - Sessions that started before the range and continue into it are counted on the first day of the range with only part of their events. Read an extra day before the range if the first day must be exact.
- GA4 reports estimate sessions with HyperLogLog++, so the denominator differs slightly from BigQuery's exact count.
Variations
Engagement rate by device category
WITH sessions AS (
SELECT
user_pseudo_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS ga_session_id,
ANY_VALUE(device.category) AS device_category,
LOGICAL_OR((SELECT COALESCE(value.string_value, CAST(value.int_value AS STRING))
FROM UNNEST(event_params) WHERE key = 'session_engaged') = '1') AS is_engaged
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
GROUP BY user_pseudo_id, ga_session_id
)
SELECT
device_category,
COUNT(*) AS sessions,
ROUND(SAFE_DIVIDE(COUNTIF(is_engaged), COUNT(*)), 4) AS engagement_rate
FROM sessions
GROUP BY device_category
ORDER BY sessions DESC;FAQ
How is engagement rate calculated in GA4 BigQuery?
Engaged sessions divided by sessions. A session is engaged if any of its events has the session_engaged parameter set to 1; group by user_pseudo_id and ga_session_id and use LOGICAL_OR to flag it.
What makes a GA4 session engaged?
Google's definition: the session lasts longer than 10 seconds, has a key event, or has 2 or more page or screen views.
Why does session_engaged = '1' miss some sessions?
Because the parameter is sometimes stored in int_value instead of string_value. Use COALESCE(value.string_value, CAST(value.int_value AS STRING)).
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
