Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save nicobrx/bc48e92878bfbf28bf29627906ec5339 to your computer and use it in GitHub Desktop.
Save nicobrx/bc48e92878bfbf28bf29627906ec5339 to your computer and use it in GitHub Desktop.
Query for getting average engagement time per page from GA4 BigQuery data.
SELECT
page_location,
SUM(engagement_time_seconds) AS engagement_time_seconds,
SUM(page_views) AS page_views,
SAFE_DIVIDE( SUM(engagement_time_seconds), SUM(page_views) ) AS average_engagement_time_per_page
FROM (
SELECT
(
SELECT
value.string_value
FROM
UNNEST (event_params)
WHERE
KEY = 'page_location' ) AS page_location,
SUM((
SELECT
value.int_value
FROM
UNNEST(event_params)
WHERE
KEY = 'engagement_time_msec'))/1000 AS engagement_time_seconds,
COUNT(
CASE
WHEN event_name = 'page_view' THEN 1
END
) AS page_views
FROM
## replace with your project & dataset
`yourbigquery.analytics_12345678.events_*`
WHERE
_TABLE_SUFFIX BETWEEN '20230501'
AND '20230531'
GROUP BY
page_location )
GROUP BY
page_location
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment