← Back to blog
DBT · DATA QUALITY

GA4 data quality with dbt: five checks and what they found in the sample dataset

On top of my dbt model of the GA4 export I added a data quality layer. It’s not a pretty dashboard: it’s a table with one row per day and a set of tests that fail or warn when something goes wrong in the tracking. And when I ran it over Google’s sample dataset, it found things that normal modelling hides.

The model: it only measures

dq_daily_health has one row per day with these indicators, calculated on the already flattened events table:

  • events, page views and sessions;
  • events with no session (ga_session_id null);
  • events with an email in the page or referrer URL;
  • purchase events, distinct transactions, repeated purchases and purchases without a transaction_id;
  • zero-value purchases;
  • the ratio between the day’s volume and the average of the previous 7 days.

Email detection looks at the plain text and the encoded form (%40), as well as typical form parameters:

regexp_contains(lower(coalesce(page_location, '')),
  r'[a-z0-9._%+-]+(@|%40)[a-z0-9.-]+\.[a-z]{2,}')
or regexp_contains(lower(coalesce(page_location, '')),
  r'[?&](e-?mail|mail|correo)=')

The tests: this is where the thresholds live

Check Severity
Any event with an email in the URL error (breaks the build)
Purchases with a valid transaction_id and zero value warning
More than 5% of purchase events repeated in a day warning
More than 5% of purchases without a valid transaction_id warning
Day’s volume below 50% of the previous average warning

Making an email in a URL an error and the rest warnings is a decision: an email sent to GA4 is a privacy problem, and the others are symptoms you need to look at.

Unit tests for the logic

What mattered most to me was not trusting my own SQL. dbt lets you write unit tests with simulated rows. One of mine feeds in six events with two URLs containing an email (one in plain text and one with %40), a duplicated purchase, a zero-value purchase and one without a valid transaction_id, and expects exactly the right counts per day. Another checks the volume ratio with three days of simple numbers.

What it found in the sample dataset

I ran the layer over the 92 days of Google’s public sample dataset (about 4.3 million events):

  • 5,692 purchase events, but only 4,466 distinct valid transactions. That’s 320 duplicated events. On 11 days the duplicates exceed 5% of the day’s purchases, and the worst day reaches almost 49%. The purchases mart already deduplicates them, but the test watches the source: if it happens in production, someone is reloading the confirmation page or there’s a duplicated tag.
  • 906 purchases (16%) without a valid transaction_id, with the value (not set). They can’t be deduplicated or reconciled against the backend, and the mart excludes them. On 42 days they exceed 5%.
  • Zero purchases without value, zero emails in URLs and no volume drops. The dataset is a sample and is obfuscated, so it’s clean on those points.

What matters isn’t the numbers from a sample dataset, but the exercise: without this layer, the model produced correct tables and the loss of 906 purchases was invisible everywhere.

What it doesn’t include

I don’t measure the ratio of events without consent. The sample dataset is from 2020 and doesn’t carry that information; it only makes sense on your own export.

The code is in the model’s repository.