Currently Available: Need a skilled Software Developer for your next project?
Categories
SEO, GEO and AEO

Reconciling AI Referrals in GA4 BigQuery and Surveys

When a website owner compares GA4 referral data with a survey answer such as “ChatGPT,” the two records may describe different moments. GA4 records what its measurement setup observed for a visit. A survey records what a respondent remembers or reports, such as where they first heard about the site. Adding the two counts treats two kinds of evidence as extra visits and inflates the visit total.

Store the two observations in separate fields. Join them only when both records share a reliable identifier for the same session. When the records match, you can describe one measured session alongside one reported answer. The match does not prove that the survey answer caused the visit or that GA4 recorded AI as the referrer. When GA4 reports (direct) / (none), leaves source data blank, or has no matching survey record, keep that uncertainty. Do not assign an AI source.

Separate a visit from a discovery answer

GA4’s BigQuery export stores one record for each event, such as a page view or purchase, with extra details called event parameters and traffic-source fields. A survey answer has a different basis. It reflects the respondent’s answer to a particular question, with its wording and time frame.

Ask about first discovery separately from what prompted the current visit. For example, “Where did you first hear about us?” and “What prompted this visit today?” measure different things. Include choices such as “I’m not sure” and “Other,” and let respondents name an AI assistant if they remember which one. A person can report AI as their first discovery source while GA4 records a later visit as direct, search, or a referral from another site.

Keep those measures side by side. GA4’s BigQuery export schema includes collected_traffic_source, which describes campaign or referral information collected with an event, and session_traffic_source_last_click, which holds session-level last-click source information where available. page_referrer can show a referrer captured on a page event. Those fields do not show when the respondent first learned about the site or every step they took before the visit.

Choose a join key the survey actually has

The preferred join key is an opaque, random token created for one session and stored with both the survey response and a GA4 event. “Opaque” means the token does not reveal a person’s identity. Send the token as an event parameter, keep it in BigQuery to join the records, and use a different token for each session. The token-based join is an implementation pattern; GA4 has no built-in attribution field for it.

If the survey system captured the same GA4 identifiers for the same visit, use stream_id, user_pseudo_id, and ga_session_id together. ga_session_id is the session identifier found in the event’s event_params array; paired with user_pseudo_id, it identifies a session in the export. Google’s BigQuery query examples show how to extract event parameters such as ga_session_id.

Do not infer a match from a shared date, landing page, or approximate timestamp. If neither a shared token nor the same session identifiers exist in both records, report the survey and GA4 totals separately. Matching records on weak clues creates false precision.

Before joining, check that each token matches no more than one GA4 session and one survey response after you remove duplicates. A reused token can make one answer appear on multiple sessions. Resolve that data-quality problem before calculating session totals.

Keep the useful fields

Extract ga_session_id from event_params. Keep stream_id and user_pseudo_id to group events from the same session. For source reporting, retain the session last-click source and medium, the collected source and medium, and the field that supplied the value. This preserves the difference between session-level attribution and information collected on an individual event.

Keep page_referrer and page_location when you need to inspect the arriving page. When GA4 captures no referrer for an event, the visitor’s route remains unknown, including whether they arrived directly.

For surveys sent into GA4, an event parameter such as discovery_reported can hold a category from a short, fixed list: ai_assistant, search_engine, colleague, other, or unsure. Add a separate parameter such as discovery_question if the event could represent either first discovery or the reason for today’s visit. You can register a categorical event parameter as an event-scoped custom dimension to use it in GA4 reports. BigQuery queries can read the exported event parameter without that registration.

Keep unique join tokens in BigQuery rather than registering each token as a custom dimension. Google cautions against unnecessary high-cardinality dimensions, which are dimensions with many distinct values. Reporting dimensions are a poor fit for values that differ for every session. Keep detailed survey answers in a controlled first-party data store. Do not send names, email addresses, or free-text answers that could contain personal information to GA4; Google’s data safeguards prohibit sending personally identifiable information to Analytics.

Build one row per session before joining

The query below uses a session-scoped token shared by GA4 and a first-party survey table. It groups GA4 events by stream, pseudonymous user, and session, then keeps the earliest completed survey response per token. That earliest-response rule is an explicit choice for this example; use a different documented rule if the survey design calls for one.

Article setup: technical explainer (private trial 2026-10-04). The table names and schema below are placeholders, not trial results. Replace them with the fields and paths in your implementation.

-- Replace the table paths and date range with your own values.
-- The example assumes session_link_id is stored in both systems.

WITH event_rows AS (
  SELECT
    stream_id,
    user_pseudo_id,
    event_timestamp,
    event_name,

    (
      SELECT value.int_value
      FROM UNNEST(event_params)
      WHERE key = 'ga_session_id'
      LIMIT 1
    ) AS ga_session_id,

    (
      SELECT value.string_value
      FROM UNNEST(event_params)
      WHERE key = 'session_link_id'
      LIMIT 1
    ) AS session_link_id,

    (
      SELECT value.string_value
      FROM UNNEST(event_params)
      WHERE key = 'discovery_reported'
      LIMIT 1
    ) AS discovery_reported,

    (
      SELECT value.string_value
      FROM UNNEST(event_params)
      WHERE key = 'page_referrer'
      LIMIT 1
    ) AS page_referrer,

    session_traffic_source_last_click.cross_channel_campaign.source
      AS last_click_source,
    session_traffic_source_last_click.cross_channel_campaign.medium
      AS last_click_medium,

    collected_traffic_source.manual_source AS collected_source,
    collected_traffic_source.manual_medium AS collected_medium

  FROM <code>PROJECT.analytics_PROPERTY_ID.events_*
  WHERE _TABLE_SUFFIX BETWEEN 'YYYYMMDD' AND 'YYYYMMDD'
),

sessions AS (
  SELECT
    stream_id,
    user_pseudo_id,
    ga_session_id,

    ARRAY_AGG(
      session_link_id IGNORE NULLS
      ORDER BY event_timestamp
      LIMIT 1
    )[SAFE_OFFSET(0)] AS session_link_id,

    ARRAY_AGG(
      IF(
        last_click_source IS NOT NULL OR last_click_medium IS NOT NULL,
        STRUCT(
          last_click_source AS source,
          last_click_medium AS medium
        ),
        NULL
      )
      IGNORE NULLS
      ORDER BY event_timestamp
      LIMIT 1
    )[SAFE_OFFSET(0)] AS last_click,

    ARRAY_AGG(
      IF(
        collected_source IS NOT NULL OR collected_medium IS NOT NULL,
        STRUCT(
          collected_source AS source,
          collected_medium AS medium
        ),
        NULL
      )
      IGNORE NULLS
      ORDER BY event_timestamp
      LIMIT 1
    )[SAFE_OFFSET(0)] AS collected,

    ARRAY_AGG(
      page_referrer IGNORE NULLS
      ORDER BY event_timestamp
      LIMIT 1
    )[SAFE_OFFSET(0)] AS first_captured_page_referrer,

    MIN(event_timestamp) AS first_event_timestamp,
    COUNTIF(event_name = 'purchase') AS purchase_event_count

  FROM event_rows
  WHERE user_pseudo_id IS NOT NULL
    AND ga_session_id IS NOT NULL
  GROUP BY stream_id, user_pseudo_id, ga_session_id
),

survey_dedup AS (
  SELECT
    session_link_id,
    discovery_question,
    discovery_answer,
    submitted_at,
    response_id
  FROM PROJECT.first_party.survey_responses
  WHERE session_link_id IS NOT NULL
    AND consent_to_link = TRUE
    AND response_status = 'complete'
  QUALIFY ROW_NUMBER() OVER (
    PARTITION BY session_link_id
    ORDER BY submitted_at ASC, response_id
  ) = 1
),

joined AS (
  SELECT
    s.stream_id,
    s.user_pseudo_id,
    s.ga_session_id,
    s.session_link_id,
    s.first_event_timestamp,

    COALESCE(s.last_click.source, s.collected.source)
      AS ga_source_observed,
    COALESCE(s.last_click.medium, s.collected.medium)
      AS ga_medium_observed,

    CASE
      WHEN s.last_click.source IS NOT NULL
        OR s.last_click.medium IS NOT NULL
        THEN 'session_traffic_source_last_click'
      WHEN s.collected.source IS NOT NULL
        OR s.collected.medium IS NOT NULL
        THEN 'collected_traffic_source'
      ELSE 'no_source_value'
    END AS ga_source_field_used,

    s.first_captured_page_referrer,
    s.purchase_event_count,
    q.discovery_question,
    q.discovery_answer,
    q.submitted_at AS survey_submitted_at

  FROM sessions AS s
  LEFT JOIN survey_dedup AS q
    USING (session_link_id)
)

SELECT
  *,
  CASE
    WHEN discovery_answer IS NULL THEN 'no_linked_survey'
    WHEN ga_source_observed IS NULL AND ga_medium_observed IS NULL
      THEN 'survey_answer_with_missing_ga_source'
    WHEN LOWER(ga_source_observed) = '(direct)'
      AND LOWER(ga_medium_observed) = '(none)'
      THEN 'survey_answer_with_direct_none'
    WHEN ga_source_observed IS NULL OR ga_medium_observed IS NULL
      THEN 'survey_answer_with_incomplete_ga_source'
    ELSE 'survey_answer_with_ga_source'
  END AS evidence_status
FROM joined;

The query uses the session last-click values first and the event-collected values as a fallback. ga_source_field_used makes that choice visible. Keep the two source types separate in the final report if the distinction matters to your analysis; the fallback is for producing a convenient observed value, not proof that the fields mean the same thing.

The LEFT JOIN keeps GA4 sessions with no linked survey. Audit the survey table separately for completed responses with no matching token so they remain visible in survey totals. Also verify token uniqueness before joining. If a token maps to multiple sessions, the join can multiply rows and inflate session or purchase counts.

The query first groups events into one row per session, then joins the survey data. This keeps page views and other repeated events from turning one visit into several referral records. It counts purchases within each session, but a purchase count and a survey-response count remain different measures.

Keep direct, missing, and unknown distinct

GA4’s (direct) / (none) label means it did not identify a clear referral source. It does not prove that someone typed a URL or used a bookmark. Google lists missing campaign details, redirects, URL shorteners, offline documents, and ad blockers among reasons a visit can receive that label in its direct traffic guidance.

A linked survey answer can support a report such as “the respondent reported first hearing about the site from an AI assistant.” It does not change the GA4 source for that session. Do not relabel direct sessions as AI referrals because some survey respondents selected AI.

Keep at least these states separate in reporting: GA4 reports direct/none; GA4 source fields are missing or incomplete; the session has no linked survey; a survey response has no matching session; and the respondent selected “unsure.” GA4 also uses labels such as “unassigned” and “(not set)” for distinct reporting conditions, so preserve the exported values rather than folding them into direct traffic. Google describes some causes in its tagging guidance.

Report coverage without adding the counts

Choose one unit for each metric. Count GA4 sessions as sessions and survey responses as responses. Count first-discovery answers as respondent reports. Do not add survey responses to GA4 sessions or conversions. A survey answer adds information to a matched session. It does not create another visit.

Report the number of GA4 sessions, sessions with a linked completed survey, completed survey responses without a matching session, and linked sessions in each source category. Show GA4 source and medium beside the survey question and answer so readers can see agreement and disagreement without forcing one record to overwrite the other.

BigQuery results also do not necessarily match every number in the GA4 interface. Google explains that the interface can apply modeling, thresholding, and identity handling that the export does not reproduce in the same way, and that some modeled data is not exported. Treat a BigQuery analysis as a defined analysis of exported data, rather than a perfect reconstruction of every interface report. For the event-query mechanics, see querying AI referrals in BigQuery. For the survey side of the distinction, see combining self-reported discovery with analytics.

What I'm building

Delegate tasks. Get software.

Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.

Take a look at vroni.com

Email updates

Usually a new article and a few links I found interesting.

No spam. Unsubscribe with one click.

Leave a Reply

Your email address will not be published. Required fields are marked *