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!

Added basic Analytics (log) API

Blaumaus28ad46328f30
diff --git a/src/analytics/analytics.controller.spec.ts b/src/analytics/analytics.controller.spec.ts
new file mode 100644
index 000000000..ea1cbfb5d
--- /dev/null
+++ b/src/analytics/analytics.controller.spec.ts
@@ -0,0 +1,18 @@
+import { Test, TestingModule } from '@nestjs/testing'
+import { AnalyticsController } from './analytics.controller'
+
+describe('AnalyticsController', () => {
+ let controller: AnalyticsController
+
+ beforeEach(async () => {
+ const module: TestingModule = await Test.createTestingModule({
+ controllers: [AnalyticsController],
+ }).compile()
+
+ controller = module.get<AnalyticsController>(AnalyticsController)
+ })
+
+ it('should be defined', () => {
+ expect(controller).toBeDefined()
+ })
+})
diff --git a/src/analytics/analytics.controller.ts b/src/analytics/analytics.controller.ts
new file mode 100644
index 000000000..854948424
--- /dev/null
+++ b/src/analytics/analytics.controller.ts
@@ -0,0 +1,24 @@
+import { Controller, Body, Query, Param, UseGuards, Get, Post, Put, Delete, BadRequestException,
+ HttpCode, NotFoundException } from '@nestjs/common'
+import { ApiTags, ApiQuery, ApiResponse } from '@nestjs/swagger'
+
+import { AnalyticsService } from './analytics.service'
+import { ProjectService } from '../project/project.service'
+import { UserType } from '../user/entities/user.entity'
+import { Pagination } from '../common/pagination/pagination'
+import { Analytics } from './entity/analytics.entity'
+import { UserService } from '../user/user.service'
+import { EventsDTO } from './dto/events.dto'
+import { AppLoggerService } from '../logger/logger.service'
+
+@ApiTags('Analytics')
+@Controller('log')
+export class AnalyticsController {
+ constructor(
+ private readonly analyticsService: AnalyticsService,
+ private readonly projectService: ProjectService,
+ private readonly logger: AppLoggerService,
+ ) {}
+
+
+}
diff --git a/src/analytics/analytics.module.ts b/src/analytics/analytics.module.ts
new file mode 100644
index 000000000..876237f6c
--- /dev/null
+++ b/src/analytics/analytics.module.ts
@@ -0,0 +1,20 @@
+import { Module, forwardRef } from '@nestjs/common'
+import { TypeOrmModule } from '@nestjs/typeorm'
+
+import { Analytics } from './entity/analytics.entity'
+import { AnalyticsService } from './analytics.service'
+import { AnalyticsController } from './analytics.controller'
+import { UserModule } from '../user/user.module'
+import { AppLoggerModule } from '../logger/logger.module'
+
+@Module({
+ imports: [
+ TypeOrmModule.forFeature([Analytics]),
+ forwardRef(() => UserModule),
+ AppLoggerModule,
+ ],
+ providers: [AnalyticsService],
+ exports: [AnalyticsService],
+ controllers: [AnalyticsController],
+})
+export class AnalyticsModule {}
diff --git a/src/analytics/analytics.service.spec.ts b/src/analytics/analytics.service.spec.ts
new file mode 100644
index 000000000..c3e0f13d0
--- /dev/null
+++ b/src/analytics/analytics.service.spec.ts
@@ -0,0 +1,18 @@
+import { Test, TestingModule } from '@nestjs/testing'
+import { AnalyticsService } from './analytics.service'
+
+describe('AnalyticsService', () => {
+ let service: AnalyticsService
+
+ beforeEach(async () => {
+ const module: TestingModule = await Test.createTestingModule({
+ providers: [AnalyticsService],
+ }).compile()
+
+ service = module.get<AnalyticsService>(AnalyticsService)
+ })
+
+ it('should be defined', () => {
+ expect(service).toBeDefined()
+ })
+})
diff --git a/src/analytics/analytics.service.ts b/src/analytics/analytics.service.ts
new file mode 100644
index 000000000..2392f7374
--- /dev/null
+++ b/src/analytics/analytics.service.ts
@@ -0,0 +1,57 @@
+import { ForbiddenException, Injectable, BadRequestException } from '@nestjs/common'
+import { InjectRepository } from '@nestjs/typeorm'
+import { Repository } from 'typeorm'
+
+import { Pagination, PaginationOptionsInterface } from '../common/pagination'
+import { Analytics } from './entity/analytics.entity'
+import { EventsDTO } from './dto/events.dto'
+import { ProjectService } from '../project/project.service'
+
+@Injectable()
+export class AnalyticsService {
+ constructor(
+ @InjectRepository(Analytics)
+ private analyticsRepository: Repository<Analytics>,
+ private readonly projectService: ProjectService
+ ) {}
+
+ // async paginate(options: PaginationOptionsInterface, where: Record<string, unknown> | undefined): Promise<Pagination<Analytics>> {
+ // const [results, total] = await this.analyticsRepository.findAndCount({
+ // take: options.take || 10,
+ // skip: options.skip || 0,
+ // where: where,
+ // order: {
+ // name: 'ASC',
+ // }
+ // })
+
+ // return new Pagination<Analytics>({
+ // results,
+ // total,
+ // })
+ // }
+
+ count(): Promise<number> {
+ return this.analyticsRepository.count()
+ }
+
+ async create(project: EventsDTO | Analytics): Promise<Analytics> {
+ return this.analyticsRepository.save(project)
+ }
+
+ async update(id: string, eventsDTO: EventsDTO): Promise<any> {
+ return this.analyticsRepository.update(id, eventsDTO)
+ }
+
+ async delete(id: string): Promise<any> {
+ return this.analyticsRepository.delete(id)
+ }
+
+ findOne(id: string): Promise<Analytics | null> {
+ return this.analyticsRepository.findOne(id)
+ }
+
+ findOneWhere(where: Record<string, unknown>): Promise<Analytics> {
+ return this.analyticsRepository.findOne({ where })
+ }
+}
diff --git a/src/analytics/dto/events.dto.ts b/src/analytics/dto/events.dto.ts
new file mode 100644
index 000000000..8079a8367
--- /dev/null
+++ b/src/analytics/dto/events.dto.ts
@@ -0,0 +1,74 @@
+import { ApiProperty } from '@nestjs/swagger'
+import { IsNotEmpty } from 'class-validator'
+
+export class EventsDTO {
+ @ApiProperty({
+ example: 'aUn1quEid-3',
+ required: true,
+ description: 'The project ID'
+ })
+ @IsNotEmpty()
+ pid: string
+
+ @ApiProperty({
+ example: 'pageviews',
+ description: 'Event type'
+ })
+ ev: string
+
+ // Tracking metrics
+ @ApiProperty({
+ example: 'UTC+2',
+ description: 'User\'s timezone'
+ })
+ tz: string
+
+ @ApiProperty({
+ example: '/articles/my-awesome-article-1',
+ description: 'A page that user sent data from'
+ })
+ pg: string
+
+ @ApiProperty({
+ example: 'en-GB',
+ description: 'User\'s locale'
+ })
+ lc: string
+
+ @ApiProperty({
+ example: 'https://example.com',
+ description: 'The referrer'
+ })
+ ref: string
+
+ @ApiProperty({
+ example: 1920,
+ description: 'Screen width'
+ })
+ sw: number
+
+ @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
+
+ // Performance metrics
+ @ApiProperty({
+ example: 261,
+ description: 'Page load time (in milliseconds)'
+ })
+ lt: number
+}
\ No newline at end of file
diff --git a/src/analytics/entity/analytics.entity.ts b/src/analytics/entity/analytics.entity.ts
new file mode 100644
index 000000000..ab84b9667
--- /dev/null
+++ b/src/analytics/entity/analytics.entity.ts
@@ -0,0 +1,48 @@
+import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm'
+
+enum EventType {
+ PAGEVIEWS = 'pageviews',
+}
+
+@Entity()
+export class Analytics {
+ @PrimaryGeneratedColumn('uuid')
+ id: string
+
+ @Column('varchar', { length: 11 })
+ pid: string
+
+ @Column({
+ type: 'enum',
+ enum: EventType,
+ default: EventType.PAGEVIEWS,
+ })
+ ev: string
+
+ @Column('varchar', { length: 6 })
+ tz: string
+
+ @Column('varchar', { length: 500 }) // maybe it should be 2083? https://support.microsoft.com/uk-ua/topic/maximum-url-length-is-2-083-characters-in-internet-explorer-174e7c8a-6666-f4e0-6fd6-908b53c12246
+ pg: string
+
+ @Column('varchar', { length: 8 })
+ lc: string
+
+ @Column('varchar', { length: 500 }) // same question as pg
+ ref: string
+
+ @Column()
+ sw: number
+
+ @Column('varchar', { length: 100 })
+ so: string
+
+ @Column('varchar', { length: 100 })
+ me: string
+
+ @Column('varchar', { length: 100 })
+ ca: string
+
+ @Column()
+ lt: number
+}