-
Notifications
You must be signed in to change notification settings - Fork 0
/
16-Bulding_conversions_funnels_And_testing_conversions_paths.sql
121 lines (107 loc) · 4.7 KB
/
16-Bulding_conversions_funnels_And_testing_conversions_paths.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
-- Demo on building conversion funnels
-- BUSINESS CONTEXT
-- we want to build a mini conversion funnel, from /lander-2 to /cart
-- we want to know how many people reach each step, and also dropoff rates
-- for simplicity of the demo, we´re looking at /lander-2 traffic ONLY
-- for simplicity of the demo, we´re looking at customers who like Mr Fuzzy only
-- STEP 1: select all pageviews for relevant sessions
-- STEP 2: identify each relevant pageview as the specific funnel step
-- STEP 3: create the session-level conversion funnel view
-- STEP 4: aggregate the data to assess funnel performance
-- first I will show you all of the pageviews we care about
-- then, I will remove the comments from my flag columns one by one to show you that looks like
SELECT
ws.website_session_id ,
wp.pageview_url ,
wp.created_at AS pageview_created_at,
CASE WHEN wp.pageview_url = '/products' THEN 1 ELSE 0 END AS products_page,
CASE WHEN wp.pageview_url ='/the-original-mr-fuzzy' THEN 1 ELSE 0 END AS mrfuzzy_page,
CASE WHEN wp.pageview_url = '/cart' THEN 1 ELSE 0 END AS cart_page
FROM website_sessions ws
LEFT JOIN website_pageviews wp
ON ws.website_session_id = wp.website_session_id
WHERE ws.created_at BETWEEN '2014-01-01' AND '2014-02-01' -- random timeframe FOR demo
AND wp.pageview_url IN ('/lander-2','/products','/the-original-mr-fuzzy','/cart')
ORDER BY
ws.website_session_id,
wp.created_at ;
-- next we will put the previous query insideasubquery(similar to temporary tables)
-- we will group by website_session_id,and take the MAX()of each of the flags
-- this MAX()becomes a made_it flag for that session,to show the session made it there
SELECT
website_session_id,
MAX(products_page) AS product_made_it,
MAX(mrfuzzy_page) AS mrfuzzy_made_it,
MAX(cart_page) AS cart_made_it
FROM (
SELECT
ws.website_session_id ,
wp.pageview_url ,
wp.created_at AS pageview_created_at,
CASE WHEN wp.pageview_url = '/products' THEN 1 ELSE 0 END AS products_page,
CASE WHEN wp.pageview_url ='/the-original-mr-fuzzy' THEN 1 ELSE 0 END AS mrfuzzy_page,
CASE WHEN wp.pageview_url = '/cart' THEN 1 ELSE 0 END AS cart_page
FROM website_sessions ws
LEFT JOIN website_pageviews wp
ON ws.website_session_id = wp.website_session_id
WHERE ws.created_at BETWEEN '2014-01-01' AND '2014-02-01' -- random timeframe FOR demo
AND wp.pageview_url IN ('/lander-2','/products','/the-original-mr-fuzzy','/cart')
ORDER BY
ws.website_session_id,
wp.created_at
) AS pageview_level
GROUP BY website_session_id;
-- next we will turn it into a temp table
DROP TABLE IF EXISTS session_level_made_it_flags_demo;
CREATE TEMPORARY TABLE session_level_made_it_flags_demo
SELECT
website_session_id,
MAX(products_page) AS product_made_it,
MAX(mrfuzzy_page) AS mrfuzzy_made_it,
MAX(cart_page) AS cart_made_it
FROM (
SELECT
ws.website_session_id ,
wp.pageview_url ,
wp.created_at AS pageview_created_at,
CASE WHEN wp.pageview_url = '/products' THEN 1 ELSE 0 END AS products_page,
CASE WHEN wp.pageview_url ='/the-original-mr-fuzzy' THEN 1 ELSE 0 END AS mrfuzzy_page,
CASE WHEN wp.pageview_url = '/cart' THEN 1 ELSE 0 END AS cart_page
FROM website_sessions ws
LEFT JOIN website_pageviews wp
ON ws.website_session_id = wp.website_session_id
WHERE ws.created_at BETWEEN '2014-01-01' AND '2014-02-01' -- random timeframe FOR demo
AND wp.pageview_url IN ('/lander-2','/products','/the-original-mr-fuzzy','/cart')
ORDER BY
ws.website_session_id,
wp.created_at
) AS pageview_level
GROUP BY website_session_id;
SELECT * FROM session_level_made_it_flags_demo;
-- then this would produce the final output (part 1)
SELECT
COUNT(DISTINCT website_session_id) AS sessions,
COUNT(DISTINCT CASE WHEN product_made_it = 1 THEN website_session_id ELSE NULL END) AS to_products,
COUNT(DISTINCT CASE WHEN mrfuzzy_made_it = 1 THEN website_session_id ELSE NULL END) AS to_mrfuzzy,
COUNT(DISTINCT CASE WHEN cart_made_it = 1 THEN website_session_id ELSE NULL END) AS to_cart
FROM session_level_made_it_flags_demo;
-- then we´ll translate those counts to click rates for final output part 2 (click rates)
-- I´ll start with the same query we just did, and show you how to calculate the rates
SELECT
COUNT(DISTINCT website_session_id) AS sessions,
(
COUNT(DISTINCT CASE WHEN product_made_it = 1 THEN website_session_id ELSE NULL END)
/
COUNT(DISTINCT website_session_id)
) AS clicked_to_products,
(
COUNT(DISTINCT CASE WHEN mrfuzzy_made_it = 1 THEN website_session_id ELSE NULL END)
/
COUNT(DISTINCT website_session_id)
) AS clicked_to_mrfuzzy,
(
COUNT(DISTINCT CASE WHEN cart_made_it = 1 THEN website_session_id ELSE NULL END)
/
COUNT(DISTINCT website_session_id)
) AS clicked_to_cart
FROM session_level_made_it_flags_demo;