📈 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
properly handle browser/os versions for filtering
Blue Mouse9819e39e8eaf
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.tsindex 91ee92c61..c4ce6c9a2 100644--- a/backend/apps/cloud/src/analytics/analytics.controller.ts+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts@@ -65,6 +65,7 @@ import {} from '../common/utils'import { GetCustomEventsDto } from './dto/get-custom-events.dto'import { GetFiltersDto } from './dto/get-filters.dto'+import { GetVersionFiltersDto } from './dto/get-version-filters.dto'import {IFunnel,IGetFunnel,@@ -525,6 +526,26 @@ export class AnalyticsController {return this.analyticsService.getErrorsFilters(pid, type)}+ @Get('filters/versions')+ @Auth(true, true)+ async getVersionFilters(+ @Query() data: GetVersionFiltersDto,+ @CurrentUserId() uid: string,+ @Headers() headers: { 'x-password'?: string },+ ) {+ const { pid, type, column } = data++ await this.analyticsService.checkProjectAccess(+ pid,+ uid,+ headers['x-password'],+ )++ await this.analyticsService.checkBillingAccess(pid)++ return this.analyticsService.getVersionFilters(pid, type, column)+ }+@Get('chart')@Auth(true, true)async getChartData(diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.tsindex f4bd8e440..6f7a6ddae 100644--- a/backend/apps/cloud/src/analytics/analytics.service.ts+++ b/backend/apps/cloud/src/analytics/analytics.service.ts@@ -2004,6 +2004,34 @@ export class AnalyticsService {return _map(data, type)}+ async getVersionFilters(+ pid: string,+ type: 'traffic' | 'errors',+ column: 'br' | 'os',+ ): Promise<Array<{ name: string; version: string }>> {+ const safeTable = type === 'errors' ? 'errors' : 'analytics'+ const safeVersionCol = column === 'br' ? 'brv' : 'osv'++ const query = `+ SELECT ${column}, ${safeVersionCol}+ FROM ${safeTable}+ WHERE pid={pid:FixedString(12)} AND ${column} IS NOT NULL AND ${safeVersionCol} IS NOT NULL+ GROUP BY ${column}, ${safeVersionCol}+ `++ const { data } = await clickhouse+ .query({+ query,+ query_params: { pid },+ })+ .then(resultSet => resultSet.json<Record<string, string>[]>())++ return data.map(row => ({+ name: row[column],+ version: row[safeVersionCol],+ }))+ }+async generateParams(parsedFilters: Array<{ [key: string]: string }>,subQuery: string,diff --git a/backend/apps/cloud/src/analytics/dto/get-version-filters.dto.ts b/backend/apps/cloud/src/analytics/dto/get-version-filters.dto.tsnew file mode 100644index 000000000..9918baef2--- /dev/null+++ b/backend/apps/cloud/src/analytics/dto/get-version-filters.dto.ts@@ -0,0 +1,38 @@+import { ApiProperty } from '@nestjs/swagger'+import { IsNotEmpty, Matches, IsIn } from 'class-validator'+import { PID_REGEX } from '../../common/constants'++export class GetVersionFiltersDto {+ @ApiProperty({+ example: 'aUn1quEid-3',+ required: true,+ description: 'The project ID',+ })+ @IsNotEmpty()+ @Matches(PID_REGEX, { message: 'The provided Project ID (pid) is incorrect' })+ pid: string++ @ApiProperty({+ example: 'traffic',+ required: true,+ description: 'Data type: traffic or errors',+ enum: ['traffic', 'errors'],+ })+ @IsNotEmpty()+ @IsIn(['traffic', 'errors'], {+ message: 'type must be either traffic or errors',+ })+ type: 'traffic' | 'errors'++ @ApiProperty({+ example: 'br',+ required: true,+ description: 'Column type: br (browser) or os',+ enum: ['br', 'os'],+ })+ @IsNotEmpty()+ @IsIn(['br', 'os'], {+ message: 'column must be either br or os',+ })+ column: 'br' | 'os'+}diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.tsindex 1beb8f056..25343eb52 100644--- a/backend/apps/community/src/analytics/analytics.controller.ts+++ b/backend/apps/community/src/analytics/analytics.controller.ts@@ -41,6 +41,7 @@ import { PageviewsDto } from './dto/pageviews.dto'import { EventsDto } from './dto/events.dto'import { GetDataDto, ChartRenderMode } from './dto/getData.dto'import { GetFiltersDto } from './dto/get-filters.dto'+import { GetVersionFiltersDto } from './dto/get-version-filters.dto'import { GetCustomEventMetadata } from './dto/get-custom-event-meta.dto'import { GetPagePropertyMetaDto } from './dto/get-page-property-meta.dto'import { GetUserFlowDto } from './dto/getUserFlow.dto'@@ -1400,6 +1401,24 @@ export class AnalyticsController {return this.analyticsService.getErrorsFilters(pid, type)}+ @Get('filters/versions')+ @Auth(true, true)+ async getVersionFilters(+ @Query() data: GetVersionFiltersDto,+ @CurrentUserId() uid: string,+ @Headers() headers: { 'x-password'?: string },+ ) {+ const { pid, type, column } = data++ await this.analyticsService.checkProjectAccess(+ pid,+ uid,+ headers['x-password'],+ )++ return this.analyticsService.getVersionFilters(pid, type, column)+ }+@Post('error')@Public()async logError(@Body() errorDTO: ErrorDto, @Headers() headers, @Ip() reqIP) {diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.tsindex 16bd55009..769e682ea 100644--- a/backend/apps/community/src/analytics/analytics.service.ts+++ b/backend/apps/community/src/analytics/analytics.service.ts@@ -487,6 +487,34 @@ export class AnalyticsService {return _map(data, type)}+ async getVersionFilters(+ pid: string,+ type: 'traffic' | 'errors',+ column: 'br' | 'os',+ ): Promise<Array<{ name: string; version: string }>> {+ const safeTable = type === 'errors' ? 'errors' : 'analytics'+ const safeVersionCol = column === 'br' ? 'brv' : 'osv'++ const query = `+ SELECT ${column}, ${safeVersionCol}+ FROM ${safeTable}+ WHERE pid={pid:FixedString(12)} AND ${column} IS NOT NULL AND ${safeVersionCol} IS NOT NULL+ GROUP BY ${column}, ${safeVersionCol}+ `++ const { data } = await clickhouse+ .query({+ query,+ query_params: { pid },+ })+ .then(resultSet => resultSet.json<Record<string, string>[]>())++ return data.map(row => ({+ name: row[column],+ version: row[safeVersionCol],+ }))+ }+getDataTypeColumns(dataType: DataType): string[] {if (dataType === DataType.ANALYTICS) {return TRAFFIC_COLUMNSdiff --git a/backend/apps/community/src/analytics/dto/get-version-filters.dto.ts b/backend/apps/community/src/analytics/dto/get-version-filters.dto.tsnew file mode 100644index 000000000..9918baef2--- /dev/null+++ b/backend/apps/community/src/analytics/dto/get-version-filters.dto.ts@@ -0,0 +1,38 @@+import { ApiProperty } from '@nestjs/swagger'+import { IsNotEmpty, Matches, IsIn } from 'class-validator'+import { PID_REGEX } from '../../common/constants'++export class GetVersionFiltersDto {+ @ApiProperty({+ example: 'aUn1quEid-3',+ required: true,+ description: 'The project ID',+ })+ @IsNotEmpty()+ @Matches(PID_REGEX, { message: 'The provided Project ID (pid) is incorrect' })+ pid: string++ @ApiProperty({+ example: 'traffic',+ required: true,+ description: 'Data type: traffic or errors',+ enum: ['traffic', 'errors'],+ })+ @IsNotEmpty()+ @IsIn(['traffic', 'errors'], {+ message: 'type must be either traffic or errors',+ })+ type: 'traffic' | 'errors'++ @ApiProperty({+ example: 'br',+ required: true,+ description: 'Column type: br (browser) or os',+ enum: ['br', 'os'],+ })+ @IsNotEmpty()+ @IsIn(['br', 'os'], {+ message: 'column must be either br or os',+ })+ column: 'br' | 'os'+}diff --git a/web/app/api/index.ts b/web/app/api/index.tsindex d76f8e2ec..df282d240 100644--- a/web/app/api/index.ts+++ b/web/app/api/index.ts@@ -1431,6 +1431,18 @@ export const getErrorsFilters = (pid: string, type: string, password = '') =>throw error})+export const getVersionFilters = (pid: string, type: 'traffic' | 'errors', column: 'br' | 'os', password = '') =>+ api+ .get(`log/filters/versions?pid=${pid}&type=${type}&column=${column}`, {+ headers: {+ 'x-password': password,+ },+ })+ .then((response): Array<{ name: string; version: string }> => response.data)+ .catch((error) => {+ throw error+ })+export const resetFilters = (pid: string, type: string, filters: string[]) =>api.delete(`project/reset-filters/${pid}?type=${type}&filters=${JSON.stringify(filters)}`)diff --git a/web/app/pages/Project/View/components/SearchFilters.tsx b/web/app/pages/Project/View/components/SearchFilters.tsxindex 3063ce078..e62e17cbd 100644--- a/web/app/pages/Project/View/components/SearchFilters.tsx+++ b/web/app/pages/Project/View/components/SearchFilters.tsx@@ -41,7 +41,7 @@ import { useState, useEffect, useCallback, useRef, useMemo, Fragment, ReactNodeimport { useTranslation } from 'react-i18next'import { useNavigate, useSearchParams } from 'react-router'-import { getFilters, getErrorsFilters } from '~/api'+import { getFilters, getErrorsFilters, getVersionFilters } from '~/api'import {FILTERS_PANELS_ORDER,ERRORS_FILTERS_PANELS_ORDER,@@ -531,36 +531,15 @@ const SearchFilters = ({ showModal, setShowModal, tnMapping, filters, type }: Setry {let result: string[]- // For browser/OS versions, fetch both parent and version data to create combined entries+ // For browser/OS versions, fetch valid combinations from the backendif (column === 'brv') {- // Fetch both browser names and versions- const [browsers, versions] = await Promise.all([- type === 'errors' ? getErrorsFilters(id, 'br', projectPassword) : getFilters(id, 'br', projectPassword),- type === 'errors' ? getErrorsFilters(id, 'brv', projectPassword) : getFilters(id, 'brv', projectPassword),- ])- // Create combined entries: "Chrome|||120.5" format- // We'll show all combinations since we don't know which versions belong to which browser- const combined: string[] = []- browsers.forEach((browser) => {- versions.forEach((version) => {- combined.push(createVersionValue(browser, version))- })- })- result = combined+ const dataType = type === 'errors' ? 'errors' : 'traffic'+ const pairs = await getVersionFilters(id, dataType, 'br', projectPassword)+ result = pairs.map((p) => createVersionValue(p.name, p.version))} else if (column === 'osv') {- // Fetch both OS names and versions- const [osList, versions] = await Promise.all([- type === 'errors' ? getErrorsFilters(id, 'os', projectPassword) : getFilters(id, 'os', projectPassword),- type === 'errors' ? getErrorsFilters(id, 'osv', projectPassword) : getFilters(id, 'osv', projectPassword),- ])- // Create combined entries- const combined: string[] = []- osList.forEach((os) => {- versions.forEach((version) => {- combined.push(createVersionValue(os, version))- })- })- result = combined+ const dataType = type === 'errors' ? 'errors' : 'traffic'+ const pairs = await getVersionFilters(id, dataType, 'os', projectPassword)+ result = pairs.map((p) => createVersionValue(p.name, p.version))} else {if (type === 'errors') {result = await getErrorsFilters(id, column, projectPassword)