Skip to content

Commit a610868

Browse files
committed
fix: track custom events
1 parent e17f220 commit a610868

File tree

7 files changed

+363
-79
lines changed

7 files changed

+363
-79
lines changed

apps/api/src/query/builders/custom-events.ts

Lines changed: 2 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export const CustomEventsBuilders: Record<string, SimpleQueryConfig> = {
1111
'MAX(time) as last_occurrence',
1212
'MIN(time) as first_occurrence',
1313
'COUNT(DISTINCT path) as unique_pages',
14+
"countIf(properties != '' AND isValidJSON(properties)) as events_with_properties",
1415
'ROUND((COUNT(*) / SUM(COUNT(*)) OVER()) * 100, 2) as percentage',
16+
"arrayDistinct(groupArray(CASE WHEN isValidJSON(properties) AND properties != '' THEN properties ELSE NULL END)) as all_properties",
1517
],
1618
where: [
1719
"event_name NOT IN ('screen_view', 'page_exit', 'error', 'web_vitals', 'link_out')",
@@ -24,65 +26,4 @@ export const CustomEventsBuilders: Record<string, SimpleQueryConfig> = {
2426
allowedFilters: ['event_name', 'path', 'device_type', 'browser_name'],
2527
customizable: true,
2628
},
27-
28-
custom_event_details: {
29-
table: 'analytics.events',
30-
fields: [
31-
'event_name',
32-
'time',
33-
'path',
34-
'anonymous_id',
35-
'session_id',
36-
'country',
37-
'region',
38-
'device_type',
39-
'browser_name',
40-
'os_name',
41-
'properties',
42-
],
43-
where: [
44-
"event_name NOT IN ('screen_view', 'page_exit', 'error', 'web_vitals', 'link_out')",
45-
"event_name != ''",
46-
],
47-
orderBy: 'time DESC',
48-
timeField: 'time',
49-
allowedFilters: ['event_name', 'path', 'device_type'],
50-
customizable: true,
51-
},
52-
53-
custom_event_trends: {
54-
table: 'analytics.events',
55-
fields: [
56-
'toDate(time) as date',
57-
'event_name',
58-
'COUNT(*) as events',
59-
'COUNT(DISTINCT anonymous_id) as users',
60-
],
61-
where: [
62-
"event_name NOT IN ('screen_view', 'page_exit', 'error', 'web_vitals', 'link_out')",
63-
"event_name != ''",
64-
],
65-
groupBy: ['toDate(time)', 'event_name'],
66-
orderBy: 'date ASC, events DESC',
67-
timeField: 'time',
68-
allowedFilters: ['event_name', 'path', 'device_type'],
69-
customizable: true,
70-
},
71-
72-
custom_event_by_page: {
73-
table: 'analytics.events',
74-
fields: [
75-
'path(path) as name',
76-
'event_name',
77-
'COUNT(*) as events',
78-
'COUNT(DISTINCT anonymous_id) as users',
79-
],
80-
where: ["event_name != ''", "path != ''"],
81-
groupBy: ['path(path)', 'event_name'],
82-
orderBy: 'events DESC',
83-
limit: 50,
84-
timeField: 'time',
85-
allowedFilters: ['path', 'event_name', 'device_type'],
86-
customizable: true,
87-
},
8829
};

apps/api/src/query/builders/pages.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
55
top_pages: {
66
table: Analytics.events,
77
fields: [
8-
'path(path) as name',
8+
"trimRight(path(path), '/') as name",
99
'COUNT(*) as pageviews',
1010
'COUNT(DISTINCT anonymous_id) as visitors',
1111
'ROUND((COUNT(*) / SUM(COUNT(*)) OVER()) * 100, 2) as percentage',
1212
],
1313
where: ["event_name = 'screen_view'", "path != ''"],
14-
groupBy: ['path(path)'],
14+
groupBy: ["trimRight(path(path), '/')"],
1515
orderBy: 'pageviews DESC',
1616
limit: 100,
1717
timeField: 'time',
@@ -48,7 +48,7 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
4848
SELECT
4949
session_id,
5050
anonymous_id,
51-
path(path) as entry_page,
51+
trimRight(path(path), '/') as entry_page,
5252
time as entry_time,
5353
ROW_NUMBER() OVER (PARTITION BY session_id ORDER BY time) as page_rank
5454
FROM analytics.events
@@ -118,7 +118,7 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
118118
),
119119
exit_pages AS (
120120
SELECT
121-
path(e.path) as path,
121+
trimRight(path(e.path), '/') as path,
122122
e.session_id,
123123
e.anonymous_id
124124
FROM analytics.events e
@@ -152,13 +152,13 @@ export const PagesBuilders: Record<string, SimpleQueryConfig> = {
152152
page_performance: {
153153
table: Analytics.events,
154154
fields: [
155-
'path(path) as name',
155+
"trimRight(path(path), '/') as name",
156156
'COUNT(*) as pageviews',
157157
'ROUND(AVG(CASE WHEN time_on_page > 0 THEN time_on_page / 1000 ELSE NULL END), 2) as avg_time_on_page',
158158
'COUNT(DISTINCT anonymous_id) as visitors',
159159
],
160160
where: ["event_name = 'screen_view'", "path != ''"],
161-
groupBy: ['path(path)'],
161+
groupBy: ["trimRight(path(path), '/')"],
162162
orderBy: 'pageviews DESC',
163163
limit: 100,
164164
timeField: 'time',

apps/api/src/query/builders/performance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
2323
slow_pages: {
2424
table: Analytics.events,
2525
fields: [
26-
'path(path) as name',
26+
"trimRight(path(path), '/') as name",
2727
'COUNT(DISTINCT anonymous_id) as visitors',
2828
'AVG(CASE WHEN load_time > 0 THEN load_time ELSE NULL END) as avg_load_time',
2929
'AVG(CASE WHEN ttfb > 0 THEN ttfb ELSE NULL END) as avg_ttfb',
@@ -35,7 +35,7 @@ export const PerformanceBuilders: Record<string, SimpleQueryConfig> = {
3535
'COUNT(*) as pageviews',
3636
],
3737
where: ["event_name = 'screen_view'", "path != ''", 'load_time > 0'],
38-
groupBy: ['path(path)'],
38+
groupBy: ["trimRight(path(path), '/')"],
3939
orderBy: 'avg_load_time DESC',
4040
limit: 100,
4141
timeField: 'time',

apps/api/src/routes/query.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,10 @@ async function executeDynamicQuery(
151151
const startDate = queryParams.start_date || queryParams.startDate;
152152
const endDate = queryParams.end_date || queryParams.endDate;
153153
const websiteId = queryParams.website_id;
154-
155-
const websiteDomain = websiteId && !domainCache?.[websiteId]
156-
? await getWebsiteDomain(websiteId)
157-
: domainCache?.[websiteId] || null;
154+
155+
const websiteDomain = websiteId
156+
? (domainCache?.[websiteId] ?? (await getWebsiteDomain(websiteId)))
157+
: null;
158158

159159
const getTimeUnit = (granularity?: string): 'hour' | 'day' => {
160160
if (['hourly', 'hour'].includes(granularity || '')) {

apps/basket/src/routes/basket.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,9 @@ async function insertTrackEvent(
409409
error_stack: undefined,
410410
error_type: undefined,
411411

412-
properties: '{}',
412+
properties: trackData.properties
413+
? JSON.stringify(trackData.properties)
414+
: '{}',
413415
created_at: now,
414416
};
415417

0 commit comments

Comments
 (0)