Funnel Analysis
Track how many users reach each step of a multi-step funnel and compute conversion rates between consecutive steps.
What Is a Conversion Funnel?
A conversion funnel models a user journey through a sequence of required steps: for example, Homepage → Product Page → Add to Cart → Checkout → Purchase. At each step, some users drop off and never reach the next step. Funnel analysis quantifies these drop-offs so teams can prioritise which step to improve first. The output is a table showing user count and conversion rate at each step.
import pandas as pd
FUNNEL_STEPS = [
'homepage',
'product_page',
'add_to_cart',
'checkout',
'purchase'
]
df = pd.read_csv('clickstream.csv', parse_dates=['timestamp'])
print(df['event_type'].value_counts())Counting Unique Users at Each Step
For each funnel step, count the number of unique users who triggered that event at least once. Use df[df['event_type'] == step]['user_id'].nunique() inside a loop and collect the results into a dictionary. Converting to a Series makes subsequent percentage calculations straightforward with a single division.
step_counts = {}
for step in FUNNEL_STEPS:
users = df[df['event_type'] == step]['user_id'].nunique()
step_counts[step] = users
funnel = pd.Series(step_counts, name='users')
print(funnel)