-
Notifications
You must be signed in to change notification settings - Fork 1
/
02. Marketing Click Through Rate Analytics.sql
72 lines (61 loc) · 1.37 KB
/
02. Marketing Click Through Rate Analytics.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
SELECT
COUNT(DISTINCT utm_campaign) AS 'Campaigns',
COUNT(DISTINCT utm_source) AS 'Sources'
FROM page_visits;
SELECT
DISTINCT page_name
FROM page_visits;
WITH first_touch AS (
SELECT user_id,
MIN(timestamp) as first_touch_at
FROM page_visits
GROUP BY user_id)
SELECT
pv.utm_campaign,
COUNT(*) AS 'Campaign First Touch CTR'
FROM first_touch AS ft
JOIN page_visits AS pv
ON ft.user_id = pv.user_id
AND ft.first_touch_at = pv.timestamp
GROUP BY
pv.utm_campaign
ORDER BY
2 DESC;
WITH last_touch AS (
SELECT user_id,
MAX(timestamp) as last_touch_at
FROM page_visits
GROUP BY user_id)
SELECT
pv.utm_campaign,
COUNT(*) AS 'Campaign Last Touch CTR'
FROM last_touch AS lt
JOIN page_visits AS pv
ON lt.user_id = pv.user_id
AND lt.last_touch_at = pv.timestamp
GROUP BY
pv.utm_campaign
ORDER BY
2 DESC;
SELECT
COUNT(DISTINCT user_id) AS 'Purchaser'
FROM page_visits
WHERE page_name = '4 - purchase';
WITH last_touch AS (
SELECT user_id,
MAX(timestamp) as last_touch_at
FROM page_visits
GROUP BY user_id)
SELECT
pv.utm_campaign,
COUNT(*) AS 'Campaign Leading purchase CTR'
FROM last_touch AS lt
JOIN page_visits AS pv
ON lt.user_id = pv.user_id
AND lt.last_touch_at = pv.timestamp
WHERE
page_name = '4 - purchase'
GROUP BY
pv.utm_campaign
ORDER BY
2 DESC;