Follow me on LinkedIn - AI, GA4, BigQuery

Use the following SQL (automatically generated via a text prompt in ChatGPT) to track Google Analytics 4 AI Traffic in BigQuery:

-- Calculate AI Traffic.

SELECT
  COALESCE(NULLIF(traffic_source.source, ''), 'null') AS traffic_source,
  COALESCE(NULLIF(traffic_source.medium, ''), 'null') AS traffic_medium,
  COUNT(
    DISTINCT
      CONCAT(
        user_pseudo_id,
        CAST(
          (
            SELECT value.int_value
            FROM UNNEST(event_params)
            WHERE key = 'ga_session_id'
          )
          AS STRING))) AS sessions,
  COUNT(
    DISTINCT
      CASE
        WHEN
          (
            SELECT value.string_value
            FROM UNNEST(event_params)
            WHERE key = 'session_engaged'
          )
          = '1'
          THEN
            CONCAT(
              user_pseudo_id,
              CAST(
                (
                  SELECT value.int_value
                  FROM UNNEST(event_params)
                  WHERE key = 'ga_session_id'
                )
                AS STRING))
        END) AS engaged_sessions
FROM
  `dbrt-ga4.analytics_207472454.events_*`
WHERE
  REGEXP_CONTAINS(
    traffic_source.source,
    r'^.*ai$|.*openai.*|.*copilot.*|.*chatgpt.*|.*gemini.*$')
  AND _TABLE_SUFFIX BETWEEN '20241001' AND '20241031'
GROUP BY
  traffic_source,
  traffic_medium
ORDER BY
  sessions DESC;

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 metrics 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. How to create GA4 Site Search Tracking report in BigQuery.
  2. How to track outbound / external links in GA4 BigQuery data table.
  3. How to track GA4 BigQuery Schema Change.
  4. Stop Splitting GA4 Tables in BigQuery Keep Them Nested.
  5. How To Correctly Calculate GA4 Active Users in BigQuery.
  6. How to Calculate GA4 Engaged Users in BigQuery.
  7. Stop Chasing GA4 Backfills in BigQuery.
  8. Google Analytics 4 Scroll Tracking Report in BigQuery.
  9. Google Analytics 4 Landing Page Report in BigQuery.
  10. Google Analytics 4 Exit Page Report in BigQuery.
  11. Google Analytics 4 Landing Page Dimension in BigQuery.
  12. GA4 (not set) Landing Pages Show 0 Entrances in BigQuery.
  13. Google Analytics 4 Page Title, Page Path & Views in BigQuery
  14. Google Analytics 4 Unique Pageviews in BigQuery.
  15. Google Analytics 4 Traffic Attribution in BigQuery.
  16. Finding Real Google Analytics 4 Conversion Paths in BigQuery.
  17. Tracking Google Analytics 4 UTM Parameters in BigQuery.
  18. Tracking Google Analytics 4 AI Traffic in BigQuery.
  19. Tracking peak time for Website Traffic in GA4 BigQuery.
  20. How to Correctly Work With Google Analytics Data Types in BigQuery.