Growth Marketing · Python · Markov Chains · Shapley Values · BigQuery
Last-click attribution was making us look smart while we were leaking money. That's the honest summary.
Every conversion got 100% credit to whichever channel the user happened to click last. Paid search and direct were getting 58% of attributed conversions between them. Display was getting 4%. Email and social, single digits each. Marketing's budget review followed the credit, the budget was reallocated up the funnel, and a few quarters later we wondered why growth was decelerating despite holding spend steady.
The honest answer was buried in the data: the median converting user touched 4.3 channels over 11 days before converting. The first touch was almost never paid search. Display showed up early and often, then disappeared from the attributed credit because it rarely closed the click. We were rewarding the salesperson at the desk and starving the entire pipeline that walked the customer to the desk.
Before going to a proper model, we ran the standard set of attribution heuristics and looked at the results side by side. None of them survived scrutiny:
All four pick an attribution rule a priori and force the data to fit it. What we needed was the opposite — let the actual journeys decide which channels were doing work and which were freeloading.
The data was already there; nobody had pulled it together. Session-level events from BigQuery, three months of user-level paths, with channel attribution per session and a binary conversion flag. After sessionising and stitching, we had ~2.5 million unique user journeys capped at a 30-day lookback before the conversion event (or the end of the window for non-converters).
WITH sessions AS (
SELECT user_id, session_start, channel, converted
FROM `growth.sessions`
WHERE session_start BETWEEN DATE_SUB(CURRENT_DATE(), INTERVAL 90 DAY) AND CURRENT_DATE()
),
journeys AS (
SELECT
user_id,
STRING_AGG(channel, ' > ' ORDER BY session_start) AS path,
MAX(converted) AS converted
FROM sessions
GROUP BY user_id
)
SELECT path, converted, COUNT(*) AS users
FROM journeys
GROUP BY path, converted;
The shape of the data alone was eye-opening. About 14% of converters had journeys ≥7 touches. Around 8% touched the same channel three or more times before converting. Display appeared in 38% of converting journeys but got 4% of the last-click credit. The model wasn't going to surprise us by a small amount.
The intuition is simple, even if the math looks intimidating. Treat the user's journey as a walk between channel "states", ending at either Conversion or Null. Look at all observed journeys and count how often each transition happens — paid search → display, display → email, email → conversion. Normalise into a transition probability matrix.
Now ask the only question that matters: what happens to the overall conversion probability if I remove a channel entirely from the graph? That drop in conversion rate is that channel's removal effect — its true contribution to the system. Normalise across channels and you have attribution weights.
It's elegant because it respects the structure of the data without forcing a rule on it. A channel that only appears as the last click in 10% of journeys but as an essential middle step in 60% will get its actual due. Last-click would call it "weak"; the removal effect calls it "load-bearing."
import numpy as np
states = ['Start', 'PaidSearch', 'PaidSocial', 'Display', 'Email',
'Organic', 'Direct', 'Referral', 'Conversion', 'Null']
def transition_matrix(journeys):
counts = np.zeros((len(states), len(states)))
for path, converted in journeys:
sequence = ['Start'] + path + (['Conversion'] if converted else ['Null'])
for a, b in zip(sequence[:-1], sequence[1:]):
counts[states.index(a), states.index(b)] += 1
row_sums = counts.sum(axis=1, keepdims=True)
return np.divide(counts, row_sums, where=row_sums > 0)
P_full = transition_matrix(journeys)
def conversion_prob(P):
# Absorbing Markov chain — solve for P(reach Conversion | starting at Start)
Q = np.delete(np.delete(P, [8, 9], axis=0), [8, 9], axis=1)
R = P[:8, 8:]
N = np.linalg.inv(np.eye(8) - Q)
B = N @ R
return B[0, 0]
base = conversion_prob(P_full)
# Removal effect per channel
removal_effects = {}
for ch in ['PaidSearch', 'PaidSocial', 'Display', 'Email', 'Organic', 'Direct', 'Referral']:
P_minus = P_full.copy()
idx = states.index(ch)
P_minus[idx, :] = 0
P_minus[idx, states.index('Null')] = 1 # route through Null
removal_effects[ch] = (base - conversion_prob(P_minus)) / base
total = sum(removal_effects.values())
attribution = {ch: re / total for ch, re in removal_effects.items()}
Markov is elegant but it's still one method, and one method that disagrees dramatically with the status quo gets pushed back hard. So we ran a second, independent attribution approach using Shapley values — treat each channel as a "player," each journey as a "coalition," and the conversion as the payout. Shapley distributes credit based on each channel's marginal contribution averaged over all possible orderings.
Shapley is mathematically cleaner, but it's computationally expensive (factorial complexity), so we restricted it to the top 7 channels and used Monte Carlo permutation sampling for the rest.
The two methods agreed within ±3 percentage points on every single channel. Two independent pieces of mathematical machinery converging on the same answer — that was the moment I stopped worrying about whether the model was right and started worrying about whether marketing would believe it.
Here's what the model said vs what last-click had been saying:
Bars normalised to relative weight, not absolute share. Display was undercredited by ~2.5×. Paid search was overcredited by ~1.6×. Direct (mostly returning users) was overcredited by ~2.4×.
Here's the thing about attribution models: two methods agreeing with each other is necessary but it's not sufficient. They could both be confidently wrong in the same direction. The only way to ground-truth attribution is to actually change something and see if the world responds the way the model predicted.
The most contentious claim coming out of the model was that display was being undercredited by 2.5×. Marketing didn't believe it. The display team obviously loved it. I ran a geo-holdout lift test: pause display in 20% of geo regions for 4 weeks, hold every other channel's spend constant, and measure the gap in incremental conversion rate vs control geos.
The model had predicted display's true contribution would show as a 10.2% incremental lift if removed. The experiment measured 9.4%. Inside 1 percentage point. That's the moment attribution stopped being a "model debate" and became a budget document.
Marketing operations rebuilt the channel ROI dashboard against Markov-weighted conversions instead of last-click. Budget reviews stopped being political — when the question is "what does the data say?" the answer doesn't depend on who owns which channel.
Over the following quarter, the reallocation was substantial but not dramatic — about 25% of paid budget moved:
No incremental media budget. Same creatives. Same channels. We just stopped paying salespeople their commission for closing deals other people had already brought to the door.
Three things:
The reframe that took me longest to internalise: attribution is not a measurement problem, it's a decision support problem. The model only matters if it changes a budget meeting. Everything else — the Markov maths, the Shapley cross-check, the lift test — exists to make the budget meeting different from what it would have been.
Run the code: github.com/.../nikunjkaushik-demos/multi-touch-attribution — Markov-chain attribution on synthetic journeys with removal-effect cross-checked against last-click.
Related reading: Multi-Touch Attribution: Moving Beyond Last Click