Skip to main content

Samarth Analytics

Testing SQL Models for Marketing Data: Assertions That Catch Attribution Bugs First

By Samarth Analytics · Published · Updated · Analytics Engineering · 7 min read

Summary: Marketing data models fail quietly. A join that fans out doubles revenue, an attribution model that gives more than one hundred percent credit inflates every channel, an unmapped campaign disappears into an unknown bucket, and nobody notices until a number is questioned in a board meeting. This guide covers the kinds of tests available in dbt and Dataform, the specific assertions that catch attribution and revenue bugs, reconciliation and freshness tests, where to run them, and how to turn bugs into tests.

Testing SQL Models for Marketing Data: Assertions That Catch Attribution Bugs First

Marketing models break silently

Software that breaks usually announces it: an error, a crash, a failing page. SQL models that break usually do not. A join that matches more rows than expected still returns a table, just with doubled revenue. A channel mapping that misses a new campaign still runs, just with a growing "unknown" row. An attribution model with a subtle bug still produces numbers, just ones that add up to more conversions than happened.

These bugs are dangerous because they look like data. A dashboard built on them looks perfectly normal until someone compares it with finance, often weeks later and in front of an audience. In our analytics engineering work, tests are the difference between finding these bugs in a pull request and finding them in a board meeting.

The kinds of tests available

Both dbt and Dataform, the two common transformation frameworks for BigQuery, support tests as part of the model code:

  1. Generic tests: a column is unique, is never null, takes only accepted values, or always matches a row in another table. In dbt these are declared in configuration; in Dataform they are assertions such as unique keys, non-null columns and row conditions.
  2. Custom tests: any SQL query that returns rows when something is wrong. The test passes when the query returns nothing.
  3. Freshness tests: a source's latest data is not older than a threshold.
  4. Volume and anomaly tests: a table's row count or a metric's daily total stays within a range.

The generic tests cover the basics. The bugs that matter most in marketing data need custom tests.

Tests that catch attribution bugs

  1. One row per conversion. In the purchases or conversions table, the transaction or lead ID must be unique. Duplicates here inflate everything downstream.
  2. Credit sums to one. In an attribution model, the credit assigned to touchpoints for each conversion must sum to exactly one. More than one means double counting; less means lost conversions.
  3. Revenue is preserved. The total revenue attributed across channels must equal total revenue in the conversions table for the same period. If the attribution step changes the total, something fanned out or dropped.
  4. No time travel. No touchpoint may be credited for a conversion that happened before the touchpoint. Bugs in window logic produce this more often than you would expect.
  5. Mapping coverage. The share of sessions or spend mapped to an "unknown" channel stays below a threshold, such as one percent. A rising unknown share means a new campaign or source needs mapping.
  6. Join fan-out. Row counts before and after each important join match expectations - for example, joining sessions to a campaign lookup must not increase the number of sessions.

Reconciliation tests

Reconciliation tests compare model outputs with independent sources:

  1. Purchases in the modelled GA4 data against orders in the order system, within an agreed tolerance.
  2. Spend in the modelled ad data against each platform's reported totals.
  3. Sessions in the sessions model against GA4's interface, within the small gap expected from estimation, using the definitions in sessionizing GA4 BigQuery events.

These tests catch problems that internal tests cannot, such as an upstream export that silently dropped a day, or a tracking change that halved purchase events. Schema changes upstream are a common cause, which is why event schema versioning and these tests work best together.

Tip: Give every reconciliation test a tolerance agreed with the business, and record it in the test's description. A test that fails at any difference will be ignored within a week; one that fails only when the gap matters will be trusted.

Freshness and volume tests

Most marketing models depend on sources that arrive on a schedule: the GA4 daily export, the Google Ads transfer, connector loads. Add freshness tests to each source, so a missing day is caught before the models build on incomplete data, and volume tests to key tables, so a sudden halving of events is flagged even if the data technically arrived. The alerting side is covered in alerting on data freshness and schema changes.

Severity and where tests run

Not every failure should stop the pipeline. Classify tests as warnings or errors. Errors - duplicate conversions, credit not summing to one, a missing source day - should block the reporting tables from updating, so dashboards keep showing the last good data rather than wrong data. Warnings - a slightly rising unknown share, a reconciliation gap near its tolerance - should notify the owner without blocking.

Run tests in two places: on every scheduled build, and on every proposed change in continuous integration, so a pull request that introduces a fan-out fails before it is merged. The targets you set for these checks form part of the data quality SLAs the business can rely on.

A worked example: doubled revenue caught in review

A team adds a new touchpoint source - email clicks from their email platform - to a multi-touch attribution model. The change joins email clicks to sessions on the customer ID, but some customers have several email addresses in the platform, so some sessions match two email records. Revenue by channel rises by eight percent, mostly in email, and the dashboard looks like good news.

The "credit sums to one" test fails in continuous integration on the pull request: a few hundred conversions now have credit summing to two. The engineer traces the fan-out to the join, deduplicates email records before joining, and the test passes. Without the test, the email team would have reported an eight percent revenue lift that did not exist, and budget would have moved on the strength of it.

Starting small: the first five tests

A team with no tests should not try to write fifty at once. Five tests cover most of the risk in a typical marketing model:

  1. Transaction or lead ID is unique in the conversions table.
  2. Attribution credit sums to one per conversion.
  3. Total attributed revenue equals total conversion revenue for each day.
  4. The unknown channel share is below an agreed threshold.
  5. The GA4 export and the ad platform sources are no more than a day old.

Add them in a week, make the first three block the reporting tables, and live with them for a month. Most teams find at least one real problem in that first month, which is usually all the persuasion needed to keep going.

Turn every bug into a test

The most valuable habit in data testing is simple: whenever a bug reaches production, write the test that would have caught it, before fixing it. Over a year, the test suite becomes a record of every way your models have failed, and those failures stop recurring. Combined with the modelling patterns in GA4 to dbt and the observability practices in our data quality and observability guide, it turns a fragile pipeline into one the business trusts.

Final thoughts

Marketing SQL models fail silently, and the failures look like data until someone compares them with reality. Unique conversion keys, credit that sums to one, preserved revenue, no time travel, mapping coverage, fan-out checks, reconciliation against independent sources, and freshness tests catch most of them. Classify severity, run tests on every build and every change, and turn every bug into a test.

If your marketing models have no tests, a free audit will show which of these checks would fail today.

Topics: analytics engineering, analytics QA, dbt, data integrity, BigQuery

More articles on the Samarth Analytics blog · Markdown version of this article