Skip to content

Instantly share code, notes, and snippets.

@jhnvdw
Last active March 20, 2023 12:40
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jhnvdw/60307e7cb26d1db42b7f7d1c8aca5da8 to your computer and use it in GitHub Desktop.
Save jhnvdw/60307e7cb26d1db42b7f7d1c8aca5da8 to your computer and use it in GitHub Desktop.
WITH
-- Subquery to define static and/or dynamic start and end date for the whole query
period AS (
SELECT
'20200220' AS start_date,
FORMAT_DATE('%Y%m%d',DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY)) AS end_date)
SELECT
-- Event count per user (metric | the number of times an individual event (change event_name 'page_view' to event that needs to be counted) was triggered divided by amount of users)
COUNT(DISTINCT
CASE
WHEN event_name = 'page_view' THEN CONCAT(user_pseudo_id, CAST(event_timestamp AS STRING))
END
) / COUNT(DISTINCT user_pseudo_id) AS event_count_per_user
FROM
-- Change this to your Google Analytics 4 export location in BigQuery
`ga4.analytics_1234567890.events_*`,
period
WHERE
_table_suffix BETWEEN period.start_date
AND period.end_date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment