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!

Convert event metadata JSON primitive values to strings

Blue Mouse41045fbdde30
diff --git a/backend/apps/cloud/src/analytics/analytics.controller.ts b/backend/apps/cloud/src/analytics/analytics.controller.ts
index 55b9114e3..4ec9744fb 100644
--- a/backend/apps/cloud/src/analytics/analytics.controller.ts
+++ b/backend/apps/cloud/src/analytics/analytics.controller.ts
@@ -13,6 +13,8 @@ import {
Body,
Query,
UseGuards,
+ UsePipes,
+ ValidationPipe,
Get,
Post,
Patch,
@@ -183,6 +185,11 @@ const TRANSPARENT_GIF_BUFFER = Buffer.from(
@ApiTags('Analytics')
@UseGuards(OptionalJwtAccessTokenGuard, RolesGuard)
+@UsePipes(
+ new ValidationPipe({
+ transform: true,
+ }),
+)
@Controller(['log', 'v1/log'])
export class AnalyticsController {
constructor(
@@ -1088,8 +1095,6 @@ export class AnalyticsController {
const { name, message, lineno, colno, filename, stackTrace, meta } =
errorDTO
- this.analyticsService.validateCustomEVMeta(meta)
-
const transformed = errorEventTransformer(
psid,
this.analyticsService.getErrorID(errorDTO),
@@ -1166,8 +1171,6 @@ export class AnalyticsController {
const ip = getIPFromHeaders(headers, true) || reqIP || ''
- this.analyticsService.validateCustomEVMeta(eventsDTO.meta)
-
const isBot = await this.analyticsService.isBot(eventsDTO.pid, userAgent)
if (isBot) {
diff --git a/backend/apps/cloud/src/analytics/analytics.service.ts b/backend/apps/cloud/src/analytics/analytics.service.ts
index 28d47fe39..8bc5322a0 100644
--- a/backend/apps/cloud/src/analytics/analytics.service.ts
+++ b/backend/apps/cloud/src/analytics/analytics.service.ts
@@ -3294,18 +3294,6 @@ export class AnalyticsService {
return result
}
- validateCustomEVMeta(meta: any) {
- if (typeof meta === 'undefined') {
- return
- }
-
- if (_some(_values(meta), val => !_isString(val))) {
- throw new UnprocessableEntityException(
- 'The provided custom event metadata is incorrect (some values are not strings)',
- )
- }
- }
-
async getCustomEventMetadata(data: GetCustomEventMetadata): Promise<{
result: IAggregatedMetadata[]
appliedFilters: GetFiltersQuery[2]
diff --git a/backend/apps/cloud/src/analytics/dto/error.dto.ts b/backend/apps/cloud/src/analytics/dto/error.dto.ts
index 22f4387ea..5c992b131 100644
--- a/backend/apps/cloud/src/analytics/dto/error.dto.ts
+++ b/backend/apps/cloud/src/analytics/dto/error.dto.ts
@@ -9,6 +9,7 @@ import {
IsObject,
Validate,
} from 'class-validator'
+import { Transform } from 'class-transformer'
import { PID_REGEX } from '../../common/constants'
import {
MAX_METADATA_KEYS,
@@ -16,6 +17,7 @@ import {
MetadataKeysQuantity,
MetadataValueType,
MetadataSizeLimit,
+ transformMetadataJsonPrimitivesToString,
} from './events.dto'
export class ErrorDto {
@@ -122,8 +124,9 @@ export class ErrorDto {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Transform(({ value }) => transformMetadataJsonPrimitivesToString(value))
@Validate(MetadataValueType, {
- message: 'All of metadata object values must be strings',
+ message: 'All of metadata object values must be primitive JSON values',
})
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have keys and values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
diff --git a/backend/apps/cloud/src/analytics/dto/events.dto.ts b/backend/apps/cloud/src/analytics/dto/events.dto.ts
index c8e837888..cc1656dbf 100644
--- a/backend/apps/cloud/src/analytics/dto/events.dto.ts
+++ b/backend/apps/cloud/src/analytics/dto/events.dto.ts
@@ -11,6 +11,7 @@ import {
Validate,
Matches,
} from 'class-validator'
+import { Transform } from 'class-transformer'
import { PID_REGEX } from '../../common/constants'
export const MAX_METADATA_KEYS = 100
@@ -43,6 +44,30 @@ export class MetadataKeysQuantity implements ValidatorConstraintInterface {
}
}
+export function transformMetadataJsonPrimitivesToString(value: any): any {
+ if (!value || typeof value !== 'object') {
+ return value
+ }
+
+ const transformed = { ...value }
+
+ for (const key in transformed) {
+ const val = transformed[key]
+
+ // Convert numbers, booleans, and null to strings
+ if (typeof val === 'number') {
+ transformed[key] = String(val)
+ } else if (typeof val === 'boolean') {
+ transformed[key] = String(val)
+ } else if (val === null) {
+ transformed[key] = 'null'
+ }
+ // Leave objects, arrays, and other types unchanged for later validation to catch
+ }
+
+ return transformed
+}
+
@ValidatorConstraint()
export class MetadataValueType implements ValidatorConstraintInterface {
validate(metadata: Record<string, string>) {
@@ -148,8 +173,9 @@ export class EventsDto {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Transform(({ value }) => transformMetadataJsonPrimitivesToString(value))
@Validate(MetadataValueType, {
- message: 'All of metadata object values must be strings',
+ message: 'All of metadata object values must be primitive JSON values',
})
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have keys and values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
diff --git a/backend/apps/cloud/src/analytics/dto/pageviews.dto.ts b/backend/apps/cloud/src/analytics/dto/pageviews.dto.ts
index a9fcb090e..d29c114c5 100644
--- a/backend/apps/cloud/src/analytics/dto/pageviews.dto.ts
+++ b/backend/apps/cloud/src/analytics/dto/pageviews.dto.ts
@@ -6,6 +6,7 @@ import {
Matches,
Validate,
} from 'class-validator'
+import { Transform } from 'class-transformer'
import {
MAX_METADATA_KEYS,
@@ -13,6 +14,7 @@ import {
MetadataKeysQuantity,
MetadataValueType,
MetadataSizeLimit,
+ transformMetadataJsonPrimitivesToString,
} from './events.dto'
import { PID_REGEX } from '../../common/constants'
@@ -123,8 +125,9 @@ export class PageviewsDto {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Transform(({ value }) => transformMetadataJsonPrimitivesToString(value))
@Validate(MetadataValueType, {
- message: 'All of metadata object values must be strings',
+ message: 'All of metadata object values must be primitive JSON values',
})
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
diff --git a/backend/apps/community/src/analytics/analytics.controller.ts b/backend/apps/community/src/analytics/analytics.controller.ts
index b1b99fd14..12ab35f8b 100644
--- a/backend/apps/community/src/analytics/analytics.controller.ts
+++ b/backend/apps/community/src/analytics/analytics.controller.ts
@@ -11,6 +11,8 @@ import {
Body,
Query,
UseGuards,
+ UsePipes,
+ ValidationPipe,
Get,
Post,
Patch,
@@ -170,6 +172,11 @@ const TRANSPARENT_GIF_BUFFER = Buffer.from(
@ApiTags('Analytics')
@UseGuards(OptionalJwtAccessTokenGuard, RolesGuard)
+@UsePipes(
+ new ValidationPipe({
+ transform: true,
+ }),
+)
@Controller(['log', 'v1/log'])
export class AnalyticsController {
constructor(
@@ -866,8 +873,6 @@ export class AnalyticsController {
const ip = getIPFromHeaders(headers) || reqIP || ''
- this.analyticsService.validateCustomEVMeta(eventsDTO.meta)
-
const isBot = await this.analyticsService.isBot(eventsDTO.pid, userAgent)
if (isBot) {
@@ -1417,8 +1422,6 @@ export class AnalyticsController {
const { name, message, lineno, colno, filename, stackTrace, meta } =
errorDTO
- this.analyticsService.validateCustomEVMeta(meta)
-
const transformed = errorEventTransformer(
psid,
this.analyticsService.getErrorID(errorDTO),
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 9fe1d2493..bda49dbef 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -2944,18 +2944,6 @@ export class AnalyticsService {
return result
}
- validateCustomEVMeta(meta: any) {
- if (typeof meta === 'undefined') {
- return
- }
-
- if (_some(_values(meta), val => !_isString(val))) {
- throw new UnprocessableEntityException(
- 'The provided custom event metadata is incorrect (some values are not strings)',
- )
- }
- }
-
async getPageProperties(
filtersQuery: string,
params: any,
diff --git a/backend/apps/community/src/analytics/dto/error.dto.ts b/backend/apps/community/src/analytics/dto/error.dto.ts
index 22f4387ea..5c992b131 100644
--- a/backend/apps/community/src/analytics/dto/error.dto.ts
+++ b/backend/apps/community/src/analytics/dto/error.dto.ts
@@ -9,6 +9,7 @@ import {
IsObject,
Validate,
} from 'class-validator'
+import { Transform } from 'class-transformer'
import { PID_REGEX } from '../../common/constants'
import {
MAX_METADATA_KEYS,
@@ -16,6 +17,7 @@ import {
MetadataKeysQuantity,
MetadataValueType,
MetadataSizeLimit,
+ transformMetadataJsonPrimitivesToString,
} from './events.dto'
export class ErrorDto {
@@ -122,8 +124,9 @@ export class ErrorDto {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Transform(({ value }) => transformMetadataJsonPrimitivesToString(value))
@Validate(MetadataValueType, {
- message: 'All of metadata object values must be strings',
+ message: 'All of metadata object values must be primitive JSON values',
})
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have keys and values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
diff --git a/backend/apps/community/src/analytics/dto/events.dto.ts b/backend/apps/community/src/analytics/dto/events.dto.ts
index c8e837888..cc1656dbf 100644
--- a/backend/apps/community/src/analytics/dto/events.dto.ts
+++ b/backend/apps/community/src/analytics/dto/events.dto.ts
@@ -11,6 +11,7 @@ import {
Validate,
Matches,
} from 'class-validator'
+import { Transform } from 'class-transformer'
import { PID_REGEX } from '../../common/constants'
export const MAX_METADATA_KEYS = 100
@@ -43,6 +44,30 @@ export class MetadataKeysQuantity implements ValidatorConstraintInterface {
}
}
+export function transformMetadataJsonPrimitivesToString(value: any): any {
+ if (!value || typeof value !== 'object') {
+ return value
+ }
+
+ const transformed = { ...value }
+
+ for (const key in transformed) {
+ const val = transformed[key]
+
+ // Convert numbers, booleans, and null to strings
+ if (typeof val === 'number') {
+ transformed[key] = String(val)
+ } else if (typeof val === 'boolean') {
+ transformed[key] = String(val)
+ } else if (val === null) {
+ transformed[key] = 'null'
+ }
+ // Leave objects, arrays, and other types unchanged for later validation to catch
+ }
+
+ return transformed
+}
+
@ValidatorConstraint()
export class MetadataValueType implements ValidatorConstraintInterface {
validate(metadata: Record<string, string>) {
@@ -148,8 +173,9 @@ export class EventsDto {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Transform(({ value }) => transformMetadataJsonPrimitivesToString(value))
@Validate(MetadataValueType, {
- message: 'All of metadata object values must be strings',
+ message: 'All of metadata object values must be primitive JSON values',
})
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have keys and values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
diff --git a/backend/apps/community/src/analytics/dto/pageviews.dto.ts b/backend/apps/community/src/analytics/dto/pageviews.dto.ts
index a9fcb090e..d29c114c5 100644
--- a/backend/apps/community/src/analytics/dto/pageviews.dto.ts
+++ b/backend/apps/community/src/analytics/dto/pageviews.dto.ts
@@ -6,6 +6,7 @@ import {
Matches,
Validate,
} from 'class-validator'
+import { Transform } from 'class-transformer'
import {
MAX_METADATA_KEYS,
@@ -13,6 +14,7 @@ import {
MetadataKeysQuantity,
MetadataValueType,
MetadataSizeLimit,
+ transformMetadataJsonPrimitivesToString,
} from './events.dto'
import { PID_REGEX } from '../../common/constants'
@@ -123,8 +125,9 @@ export class PageviewsDto {
@Validate(MetadataKeysQuantity, {
message: `Metadata object can't have more than ${MAX_METADATA_KEYS} keys`,
})
+ @Transform(({ value }) => transformMetadataJsonPrimitivesToString(value))
@Validate(MetadataValueType, {
- message: 'All of metadata object values must be strings',
+ message: 'All of metadata object values must be primitive JSON values',
})
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,