Skip to content

Commit d7c6eba

Browse files
committed
cleanup, update to new SQL with CTEs
1 parent 1017a03 commit d7c6eba

File tree

4 files changed

+349
-101
lines changed

4 files changed

+349
-101
lines changed
Lines changed: 173 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,184 @@
1-
import type { SimpleQueryConfig } from '../types';
1+
import type { Filter, SimpleQueryConfig } from '../types';
22

33
export const CustomEventsBuilders: Record<string, SimpleQueryConfig> = {
44
custom_events: {
5-
table: 'analytics.events',
6-
fields: [
7-
'event_name as name',
8-
'COUNT(*) as total_events',
9-
'COUNT(DISTINCT anonymous_id) as unique_users',
10-
'COUNT(DISTINCT session_id) as unique_sessions',
11-
'MAX(time) as last_occurrence',
12-
'MIN(time) as first_occurrence',
13-
'COUNT(DISTINCT path) as unique_pages',
14-
"countIf(properties != '{}' AND isValidJSON(properties)) as events_with_properties",
15-
'ROUND((COUNT(*) / SUM(COUNT(*)) OVER()) * 100, 2) as percentage',
16-
],
17-
where: [
18-
"event_name NOT IN ('screen_view', 'page_exit', 'error', 'web_vitals', 'link_out')",
19-
"event_name != ''",
5+
customSql: (
6+
websiteId: string,
7+
startDate: string,
8+
endDate: string,
9+
_filters?: Filter[],
10+
_granularity?: unknown,
11+
_limit?: number,
12+
_offset?: number,
13+
_timezone?: string,
14+
filterConditions?: string[],
15+
filterParams?: Record<string, Filter['value']>
16+
) => {
17+
const limit = _limit || 10_000;
18+
const combinedWhereClause = filterConditions?.length
19+
? `AND ${filterConditions.join(' AND ')}`
20+
: '';
21+
22+
return {
23+
sql: `
24+
WITH enriched_events AS (
25+
SELECT
26+
ce.event_name,
27+
ce.anonymous_id,
28+
ce.session_id,
29+
ce.timestamp,
30+
ce.properties,
31+
-- Get context from events table using session_id
32+
e.path,
33+
e.country,
34+
e.device_type,
35+
e.browser_name,
36+
e.os_name,
37+
e.referrer,
38+
e.utm_source,
39+
e.utm_medium,
40+
e.utm_campaign
41+
FROM analytics.custom_events ce
42+
LEFT JOIN analytics.events e ON (
43+
ce.session_id = e.session_id
44+
AND ce.client_id = e.client_id
45+
AND abs(dateDiff('second', ce.timestamp, e.time)) < 60
46+
)
47+
WHERE
48+
ce.client_id = {websiteId:String}
49+
AND ce.timestamp >= parseDateTimeBestEffort({startDate:String})
50+
AND ce.timestamp <= parseDateTimeBestEffort(concat({endDate:String}, ' 23:59:59'))
51+
AND ce.event_name != ''
52+
${combinedWhereClause}
53+
)
54+
SELECT
55+
event_name as name,
56+
COUNT(*) as total_events,
57+
COUNT(DISTINCT anonymous_id) as unique_users,
58+
COUNT(DISTINCT session_id) as unique_sessions,
59+
MAX(timestamp) as last_occurrence,
60+
MIN(timestamp) as first_occurrence,
61+
countIf(properties != '{}' AND isValidJSON(properties)) as events_with_properties,
62+
ROUND((COUNT(*) / SUM(COUNT(*)) OVER()) * 100, 2) as percentage
63+
FROM enriched_events
64+
GROUP BY event_name
65+
ORDER BY total_events DESC
66+
LIMIT {limit:UInt32}
67+
`,
68+
params: {
69+
websiteId,
70+
startDate,
71+
endDate,
72+
limit,
73+
...filterParams,
74+
},
75+
};
76+
},
77+
timeField: 'timestamp',
78+
allowedFilters: [
79+
'path',
80+
'country',
81+
'device_type',
82+
'browser_name',
83+
'os_name',
84+
'referrer',
85+
'utm_source',
86+
'utm_medium',
87+
'utm_campaign',
88+
'client_id',
89+
'anonymous_id',
90+
'session_id',
91+
'event_name',
2092
],
21-
groupBy: ['event_name'],
22-
orderBy: 'total_events DESC',
23-
limit: 10_000,
24-
timeField: 'time',
2593
customizable: true,
2694
},
2795
custom_event_properties: {
28-
table: 'analytics.events',
29-
fields: [
30-
'event_name as name',
31-
'arrayJoin(JSONExtractKeys(properties)) as property_key',
32-
'JSONExtractRaw(properties, property_key) as property_value',
33-
'COUNT(*) as count',
34-
],
35-
where: [
36-
"event_name NOT IN ('screen_view', 'page_exit', 'error', 'web_vitals', 'link_out')",
37-
"event_name != ''",
38-
"properties != '{}'",
39-
'isValidJSON(properties)',
96+
customSql: (
97+
websiteId: string,
98+
startDate: string,
99+
endDate: string,
100+
_filters?: Filter[],
101+
_granularity?: unknown,
102+
_limit?: number,
103+
_offset?: number,
104+
_timezone?: string,
105+
filterConditions?: string[],
106+
filterParams?: Record<string, Filter['value']>
107+
) => {
108+
const limit = _limit || 10_000;
109+
const combinedWhereClause = filterConditions?.length
110+
? `AND ${filterConditions.join(' AND ')}`
111+
: '';
112+
113+
return {
114+
sql: `
115+
WITH enriched_events AS (
116+
SELECT
117+
ce.event_name,
118+
ce.anonymous_id,
119+
ce.session_id,
120+
ce.timestamp,
121+
ce.properties,
122+
-- Get context from events table using session_id
123+
e.path,
124+
e.country,
125+
e.device_type,
126+
e.browser_name,
127+
e.os_name,
128+
e.referrer,
129+
e.utm_source,
130+
e.utm_medium,
131+
e.utm_campaign
132+
FROM analytics.custom_events ce
133+
LEFT JOIN analytics.events e ON (
134+
ce.session_id = e.session_id
135+
AND ce.client_id = e.client_id
136+
AND abs(dateDiff('second', ce.timestamp, e.time)) < 60
137+
)
138+
WHERE
139+
ce.client_id = {websiteId:String}
140+
AND ce.timestamp >= parseDateTimeBestEffort({startDate:String})
141+
AND ce.timestamp <= parseDateTimeBestEffort(concat({endDate:String}, ' 23:59:59'))
142+
AND ce.event_name != ''
143+
AND ce.properties != '{}'
144+
AND isValidJSON(ce.properties)
145+
${combinedWhereClause}
146+
)
147+
SELECT
148+
event_name as name,
149+
arrayJoin(JSONExtractKeys(properties)) as property_key,
150+
JSONExtractRaw(properties, property_key) as property_value,
151+
COUNT(*) as count
152+
FROM enriched_events
153+
GROUP BY event_name, property_key, property_value
154+
ORDER BY count DESC
155+
LIMIT {limit:UInt32}
156+
`,
157+
params: {
158+
websiteId,
159+
startDate,
160+
endDate,
161+
limit,
162+
...filterParams,
163+
},
164+
};
165+
},
166+
timeField: 'timestamp',
167+
allowedFilters: [
168+
'path',
169+
'country',
170+
'device_type',
171+
'browser_name',
172+
'os_name',
173+
'referrer',
174+
'utm_source',
175+
'utm_medium',
176+
'utm_campaign',
177+
'client_id',
178+
'anonymous_id',
179+
'session_id',
180+
'event_name',
40181
],
41-
groupBy: ['event_name', 'property_key', 'property_value'],
42-
orderBy: 'count DESC',
43-
limit: 10_000,
44-
timeField: 'time',
45182
customizable: true,
46183
},
47184
};

0 commit comments

Comments
 (0)