Follow me on LinkedIn - AI, GA4, BigQuery

Use the following SQL (automatically generated via a text prompt in ChatGPT) to create a Google Analytics 4 landing page report in BigQuery:
WITH base_events AS (
  SELECT
    user_pseudo_id,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'page_location') AS page_location,
    (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_id') AS session_id,
    (SELECT value.string_value FROM UNNEST(event_params) WHERE key = 'session_engaged') AS session_engaged,
    (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'ga_session_number') AS session_number,
    (SELECT value.int_value FROM UNNEST(event_params) WHERE key = 'engagement_time_msec') AS engagement_time_msec,
    event_name
  FROM `dbrt-ga4.analytics_207472454.events_*`
  WHERE
    _TABLE_SUFFIX BETWEEN FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 7 DAY))
    AND FORMAT_DATE('%Y%m%d', DATE_SUB(CURRENT_DATE(), INTERVAL 1 DAY))
),

landing_page_sessions AS (
  SELECT
    page_location AS landing_page,
    user_pseudo_id,
    session_id,
    session_engaged,
    engagement_time_msec
  FROM base_events
  WHERE event_name = 'page_view'
),

landing_page_metrics AS (
  SELECT
    landing_page,
    COUNT(*) AS pageviews,
    COUNT(DISTINCT CONCAT(user_pseudo_id, session_id)) AS sessions,
    COUNT(DISTINCT user_pseudo_id) AS users,
    COUNT(DISTINCT CASE
      WHEN session_engaged = '1'
      THEN CONCAT(user_pseudo_id, session_id)
    END) AS engaged_sessions,
    SUM(CASE
      WHEN session_engaged = '1' THEN engagement_time_msec
    END) / 1000 AS total_engagement_time_sec
  FROM landing_page_sessions
  GROUP BY landing_page
),

returning_users AS (
  SELECT
    page_location AS landing_page,
    COUNT(DISTINCT user_pseudo_id) AS returning_users
  FROM base_events
  WHERE event_name = 'session_start'
    AND session_number > 1
  GROUP BY landing_page
)

SELECT
  lpm.landing_page,
  lpm.pageviews,
  lpm.sessions,
  lpm.users,
  IFNULL(ru.returning_users, 0) AS returning_users,
  lpm.engaged_sessions,
  ROUND(SAFE_DIVIDE(lpm.engaged_sessions, lpm.sessions) * 100, 2) AS engagement_rate,
  ROUND(
    SAFE_DIVIDE(lpm.total_engagement_time_sec, lpm.engaged_sessions),
    2
  ) AS avg_engagement_time_sec
FROM landing_page_metrics lpm
LEFT JOIN returning_users ru
  ON lpm.landing_page = ru.landing_page
ORDER BY lpm.pageviews DESC
LIMIT 20;

I am not a fan of cookie-cutter reports. But if you want one, this one could be for you. All you have to do is supply your table ID to generate SQL that works for you.


If you want to customize this report according to your unique data analysis requirements then you need to understand the “logic” used behind calculating the various dimensions and metrics used in this report.

Once you understand the logic, customizing this report is easy peasy.


I teach you such logic in my GA4 BigQuery course, where you can learn to automate SQL generation via text prompts in ChatGPT.

There could be ‘N’ use cases, and it won’t be possible to provide ready-made SQL code for every possible case.

So, it is better that you understand the logic to scale SQL generation to the moon.


The future belongs to those who can query raw GA4 data in BigQuery, apply SQL logic, automate SQL generation via AI and drive insights beyond what GA4 UI can offer.
  1. GA4 BigQuery Video Tracking Report.
  2. Counting GA4 Sessions in BigQuery? Watch for These Caveats.
  3. Calculating User Paths in GA4 BigQuery.
  4. Calculating Conversion Paths in GA4 BigQuery.
  5. Don’t Aggregate Unconsented Events in GA4 BigQuery.
  6. How to track file downloads in GA4 BigQuery.
  7. How to create GA4 Content Group Report in BigQuery.
  8. How to create GA4 Site Search Tracking report in BigQuery.
  9. How to track outbound / external links in GA4 BigQuery data table.
  10. How to track GA4 BigQuery Schema Change.
  11. Stop Splitting GA4 Tables in BigQuery Keep Them Nested.
  12. How To Correctly Calculate GA4 Active Users in BigQuery.
  13. How to Calculate GA4 Engaged Users in BigQuery.
  14. Stop Chasing GA4 Backfills in BigQuery.
  15. Google Analytics 4 Scroll Tracking Report in BigQuery.
  16. Google Analytics 4 Landing Page Report in BigQuery.
  17. Google Analytics 4 Exit Page Report in BigQuery.
  18. Google Analytics 4 Landing Page Dimension in BigQuery.
  19. GA4 (not set) Landing Pages Show 0 Entrances in BigQuery.
  20. Google Analytics 4 Page Title, Page Path & Views in BigQuery