Skip to content

Instantly share code, notes, and snippets.

@ddikman
Created December 16, 2021 07:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ddikman/1340d934b5bcff5f3d8f971b2a796126 to your computer and use it in GitHub Desktop.
Save ddikman/1340d934b5bcff5f3d8f971b2a796126 to your computer and use it in GitHub Desktop.
Monthly total user retention numbers in BigQuery with google analytics
SELECT
last as last_month,
DATETIME_DIFF(last, first, MONTH) as retained_months,
COUNT(*) as users
FROM (
SELECT
DATE(DATE_TRUNC(TIMESTAMP_MICROS(min(event_timestamp)), MONTH)) as first,
DATE(DATE_TRUNC(TIMESTAMP_MICROS(max(event_timestamp)), MONTH)) as last
FROM `<your-project>.events_*`
WHERE event_name = 'view_item'
GROUP BY user_pseudo_id
)
WHERE DATETIME_DIFF(last, first, MONTH) > 0
GROUP BY last_month, retained_months
ORDER BY last_month, retained_months
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment