Sub topic 2
Building the customer data foundation
This chapter assumes you know basic marketing terms (campaigns, channels, conversion), can read simple pandas code, and understand tables and averages. Every later step, from clustering to templates, depends on the data you assemble here. Weak data produces weak segments, however good the model is.
What the foundation must provide
Personalisation needs one trustworthy record per customer. That record combines three kinds of information:
- Profile data: attributes such as country, signup date and language.
- Behavioural data: events such as page views, purchases, email clicks and support contacts.
- Consent data: what each customer has agreed to. The next chapter covers this in detail.
These usually live in different systems: a shop database, a web analytics tool, an email platform. The foundation joins them into one customer table.
Identity resolution
Identity resolution is the process of deciding that several records belong to the same person. A visitor may browse anonymously on a phone, sign up on a laptop, and later buy in a store using a loyalty card. Without resolution, that person looks like three customers, and their purchase counts are split three ways.
There are two common approaches:
- Deterministic matching links records through an identifier they share, such as a login ID or a hashed email address. It is precise but only covers customers who identified themselves.
- Probabilistic matching links records that look similar, for example by device and location patterns. It covers more records but makes mistakes, so it needs a confidence threshold.
For segmentation, prefer deterministic matching and accept lower coverage. A wrong merge puts one person's behaviour into another person's profile, and the damage is hard to notice.
From events to a customer table
Raw events have many rows per customer. Segmentation needs one row per customer. You get there by aggregating. The example below turns an order log into one row with a customer ID, the number of orders, total spend and the date of the last order.
import pandas as pd
orders = pd.DataFrame({
"customer_id": [1, 1, 2, 3, 3, 3],
"order_date": pd.to_datetime([
"2026-10-01", "2026-11-10", "2026-09-15",
"2026-08-01", "2026-10-20", "2026-11-20",
]),
"amount": [40.0, 55.0, 20.0, 30.0, 45.0, 60.0],
})
customers = (
orders.groupby("customer_id")
.agg(
order_count=("order_id", "count") if "order_id" in orders else ("amount", "count"),
total_spend=("amount", "sum"),
last_order=("order_date", "max"),
)
.reset_index()
)
print(customers)Aggregation is also where you choose the time window. "Orders in the last 90 days" and "orders ever" describe different customers. Pick a window that matches how often people buy in your business, and keep it the same in training and in production.
Data quality checks
Run checks before anyone uses the table. These are the common failures:
| Problem | Typical cause | Check |
|---|---|---|
| Duplicate customers | Missing identity resolution | Count rows per email or login ID |
| Missing values | Fields not collected on some channels | Share of nulls per column |
| Impossible values | Refunds stored as negative orders, test accounts | Min and max per column |
| Stale data | A feed stopped running | Latest event date per source |
Keep the table reproducible
Build the table with a script, not by hand, and record the date it was built. When a segment looks wrong three months later, you need to rebuild exactly what the model saw. Store the aggregation window, the source extraction dates and the list of excluded records, such as staff and test accounts.
Exercise
A customer logs in on a laptop and buys twice. The same person browses on a phone without logging in and buys once as a guest using the same email address. Which matching approach links these records most safely, and what happens to this customer's order count if you do not link them?
Show answer
Deterministic matching on the shared email address (hashed) links them safely, because both records contain the same identifier. Without linking, the order count is split into two customers with two orders and one order, so neither shows the true frequency of three orders. Both would look less valuable than the real customer.