Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Use deterministic, earliest event selection and apply top-3 cutoff in SQL

Blue Moused15048e489a1
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 388851bf7..dc5528d42 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -1565,15 +1565,15 @@ export class AnalyticsService {
session_info AS (
SELECT
psid,
- any(cc) AS cc,
- any(if(domain(ref) != '', domain(ref), 'Direct / None')) AS source
+ argMin(cc, created) AS cc,
+ argMin(if(domain(ref) != '', domain(ref), 'Direct / None'), created) AS source
FROM (
- SELECT psid, cc, ref
+ SELECT psid, cc, ref, created
FROM analytics
WHERE pid = {pid:FixedString(12)} AND psid != 0
AND created BETWEEN {groupFrom:String} AND {groupTo:String}
UNION ALL
- SELECT psid, cc, ref
+ SELECT psid, cc, ref, created
FROM customEV
WHERE pid = {pid:FixedString(12)} AND psid != 0
AND created BETWEEN {groupFrom:String} AND {groupTo:String}
@@ -1595,6 +1595,7 @@ export class AnalyticsService {
GROUP BY e.step, si.source
)
ORDER BY step, type, cnt DESC
+ LIMIT 3 BY step, type
`
const { data } = await clickhouse
@@ -1618,14 +1619,10 @@ export class AnalyticsService {
const { step } = row
if (row.type === 'cc') {
if (!countries[step]) countries[step] = {}
- if (Object.keys(countries[step]).length < 3) {
- countries[step][row.val] = row.cnt
- }
+ countries[step][row.val] = row.cnt
} else if (row.type === 'so') {
if (!sources[step]) sources[step] = {}
- if (Object.keys(sources[step]).length < 3) {
- sources[step][row.val] = row.cnt
- }
+ sources[step][row.val] = row.cnt
}
}
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index aa4ee7882..95376b1f1 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -1526,15 +1526,15 @@ export class AnalyticsService {
session_info AS (
SELECT
psid,
- any(cc) AS cc,
- any(if(domain(ref) != '', domain(ref), 'Direct / None')) AS source
+ argMin(cc, created) AS cc,
+ argMin(if(domain(ref) != '', domain(ref), 'Direct / None'), created) AS source
FROM (
- SELECT psid, cc, ref
+ SELECT psid, cc, ref, created
FROM analytics
WHERE pid = {pid:FixedString(12)} AND psid != 0
AND created BETWEEN {groupFrom:String} AND {groupTo:String}
UNION ALL
- SELECT psid, cc, ref
+ SELECT psid, cc, ref, created
FROM customEV
WHERE pid = {pid:FixedString(12)} AND psid != 0
AND created BETWEEN {groupFrom:String} AND {groupTo:String}
@@ -1556,6 +1556,7 @@ export class AnalyticsService {
GROUP BY e.step, si.source
)
ORDER BY step, type, cnt DESC
+ LIMIT 3 BY step, type
`
const { data } = await clickhouse
@@ -1579,14 +1580,10 @@ export class AnalyticsService {
const { step } = row
if (row.type === 'cc') {
if (!countries[step]) countries[step] = {}
- if (Object.keys(countries[step]).length < 3) {
- countries[step][row.val] = row.cnt
- }
+ countries[step][row.val] = row.cnt
} else if (row.type === 'so') {
if (!sources[step]) sources[step] = {}
- if (Object.keys(sources[step]).length < 3) {
- sources[step][row.val] = row.cnt
- }
+ sources[step][row.val] = row.cnt
}
}