Skip to Content
Course content

165: Attribution Modeling in R

Click on the "Edit" button in the top corner of the screen to edit your slide content.

Imagine you're watching a basketball game. A player sinks a three-pointer right at the buzzer to win the game. The crowd goes wild for the shooter, but as a coach, you know the story is deeper. The point guard stole the ball to start the play, and the power forward made a crucial pass to get the ball to the shooter. If you only give the "credit" for the win to the person who took the shot, you're ignoring the effort that actually made the shot possible.

Attribution modeling in marketing is exactly the same. A customer might see a Facebook ad on Monday, read a blog post on Wednesday, and finally click a Google search ad on Friday to buy your product. If you use a "Last-Touch" model, Google gets 100% of the credit. But that's a lie—the Facebook ad and the blog post did the heavy lifting of introducing the brand. We're going to use R to stop lying to ourselves about where our conversions are actually coming from.

Who actually gets the credit?

In R, we usually handle this by structuring our data as a "customer journey." You need a dataframe where every row is a touchpoint (an interaction), tied to a user ID and a timestamp. Once you have that, you apply a rule to distribute the "conversion value" (usually 1 for a sale) across those touchpoints.

Let's look at how we'd set this up using dplyr. I'll create a small dataset of a user's path to purchase so we can see the math in action.

library(dplyr)

# A simple customer journey dataset
journeys <- data.frame(
  user_id = c(1, 1, 1, 2, 2),
  touchpoint = c("Facebook", "Blog", "Google", "Email", "Google"),
  timestamp = as.POSIXct(c("2023-01-01 10:00", "2023-01-02 12:00", "2023-01-03 15:00", 
                           "2023-01-01 09:00", "2023-01-02 11:00")),
  converted = c(0, 0, 1, 0, 1)
)

# We only care about journeys that ended in a conversion
conversions <- journeys %>%
  group_by(user_id) %>%
  filter(any(converted == 1)) %>%
  arrange(timestamp)

Translating the basketball logic to R

Now we apply the models. The "Last-Touch" model is the "shooter" analogy—it only cares about the final interaction. The "Linear" model is the "team effort"—everyone gets an equal slice of the pie.

I prefer writing these as custom mutations. It keeps the logic transparent, which is vital when you have to explain these numbers to a marketing manager who doesn't know R.

# Calculating Linear Attribution
linear_attribution <- conversions %>%
  group_by(user_id) %>%
  mutate(credit = 1 / n()) %>% # Split 1 conversion equally among all touchpoints
  ungroup() %>%
  group_by(touchpoint) %>%
  summarise(total_credit = sum(credit))

# Calculating Last-Touch Attribution
last_touch_attribution <- conversions %>%
  group_by(user_id) %>%
  slice_tail(n = 1) %>% # Only take the very last interaction
  ungroup() %>%
  group_by(touchpoint) %>%
  summarise(total_credit = n())

print(linear_attribution)
print(last_touch_attribution)

Why Last-Touch usually lies to you

If you run the code above, you'll notice that "Google" looks like a superhero in the Last-Touch model. But in the Linear model, "Facebook" and "Blog" suddenly appear on the scoreboard. I've seen companies slash their social media budgets because Last-Touch data suggested those channels weren't "converting," only to see their total sales plummet three months later because they stopped filling the top of the funnel.

The reality is that most businesses need a "Position-Based" (or U-Shaped) model. This gives 40% to the first touch (the discovery), 40% to the last touch (the closer), and splits the remaining 20% among the middle. It's a bit more complex to code, but it's far more honest about how people actually shop.




📋 Practical Task

Exercise: Building a U-Shaped Attribution Calculator for an E-commerce Store

You have been handed a dataset of customer interactions. Your goal is to implement a Position-Based (U-Shaped) Attribution Model.

The Requirements:

  • Create a dataframe with at least 3 different users. One user should have a journey of 1 touchpoint, one should have 2, and one should have 4. All must end in a conversion (converted = 1).
  • Assign 40% of the credit to the first touchpoint.
  • Assign 40% of the credit to the last touchpoint.
  • Distribute the remaining 20% equally among any touchpoints that occurred in the middle.
  • Special Case: If a user only had one touchpoint, that touchpoint gets 100% of the credit. If they had two, each gets 50%.
  • Output a final summary table showing the total credit assigned to each channel (e.g., "Instagram", "Organic Search", "Referral").
Rating
0 0

There are no comments for now.

to be the first to leave a comment.