Marketing ROI Analysis
Campaign spend vs. attributed revenue â sessions credited to a campaign flight, conversions joined on visitor. Budgets are not prorated for clipped flights.
View SQL â Query 01 · marketing_campaigns â website_visitors â conversions
/* Query 01 â Marketing ROI Analysis
Attribution model: a session is credited to a campaign when its date falls
inside the campaign flight. Flights never overlap in this dataset, so each
session maps to at most one campaign. Conversions join on visitor_id, so â
as written â a visitor contributes their ENTIRE conversion history, which
is realistic (campaigns keep converting after the flight ends). */
SELECT mc.name,
COUNT(DISTINCT wv.visitor_id) AS visitors,
COUNT(DISTINCT c.conversion_id) AS conversions,
SUM(c.value) AS total_revenue,
mc.budget_spent,
(SUM(c.value) - mc.budget_spent) / mc.budget_spent * 100 AS roi_percent
FROM marketing_campaigns mc
LEFT JOIN website_visitors wv
ON DATE(wv.session_start) BETWEEN mc.start_date AND mc.end_date
LEFT JOIN conversions c
ON wv.visitor_id = c.visitor_id
GROUP BY mc.campaign_id, mc.name -- name added for display; grouped by id
ORDER BY roi_percent DESC;
Funnel Conversion Rates
Stage-by-stage drop-off for the selected window. Stages nest: a visitor must view 3+ pages before a signup is possible, and every buyer has signed up.
View SQL â Query 02 · website_visitors â conversions
/* Query 02 â Funnel Conversion Rates (trailing window)
Note on semantics: SUM(CASE â¦) counts conversion ROWS, not visitors. A
repeat buyer contributes two 'purchase' rows, so `purchases` is an
order-count. The funnel chart heights its stages on COUNT(DISTINCT
buyers) while this table reports orders, exactly as specified. */
SELECT wv.source,
COUNT(DISTINCT wv.visitor_id) AS total_visitors,
SUM(CASE WHEN c.conversion_type = 'signup' THEN 1 ELSE 0 END) AS signups,
SUM(CASE WHEN c.conversion_type = 'purchase' THEN 1 ELSE 0 END) AS purchases,
(SUM(CASE WHEN c.conversion_type = 'purchase' THEN 1 ELSE 0 END) * 1.0
/ COUNT(DISTINCT wv.visitor_id)) * 100 AS conversion_rate
FROM website_visitors wv
LEFT JOIN conversions c ON wv.visitor_id = c.visitor_id
WHERE wv.session_start >= DATE_SUB(NOW(), INTERVAL 30 DAY) -- window parameterised in-app
GROUP BY wv.source
ORDER BY total_visitors DESC;
Cohort Analysis
Monthly session cohorts â volume, engagement depth and conversion rate, rendered as a column-normalised heat map.
View SQL â Query 03 · website_visitors â conversions
/* Query 03 â Cohort Analysis (monthly)
Corrected numerator: the brief's COUNT(DISTINCT conversion_id) counts
events, so a visitor holding both a signup AND a purchase would push the
rate above 100%. Counting converting VISITORS keeps the ratio in [0,100]. */
SELECT DATE_FORMAT(wv.session_start, '%Y-%m') AS cohort_month,
COUNT(DISTINCT wv.visitor_id) AS visitors,
AVG(wv.pages_viewed) AS avg_pages_per_session,
COUNT(DISTINCT CASE WHEN c.conversion_id IS NOT NULL
THEN wv.visitor_id END)
/ COUNT(DISTINCT wv.visitor_id) * 100 AS conversion_rate_percent
FROM website_visitors wv
LEFT JOIN conversions c ON wv.visitor_id = c.visitor_id
GROUP BY DATE_FORMAT(wv.session_start, '%Y-%m')
ORDER BY cohort_month;
Customer Lifetime Value by Source
Revenue per purchaser by acquisition source. Lifetime value vs. average order value diverge wherever repeat buyers exist (~18% of this dataset).
View SQL â Query 04 · website_visitors â conversions (purchase only)
/* Query 04 â Customer Lifetime Value by Source
Corrected denominator: the brief divided revenue by COUNT(DISTINCT
conversion_id), which reproduces avg_order_value whenever a customer may
place more than one order. Revenue / unique CUSTOMERS is the true lifetime
value; ~18% of buyers in this dataset order twice, so the two diverge. */
SELECT wv.source,
COUNT(DISTINCT wv.visitor_id) AS unique_customers,
AVG(c.value) AS avg_order_value,
SUM(c.value) / COUNT(DISTINCT wv.visitor_id) AS lifetime_value,
SUM(c.value) AS total_revenue
FROM website_visitors wv
JOIN conversions c
ON wv.visitor_id = c.visitor_id
AND c.conversion_type = 'purchase'
GROUP BY wv.source
ORDER BY lifetime_value DESC;