Unlock the power of Google Analytics with Big Query

Rebuilding Attribution Models in GA4 with BigQuery After Consent Delay

Google Analytics 4’s Consent Mode helps websites respect user privacy by adjusting data collection based on consent. While this is essential for GDPR and CCPA compliance, it introduces a challenge: attribution gaps.

When users initially deny consent but later accept it (or give partial consent), GA4 sessions and conversions may get split or missed in the UI reports. This results in incomplete or inaccurate attribution models, complicating marketing analysis.

Why Attribution is Missing in Standard GA4 UI
SQL Model: Assigning Sessions to Source/Medium After Consent

To rebuild accurate attribution, you can use BigQuery to:

WITH session_data AS (
  SELECT
    user_pseudo_id,
    ga_session_id,
    event_timestamp,
    traffic_source.source,
    traffic_source.medium,
    consent_given,
    -- Assume a boolean field `consent_given` indicates consent status per event
    FIRST_VALUE(traffic_source.source) OVER (
      PARTITION BY user_pseudo_id
      ORDER BY CASE WHEN consent_given THEN event_timestamp ELSE NULL END
      ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
    ) AS modeled_source,
    FIRST_VALUE(traffic_source.medium) OVER (
      PARTITION BY user_pseudo_id
      ORDER BY CASE WHEN consent_given THEN event_timestamp ELSE NULL END
      ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
    ) AS modeled_medium
  FROM
    `your_project.your_dataset.events_*`
)

SELECT
  user_pseudo_id,
  ga_session_id,
  event_timestamp,
  COALESCE(traffic_source.source, modeled_source) AS final_source,
  COALESCE(traffic_source.medium, modeled_medium) AS final_medium
FROM
  session_data
WHERE
  event_name = 'purchase' -- or any conversion event
ORDER BY
  event_timestamp
Optional Enhancements
Related Posts
TL;DR

Discover more from GA4 BigQuery

Subscribe now to keep reading and get access to the full archive.

Continue reading