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!

Get rid of viewId parameter in favour of metrics

Blue Mouse5319a2fac621
diff --git a/apps/production/src/analytics/analytics.controller.ts b/apps/production/src/analytics/analytics.controller.ts
index d00ed9c64..fa7e4e316 100644
--- a/apps/production/src/analytics/analytics.controller.ts
+++ b/apps/production/src/analytics/analytics.controller.ts
@@ -84,7 +84,6 @@ import { ErrorDTO } from './dto/error.dto'
import { GetErrorsDto } from './dto/get-errors.dto'
import { GetErrorDTO } from './dto/get-error.dto'
import { PatchStatusDTO } from './dto/patch-status.dto'
-import { ProjectsViewsRepository } from '../project/repositories/projects-views.repository'
import {
customEventTransformer,
errorEventTransformer,
@@ -194,7 +193,6 @@ export class AnalyticsController {
constructor(
private readonly analyticsService: AnalyticsService,
private readonly logger: AppLoggerService,
- private readonly projectsViewsRepository: ProjectsViewsRepository,
) {}
@ApiBearerAuth()
@@ -215,7 +213,7 @@ export class AnalyticsController {
filters,
timezone = DEFAULT_TIMEZONE,
mode = ChartRenderMode.PERIODICAL,
- viewId,
+ metrics,
} = data
this.analyticsService.validatePID(pid)
@@ -231,37 +229,6 @@ export class AnalyticsController {
await this.analyticsService.checkBillingAccess(pid)
- if (viewId && filters) {
- throw new ConflictException('Cannot specify both viewId and filters.')
- }
-
- let meta: Awaited<ReturnType<typeof this.analyticsService.getMetaResult>>
-
- if (viewId) {
- const view = await this.projectsViewsRepository.findProjectView(
- pid,
- viewId,
- )
-
- if (!view) {
- throw new NotFoundException('View not found.')
- }
-
- const customEvents = view.customEvents.map(event => ({
- customEventName: event.customEventName,
- metaKey: event.metaKey,
- metaValue: event.metaValue,
- metaValueType: event.metaValueType,
- }))
-
- const metaKeys = customEvents.map(event => event.metaKey)
- meta = await this.analyticsService.getMetaResult(
- pid,
- metaKeys,
- customEvents,
- )
- }
-
let newTimebucket = timeBucket
let allowedTumebucketForPeriodAll
@@ -367,6 +334,18 @@ export class AnalyticsController {
)
}
+ let meta: Awaited<ReturnType<typeof this.analyticsService.getMetaResult>>
+
+ if (!isCaptcha) {
+ const parsedMetrics = this.analyticsService.parseMetrics(metrics)
+
+ console.log('parsedMetrics:', parsedMetrics)
+
+ if (!_isEmpty(parsedMetrics)) {
+ meta = await this.analyticsService.getMetaResult(pid, parsedMetrics)
+ }
+ }
+
return {
...result,
customs,
diff --git a/apps/production/src/analytics/analytics.service.ts b/apps/production/src/analytics/analytics.service.ts
index 17c5c84a1..8fdceeef0 100644
--- a/apps/production/src/analytics/analytics.service.ts
+++ b/apps/production/src/analytics/analytics.service.ts
@@ -100,6 +100,7 @@ import {
import { ErrorDTO } from './dto/error.dto'
import { GetPagePropertyMetaDTO } from './dto/get-page-property-meta.dto'
import { ProjectViewCustomEventMetaValueType } from '../project/entity/project-view-custom-event.entity'
+import { ProjectViewCustomEventDto } from '../project/dto/create-project-view.dto'
dayjs.extend(utc)
dayjs.extend(dayjsTimezone)
@@ -879,6 +880,21 @@ export class AnalyticsService {
)
}
+ parseMetrics(metrics: string): ProjectViewCustomEventDto[] {
+ if (metrics === '""' || _isEmpty(metrics)) {
+ return []
+ }
+
+ try {
+ return JSON.parse(metrics)
+ } catch {
+ console.error(
+ `[ERROR] parseMetrics: Cannot parse the metrics array: ${metrics}`,
+ )
+ return []
+ }
+ }
+
// returns SQL filters query in a format like 'AND col=value AND ...'
getFiltersQuery(
filters: string,
@@ -3790,16 +3806,8 @@ export class AnalyticsService {
}
}
- async getMetaResult(
- pid: string,
- metaKeys: string[],
- customEvents: {
- customEventName: string
- metaKey: string
- metaValue: string
- metaValueType: string
- }[],
- ) {
+ async getMetaResult(pid: string, customEvents: ProjectViewCustomEventDto[]) {
+ const metaKeys = customEvents.map(event => event.metaKey)
const params = {
pid,
..._reduce(
@@ -3849,12 +3857,14 @@ export class AnalyticsService {
`
try {
- return await clickhouse
+ const { data } = await clickhouse
.query({
query,
query_params: params,
})
.then(resultSet => resultSet.json())
+
+ return data
} catch (reason) {
console.error('[ERROR] (getMetaResult) - Clickhouse query error:')
console.error(reason)
diff --git a/apps/production/src/analytics/dto/getData.dto.ts b/apps/production/src/analytics/dto/getData.dto.ts
index e6a19183e..c11be4bde 100644
--- a/apps/production/src/analytics/dto/getData.dto.ts
+++ b/apps/production/src/analytics/dto/getData.dto.ts
@@ -1,5 +1,5 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsNotEmpty, IsOptional, IsString, IsUUID } from 'class-validator'
+import { IsNotEmpty, IsOptional } from 'class-validator'
import { DEFAULT_TIMEZONE } from '../../user/entities/user.entity'
export enum TimeBucketType {
@@ -54,9 +54,9 @@ export class AnalyticsGET_DTO {
})
mode?: ChartRenderMode
- @ApiProperty({ description: 'The id of the view' })
- @IsUUID('4')
- @IsString()
+ @ApiProperty({
+ required: false,
+ })
@IsOptional()
- viewId: string
+ metrics?: string
}