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!

3.7 KB
Raw Blame
import { ApiProperty } from '@nestjs/swagger'
import {
  IsNotEmpty,
  IsObject,
  IsOptional,
  IsString,
  Matches,
  MaxLength,
  Validate,
} from 'class-validator'
import { Transform } from 'class-transformer'

import {
  MAX_METADATA_KEYS,
  MAX_METADATA_VALUE_LENGTH,
  MetadataKeysQuantity,
  MetadataValueType,
  MetadataSizeLimit,
  transformMetadataJsonPrimitivesToString,
} from './events.dto'
import { PID_REGEX } from '../../common/constants'

export class PageviewsDto {
  @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

  // 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: 'Pricing | Swetrix',
    required: false,
    nullable: true,
    description: 'Page title at the time of the pageview',
    maxLength: 2048,
  })
  @IsOptional()
  @IsString()
  @MaxLength(2048)
  title?: string | null

  @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: false,
    description: 'If true, only unique events will be saved',
  })
  unique?: boolean

  @ApiProperty({
    example: {
      conn: 120,
      dns: 170,
      dom_load: 975.5,
      page_load: 1515,
      render: 532.25,
      response: 170,
      tls: 0,
      ttfb: 78.3,
    },
    description: 'Performance metrics',
  })
  perf?: {
    dns: number
    tls: number
    conn: number
    response: number
    render: number
    dom_load: number
    page_load: number
    ttfb: number
  }

  @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 values with total length more than ${MAX_METADATA_VALUE_LENGTH} characters`,
  })
  meta?: Record<string, string>
}