How to build an identity graph and rules-based attribution models in BigQuery hero image

How to build an identity graph and rules-based attribution models in BigQuery

1 Dec 2026 • 17 min read

GA4's BigQuery export only credits conversions per browser, and it won't give you a model it doesn't ship. Here's how I built a deterministic identity graph in Dataform, then a set of rules-based attribution models on top of it (last, first and linear non-direct), and what changed in the numbers. There's a blueprint at the end you can load into Claude to build your own.


In part 1 I built a marketing data warehouse in BigQuery and Dataform, and an AI analyst on top of it (I called it Flippy), with one strict rule - every attributed number names its model. At the time that meant three models: GA4's last non-direct click, GA4's first click and each ad platform's own reporting. I said custom attribution models would come later, and this is that post.

The ask sounded simple enough: a linear model with a non-direct element, so each touchpoint in the 90 days before a booking gets an equal share. When I started scoping it, though, two things became obvious. GA4's view of a "user" is a browser (its user_pseudo_id, which GA4's interface calls the device ID), so the same customer on their phone and laptop is two people with two half-journeys. And a model built straight on top of GA4 sessions would inherit the way GA4 labels them, which is not the same as how someone actually arrived. So I paused the model and built the identity layer first.

Why GA4's attribution wasn't enough

GA4 does a decent job within one browser, but it falls short in a few ways that matter for a business where conversions can come from logged-in, returning customers.

  • The BigQuery export credits each browser on its own. If someone clicks an ad on their phone and books on their laptop a week later, the export's attribution fields credit the laptop's history, which is often Direct. (GA4's interface can stitch some of this with User-ID, but the export works per browser.) On this build, GA4's last non-direct click put roughly three times as many bookings on Direct as the warehouse's version did once devices were linked.
  • The traffic source labels are inherited. session_traffic_source_last_click is already last non-direct, so a direct return carries the previous campaign's channel source. That's fine for GA4's own reports, but if you treat every session as a touchpoint, one email click can count three or four times in a linear model.

Diagram: a customer clicks a Google Ads search ad on their phone and books on their laptop a week later. Linked as one customer, the booking goes to Google Ads; in GA4's export the two browsers are separate users, so the laptop booking is credited to Direct
The same booking, credited to Google Ads once both browsers are linked, and to Direct when they aren't

On top of that, Safari's tracking prevention caps first-party cookie life, so the same person on the same iPhone can turn up as a new browser every week or so. Any model that treats the browser as the person will over-credit whatever brought them back after the cookie reset.

Building the identity graph first

The phrase "identity graph" sounds like CDP territory, and it is, but the version you need for attribution is much smaller than a CDP. Think of it as a lookup that says "these browsers belong to the same customer", built from evidence you already hold, with rules you can explain to a DPO.

The rules I settled on

After reading up on how CDPs and the big identity vendors approach it, I went with the conservative end of the scale.

  • Deterministic only. A browser and a customer are linked only when the customer logged in on that browser (GA4's user_id). No fingerprinting, no IP matching, no probabilistic stitching. (More on this in a future post in the series!)
  • Consented data only. Observations come from sessions where analytics consent was granted. Unconsented activity stays unconsented.
  • One anchor per profile. The customer ID is the anchor, and a profile holds at most one. Browsers attach to their strongest anchor, measured by the number of days the two were seen together, then how recently.
  • Shared devices are quarantined. A browser linked to more than three customer IDs is almost certainly a shared machine, so its links are dropped and logged rather than merging several customers into one. Two or three keeps the strongest link only.
  • No cap on browsers per customer. Because of Safari, a loyal customer can genuinely have dozens. One account I looked into appeared on over a hundred user_pseudo_id values, and every one of them was Safari on that customer's Mac or iPhone, so it was the cookie resetting rather than a shared device. That's why there's an alert threshold rather than a hard cap.
  • Rebuilt from scratch every run. Every identity table is a full rebuild, so a rule change, a new ID type or a right-to-erasure deletion flows through on the next run with no backfill. The cost is that history restates slightly as the graph learns, which I think is the right trade for attribution (and is how every CDP behaves anyway).

Note: if you're in the UK, the Data (Use and Access) Act 2025 loosened the consent rules for some analytics cookies, but it doesn't change the case for stitching consented data only. I'd still run any identity work past whoever owns your DPIA first.

Five tables, two of which you'll actually use

The graph lives in staging and core like everything else from part 1, rather than in a separate restricted dataset, because the only identifiers in it so far are a GA4 client ID and a customer number that core already held. If emails or CRM IDs arrive later, that's the point to split it out.

  • int_identity__observations - one row per day an ID was seen, and what it was seen with.
  • int_identity__edges - every link between two IDs, with a status: kept, blocked, quarantined or dropped as the weaker link.
  • int_identity__quarantine - the links that weren't kept and why, for review after any rule change.
  • dim_identity_map - one row per ID per strategy, giving its profile_key. This is the one you join to.
  • dim_identity_profiles - one row per profile, with counts of each ID type, a staff flag for internal users and whether any of its IDs were quarantined. This is the one you report on and sanity-check.

The profile_key is a SHA-256 hash of the anchor ID, so it's stable across runs and doesn't expose the customer number. It's never stamped onto the incremental fact tables (they keep their native IDs), and it never reaches the reporting layer or Flippy. Models resolve it at build time with a join to the map.

There are also two strategies - browser where every browser is its own profile (GA4's view, useful as a baseline), and customer where browsers are grouped under their customer ID. The attribution models use the customer strategy, and adding a third later (say, an email-based one) is a config change rather than a redesign.

Diagram: three browsers (a phone, a laptop and the same phone after a cookie reset) each link to one customer ID and form one profile, while a shared browser logged in by five customers is quarantined rather than merged
Browsers attach to their strongest customer ID - a browser linked to too many customers is quarantined, not merged

Relabelling staff rather than excluding them

One handy side effect of having profiles is that internal traffic stops being guesswork. The customer data held an account type, and staff accounts are their own type, so any profile with a staff account is flagged. Rather than filtering staff out (which makes your totals disagree with GA4 and every other report), the reporting tables move their activity onto a staff channel. Totals still add up, other channels stop being inflated, and staff traffic turned out to be a small but uneven slice - well under 1% of sessions overall, but a few percent of one paid search channel.

Touchpoints are arrivals, not sessions

My first version of the models used sessions as touchpoints, with a check that the session had its own campaign signal rather than an inherited label. It worked, but when I walked through a few real journeys, two things didn't sit right. A session that started directly and then had an email click halfway through only counted the direct start, and plenty of "touchpoints" were the same landing recorded twice. So the touchpoint became an "arrival" - any moment someone came to the site from somewhere, even if mid-session.

What counts as an arrival

  • A landing - a session that brought its own campaign tags, an ad click ID or an external referrer, and that GA4 doesn't label as Direct.
  • A mid-session return - a page view more than 60 seconds into a session that carries a new source in collected_traffic_source or a new click ID. Every return counts, including someone coming back through organic search or a referral, not just tagged links.

Diagram: one GA4 session on a timeline - a Google Ads landing, the same tags repeated two seconds later and ignored, a newsletter click twelve minutes in, then a booking. Counting sessions gives one touchpoint (Google Ads); counting arrivals gives two (Google Ads, then email)
A session only records how the visit started, while arrivals also catch the email click part-way through

Two GA4 export details made this possible. collected_traffic_source holds the UTMs at the event level, even when GA4's processing has stripped them from page_location before the data reaches the BigQuery export, which was the case for about a third of tagged sessions here. And as it's set per event, not per session, we can see a return part-way through a visit. The 60-second rule is there because the same landing often turns up on both session_start and the first page_view, which on the first attempt made about one in six sessions that came from a campaign look like they'd had two.

Click IDs without UTMs get a sensible mapping (gclid, gbraid and wbraid to google/cpc, msclkid to bing/cpc, srsltid to google/organic for shopping results and so on), and every arrival keeps the source, medium and campaign it came with.

Note: GA4's ga_session_id is a timestamp, so it isn't unique. The usual advice is to concatenate it with user_pseudo_id, but even that repeated a handful of times, days apart, so every join in the attribution layer uses the key and the date.

Deduping repeats

Once mid-session returns count, you get noise: someone clicking the same email twice in a minute, or bouncing between two tabs. The rule is that back-to-back arrivals from the same source and medium within 30 minutes count once, across any of the customer's browsers.

The "back-to-back" bit matters more than it looks. My first version dropped any arrival from a source that had arrived in the previous 30 minutes, regardless of what came in between. For a journey like Google Ads → Google organic → Google Ads, it dropped the second ad click. So the booking went to organic, and it moved around 13% of Google Ads' last non-direct bookings to other channels across two months. Comparing each arrival only to the one straight before it fixed that.

deduped AS (
  SELECT *
  FROM (
    SELECT
      *,
      LAG(STRUCT(touchpoint_ts AS ts, LOWER(source) AS source, LOWER(medium) AS medium))
        OVER (PARTITION BY profile_key ORDER BY touchpoint_ts, arrival_type, session_key) AS prev
    FROM arrivals
  )
  WHERE prev.ts IS NULL
     OR NOT (prev.source = LOWER(source) AND prev.medium = LOWER(medium)
             AND touchpoint_ts < TIMESTAMP_ADD(prev.ts, INTERVAL 30 MINUTE))
)

The attribution models

One credits table for every model

Rather than a column per model on each fact, every model writes to one table, fct_attribution_credits, with one row per attributed thing, per model, per credited touchpoint, and a credit_share. A last-click model writes one row with a share of 1, a linear model writes one row per arrival with a share of 1/n, and the shares for any conversion and model always sum to exactly 1.

For a booking with four arrivals in its window, the last non-direct and linear rows look something like this (first non-direct would add one more row, for the Google Ads click):

Model Credited to Days before Resolution Share
last_non_direct newsletter / email 0 arrival_same_session 1
linear_non_direct google / cpc 20 arrival_other_browser 0.25
linear_non_direct google / organic 9 arrival_same_browser 0.25
linear_non_direct facebook / paid_social 4 arrival_same_browser 0.25
linear_non_direct newsletter / email 0 arrival_same_session 0.25

The table is deliberately generic. The attributed object can be a session, a booking or a sign-up (attributed_object_type), the touchpoint has a type (web_arrival for now), and nothing in it assumes GA4. When offline touchpoints like a mailing or a call centre contact arrive, they become another touchpoint type in the same timeline rather than another model.

Each row also carries an attribution_resolution, which says where the credit came from - the same session, an earlier session on the same browser, another of the customer's browsers, Direct (no arrival in the window) or unconsented. That one column turned out to be the most useful thing for explaining the change to the marketing team, because it shows exactly how much credit only exists because of the identity graph.

Last, first and linear non-direct

All three use a 90-day window, set once as a project variable, and each conversion looks back independently even if another conversion by the same customer covers the same period.

  • Last non-direct - the most recent arrival on the customer's timeline in the 90 days before, on any browser, else Direct. It applies to sessions as well as conversions, so traffic reports use it too. I did consider keeping GA4's label when it isn't Direct and only filling the gaps, but that would mean a mid-session email click never wins, so the model always takes the last arrival.
  • First non-direct - the earliest arrival in the window. This replaced GA4's first click, which is per browser and has no window, so it happily credits a channel from two years ago on one device. It also skips Direct, which GA4's version doesn't.
  • Linear non-direct - an equal share for every arrival in the window. Counts become fractions, which is fine in the warehouse but worth a line in any report that shows them.

Keeping GA4's own view

I renamed GA4's own label ga4_last_non_direct and kept it as a model, purely for reconciling with the GA4 interface. The rule from part 1 still holds, so every column says which model it belongs to (attribution_linear_non_direct_bookings, attribution_ga4_last_non_direct_bookings), and model names only mention GA4 when the logic really is GA4's. Flippy got the new models the same day, with GA4's flagged as "for reconciling only" so it doesn't become the default answer by accident.

Capping long journeys

Linear models have a reputation for spreading credit too thin on long journeys, and the median path here was seven arrivals, with a long tail - one in six bookings had more than 20, and the top 10% had 30 or more.

Histogram of marketing arrivals in the 90 days before each booking, peaking at one arrival, with the median of seven highlighted and a long tail past 40
Seven arrivals for the median booking, and a long tail

I tested keeping only the last 20 arrivals before deciding. It moved every main channel by less than 2%, because the long paths are mostly the same two or three channels repeating, so I left it uncapped. A cap of 10 started tilting credit towards the channels nearest the booking, which defeats the point of a linear model.

What changed in the numbers

Looking at two months of consented bookings, with every model crediting exactly the same total:

  • Direct fell by about two-thirds under last non-direct compared with GA4's own label. Most "direct" bookings were people who'd come through a channel earlier, often on another device.
  • Organic search rose by around 60% and app referrals by about a quarter, while Google Ads dropped about 10% under last non-direct. A lot of people click an ad, then come back through organic search or the app before booking, and the session-level label was hiding those returns.
  • Google Ads' share was highest under first non-direct, at about 45% of bookings against 39% under GA4's label, and email's role in starting journeys was roughly double its last-click credit.

Grouped bar chart of the share of bookings by channel under GA4 last non-direct, last non-direct, first non-direct and linear non-direct. Direct drops from 15% under GA4's label to 5% under every warehouse model
Same bookings, four models - the totals match, the channel split doesn't

None of this means GA4 is wrong. It answers a narrower question (what channel did this browser last come from) and the warehouse models answer the one the marketing team was actually asking. Expect the figures to restate slightly as the identity graph learns more links. It's worth telling stakeholders that up front, and Flippy adds a reminder to its answers too, which helps.

How much of it is the identity graph

The attribution_resolution column shows where the change comes from. Under last non-direct, about one in eight consented bookings is credited to an arrival on a different browser from the one that booked, and under first non-direct it's nearly half, which is credit a per-browser model simply can't give. To be fair, "another browser" also includes the same iPhone after Safari has reset its cookie, so not all of it is strictly cross-device.

Stacked bars showing where each booking's credit comes from. Last non-direct: 73% same session, 10% earlier visit on the same browser, 13% another of the customer's browsers, 5% Direct. First non-direct: 12%, 35%, 48% and 5%
Nearly half of first non-direct credit comes from another of the customer's browsers

Openers and closers

Dividing each channel's first non-direct credit by its last non-direct credit shows which channels tend to start journeys and which tend to finish them. Email starts about twice as many as it finishes, and paid social and Google Ads lean towards opening too, while the app and organic search are mostly how people come back to book.

Bar chart of first non-direct credit divided by last non-direct credit by channel: Email 2.07, Paid Social 1.65, Other 1.52, Google paid search 1.29, Microsoft paid search 0.87, Organic Search 0.74, App 0.58
Above 1 opens more journeys than it closes, below 1 closes more than it opens

My rule of thumb is to judge openers on first non-direct or linear, and closers on last non-direct. Cutting an opener because its last-click number looks weak quietly shrinks what the closers have to work with. Linear sits in between (i.e. who helped along the way), which makes it a fairer basis for splitting a total budget across channels.

Marketers can ask Flippy this directly now, and the app and email move just as the chart suggests.

Claude answering
Flippy comparing linear and last non-direct by channel

One booking, four answers

Here's an example five-arrival journey, based on a real one from the two months:

Day Browser Arrival
-88 Laptop (browser B) Organic search
-10 Phone (browser A) Paid search (campaign x)
-7 Phone (browser A) Paid search (campaign y)
0 Phone (browser A) Organic search
0 Phone (browser A) App (returning mid-visit and converting)

And here's how each model credits it:

Model App Organic search Paid search
GA4 last non-direct 100%
Last non-direct 100%
GA4 first 100%
First non-direct 100%
Linear non-direct 20% 40% 40%

So GA4 and the warehouse models don't agree on this one booking, and they're not meant to - each answers a slightly different question and each has its blind spots. Having them side by side is what lets you build the story of how people actually book, and make better decisions from it, which is the same point I made in my BrightonSEO talk on the attribution illusion.

Testing it and releasing it

Everything went through the same dev-then-production flow as part 1, with a few attribution-specific checks as Dataform assertions:

  • Shares sum to 1 for every conversion and model.
  • Coverage - every session, booking and sign-up has exactly one set of credits per model, and nothing is credited that doesn't exist.
  • Reconciliation - every model's totals in the reporting table match each other and the facts, to the booking and the penny.

Before writing any models, I ran each candidate version as one big query against production data (identity, arrivals and credits in CTEs) to see the channel split, path lengths and edge cases. It's much cheaper to change your mind about the dedupe rule in a scratch query than after a backfill.

The release needed a little care because the facts are protected incremental tables. GA4's attribution columns were renamed with ALTER TABLE ... RENAME COLUMN (metadata only, instant), the new session-level arrival tables were loaded once in backfill mode, and the AI marketing analyst Flippy's update went out the same afternoon so its old model names didn't break for long.

Build your own identity graph and attribution models

The blueprint has the detail I glossed over in this post - the ID type and strategy config, the edge and quarantine rules, the arrival definition and click ID mapping, the credits table schema, the model registry, the assertions, the release steps for protected tables and the gotchas I hit along the way. It assumes you've got something like the warehouse from part 1, but it works on any GA4 export in BigQuery.

Download identity-attribution-blueprint.md and drop it into Claude or your CLAUDE.md, then work through it with your own decisions (the window, the dedupe rule, which IDs you trust).

If you build one, I'd love to hear what you do differently! Drop me a message on LinkedIn.