Request a demo

GA4 in BigQuery · Retention and cohorts

Weekly user retention cohorts in GA4 BigQuery

Assign each user to the week of their first_visit, find the weeks they were active afterwards, and count users per cohort and week offset. Dividing by the cohort size gives the retention table GA4 shows in cohort explorations. It needs only three small columns.

Runs on Google's public sample bigquery-public-data.ga4_obfuscated_sample_ecommerce · Scans 179 MiB, under $0.01 on-demand · Checked in November 2026

The query

WITH first_seen AS (
  SELECT
    user_pseudo_id,
    DATE_TRUNC(MIN(PARSE_DATE('%Y%m%d', event_date)), WEEK(MONDAY)) AS cohort_week
  FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
  WHERE _TABLE_SUFFIX BETWEEN '20201101' AND '20210131'
    AND event_name = 'first_visit'
  GROUP BY user_pseudo_id
),
activity AS (
  SELECT DISTINCT
    user_pseudo_id,
    DATE_TRUNC(PARSE_DATE('%Y%m%d', event_date), WEEK(MONDAY)) AS active_week
  FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
  WHERE _TABLE_SUFFIX BETWEEN '20201101' AND '20210131'
),
cohorts AS (
  SELECT
    f.cohort_week,
    DATE_DIFF(a.active_week, f.cohort_week, WEEK(MONDAY)) AS week_number,
    COUNT(*)                                              AS users
  FROM first_seen AS f
  JOIN activity AS a USING (user_pseudo_id)
  WHERE a.active_week >= f.cohort_week
  GROUP BY cohort_week, week_number
)
SELECT
  cohort_week,
  week_number,
  users,
  ROUND(users / FIRST_VALUE(users) OVER (PARTITION BY cohort_week ORDER BY week_number), 4) AS retention
FROM cohorts
ORDER BY cohort_week, week_number;

How it works

A retention cohort groups users by when they arrived and follows how many come back. The query uses the week of the user's first_visit as the cohort, and every week with any event as an active week. DATE_DIFF(..., WEEK(MONDAY)) turns the two into a week number: 0 is the arrival week, 1 the following week, and so on. Retention is users in a week divided by users in week 0.

Week 0 always contains the whole cohort, because the first visit itself is an event in that week, so FIRST_VALUE(users) over the cohort is the cohort size.

Two edges of the sample matter. 2020-11-01 is a Sunday, so with Monday weeks the first cohort (week of 2020-10-26) holds only one day of arrivals. And users whose first_visit happened before November 2020 have no first_visit in range and are left out, which is correct for a cohort table. Later cohorts have fewer weeks to show, and their last cells are the least reliable.

"Active" here means any event. GA4's cohort exploration lets you choose the return criterion; to count only engaged returns, add the session_engaged filter from engagement rate, at a higher scan cost.

ColumnMeaning
cohort_weekMonday of the week of the user's first_visit.
week_numberWeeks since the cohort week. 0 is the arrival week.
usersUsers from the cohort with any event in that week.
retentionUsers divided by the cohort's week 0 users.

Notes and cost

  • Cost: the whole sample, but only event_date, event_name and user_pseudo_id: 179.0 MiB. Retention is one of the few GA4 questions that needs a long date range, so keeping it free of event_params matters most here.
  • On a growing export, the full-history scan grows every day. Store user_pseudo_id with its first visit date and a per-user active-weeks table, and update both incrementally (see daily summary table).
  • Users who clear cookies come back as new user_pseudo_id values and look like churn. Logged-in sites retain better with user_id (see user_id vs user_pseudo_id).

Variations

Pivoted retention table

One row per cohort, one column per week, ready to paste into a sheet.

WITH first_seen AS (
  SELECT
    user_pseudo_id,
    DATE_TRUNC(MIN(PARSE_DATE('%Y%m%d', event_date)), WEEK(MONDAY)) AS cohort_week
  FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
  WHERE _TABLE_SUFFIX BETWEEN '20201101' AND '20210131'
    AND event_name = 'first_visit'
  GROUP BY user_pseudo_id
),
activity AS (
  SELECT DISTINCT
    user_pseudo_id,
    DATE_TRUNC(PARSE_DATE('%Y%m%d', event_date), WEEK(MONDAY)) AS active_week
  FROM `bigquery-public-data.ga4_obfuscated_sample_ecommerce.events_*`
  WHERE _TABLE_SUFFIX BETWEEN '20201101' AND '20210131'
)
SELECT *
FROM (
  SELECT
    f.cohort_week,
    DATE_DIFF(a.active_week, f.cohort_week, WEEK(MONDAY)) AS week_number,
    a.user_pseudo_id
  FROM first_seen AS f
  JOIN activity AS a USING (user_pseudo_id)
  WHERE a.active_week >= f.cohort_week
)
PIVOT (COUNT(DISTINCT user_pseudo_id) FOR week_number IN (0 AS w0, 1 AS w1, 2 AS w2, 3 AS w3, 4 AS w4))
ORDER BY cohort_week;

FAQ

How do I calculate user retention from GA4 BigQuery?

Assign each user a cohort from their first_visit date, list the periods they were active in, and count users per cohort and period offset. Divide by the cohort size for retention.

Why does my first cohort look small?

If the date range starts mid-week, the first cohort contains only part of a week of new users. Start the range on the first day of a week, or drop the first cohort.

Can I use user_first_touch_timestamp instead of first_visit?

Yes. user_first_touch_timestamp is carried on events after the first one too (4,209,536 of the sample's 4,295,584 events have it), so it also covers users whose first visit was before your range. Convert it with TIMESTAMP_MICROS and truncate to the week.

All GA4 export queries · BigQuery cost queries · The GA4 sample dataset

Cut BigQuery spend without guessing

Masthead attributes compute and storage cost to every project, user, pipeline and table, finds unused and dead-end tables, and shows where editions or on-demand pricing is cheaper. Mnemonic cut its BigQuery compute cost by 20% within 24 hours of onboarding.