📈 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
import _keys from 'lodash/keys'
import _values from 'lodash/values'
import _some from 'lodash/some'
import { ApiProperty } from '@nestjs/swagger'
import {
IsNotEmpty,
IsOptional,
IsObject,
IsString,
MaxLength,
ValidatorConstraint,
ValidatorConstraintInterface,
Validate,
Matches,
} from 'class-validator'
import { Transform } from 'class-transformer'
import { PID_REGEX } from '../../common/constants'
export const MAX_METADATA_KEYS = 100
export const MAX_METADATA_VALUE_LENGTH = 2000
@ValidatorConstraint()
export class MetadataSizeLimit implements ValidatorConstraintInterface {
validate(metadata: Record<string, string>) {
const keys = _keys(metadata)
const values = _values(metadata)
let totalSize = 0
for (let i = 0; i < keys.length; i++) {
totalSize +=
keys[i].length +
(values[i] && typeof values[i] === 'string' ? values[i].length : 0)
if (totalSize > MAX_METADATA_VALUE_LENGTH) {
return false
}
}
return true
}
}
@ValidatorConstraint()
export class MetadataKeysQuantity implements ValidatorConstraintInterface {
validate(metadata: Record<string, string>) {
return _keys(metadata).length <= MAX_METADATA_KEYS
}
}
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>) {
const values = _values(metadata)
return !_some(values, (value) => typeof value !== 'string')
}
}
export class EventsDto {
@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: 'user_12345',
description:
'Optional profile ID for long-term user tracking. If not provided, one will be auto-generated.',
maxLength: 256,
})
@IsOptional()
@IsString()
@MaxLength(256)
profileId?: string
@ApiProperty({
example: 'User Subscribed',
description: 'Event name',
maxLength: 256,
})
@IsNotEmpty()
@IsString()
@MaxLength(256)
ev: string
@ApiProperty({
description:
'If true, only 1 event with the same ID will be saved per user session',
})
unique: boolean
// Tracking metrics
@ApiProperty({
example: 'Europe/Kiev',
description: "User's timezone",
})
tz?: string
@ApiProperty({
example: '/articles/my-awesome-article-1',
description: 'A page that user sent data from',
})
@IsOptional()
@IsString()
pg?: string
@ApiProperty({
example: 'en-GB',
description: "User's locale",
})
lc?: string
@ApiProperty({
example: 'https://example.com',
description: 'The referrer',
})
ref?: string
@ApiProperty({
example: 'duckduckgo',
description: 'utm_source URL parameter',
})
so?: string
@ApiProperty({
example: 'cpc',
description: 'utm_medium URL parameter',
})
me?: string
@ApiProperty({
example: 'spring_sale',
description: 'utm_campaign URL parameter',
})
ca?: string
@ApiProperty({
example: 'running+shoes',
description: 'utm_term URL parameter',
})
te?: string
@ApiProperty({
example: 'logolink',
description: 'utm_content URL parameter',
})
co?: string
@ApiProperty({
example: 'fbclid=AbCdEf123&utm_source=newsletter',
description:
'Raw URL query string of the landing page (without leading ?). Used server-side to recover traffic source from ad/social click IDs when the browser stripped the referrer.',
maxLength: 2048,
})
@IsOptional()
@IsString()
@MaxLength(2048)
qs?: string
@ApiProperty({
example: {
affiliate: 'Yes',
protocol: 'HTTPS',
},
description: 'Event-related metadata object with string values',
})
@IsOptional()
@IsObject()
@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 primitive JSON values',
})
@Validate(MetadataSizeLimit, {
message: `Metadata object can't have keys and values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
})
meta?: Record<string, string>
}