WITH LastTouchProspects AS (
-- For every customer, find their final marketing touchpoint.
SELECT
customer_id,
source AS channel
FROM customer_stream
-- To isolate marketing, you can filter for specific sources.
-- WHERE source IN ('paid_social', 'paid_search', 'organic', 'email')
QUALIFY ROW_NUMBER() OVER(PARTITION BY customer_id ORDER BY ts DESC) = 1
),
Conversions AS (
-- Identify every customer who converted.
SELECT DISTINCT customer_id
FROM customer_stream
WHERE activity = 'created_subscription'
)
SELECT
ltp.channel,
COUNT(DISTINCT ltp.customer_id) AS total_prospects,
COUNT(DISTINCT c.customer_id) AS total_conversions,
(COUNT(DISTINCT c.customer_id) * 1.0 / COUNT(DISTINCT ltp.customer_id)) AS last_touch_win_rate
FROM LastTouchProspects ltp
LEFT JOIN Conversions c ON ltp.customer_id = c.customer_id
GROUP BY 1
ORDER BY 4 DESC;