GA4 in BigQuery · Engagement and events
Engagement time from engagement_time_msec in GA4 BigQuery
GA4 engagement time is the sum of the engagement_time_msec parameter across events. Sum it per day and divide by active users to get average engagement time per active user, or by sessions for engagement time per session. Wall-clock session length is a different number.
The query
WITH events AS (
SELECT
event_date,
user_pseudo_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'engagement_time_msec') AS engagement_time_msec
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
)
SELECT
PARSE_DATE('%Y%m%d', event_date) AS day,
ROUND(SUM(engagement_time_msec) / 1000 / 3600, 1) AS engagement_hours,
COUNT(DISTINCT IF(engagement_time_msec IS NOT NULL, user_pseudo_id, NULL)) AS engaged_users,
ROUND(SAFE_DIVIDE(SUM(engagement_time_msec) / 1000,
COUNT(DISTINCT IF(engagement_time_msec IS NOT NULL, user_pseudo_id, NULL))), 1)
AS avg_engagement_seconds_per_user
FROM events
GROUP BY day
ORDER BY day;How it works
GA4 measures engagement as the time the page is in focus or the app is in the foreground. The tag reports it in the engagement_time_msec parameter on events such as user_engagement, page_view and scroll: each value is the engagement time since the previous report, so the total for a user or session is the sum across their events, not the maximum.
The query sums the parameter per day, in hours, and divides by the users who reported any engagement time. Users with an engagement_time_msec value are, by Google's definition, active users on the web, so the result is comparable to Average engagement time per active user in GA4, before HyperLogLog++ and modeling differences.
Do not confuse engagement time with session duration. MAX(event_timestamp) - MIN(event_timestamp) per session is wall-clock time between the first and last event, including time the tab sat in the background. It is usually longer, and it is zero for single-event sessions.
In the sample, 2,431,818 events carry engagement_time_msec, all as integers.
| Column | Meaning |
|---|---|
engagement_hours | Sum of engagement_time_msec for the day, in hours. |
engaged_users | Users with at least one engagement_time_msec value that day. |
avg_engagement_seconds_per_user | Engagement seconds divided by engaged users. |
Notes and cost
- Cost:
engagement_time_msecis an integer, so the query readsevent_params.keyandint_valuebut notstring_value: 98.8 MiB for one week. Adding any string parameter (such aspage_location) to the same query adds thestring_valuecolumn, the largest in the export. - Daily averages do not average into a weekly average. Sum engagement time and count distinct users over the whole week instead.
Variations
Engagement time per page
Engagement time reported on events with a given page_location, per page view.
WITH events AS (
SELECT
event_name,
REGEXP_REPLACE(
(SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location'),
r'[?#].*$', '') AS page,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'engagement_time_msec') AS engagement_time_msec
FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
WHERE _TABLE_SUFFIX BETWEEN '20201201' AND '20201207'
)
SELECT
page,
COUNTIF(event_name = 'page_view') AS views,
ROUND(SUM(engagement_time_msec) / 1000 / NULLIF(COUNTIF(event_name = 'page_view'), 0), 1) AS engagement_seconds_per_view
FROM events
GROUP BY page
HAVING views >= 100
ORDER BY views DESC
LIMIT 20;Engagement time per session
WITH sessions AS (
SELECT
user_pseudo_id,
(SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS ga_session_id,
SUM((SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'engagement_time_msec')) AS engagement_time_msec
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
COUNT(*) AS sessions,
ROUND(AVG(IFNULL(engagement_time_msec, 0)) / 1000, 1) AS avg_engagement_seconds_per_session
FROM sessions;FAQ
How do I calculate average engagement time in GA4 BigQuery?
Sum engagement_time_msec over the period and divide by the number of active users (users with any engagement time). Divide by 1000 for seconds.
Should I use SUM or MAX of engagement_time_msec?
SUM. Each event reports the engagement time since the previous report, so the values add up to the total.
Is engagement time the same as session duration?
No. Engagement time counts only time in focus or in the foreground. Session duration from timestamps includes background time and is zero for one-event sessions.
Related
All GA4 export queries · BigQuery cost queries · The GA4 sample dataset
