📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
remove duplicated code
Blue Mouse81a21eeaeb23
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.tsindex 0f440424c..1bb037f92 100644--- a/backend/apps/cloud/src/analytics/analytics.controller.ts+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts@@ -1116,7 +1116,11 @@ export class AnalyticsController {errorDTO.profileId,)- await this.analyticsService.recordSession(psid, errorDTO.pid, profileId)+ await this.analyticsService.recordSessionActivity(+ psid,+ errorDTO.pid,+ profileId,+ )const { city, region, regionCode, country } = getGeoDetails(ip, errorDTO.tz)@@ -1251,7 +1255,11 @@ export class AnalyticsController {eventsDTO.profileId,)- await this.analyticsService.recordSession(psid, eventsDTO.pid, profileId)+ await this.analyticsService.recordSessionActivity(+ psid,+ eventsDTO.pid,+ profileId,+ )const transformed = customEventTransformer(psid,@@ -1335,7 +1343,7 @@ export class AnalyticsController {)await this.analyticsService.extendSessionTTL(psid)- await this.analyticsService.updateSessionActivity(psid, pid, profileId)+ await this.analyticsService.recordSessionActivity(psid, pid, profileId)return {}}@@ -1370,7 +1378,11 @@ export class AnalyticsController {logDTO.profileId,)- await this.analyticsService.recordSession(psid, logDTO.pid, profileId)+ await this.analyticsService.recordSessionActivity(+ psid,+ logDTO.pid,+ profileId,+ )if (!unique && logDTO.unique) {throw new ForbiddenException(@@ -1511,7 +1523,11 @@ export class AnalyticsController {ip,)- await this.analyticsService.recordSession(psid, logDTO.pid, profileId)+ await this.analyticsService.recordSessionActivity(+ psid,+ logDTO.pid,+ profileId,+ )const { city, region, regionCode, country } = getGeoDetails(ip)diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex dcd8b1c08..b66121e66 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -1218,7 +1218,7 @@ export class AnalyticsService {return profileId.startsWith(AnalyticsService.PROFILE_PREFIX_USER)}- async recordSession(+ async recordSessionActivity(psid: string,pid: string,profileId: string,@@ -1266,59 +1266,6 @@ export class AnalyticsService {}}- async updateSessionActivity(- psid: string,- pid: string,- profileId: string,- ): Promise<void> {- const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')-- try {- // TODO: Potentially slow because every write request reads from the database - consider caching or other optimisation- const query = `- SELECT firstSeen, pageviews, events- FROM sessions FINAL- WHERE psid = {psid:UInt64}- AND pid = {pid:FixedString(12)}- LIMIT 1- `-- const { data } = await clickhouse- .query({- query,- query_params: { psid, pid },- })- .then(resultSet =>- resultSet.json<{- firstSeen: string- pageviews: number- events: number- }>(),- )-- const existingSession = data[0]-- await clickhouse.insert({- table: 'sessions',- format: 'JSONEachRow',- values: [- {- psid,- pid,- profileId,- firstSeen: existingSession?.firstSeen ?? now,- lastSeen: now,- pageviews: existingSession?.pageviews ?? 0,- events: existingSession?.events ?? 0,- },- ],- clickhouse_settings: { async_insert: 1 },- })- } catch (error) {- console.error('Failed to update session activity:', error)- }- }-async checkSessionExistsInClickHouse(psid: string,pid: string,diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.tsindex cc4ffc843..7643df280 100644--- a/backend/apps/community/src/analytics/analytics.controller.ts+++ b/backend/apps/community/src/analytics/analytics.controller.ts@@ -923,7 +923,11 @@ export class AnalyticsController {eventsDTO.profileId,)- await this.analyticsService.recordSession(psid, eventsDTO.pid, profileId)+ await this.analyticsService.recordSessionActivity(+ psid,+ eventsDTO.pid,+ profileId,+ )const transformed = customEventTransformer(psid,@@ -1007,7 +1011,7 @@ export class AnalyticsController {)await this.analyticsService.extendSessionTTL(psid)- await this.analyticsService.updateSessionActivity(psid, pid, profileId)+ await this.analyticsService.recordSessionActivity(psid, pid, profileId)return {}}@@ -1041,7 +1045,11 @@ export class AnalyticsController {logDTO.profileId,)- await this.analyticsService.recordSession(psid, logDTO.pid, profileId)+ await this.analyticsService.recordSessionActivity(+ psid,+ logDTO.pid,+ profileId,+ )if (unique && logDTO.unique) {throw new ForbiddenException(@@ -1183,7 +1191,11 @@ export class AnalyticsController {ip,)- await this.analyticsService.recordSession(psid, logDTO.pid, profileId)+ await this.analyticsService.recordSessionActivity(+ psid,+ logDTO.pid,+ profileId,+ )const { city, region, regionCode, country } = getGeoDetails(ip, null)@@ -1486,7 +1498,11 @@ export class AnalyticsController {errorDTO.profileId,)- await this.analyticsService.recordSession(psid, errorDTO.pid, profileId)+ await this.analyticsService.recordSessionActivity(+ psid,+ errorDTO.pid,+ profileId,+ )const { city, region, regionCode, country } = getGeoDetails(ip, errorDTO.tz)diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.tsindex 708830575..66b754058 100644--- a/backend/apps/community/src/analytics/analytics.service.ts+++ b/backend/apps/community/src/analytics/analytics.service.ts@@ -1231,7 +1231,7 @@ export class AnalyticsService {return profileId.startsWith(AnalyticsService.PROFILE_PREFIX_USER)}- async recordSession(+ async recordSessionActivity(psid: string,pid: string,profileId: string,@@ -1280,56 +1280,6 @@ export class AnalyticsService {}}- async updateSessionActivity(- psid: string,- pid: string,- profileId: string,- ): Promise<void> {- const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')-- try {- // TODO: Potentially slow because every write request reads from the database - consider caching or other optimisation- const query = `- SELECT firstSeen- FROM sessions FINAL- WHERE psid = {psid:UInt64}- AND pid = {pid:FixedString(12)}- LIMIT 1- `-- const { data } = await clickhouse- .query({- query,- query_params: { psid, pid },- })- .then(resultSet =>- resultSet.json<{- firstSeen: string- }>(),- )-- const existingSession = data[0]-- await clickhouse.insert({- table: 'sessions',- format: 'JSONEachRow',- values: [- {- psid,- pid,- profileId,- firstSeen: existingSession?.firstSeen ?? now,- lastSeen: now,- },- ],- clickhouse_settings: { async_insert: 1 },- })- } catch (error) {- this.logger.error('Failed to update session activity:', error)- throw error- }- }-formatFunnel(data: IFunnelCHResponse[], pages: string[]): IFunnel[] {const funnel = _map(data, (row, index) => {const value = pages[index]