📈 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
added an indicator that shows how many visitors Project has on the browser tab
Blue Mousea7f737222ef6
diff --git a/apps/production/src/user/dto/set-show-live-visitors.dto.ts b/apps/production/src/user/dto/set-show-live-visitors.dto.tsnew file mode 100644index 000000000..37167ba61--- /dev/null+++ b/apps/production/src/user/dto/set-show-live-visitors.dto.ts@@ -0,0 +1,8 @@+import { ApiProperty } from '@nestjs/swagger'+import { IsBoolean } from 'class-validator'++export class SetShowLiveVisitorsDTO {+ @ApiProperty({ required: true })+ @IsBoolean()+ show: boolean+}diff --git a/apps/production/src/user/entities/user.entity.ts b/apps/production/src/user/entities/user.entity.tsindex 1741370b1..484e62e8f 100644--- a/apps/production/src/user/entities/user.entity.ts+++ b/apps/production/src/user/entities/user.entity.ts@@ -216,6 +216,9 @@ export class User {@Column({ default: false })trialReminderSent: boolean+ @Column({ default: false })+ showLiveVisitorsInTitle: boolean+@BeforeUpdate()updateTimestamp() {this.updated = new Date()diff --git a/apps/production/src/user/user.controller.ts b/apps/production/src/user/user.controller.tsindex c72bc0f51..986b4f236 100644--- a/apps/production/src/user/user.controller.ts+++ b/apps/production/src/user/user.controller.ts@@ -54,6 +54,7 @@ import {import { RolesGuard } from '../auth/guards/roles.guard'import { UpdateUserProfileDTO } from './dto/update-user.dto'import { AdminUpdateUserProfileDTO } from './dto/admin-update-user.dto'+import { SetShowLiveVisitorsDTO } from './dto/set-show-live-visitors.dto'import { ActionTokensService } from '../action-tokens/action-tokens.service'import { MailerService } from '../mailer/mailer.service'import { ActionTokenType } from '../action-tokens/action-token.entity'@@ -126,6 +127,22 @@ export class UserController {return this.userService.update(userId, { theme })}+ @Put('/live-visitors')+ @UseGuards(JwtAccessTokenGuard, RolesGuard)+ @Roles(UserType.CUSTOMER, UserType.ADMIN)+ async setShowLiveVisitors(+ @CurrentUserId() userId: string,+ @Body() body: SetShowLiveVisitorsDTO,+ ): Promise<Partial<User>> {+ const { show } = body++ await this.userService.update(userId, { showLiveVisitorsInTitle: show })++ return {+ showLiveVisitorsInTitle: show,+ }+ }+@Get('/search')@ApiQuery({ name: 'query', required: false })@UseGuards(JwtAccessTokenGuard, RolesGuard)diff --git a/apps/selfhosted/src/common/utils.ts b/apps/selfhosted/src/common/utils.tsindex 75509f2e3..2567d8209 100644--- a/apps/selfhosted/src/common/utils.ts+++ b/apps/selfhosted/src/common/utils.ts@@ -189,6 +189,7 @@ const deleteRefreshTokenClickhouse = async (interface IClickhouseUser {timezone?: stringtimeFormat?: string+ showLiveVisitorsInTitle?: number}const CLICKHOUSE_SETTINGS_ID = 'sfuser'@@ -201,7 +202,7 @@ const createUserClickhouse = async (user: IClickhouseUser) => {},}- const query = `INSERT INTO sfuser (*) VALUES ({id:String},{timezone:String},{timeFormat:String})`+ const query = `INSERT INTO sfuser (*) VALUES ({id:String},{timezone:String},{timeFormat:String},{showLiveVisitorsInTitle:Int8})`return clickhouse.query(query, paramsData).toPromise()}@@ -234,7 +235,6 @@ const userClickhouseExists = async () => {}const updateUserClickhouse = async (user: IClickhouseUser) => {- console.log(user)const userExists = await userClickhouseExists()if (!userExists) {@@ -249,23 +249,25 @@ const updateUserClickhouse = async (user: IClickhouseUser) => {}let query = 'ALTER table sfuser UPDATE '+ let separator = ''if (user.timezone) {- query += `timezone={timezone:String}`+ query += `${separator}timezone={timezone:String}`+ separator = ', '}if (user.timeFormat) {- if (user.timezone) {- query += ', '- }+ query += `${separator}timeFormat={timeFormat:String}`+ separator = ', '+ }- query += `timeFormat={timeFormat:String}`+ if (user.showLiveVisitorsInTitle) {+ query += `${separator}showLiveVisitorsInTitle={showLiveVisitorsInTitle:Int8}`+ separator = ', '}query += ` WHERE id={id:String}`- console.log(query, paramsData)-return clickhouse.query(query, paramsData).toPromise()}diff --git a/apps/selfhosted/src/user/dto/set-show-live-visitors.dto.ts b/apps/selfhosted/src/user/dto/set-show-live-visitors.dto.tsnew file mode 100644index 000000000..37167ba61--- /dev/null+++ b/apps/selfhosted/src/user/dto/set-show-live-visitors.dto.ts@@ -0,0 +1,8 @@+import { ApiProperty } from '@nestjs/swagger'+import { IsBoolean } from 'class-validator'++export class SetShowLiveVisitorsDTO {+ @ApiProperty({ required: true })+ @IsBoolean()+ show: boolean+}diff --git a/apps/selfhosted/src/user/entities/user.entity.ts b/apps/selfhosted/src/user/entities/user.entity.tsindex 5d48e7374..43b00a70f 100644--- a/apps/selfhosted/src/user/entities/user.entity.ts+++ b/apps/selfhosted/src/user/entities/user.entity.ts@@ -23,6 +23,7 @@ export interface SelfhostedUser {isActive: booleantimezone: stringtimeFormat: TimeFormat+ showLiveVisitorsInTitle: boolean}export const generateSelfhostedUser = (): SelfhostedUser => ({@@ -34,6 +35,7 @@ export const generateSelfhostedUser = (): SelfhostedUser => ({isActive: true,timezone: DEFAULT_TIMEZONE,timeFormat: TimeFormat['12-hour'],+ showLiveVisitorsInTitle: false,})export const getSelfhostedUser = async (): Promise<SelfhostedUser> => {diff --git a/apps/selfhosted/src/user/user.controller.ts b/apps/selfhosted/src/user/user.controller.tsindex b6eacb317..d136a4f1f 100644--- a/apps/selfhosted/src/user/user.controller.ts+++ b/apps/selfhosted/src/user/user.controller.ts@@ -17,6 +17,7 @@ import {getSelfhostedUser,} from './entities/user.entity'import { UpdateUserProfileDTO } from './dto/update-user.dto'+import { SetShowLiveVisitorsDTO } from './dto/set-show-live-visitors.dto'import { Roles } from '../auth/decorators/roles.decorator'import { RolesGuard } from '../auth/guards/roles.guard'import { CurrentUserId } from '../auth/decorators/current-user-id.decorator'@@ -38,6 +39,29 @@ export class UserController {return getSelfhostedUser()}+ @Put('/live-visitors')+ @UseGuards(JwtAccessTokenGuard, RolesGuard)+ @Roles(UserType.CUSTOMER, UserType.ADMIN)+ async setShowLiveVisitors(+ @Body() body: SetShowLiveVisitorsDTO,+ ): Promise<Partial<SelfhostedUser>> {+ const { show } = body++ try {+ await updateUserClickhouse({+ showLiveVisitorsInTitle: Number(show),+ })+ } catch (_) {+ throw new BadRequestException(+ 'An error occurred while updating live visitors user setting',+ )+ }++ return {+ showLiveVisitorsInTitle: show,+ }+ }+@Put('/')@UseGuards(JwtAccessTokenGuard, RolesGuard)@Roles(UserType.CUSTOMER, UserType.ADMIN)diff --git a/migrations/clickhouse/selfhosted_2023_05_28.js b/migrations/clickhouse/selfhosted_2023_05_28.jsnew file mode 100644index 000000000..6c034d0f3--- /dev/null+++ b/migrations/clickhouse/selfhosted_2023_05_28.js@@ -0,0 +1,23 @@+const { queriesRunner, dbName } = require('./setup')++const queries = [+ // Added 'showLiveVisitorsInTitle' column+ `DROP TABLE IF EXISTS ${dbName}.sfuser_temp`,+ `CREATE TABLE IF NOT EXISTS ${dbName}.sfuser_temp+ (+ id String,+ timezone Nullable(String),+ timeFormat Nullable(String)+ showLiveVisitorsInTitle Int8+ )+ ENGINE = MergeTree()+ PRIMARY KEY id;`,++ `INSERT INTO ${dbName}.sfuser_temp (*)+ SELECT id, timezone, timeFormat, 0 FROM ${dbName}.sfuser`,++ `DROP TABLE ${dbName}.sfuser`,+ `RENAME TABLE ${dbName}.sfuser_temp TO ${dbName}.sfuser`,+]++queriesRunner(queries)diff --git a/migrations/mysql/2023_05_28.sql b/migrations/mysql/2023_05_28.sqlnew file mode 100644index 000000000..447714be0--- /dev/null+++ b/migrations/mysql/2023_05_28.sql@@ -0,0 +1 @@+alter table user add column `showLiveVisitorsInTitle` tinyint NOT NULL DEFAULT '0' AFTER tierCurrency;