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!

fix(analytics): resolve ClickHouse ev column error, all-time chart bucket mismatch, and swallowed HTTP exceptions

Blue Mouse6200fec458c4
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.ts
index b2ba6f434..daae298ed 100644
--- a/backend/apps/cloud/src/analytics/analytics.controller.ts
+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts
@@ -2279,13 +2279,16 @@ export class AnalyticsController {
'GET /analytics/profile/sessions',
)
+ const [filtersQuery, filtersParams, appliedFilters, customEVFilterApplied] =
+ this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS)
+
let timeBucket
let diff
if (period === 'all') {
const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- 'analytics',
+ customEVFilterApplied ? 'customEV' : 'analytics',
)
timeBucket = res.timeBucket[0]
@@ -2294,9 +2297,6 @@ export class AnalyticsController {
timeBucket = getLowestPossibleTimeBucket(period, from, to)
}
- const [filtersQuery, filtersParams, appliedFilters] =
- this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS)
-
const safeTimezone = this.analyticsService.getSafeTimezone(timezone)
const { groupFromUTC, groupToUTC } = this.analyticsService.getGroupFromTo(
from,
@@ -2324,6 +2324,7 @@ export class AnalyticsController {
safeTimezone,
take,
skip,
+ customEVFilterApplied,
)
return { sessions, appliedFilters, take, skip }
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 8542fa7f5..c93d16037 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -1933,13 +1933,18 @@ export class AnalyticsService {
customEVFilterApplied,
}
- // For 'all' period, we use month time bucket for chart
if (includeChart) {
- const allTimeChartBucket = TimeBucketType.MONTH
- const { diff: allDiff } = await this.calculateTimeBucketForAllTime(
- pid,
- customEVFilterApplied ? 'customEV' : 'analytics',
+ const { diff: allDiff, timeBucket: allowedBuckets } =
+ await this.calculateTimeBucketForAllTime(
+ pid,
+ customEVFilterApplied ? 'customEV' : 'analytics',
+ )
+ const allTimeChartBucket = _includes(
+ allowedBuckets,
+ TimeBucketType.MONTH,
)
+ ? TimeBucketType.MONTH
+ : _last(allowedBuckets) || TimeBucketType.MONTH
const { groupFrom: allFrom, groupTo: allTo } = this.getGroupFromTo(
undefined,
undefined,
@@ -2201,6 +2206,10 @@ export class AnalyticsService {
result[pid].chart = chartData
}
} catch (reason) {
+ if (reason instanceof HttpException) {
+ throw reason
+ }
+
console.error(
`[ERROR] (getAnalyticsSummary) Error occurred for PID ${pid}`,
)
@@ -5416,9 +5425,63 @@ export class AnalyticsService {
safeTimezone: string,
take = 30,
skip = 0,
+ customEVFilterApplied = false,
): Promise<object[]> {
- const query = `
- WITH all_profile_events AS (
+ let allProfileEventsCTE: string
+
+ if (customEVFilterApplied) {
+ allProfileEventsCTE = `
+ all_profile_events AS (
+ SELECT
+ all_events.psidCasted,
+ all_events.pid,
+ all_events.profileId,
+ all_events.cc,
+ all_events.os,
+ all_events.br,
+ all_events.created_tz
+ FROM (
+ SELECT
+ CAST(psid, 'String') AS psidCasted,
+ pid,
+ profileId,
+ cc,
+ os,
+ br,
+ toTimeZone(created, {timezone:String}) AS created_tz
+ FROM analytics
+ WHERE pid = {pid:FixedString(12)}
+ AND profileId = {profileId:String}
+ AND psid IS NOT NULL
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ UNION ALL
+ SELECT
+ CAST(psid, 'String') AS psidCasted,
+ pid,
+ profileId,
+ cc,
+ os,
+ br,
+ toTimeZone(created, {timezone:String}) AS created_tz
+ FROM customEV
+ WHERE pid = {pid:FixedString(12)}
+ AND profileId = {profileId:String}
+ AND psid IS NOT NULL
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ ) AS all_events
+ WHERE all_events.psidCasted IN (
+ SELECT DISTINCT CAST(psid, 'String')
+ FROM customEV
+ WHERE pid = {pid:FixedString(12)}
+ AND profileId = {profileId:String}
+ AND psid IS NOT NULL
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ ${filtersQuery}
+ )
+ )`
+ } else {
+ allProfileEventsCTE = `
+ all_profile_events AS (
SELECT
CAST(psid, 'String') AS psidCasted,
pid,
@@ -5448,7 +5511,11 @@ export class AnalyticsService {
AND psid IS NOT NULL
AND created BETWEEN {groupFrom:String} AND {groupTo:String}
${filtersQuery}
- ),
+ )`
+ }
+
+ const query = `
+ WITH ${allProfileEventsCTE},
profile_sessions AS (
SELECT
psidCasted,
diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts
index fc1919670..11c8bdd21 100644
--- a/backend/apps/community/src/analytics/analytics.controller.ts
+++ b/backend/apps/community/src/analytics/analytics.controller.ts
@@ -2216,13 +2216,16 @@ export class AnalyticsController {
'GET /analytics/profile/sessions',
)
+ const [filtersQuery, filtersParams, appliedFilters, customEVFilterApplied] =
+ this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS)
+
let timeBucket
let diff
if (period === 'all') {
const res = await this.analyticsService.calculateTimeBucketForAllTime(
pid,
- 'analytics',
+ customEVFilterApplied ? 'customEV' : 'analytics',
)
timeBucket = res.timeBucket[0]
@@ -2231,9 +2234,6 @@ export class AnalyticsController {
timeBucket = getLowestPossibleTimeBucket(period, from, to)
}
- const [filtersQuery, filtersParams, appliedFilters] =
- this.analyticsService.getFiltersQuery(filters, DataType.ANALYTICS)
-
const safeTimezone = this.analyticsService.getSafeTimezone(timezone)
const { groupFromUTC, groupToUTC } = this.analyticsService.getGroupFromTo(
from,
@@ -2261,6 +2261,7 @@ export class AnalyticsController {
safeTimezone,
take,
skip,
+ customEVFilterApplied,
)
return { sessions, appliedFilters, take, skip }
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 3d31e7c3b..529dbd7b7 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -31,6 +31,7 @@ import {
Injectable,
BadRequestException,
InternalServerErrorException,
+ HttpException,
UnprocessableEntityException,
PreconditionFailedException,
Logger,
@@ -1614,13 +1615,18 @@ export class AnalyticsService {
customEVFilterApplied,
}
- // For 'all' period, we use month time bucket for chart
if (includeChart) {
- const allTimeChartBucket = TimeBucketType.MONTH
- const { diff: allDiff } = await this.calculateTimeBucketForAllTime(
- pid,
- customEVFilterApplied ? 'customEV' : 'analytics',
+ const { diff: allDiff, timeBucket: allowedBuckets } =
+ await this.calculateTimeBucketForAllTime(
+ pid,
+ customEVFilterApplied ? 'customEV' : 'analytics',
+ )
+ const allTimeChartBucket = _includes(
+ allowedBuckets,
+ TimeBucketType.MONTH,
)
+ ? TimeBucketType.MONTH
+ : _last(allowedBuckets) || TimeBucketType.MONTH
const { groupFrom: allFrom, groupTo: allTo } = this.getGroupFromTo(
undefined,
undefined,
@@ -1882,6 +1888,10 @@ export class AnalyticsService {
result[pid].chart = chartData
}
} catch (reason) {
+ if (reason instanceof HttpException) {
+ throw reason
+ }
+
console.error(
`[ERROR] (getAnalyticsSummary) Error occurred for PID ${pid}`,
)
@@ -5633,9 +5643,63 @@ export class AnalyticsService {
safeTimezone: string,
take = 30,
skip = 0,
+ customEVFilterApplied = false,
): Promise<object[]> {
- const query = `
- WITH all_profile_events AS (
+ let allProfileEventsCTE: string
+
+ if (customEVFilterApplied) {
+ allProfileEventsCTE = `
+ all_profile_events AS (
+ SELECT
+ all_events.psidCasted,
+ all_events.pid,
+ all_events.profileId,
+ all_events.cc,
+ all_events.os,
+ all_events.br,
+ all_events.created_tz
+ FROM (
+ SELECT
+ CAST(psid, 'String') AS psidCasted,
+ pid,
+ profileId,
+ cc,
+ os,
+ br,
+ toTimeZone(created, {timezone:String}) AS created_tz
+ FROM analytics
+ WHERE pid = {pid:FixedString(12)}
+ AND profileId = {profileId:String}
+ AND psid IS NOT NULL
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ UNION ALL
+ SELECT
+ CAST(psid, 'String') AS psidCasted,
+ pid,
+ profileId,
+ cc,
+ os,
+ br,
+ toTimeZone(created, {timezone:String}) AS created_tz
+ FROM customEV
+ WHERE pid = {pid:FixedString(12)}
+ AND profileId = {profileId:String}
+ AND psid IS NOT NULL
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ ) AS all_events
+ WHERE all_events.psidCasted IN (
+ SELECT DISTINCT CAST(psid, 'String')
+ FROM customEV
+ WHERE pid = {pid:FixedString(12)}
+ AND profileId = {profileId:String}
+ AND psid IS NOT NULL
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ ${filtersQuery}
+ )
+ )`
+ } else {
+ allProfileEventsCTE = `
+ all_profile_events AS (
SELECT
CAST(psid, 'String') AS psidCasted,
pid,
@@ -5665,7 +5729,11 @@ export class AnalyticsService {
AND psid IS NOT NULL
AND created BETWEEN {groupFrom:String} AND {groupTo:String}
${filtersQuery}
- ),
+ )`
+ }
+
+ const query = `
+ WITH ${allProfileEventsCTE},
profile_sessions AS (
SELECT
psidCasted,