ds@mcpanalytics.aiEmail your data and your question and get an answer about your own data, with the method shown.Email your data and question. Get an answer.

Shopify Customer Churn: Prediction & Analysis

By MCP Analytics Team | Published , updated

Identify At-Risk Customers Before They Leave

Have a subscriber list with cancellations? Run churn prediction on your CSV: free, no signup →

Introduction to Customer Churn Prediction

Customer churn is the rate at which customers stop buying from you. In a store where customers do not cancel anything, nobody tells you they have left: they simply stop ordering. This tutorial is about that case, repeat-purchase churn from order history. (If you have a subscriber list with cancellation dates, the tool linked at the top of this page starts from that instead.)

To see how many of your own customers churn and what separates the ones who leave, try the free churn analysis tool.

A churn model scores each current customer by how likely they are to place no order in a fixed window ahead. Two details decide whether such a model can be trusted, and most tutorials skip them. First, the label has to be defined from orders after the date the features are computed, never from the same orders. Second, the model has to be tested on a later period than the one it learned from, because that is how it will be used.

We build it step by step in Python on a synthetic order table generated for this tutorial (4,000 customers, three years of orders). It is not a real store, so the figures are illustrations of how to read the output, not benchmarks for your store.

What You'll Learn

Step 1: Data and the Churn Definition

1.1 The table you need

One row per order, with four columns. Build it from your Shopify order export (Shopify's own column names differ, so map them to these):

How much data you need depends on how many churn and non-churn events each snapshot holds, and on how noisy your result is; there is no fixed minimum customer count that makes a model reliable. A small test set gives wide uncertainty on every metric below, so treat results from a few hundred customers cautiously. At least two snapshots a full label window apart are needed, which usually means more than a year of orders.

1.2 Choose the label window from your own order gaps

Define churn as no order in the next N days after the prediction date. Pick N from the gaps between a customer's consecutive orders: the window should be long enough that a quiet stretch of that length is unusual for your store. A single rule of thumb does not fit every store, so look at the distribution first. The next two blocks create the order table, then compute the gaps and build the labelled snapshots.

import numpy as np
import pandas as pd

# Synthetic stand-in for an order export: one row per order.
rng = np.random.default_rng(42)
start, end = pd.Timestamp('2022-01-01'), pd.Timestamp('2024-12-31')
rows = []
for cid in range(4000):
    first = start + pd.Timedelta(days=int(rng.integers(0, (end - start).days - 30)))
    rate = rng.gamma(2.0, 1 / 70)     # orders per day while the customer is still buying
    life = rng.exponential(500)        # days until the customer quietly stops buying
    basket = rng.lognormal(3.9, 0.4)   # typical order value
    disc = rng.beta(2, 5)              # share of orders placed with a discount code
    t = 0.0
    while t <= life and first + pd.Timedelta(days=t) <= end:
        rows.append((cid, first + pd.Timedelta(days=int(t)), round(basket * rng.lognormal(0, 0.3), 2), int(rng.random() < disc)))
        t += rng.exponential(1 / rate)
orders = pd.DataFrame(rows, columns=['customer_id', 'order_date', 'order_total', 'discount_used'])

print(f"{len(orders):,} orders from {orders['customer_id'].nunique():,} customers, "
      f"{orders['order_date'].min():%Y-%m-%d} to {orders['order_date'].max():%Y-%m-%d}")
print(orders.head(3).to_string(index=False))

Expected Output

37,917 orders from 4,000 customers, 2022-01-01 to 2024-12-30
 customer_id order_date  order_total  discount_used
           0 2022-04-06        17.53              0
           0 2022-07-31        23.09              0
           1 2024-04-04        67.02              0

In this synthetic store the median gap between a customer's orders is 17 days and the 90th percentile is 68, so a 90-day window is longer than almost all normal gaps. The window is a choice, not a fact about customers; a different N changes what "churned" means and the share of customers who count as churned.

Step 2: Features and Labels at a Prediction Date

The function below takes a prediction date T. Every feature is computed from orders strictly before T, and the label from the 90 days starting at T, so no feature can see the outcome. Only customers who ordered in the 12 months before T are scored, since a customer who has not ordered for a year is already gone by any sensible definition. Two snapshots are built: one to train on and a later one to test on, whose label window starts after the training label window ends.

HORIZON = 90  # label window in days

def snapshot(orders, T, horizon=HORIZON):
    """Features use only orders BEFORE T. The label looks only at the `horizon` days AFTER T."""
    past = orders[orders['order_date'] < T]
    future = orders[(orders['order_date'] >= T) & (orders['order_date'] < T + pd.Timedelta(days=horizon))]
    last = past.groupby('customer_id')['order_date'].max()
    active = last[last >= T - pd.Timedelta(days=365)].index          # bought in the 12 months before T
    p = past[past['customer_id'].isin(active)]
    p12 = p[p['order_date'] >= T - pd.Timedelta(days=365)]
    g = p.groupby('customer_id')
    X = pd.DataFrame({
        'recency_days': (T - g['order_date'].max()).dt.days,
        'frequency_365': p12.groupby('customer_id').size(),
        'spend_365': p12.groupby('customer_id')['order_total'].sum(),
        'avg_order': g['order_total'].mean(),
        'tenure_days': (T - g['order_date'].min()).dt.days,
        'discount_share': g['discount_used'].mean(),
        'n_orders': g.size(),
    })
    gap = g['order_date'].apply(lambda d: d.sort_values().diff().dt.days.mean())   # this customer's usual gap
    X['mean_gap_days'] = gap.fillna(365).clip(lower=1)                              # one order so far: no gap yet; floor of 1 day
    X['recency_vs_gap'] = X['recency_days'] / X['mean_gap_days']                   # late relative to own rhythm
    y = (~X.index.isin(future['customer_id'])).astype(int)           # 1 = no order in the next 90 days
    return X, pd.Series(y, index=X.index, name='churned')

T_train, T_test = pd.Timestamp('2024-03-31'), pd.Timestamp('2024-09-30')
X_train, y_train = snapshot(orders, T_train)
X_test, y_test = snapshot(orders, T_test)

gaps = (orders[orders['order_date'] < T_train].sort_values('order_date')
        .groupby('customer_id')['order_date'].diff().dt.days.dropna())
print(f"Days between a customer's orders (before {T_train:%Y-%m-%d}): median {gaps.median():.0f}, 90th percentile {gaps.quantile(.9):.0f}")
print(f"Train: features at {T_train:%Y-%m-%d}, label window to {T_train + pd.Timedelta(days=HORIZON - 1):%Y-%m-%d}: "
      f"{len(y_train):,} customers, {y_train.mean():.1%} churned")
print(f"Test:  features at {T_test:%Y-%m-%d}, label window to {T_test + pd.Timedelta(days=HORIZON - 1):%Y-%m-%d}: "
      f"{len(y_test):,} customers, {y_test.mean():.1%} churned")
print(f"Customers in both snapshots: {len(X_train.index.intersection(X_test.index)):,}")

Expected Output

Days between a customer's orders (before 2024-03-31): median 17, 90th percentile 68
Train: features at 2024-03-31, label window to 2024-06-28: 2,335 customers, 51.7% churned
Test:  features at 2024-09-30, label window to 2024-12-28: 2,551 customers, 51.7% churned
Customers in both snapshots: 1,826

The test snapshot is six months later than the training snapshot, and its label window (1 October to 28 December) does not overlap the training window (1 April to 28 June). Because the same customers can appear in both snapshots (1,826 here), the test is a test on a later period, not on unseen customers. If you also want to know how the model treats new customers, hold out customers by identifier as well. Churn is 51.7% in both snapshots; in your store it will differ, and a model has to be judged against that base rate.

Step 3: Train the Model and Test It on the Later Period

A gradient boosting classifier is trained on the earlier snapshot and scored on the later one. Three comparisons guard against fooling ourselves: a model that always says "churned", the churn rate used as a constant probability, and recency alone (days since the last order) used as a risk score.

from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import roc_auc_score, brier_score_loss, accuracy_score

model = GradientBoostingClassifier(n_estimators=150, max_depth=3, learning_rate=0.05, random_state=0)
model.fit(X_train, y_train)
p = pd.Series(model.predict_proba(X_test)[:, 1], index=X_test.index)

base = y_train.mean()
print(f"ROC-AUC, model:                {roc_auc_score(y_test, p):.3f}")
print(f"ROC-AUC, recency alone:        {roc_auc_score(y_test, X_test['recency_days']):.3f}")
print(f"Accuracy at 0.5, model:        {accuracy_score(y_test, p > 0.5):.3f}")
print(f"Accuracy, always 'churned':    {y_test.mean():.3f}")
print(f"Brier score, model:            {brier_score_loss(y_test, p):.3f}")
print(f"Brier score, train base rate:  {brier_score_loss(y_test, np.full(len(y_test), base)):.3f}")
print()

# Operating point: contact the 20% of customers with the highest predicted risk.
k = int(len(p) * 0.2)
top = p.sort_values(ascending=False).index[:k]
hit = y_test.loc[top].sum()
print(f"Top 20% by risk ({k} customers): precision {hit / k:.1%}, recall {hit / y_test.sum():.1%}, "
      f"lift {hit / k / y_test.mean():.2f}x over the {y_test.mean():.1%} overall churn rate")

Expected Output

ROC-AUC, model:                0.849
ROC-AUC, recency alone:        0.847
Accuracy at 0.5, model:        0.762
Accuracy, always 'churned':    0.517
Brier score, model:            0.157
Brier score, train base rate:  0.250

Top 20% by risk (510 customers): precision 95.9%, recall 37.1%, lift 1.85x over the 51.7% overall churn rate

What the numbers say. ROC-AUC is 0.849 on the later period, but recency alone scores 0.847, so in this data the extra features add almost nothing to how well customers are ranked and a one-column rule is nearly as good as the model. That can happen with repeat-purchase churn, and it is why the baseline is in the table. Accuracy at a 0.5 cut-off is 76.2%, against 51.7% for always predicting the majority class; accuracy depends heavily on the churn rate and on the cut-off, so it is not a quality score on its own. The Brier score (0.157 against 0.250 for the constant base rate) measures how close the predicted probabilities are to what happened; lower is better.

At the operating point of contacting the 20% of customers with the highest predicted risk, 95.9% of the 510 contacted churned (precision) and these were 37.1% of everyone who churned (recall), a lift of 1.85 over the overall rate. Which operating point is right depends on what a contact costs and what a retained customer is worth, not on a universal accuracy or AUC threshold.

Step 4: Risk Groups, Calibration and Drivers

Rank the test customers by predicted probability and cut the ranking into groups. Comparing the average predicted probability with the share that actually churned in each group is a calibration check. The spend column is what each group spent in the 12 months before the prediction date. It is spend at stake, not a prediction of lost revenue: someone who churns may have bought again less, and someone who stays may buy more.

# Calibration and risk groups on the test snapshot, ranked by predicted probability.
rank = p.rank(ascending=False, method='first') / len(p)
group = pd.cut(rank, [0, .2, .5, 1.0], labels=['high (top 20%)', 'medium (next 30%)', 'low (bottom 50%)'])
seg = pd.DataFrame({'p': p, 'churned': y_test, 'spend_365': X_test['spend_365'], 'group': group})
table = seg.groupby('group', observed=True).agg(customers=('p', 'size'), predicted=('p', 'mean'),
                                                 actual=('churned', 'mean'), spend_365=('spend_365', 'sum'))
table['predicted'] = table['predicted'].round(3); table['actual'] = table['actual'].round(3)
table['spend_365'] = table['spend_365'].round(0)
print(table.to_string())
print(f"Total: {table['customers'].sum():,} customers, ${table['spend_365'].sum():,.0f} spent in the 12 months before {T_test:%Y-%m-%d}")

Expected Output

                   customers  predicted  actual  spend_365
group                                                     
high (top 20%)           510      0.945   0.959   131444.0
medium (next 30%)        765      0.692   0.664   154721.0
low (bottom 50%)        1276      0.231   0.252   642889.0
Total: 2,551 customers, $929,054 spent in the 12 months before 2024-09-30

The group sizes add up to the 2,551 customers scored in Step 2, and the predicted and actual churn rates agree closely in each group (0.945 against 0.959, 0.692 against 0.664, 0.231 against 0.252), so the probabilities can be read as rough probabilities on this test period. The high-risk group is 20% of customers but holds 14% of past spend ($131,444 of $929,054); the low-risk half holds 69% of it. A rule that targets only the highest-risk group would therefore miss most of the spend, which is a reason to weigh value as well as risk when choosing whom to contact.

To see which inputs matter, shuffle one feature at a time on the test set and measure how much ROC-AUC falls (permutation importance):

from sklearn.inspection import permutation_importance

imp = permutation_importance(model, X_test, y_test, scoring='roc_auc', n_repeats=5, random_state=0)
imp = pd.Series(imp.importances_mean, index=X_test.columns).sort_values(ascending=False)
print("Drop in test ROC-AUC when one feature is shuffled")
print(imp.round(3).to_string())

Expected Output

Drop in test ROC-AUC when one feature is shuffled
recency_days      0.210
mean_gap_days     0.022
recency_vs_gap    0.015
tenure_days       0.009
spend_365         0.003
avg_order         0.000
n_orders         -0.000
frequency_365    -0.000
discount_share   -0.001

Recency dominates (0.210). The customer's own usual gap and recency relative to that gap add a little (0.022 and 0.015), and the rest add nothing measurable. Importance describes what the model uses to predict, not what causes customers to leave, and correlated features share credit unpredictably.

Step 5: Acting on the Scores

5.1 A churn score is not an uplift score

A customer with a high churn probability may leave whatever you do, and one with a lower score might have bought anyway. What decides whether an offer pays is the difference it makes: the repeat-purchase rate of contacted customers minus that of comparable customers you did not contact. The score tells you where the risk is, not who is persuadable.

5.2 Test the offer with a holdout

Before sending a retention offer to the whole high-risk group, randomly set aside part of it (a holdout) and send the offer to the rest. After the label window, compare the share who ordered in the two parts. The difference is the effect of the offer for that group, with the usual sampling uncertainty, which is large when the groups are small. Do this before putting numbers on response rates or return on spend; any rate you quote without a holdout is an assumption.

5.3 Break-even

Per contacted customer, the offer pays when

(extra orders caused) × (margin per order) > (cost of the contact) + (discount given to customers who would have ordered anyway)

All four quantities come from your own business and your holdout. Write the inputs down before the campaign, so the result is judged against a number fixed in advance.

Step 6: Keep the Model Honest

Run It on Your Own Shopify Export

Export your orders and customers from Shopify as a CSV and upload it at MCP Analytics, then ask the question in plain language; the report names the method. Nothing connects to your Shopify account.

Try it on your data

Troubleshooting: Common Issues and Solutions

The model is no better than recency alone

That is a legitimate finding, as in Step 3. Check that your extra features carry information that recency does not (for example a product category that really does drive repeat orders). If they do not, use the recency rule.

The test result is much worse than the training result

Look for leakage first: any feature computed from orders on or after the prediction date makes the training score look better than reality. Then check whether the test period differs from the training period (a sale, a stock-out, a season).

Too many flagged customers go on to buy

Contact fewer customers (a smaller top share), or lengthen the label window so that a customer must be quiet for longer to count as churned. Fewer flagged customers raises precision and lowers recall.

Predicted churn does not match actual churn

Use the calibration table in Step 4. A gap in every group means the base rate changed between periods; refit on a more recent snapshot. A gap in one group means the model is miscalibrated there.

Conclusion: From Scores to Decisions

A churn model for a store without cancellations is only as good as its label and its test. Define churn from a window after the prediction date, compute features from before it, train on an earlier period, test on a later one, and compare against a recency baseline. Then decide whom to contact by cost and value, and measure the offer with a holdout.

Explore more: Shopify Analytics: all tools, tutorials, and guides →

Your turn

Bring your own data and the question you actually need answered.

MCP AnalyticsData science by email Send us your data and your question, and we will reply. ds@mcpanalytics.ai