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!

CE goals support

Blue Mousee9b6dc7bb802
diff --git a/backend/apps/community/src/analytics/analytics.service.ts b/backend/apps/community/src/analytics/analytics.service.ts
index 2ebcdaabb..57a5da80c 100644
--- a/backend/apps/community/src/analytics/analytics.service.ts
+++ b/backend/apps/community/src/analytics/analytics.service.ts
@@ -2340,13 +2340,12 @@ export class AnalyticsService {
generateAnalyticsAggregationQuery(
timeBucket: TimeBucketType,
filtersQuery: string,
- safeTimezone: string,
mode: ChartRenderMode,
): string {
const timeBucketFunc = timeBucketConversion[timeBucket]
const [selector, groupBy] = this.getGroupSubquery(timeBucket)
- const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), '${safeTimezone}')`
- const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), '${safeTimezone}')`
+ const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), {timezone:String})`
+ const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), {timezone:String})`
const baseQuery = `
SELECT
@@ -2358,7 +2357,7 @@ export class AnalyticsService {
SELECT
pid,
psid,
- ${timeBucketFunc}(toTimeZone(created, '${safeTimezone}')) as tz_created
+ ${timeBucketFunc}(toTimeZone(created, {timezone:String})) as tz_created
FROM analytics
PREWHERE pid = {pid:FixedString(12)}
WHERE created BETWEEN ${tzFromDate} AND ${tzToDate}
@@ -2394,12 +2393,11 @@ export class AnalyticsService {
generateSessionAggregationQuery(
timeBucket: TimeBucketType,
filtersQuery: string,
- safeTimezone: string,
): string {
const timeBucketFunc = timeBucketConversion[timeBucket]
const [selector, groupBy] = this.getGroupSubquery(timeBucket)
- const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), '${safeTimezone}')`
- const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), '${safeTimezone}')`
+ const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), {timezone:String})`
+ const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), {timezone:String})`
const baseQuery = `
SELECT
@@ -2407,7 +2405,7 @@ export class AnalyticsService {
count() as pageviews
FROM (
SELECT
- ${timeBucketFunc}(toTimeZone(created, '${safeTimezone}')) as tz_created
+ ${timeBucketFunc}(toTimeZone(created, {timezone:String})) as tz_created
FROM analytics
PREWHERE pid = {pid:FixedString(12)}
WHERE created BETWEEN ${tzFromDate} AND ${tzToDate}
@@ -2423,12 +2421,11 @@ export class AnalyticsService {
generateSessionCustomEventsAggregationQuery(
timeBucket: TimeBucketType,
filtersQuery: string,
- safeTimezone: string,
): string {
const timeBucketFunc = timeBucketConversion[timeBucket]
const [selector, groupBy] = this.getGroupSubquery(timeBucket)
- const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), '${safeTimezone}')`
- const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), '${safeTimezone}')`
+ const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), {timezone:String})`
+ const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), {timezone:String})`
return `
SELECT
@@ -2436,7 +2433,7 @@ export class AnalyticsService {
count() as customEventsCount
FROM (
SELECT
- ${timeBucketFunc}(toTimeZone(created, '${safeTimezone}')) as tz_created
+ ${timeBucketFunc}(toTimeZone(created, {timezone:String})) as tz_created
FROM customEV
PREWHERE pid = {pid:FixedString(12)}
WHERE created BETWEEN ${tzFromDate} AND ${tzToDate}
@@ -2450,12 +2447,11 @@ export class AnalyticsService {
generateSessionErrorsAggregationQuery(
timeBucket: TimeBucketType,
filtersQuery: string,
- safeTimezone: string,
): string {
const timeBucketFunc = timeBucketConversion[timeBucket]
const [selector, groupBy] = this.getGroupSubquery(timeBucket)
- const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), '${safeTimezone}')`
- const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), '${safeTimezone}')`
+ const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), {timezone:String})`
+ const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), {timezone:String})`
return `
SELECT
@@ -2463,7 +2459,7 @@ export class AnalyticsService {
count() as errorsCount
FROM (
SELECT
- ${timeBucketFunc}(toTimeZone(created, '${safeTimezone}')) as tz_created
+ ${timeBucketFunc}(toTimeZone(created, {timezone:String})) as tz_created
FROM errors
PREWHERE pid = {pid:FixedString(12)}
WHERE created BETWEEN ${tzFromDate} AND ${tzToDate}
@@ -2477,13 +2473,12 @@ export class AnalyticsService {
generateCustomEventsAggregationQuery(
timeBucket: TimeBucketType,
filtersQuery: string,
- safeTimezone: string,
mode: ChartRenderMode,
): string {
const timeBucketFunc = timeBucketConversion[timeBucket]
const [selector, groupBy] = this.getGroupSubquery(timeBucket)
- const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), '${safeTimezone}')`
- const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), '${safeTimezone}')`
+ const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), {timezone:String})`
+ const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), {timezone:String})`
const baseQuery = `
SELECT
@@ -2491,7 +2486,7 @@ export class AnalyticsService {
count() as count
FROM (
SELECT *,
- ${timeBucketFunc}(toTimeZone(created, '${safeTimezone}')) as tz_created
+ ${timeBucketFunc}(toTimeZone(created, {timezone:String})) as tz_created
FROM customEV
WHERE pid = {pid:FixedString(12)}
AND created BETWEEN ${tzFromDate} AND ${tzToDate}
@@ -2516,13 +2511,12 @@ export class AnalyticsService {
generatePerformanceAggregationQuery(
timeBucket: TimeBucketType,
filtersQuery: string,
- safeTimezone: string,
measure: PerfMeasure,
): string {
const timeBucketFunc = timeBucketConversion[timeBucket]
const [selector, groupBy] = this.getGroupSubquery(timeBucket)
- const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), '${safeTimezone}')`
- const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), '${safeTimezone}')`
+ const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), {timezone:String})`
+ const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), {timezone:String})`
const cols = ['dns', 'tls', 'conn', 'response', 'render', 'domLoad', 'ttfb']
@@ -2536,7 +2530,7 @@ export class AnalyticsService {
${columnSelectors}
FROM (
SELECT *,
- ${timeBucketFunc}(toTimeZone(created, '${safeTimezone}')) as tz_created
+ ${timeBucketFunc}(toTimeZone(created, {timezone:String})) as tz_created
FROM performance
WHERE
pid = {pid:FixedString(12)}
@@ -2551,12 +2545,11 @@ export class AnalyticsService {
generatePerformanceQuantilesQuery(
timeBucket: TimeBucketType,
filtersQuery: string,
- safeTimezone: string,
): string {
const timeBucketFunc = timeBucketConversion[timeBucket]
const [selector, groupBy] = this.getGroupSubquery(timeBucket)
- const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), '${safeTimezone}')`
- const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), '${safeTimezone}')`
+ const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), {timezone:String})`
+ const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), {timezone:String})`
const cols = ['dns', 'tls', 'conn', 'response', 'render', 'domLoad', 'ttfb']
@@ -2570,7 +2563,7 @@ export class AnalyticsService {
${columnSelectors}
FROM (
SELECT *,
- ${timeBucketFunc}(toTimeZone(created, '${safeTimezone}')) as tz_created
+ ${timeBucketFunc}(toTimeZone(created, {timezone:String})) as tz_created
FROM performance
WHERE
pid = {pid:FixedString(12)}
@@ -2816,14 +2809,13 @@ export class AnalyticsService {
const query = this.generateCustomEventsAggregationQuery(
timeBucket,
filtersQuery,
- safeTimezone,
mode,
)
const { data } = await clickhouse
.query({
query,
- query_params: paramsData.params,
+ query_params: { ...paramsData.params, timezone: safeTimezone },
})
.then(resultSet => resultSet.json<TrafficCEFilterCHResponse>())
@@ -2845,14 +2837,13 @@ export class AnalyticsService {
const query = this.generateAnalyticsAggregationQuery(
timeBucket,
filtersQuery,
- safeTimezone,
mode,
)
const { data } = await clickhouse
.query({
query,
- query_params: paramsData.params,
+ query_params: { ...paramsData.params, timezone: safeTimezone },
})
.then(resultSet => resultSet.json<TrafficCHResponse>())
@@ -2967,36 +2958,35 @@ export class AnalyticsService {
const pageviewsQuery = this.generateSessionAggregationQuery(
timeBucket,
filtersQuery,
- safeTimezone,
)
const customEventsQuery = this.generateSessionCustomEventsAggregationQuery(
timeBucket,
filtersQuery,
- safeTimezone,
)
const errorsQuery = this.generateSessionErrorsAggregationQuery(
timeBucket,
filtersQuery,
- safeTimezone,
)
+ const queryParams = { ...paramsData.params, timezone: safeTimezone }
+
const [pageviewsData, customEventsData, errorsData] = await Promise.all([
clickhouse
.query({
query: pageviewsQuery,
- query_params: paramsData.params,
+ query_params: queryParams,
})
.then(resultSet => resultSet.json<any>()),
clickhouse
.query({
query: customEventsQuery,
- query_params: paramsData.params,
+ query_params: queryParams,
})
.then(resultSet => resultSet.json<any>()),
clickhouse
.query({
query: errorsQuery,
- query_params: paramsData.params,
+ query_params: queryParams,
})
.then(resultSet => resultSet.json<any>()),
])
@@ -3194,13 +3184,12 @@ export class AnalyticsService {
const query = this.generatePerformanceQuantilesQuery(
timeBucket,
filtersQuery,
- safeTimezone,
)
const { data } = await clickhouse
.query({
query,
- query_params: paramsData.params,
+ query_params: { ...paramsData.params, timezone: safeTimezone },
})
.then(resultSet => resultSet.json())
@@ -3213,14 +3202,13 @@ export class AnalyticsService {
const query = this.generatePerformanceAggregationQuery(
timeBucket,
filtersQuery,
- safeTimezone,
measure,
)
const { data } = await clickhouse
.query({
query,
- query_params: paramsData.params,
+ query_params: { ...paramsData.params, timezone: safeTimezone },
})
.then(resultSet => resultSet.json<PerformanceCHResponse>())
@@ -3600,8 +3588,8 @@ export class AnalyticsService {
const timeBucketFunc = timeBucketConversion[timeBucket]
const [selector, groupBy] = this.getGroupSubquery(timeBucket)
- const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), '${safeTimezone}')`
- const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), '${safeTimezone}')`
+ const tzFromDate = `toTimeZone(parseDateTimeBestEffort({groupFrom:String}), {timezone:String})`
+ const tzToDate = `toTimeZone(parseDateTimeBestEffort({groupTo:String}), {timezone:String})`
const query = `
SELECT
@@ -3610,7 +3598,7 @@ export class AnalyticsService {
count() as count
FROM (
SELECT *,
- ${timeBucketFunc}(toTimeZone(created, '${safeTimezone}')) as tz_created
+ ${timeBucketFunc}(toTimeZone(created, {timezone:String})) as tz_created
FROM customEV
WHERE
pid = {pid:FixedString(12)}
@@ -3624,7 +3612,7 @@ export class AnalyticsService {
const { data } = await clickhouse
.query({
query,
- query_params: paramsData.params,
+ query_params: { ...paramsData.params, timezone: safeTimezone },
})
.then(resultSet => resultSet.json<CustomsCHAggregatedResponse>())
@@ -3676,7 +3664,7 @@ export class AnalyticsService {
SELECT
'pageview' AS type,
pg AS value,
- toTimeZone(analytics.created, '${safeTimezone}') AS created,
+ toTimeZone(analytics.created, {timezone:String}) AS created,
pid,
psid,
groupArrayIf(tuple(meta.key, meta.value), notEmpty(meta.key) AND notEmpty(meta.value)) AS metadata
@@ -3693,7 +3681,7 @@ export class AnalyticsService {
SELECT
'event' AS type,
ev AS value,
- toTimeZone(customEV.created, '${safeTimezone}') AS created,
+ toTimeZone(customEV.created, {timezone:String}) AS created,
pid,
psid,
groupArrayIf(tuple(meta.key, meta.value), notEmpty(meta.key) AND notEmpty(meta.value)) AS metadata
@@ -3710,7 +3698,7 @@ export class AnalyticsService {
SELECT
'error' AS type,
errors.name AS value,
- toTimeZone(errors.created, '${safeTimezone}') AS created,
+ toTimeZone(errors.created, {timezone:String}) AS created,
pid,
psid,
[
@@ -3760,6 +3748,7 @@ export class AnalyticsService {
params: {
pid,
psid,
+ timezone: safeTimezone,
},
}
@@ -3895,7 +3884,7 @@ export class AnalyticsService {
${primaryEventsTable}.cc,
${primaryEventsTable}.os,
${primaryEventsTable}.br,
- toTimeZone(${primaryEventsTable}.created, '${safeTimezone}') AS created_for_grouping
+ toTimeZone(${primaryEventsTable}.created, {timezone:String}) AS created_for_grouping
FROM ${primaryEventsTable}
WHERE
${primaryEventsTable}.pid = {pid:FixedString(12)}
@@ -3982,7 +3971,7 @@ export class AnalyticsService {
const { data } = await clickhouse
.query({
query,
- query_params: paramsData.params,
+ query_params: { ...paramsData.params, timezone: safeTimezone },
})
.then(resultSet => resultSet.json())
@@ -4017,7 +4006,7 @@ export class AnalyticsService {
max(created) as last_seen,
status.status
FROM (
- SELECT eid, name, message, filename, toTimeZone(errors.created, '${safeTimezone}') AS created
+ SELECT eid, name, message, filename, toTimeZone(errors.created, {timezone:String}) AS created
FROM errors
WHERE pid = {pid:FixedString(12)}
AND errors.created BETWEEN {groupFrom:String} AND {groupTo:String}
@@ -4045,7 +4034,7 @@ export class AnalyticsService {
const { data } = await clickhouse
.query({
query,
- query_params: paramsData.params,
+ query_params: { ...paramsData.params, timezone: safeTimezone },
})
.then(resultSet => resultSet.json())
diff --git a/backend/apps/community/src/app.module.ts b/backend/apps/community/src/app.module.ts
index 0106c5825..c08f836e4 100644
--- a/backend/apps/community/src/app.module.ts
+++ b/backend/apps/community/src/app.module.ts
@@ -10,6 +10,7 @@ import { AnalyticsModule } from './analytics/analytics.module'
import { ProjectModule } from './project/project.module'
import { TaskManagerModule } from './task-manager/task-manager.module'
import { PingModule } from './ping/ping.module'
+import { GoalModule } from './goal/goal.module'
import { getI18nConfig } from './configs'
import { AuthModule } from './auth/auth.module'
import { AppController } from './app.controller'
@@ -64,6 +65,7 @@ const modules = [
ProjectModule,
AnalyticsModule,
PingModule,
+ GoalModule,
AuthModule,
]
diff --git a/backend/apps/community/src/common/pagination/pagination.results.interface.ts b/backend/apps/community/src/common/pagination/pagination.results.interface.ts
index fc38ee98a..e53b43a58 100644
--- a/backend/apps/community/src/common/pagination/pagination.results.interface.ts
+++ b/backend/apps/community/src/common/pagination/pagination.results.interface.ts
@@ -4,3 +4,8 @@ export interface PaginationResultInterface<PaginationEntity> {
next?: string
previous?: string
}
+
+export interface PaginationOptionsInterface {
+ take?: number
+ skip?: number
+}
diff --git a/backend/apps/community/src/goal/dto/goal.dto.ts b/backend/apps/community/src/goal/dto/goal.dto.ts
new file mode 100644
index 000000000..61d5ad5a5
--- /dev/null
+++ b/backend/apps/community/src/goal/dto/goal.dto.ts
@@ -0,0 +1,100 @@
+import {
+ IsString,
+ IsEnum,
+ IsOptional,
+ IsArray,
+ ValidateNested,
+ MaxLength,
+ IsBoolean,
+ IsNotEmpty,
+} from 'class-validator'
+import { Type } from 'class-transformer'
+import { GoalType, GoalMatchType, MetadataFilter } from '../entity/goal.entity'
+
+export class MetadataFilterDto implements MetadataFilter {
+ @IsString()
+ @IsNotEmpty()
+ key: string
+
+ @IsString()
+ @IsNotEmpty()
+ value: string
+}
+
+export class CreateGoalDto {
+ @IsString()
+ @IsNotEmpty()
+ pid: string
+
+ @IsString()
+ @MaxLength(100)
+ @IsNotEmpty()
+ name: string
+
+ @IsEnum(GoalType)
+ type: GoalType
+
+ @IsEnum(GoalMatchType)
+ matchType: GoalMatchType
+
+ @IsString()
+ @MaxLength(500)
+ @IsOptional()
+ value?: string
+
+ @IsArray()
+ @ValidateNested({ each: true })
+ @Type(() => MetadataFilterDto)
+ @IsOptional()
+ metadataFilters?: MetadataFilterDto[]
+}
+
+export class UpdateGoalDto {
+ @IsString()
+ @MaxLength(100)
+ @IsOptional()
+ name?: string
+
+ @IsEnum(GoalType)
+ @IsOptional()
+ type?: GoalType
+
+ @IsEnum(GoalMatchType)
+ @IsOptional()
+ matchType?: GoalMatchType
+
+ @IsString()
+ @MaxLength(500)
+ @IsOptional()
+ value?: string
+
+ @IsArray()
+ @ValidateNested({ each: true })
+ @Type(() => MetadataFilterDto)
+ @IsOptional()
+ metadataFilters?: MetadataFilterDto[]
+
+ @IsBoolean()
+ @IsOptional()
+ active?: boolean
+}
+
+export class GoalDto {
+ id: string
+ name: string
+ type: GoalType
+ matchType: GoalMatchType
+ value: string | null
+ metadataFilters: MetadataFilterDto[] | null
+ active: boolean
+ pid: string
+ created: string
+}
+
+export class GoalStatsDto {
+ conversions: number
+ uniqueSessions: number
+ conversionRate: number
+ previousConversions: number
+ trend: number
+}
diff --git a/backend/apps/community/src/goal/entity/goal.entity.ts b/backend/apps/community/src/goal/entity/goal.entity.ts
new file mode 100644
index 000000000..da63701a1
--- /dev/null
+++ b/backend/apps/community/src/goal/entity/goal.entity.ts
@@ -0,0 +1,39 @@
+export enum GoalType {
+ PAGEVIEW = 'pageview',
+ CUSTOM_EVENT = 'custom_event',
+}
+
+export enum GoalMatchType {
+ EXACT = 'exact',
+ CONTAINS = 'contains',
+ REGEX = 'regex',
+}
+
+export interface MetadataFilter {
+ key: string
+ value: string
+}
+
+export interface Goal {
+ id: string
+ name: string
+ type: GoalType
+ matchType: GoalMatchType
+ value: string | null
+ metadataFilters: MetadataFilter[] | null
+ active: boolean
+ projectId: string
+ created: string
+}
+
+export interface ClickhouseGoal {
+ id: string
+ name: string
+ type: string
+ matchType: string
+ value: string | null
+ metadataFilters: string | null // JSON string in ClickHouse
+ active: number // Int8 in ClickHouse
+ projectId: string
+ created: string
+}
diff --git a/backend/apps/community/src/goal/goal.controller.ts b/backend/apps/community/src/goal/goal.controller.ts
new file mode 100644
index 000000000..598bba7b3
--- /dev/null
+++ b/backend/apps/community/src/goal/goal.controller.ts
@@ -0,0 +1,735 @@
+import {
+ Controller,
+ Get,
+ Put,
+ Delete,
+ Query,
+ Param,
+ Body,
+ NotFoundException,
+ Post,
+ BadRequestException,
+ HttpCode,
+ HttpStatus,
+ ParseIntPipe,
+} from '@nestjs/common'
+import _isEmpty from 'lodash/isEmpty'
+import _map from 'lodash/map'
+import _omit from 'lodash/omit'
+import _pick from 'lodash/pick'
+import _round from 'lodash/round'
+import dayjs from 'dayjs'
+
+import { ProjectService } from '../project/project.service'
+import { AppLoggerService } from '../logger/logger.service'
+import {
+ AnalyticsService,
+ getLowestPossibleTimeBucket,
+} from '../analytics/analytics.service'
+import { Auth } from '../auth/decorators'
+import { CurrentUserId } from '../auth/decorators/current-user-id.decorator'
+import { Goal, GoalType, GoalMatchType } from './entity/goal.entity'
+import {
+ CreateGoalDto,
+ UpdateGoalDto,
+ GoalDto,
+ GoalStatsDto,
+} from './dto/goal.dto'
+import { GoalService } from './goal.service'
+import { clickhouse } from '../common/integrations/clickhouse'
+
+const GOALS_MAXIMUM = 50 // Maximum goals per project
+
+const timeBucketConversion: Record<string, string> = {
+ minute: 'toStartOfMinute',
+ hour: 'toStartOfHour',
+ day: 'toStartOfDay',
+ month: 'toStartOfMonth',
+ year: 'toStartOfYear',
+}
+
+@Controller('goal')
+export class GoalController {
+ constructor(
+ private readonly goalService: GoalService,
+ private readonly projectService: ProjectService,
+ private readonly logger: AppLoggerService,
+ private readonly analyticsService: AnalyticsService,
+ ) {}
+
+ @Get('/:goalId')
+ @Auth()
+ async getGoal(
+ @CurrentUserId() userId: string,
+ @Param('goalId') goalId: string,
+ ) {
+ this.logger.log({ userId, goalId }, 'GET /goal/:goalId')
+
+ const goal = await this.goalService.findOne(goalId)
+
+ if (_isEmpty(goal)) {
+ throw new NotFoundException('Goal not found')
+ }
+
+ const project = await this.projectService.getFullProject(goal.projectId)
+
+ this.projectService.allowedToView(project, userId)
+
+ return {
+ ...goal,
+ pid: goal.projectId,
+ }
+ }
+
+ @Get('/project/:projectId')
+ @Auth()
+ async getProjectGoals(
+ @CurrentUserId() userId: string,
+ @Param('projectId') projectId: string,
+ @Query('take', new ParseIntPipe({ optional: true })) take?: number,
+ @Query('skip', new ParseIntPipe({ optional: true })) skip?: number,
+ ) {
+ this.logger.log(
+ { userId, projectId, take, skip },
+ 'GET /goal/project/:projectId',
+ )
+
+ const project = await this.projectService.getFullProject(projectId)
+
+ if (_isEmpty(project)) {
+ throw new NotFoundException('Project not found')
+ }
+
+ this.projectService.allowedToView(project, userId)
+
+ const result = await this.goalService.paginate({ take, skip }, projectId)
+
+ return {
+ ...result,
+ results: _map(result.results, goal => ({
+ ...goal,
+ pid: goal.projectId,
+ })),
+ }
+ }
+
+ @Post('/')
+ @Auth()
+ async createGoal(
+ @Body() goalDto: CreateGoalDto,
+ @CurrentUserId() uid: string,
+ ) {
+ this.logger.log({ uid, pid: goalDto.pid }, 'POST /goal')
+
+ const project = await this.projectService.getFullProject(goalDto.pid)
+
+ if (_isEmpty(project)) {
+ throw new NotFoundException('Project not found')
+ }
+
+ this.projectService.allowedToManage(
+ project,
+ uid,
+ 'You are not allowed to add goals to this project',
+ )
+
+ const goalsCount = await this.goalService.count(goalDto.pid)
+
+ if (goalsCount >= GOALS_MAXIMUM) {
+ throw new BadRequestException(
+ `You cannot create more than ${GOALS_MAXIMUM} goals per project.`,
+ )
+ }
+
+ try {
+ const newGoal = await this.goalService.create({
+ name: goalDto.name,
+ type: goalDto.type,
+ matchType: goalDto.matchType,
+ value: goalDto.value || null,
+ metadataFilters: goalDto.metadataFilters || null,
+ projectId: goalDto.pid,
+ })
+
+ return {
+ ...newGoal,
+ pid: goalDto.pid,
+ }
+ } catch (reason) {
+ this.logger.log({ reason }, 'Error while creating goal')
+ throw new BadRequestException('Error occurred while creating goal')
+ }
+ }
+
+ @Put('/:id')
+ @Auth()
+ async updateGoal(
+ @Param('id') id: string,
+ @Body() goalDto: UpdateGoalDto,
+ @CurrentUserId() uid: string,
+ ) {
+ this.logger.log({ id, uid }, 'PUT /goal/:id')
+
+ const goal = await this.goalService.findOne(id)
+
+ if (_isEmpty(goal)) {
+ throw new NotFoundException()
+ }
+
+ const project = await this.projectService.getFullProject(goal.projectId)
+
+ this.projectService.allowedToManage(
+ project,
+ uid,
+ 'You are not allowed to manage this goal',
+ )
+
+ const updatePayload: Partial<Goal> = {
+ ..._pick(goalDto, [
+ 'name',
+ 'type',
+ 'matchType',
+ 'value',
+ 'metadataFilters',
+ 'active',
+ ]),
+ }
+
+ const updatedGoal = await this.goalService.update(id, updatePayload)
+
+ if (!updatedGoal) {
+ throw new NotFoundException('Goal not found after update')
+ }
+
+ return {
+ ...updatedGoal,
+ pid: goal.projectId,
+ }
+ }
+
+ @Delete('/:id')
+ @Auth()
+ @HttpCode(HttpStatus.NO_CONTENT)
+ async deleteGoal(@Param('id') id: string, @CurrentUserId() uid: string) {
+ this.logger.log({ id, uid }, 'DELETE /goal/:id')
+
+ const goal = await this.goalService.findOne(id)
+
+ if (_isEmpty(goal)) {
+ throw new NotFoundException()
+ }
+
+ const project = await this.projectService.getFullProject(goal.projectId)
+
+ this.projectService.allowedToManage(
+ project,
+ uid,
+ 'You are not allowed to manage this goal',
+ )
+
+ await this.goalService.delete(id)
+ }
+
+ private buildGoalMatchCondition(
+ goal: Goal,
+ _table: 'analytics' | 'customEV',
+ ): { condition: string; params: Record<string, string> } {
+ const params: Record<string, string> = {}
+
+ if (goal.type === GoalType.CUSTOM_EVENT) {
+ // For custom events, match the event name
+ if (goal.matchType === GoalMatchType.EXACT) {
+ params.goalValue = goal.value || ''
+ return { condition: `ev = {goalValue:String}`, params }
+ } else if (goal.matchType === GoalMatchType.CONTAINS) {
+ params.goalValue = goal.value || ''
+ return {
+ condition: `ev ILIKE concat('%', {goalValue:String}, '%')`,
+ params,
+ }
+ } else {
+ // Regex match
+ if (!goal.value) {
+ return { condition: '0', params }
+ }
+ params.goalValue = goal.value
+ return { condition: `match(ev, {goalValue:String})`, params }
+ }
+ } else {
+ // For pageview goals, match the page path
+ if (goal.matchType === GoalMatchType.EXACT) {
+ params.goalValue = goal.value || ''
+ return { condition: `pg = {goalValue:String}`, params }
+ } else if (goal.matchType === GoalMatchType.CONTAINS) {
+ params.goalValue = goal.value || ''
+ return {
+ condition: `pg ILIKE concat('%', {goalValue:String}, '%')`,
+ params,
+ }
+ } else {
+ // Regex match
+ if (!goal.value) {
+ return { condition: '0', params }
+ }
+ params.goalValue = goal.value
+ return { condition: `match(pg, {goalValue:String})`, params }
+ }
+ }
+ }
+
+ private buildMetadataCondition(goal: Goal): {
+ condition: string
+ params: Record<string, string>
+ } {
+ if (!goal.metadataFilters || goal.metadataFilters.length === 0) {
+ return { condition: '', params: {} }
+ }
+
+ const conditions: string[] = []
+ const params: Record<string, string> = {}
+
+ goal.metadataFilters.forEach((filter, index) => {
+ const keyParam = `metaKey${index}`
+ const valueParam = `metaValue${index}`
+ params[keyParam] = filter.key
+ params[valueParam] = filter.value
+ conditions.push(
+ `has(meta.key, {${keyParam}:String}) AND meta.value[indexOf(meta.key, {${keyParam}:String})] = {${valueParam}:String}`,
+ )
+ })
+
+ return {
+ condition:
+ conditions.length > 0 ? `AND (${conditions.join(' AND ')})` : '',
+ params,
+ }
+ }
+
+ @Get('/:id/stats')
+ @Auth()
+ async getGoalStats(
+ @CurrentUserId() userId: string,
+ @Param('id') id: string,
+ @Query('period') period: string,
+ @Query('from') from?: string,
+ @Query('to') to?: string,
+ @Query('timezone') timezone?: string,
+ ) {
+ this.logger.log({ userId, id, period, from, to }, 'GET /goal/:id/stats')
+
+ const goal = await this.goalService.findOne(id)
+
+ if (_isEmpty(goal)) {
+ throw new NotFoundException('Goal not found')
+ }
+
+ const project = await this.projectService.getFullProject(goal.projectId)
+ this.projectService.allowedToView(project, userId)
+
+ const safeTimezone = this.analyticsService.getSafeTimezone(timezone)
+ const timeBucket = getLowestPossibleTimeBucket(period, from, to)
+
+ const { groupFromUTC, groupToUTC } = this.analyticsService.getGroupFromTo(
+ from,
+ to,
+ timeBucket,
+ period,
+ safeTimezone,
+ )
+
+ const table = goal.type === GoalType.CUSTOM_EVENT ? 'customEV' : 'analytics'
+ const { condition: matchCondition, params: matchParams } =
+ this.buildGoalMatchCondition(goal, table)
+ const { condition: metaCondition, params: metaParams } =
+ this.buildMetadataCondition(goal)
+
+ // Get conversions for current period
+ const conversionsQuery = `
+ SELECT
+ count(*) as conversions,
+ uniqExact(psid) as uniqueSessions
+ FROM ${table}
+ WHERE
+ pid = {pid:FixedString(12)}
+ AND ${matchCondition}
+ ${metaCondition}
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ `
+
+ const queryParams = {
+ pid: goal.projectId,
+ groupFrom: groupFromUTC,
+ groupTo: groupToUTC,
+ ...matchParams,
+ ...metaParams,
+ }
+
+ const { data: conversionsData } = await clickhouse
+ .query({ query: conversionsQuery, query_params: queryParams })
+ .then(resultSet =>
+ resultSet.json<{ conversions: number; uniqueSessions: number }>(),
+ )
+
+ const conversions = conversionsData[0]?.conversions || 0
+ const uniqueSessions = conversionsData[0]?.uniqueSessions || 0
+
+ // Get total unique sessions for conversion rate
+ const totalSessionsQuery = `
+ SELECT uniqExact(psid) as totalSessions
+ FROM analytics
+ WHERE
+ pid = {pid:FixedString(12)}
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ `
+
+ const { data: totalData } = await clickhouse
+ .query({
+ query: totalSessionsQuery,
+ query_params: {
+ pid: goal.projectId,
+ groupFrom: groupFromUTC,
+ groupTo: groupToUTC,
+ },
+ })
+ .then(resultSet => resultSet.json<{ totalSessions: number }>())
+
+ const totalSessions = totalData[0]?.totalSessions || 1
+ const conversionRate = _round((uniqueSessions / totalSessions) * 100, 2)
+
+ // Get previous period conversions for trend
+ const periodDays = dayjs(groupToUTC).diff(dayjs(groupFromUTC), 'day') || 1
+ const previousFrom = dayjs(groupFromUTC)
+ .subtract(periodDays, 'day')
+ .format('YYYY-MM-DD HH:mm:ss')
+ const previousTo = groupFromUTC
+
+ const previousQueryParams = {
+ pid: goal.projectId,
+ groupFrom: previousFrom,
+ groupTo: previousTo,
+ ...matchParams,
+ ...metaParams,
+ }
+
+ const { data: previousData } = await clickhouse
+ .query({ query: conversionsQuery, query_params: previousQueryParams })
+ .then(resultSet =>
+ resultSet.json<{ conversions: number; uniqueSessions: number }>(),
+ )
+
+ const previousConversions = previousData[0]?.conversions || 0
+ const trend =
+ previousConversions > 0
+ ? _round(
+ ((conversions - previousConversions) / previousConversions) * 100,
+ 2,
+ )
+ : conversions > 0
+ ? 100
+ : 0
+
+ return {
+ conversions,
+ uniqueSessions,
+ conversionRate,
+ previousConversions,
+ trend,
+ }
+ }
+
+ private getGroupSubquery(
+ timeBucket: string,
+ ): [selector: string, groupBy: string] {
+ if (timeBucket === 'minute') {
+ return [
+ 'toYear(tz_created) as year, toMonth(tz_created) as month, toDayOfMonth(tz_created) as day, toHour(tz_created) as hour, toMinute(tz_created) as minute',
+ 'year, month, day, hour, minute',
+ ]
+ }
+
+ if (timeBucket === 'hour') {
+ return [
+ 'toYear(tz_created) as year, toMonth(tz_created) as month, toDayOfMonth(tz_created) as day, toHour(tz_created) as hour',
+ 'year, month, day, hour',
+ ]
+ }
+
+ if (timeBucket === 'day') {
+ return [
+ 'toYear(tz_created) as year, toMonth(tz_created) as month, toDayOfMonth(tz_created) as day',
+ 'year, month, day',
+ ]
+ }
+
+ if (timeBucket === 'month') {
+ return [
+ 'toYear(tz_created) as year, toMonth(tz_created) as month',
+ 'year, month',
+ ]
+ }
+
+ // year
+ return ['toYear(tz_created) as year', 'year']
+ }
+
+ private generateDateString(row: { [key: string]: number }): string {
+ const { year, month, day, hour, minute } = row
+
+ let dateString = `${year}`
+
+ if (typeof month === 'number') {
+ if (month < 10) {
+ dateString += `-0${month}`
+ } else {
+ dateString += `-${month}`
+ }
+ }
+
+ if (typeof day === 'number') {
+ if (day < 10) {
+ dateString += `-0${day}`
+ } else {
+ dateString += `-${day}`
+ }
+ }
+
+ if (typeof hour === 'number') {
+ const strMinute =
+ typeof minute === 'number'
+ ? minute < 10
+ ? `0${minute}`
+ : minute
+ : '00'
+
+ if (hour < 10) {
+ dateString += ` 0${hour}:${strMinute}:00`
+ } else {
+ dateString += ` ${hour}:${strMinute}:00`
+ }
+ }
+
+ return dateString
+ }
+
+ @Get('/:id/chart')
+ @Auth()
+ async getGoalChart(
+ @CurrentUserId() userId: string,
+ @Param('id') id: string,
+ @Query('period') period: string,
+ @Query('from') from?: string,
+ @Query('to') to?: string,
+ @Query('timeBucket') timeBucket?: string,
+ @Query('timezone') timezone?: string,
+ ) {
+ this.logger.log(
+ { userId, id, period, from, to, timeBucket },
+ 'GET /goal/:id/chart',
+ )
+
+ const goal = await this.goalService.findOne(id)
+
+ if (_isEmpty(goal)) {
+ throw new NotFoundException('Goal not found')
+ }
+
+ const project = await this.projectService.getFullProject(goal.projectId)
+ this.projectService.allowedToView(project, userId)
+
+ const safeTimezone = this.analyticsService.getSafeTimezone(timezone)
+ const resolvedTimeBucket =
+ timeBucket || getLowestPossibleTimeBucket(period, from, to)
+
+ const { groupFromUTC, groupToUTC } = this.analyticsService.getGroupFromTo(
+ from,
+ to,
+ resolvedTimeBucket as any,
+ period,
+ safeTimezone,
+ )
+
+ // Generate complete X axis with all time buckets
+ const { xShifted } = this.analyticsService.generateXAxis(
+ resolvedTimeBucket as any,
+ groupFromUTC,
+ groupToUTC,
+ safeTimezone,
+ )
+
+ const table = goal.type === GoalType.CUSTOM_EVENT ? 'customEV' : 'analytics'
+ const timeBucketFunc =
+ timeBucketConversion[resolvedTimeBucket] || 'toStartOfDay'
+ const [selector, groupBy] = this.getGroupSubquery(resolvedTimeBucket)
+
+ const { condition: matchCondition, params: matchParams } =
+ this.buildGoalMatchCondition(goal, table)
+ const { condition: metaCondition, params: metaParams } =
+ this.buildMetadataCondition(goal)
+
+ const chartQuery = `
+ SELECT
+ ${selector},
+ count(*) as conversions,
+ uniqExact(psid) as uniqueSessions
+ FROM (
+ SELECT *,
+ ${timeBucketFunc}(toTimeZone(created, {timezone:String})) as tz_created
+ FROM ${table}
+ WHERE
+ pid = {pid:FixedString(12)}
+ AND ${matchCondition}
+ ${metaCondition}
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ ) as subquery
+ GROUP BY ${groupBy}
+ ORDER BY ${groupBy}
+ `
+
+ const queryParams = {
+ pid: goal.projectId,
+ groupFrom: groupFromUTC,
+ groupTo: groupToUTC,
+ timezone: safeTimezone,
+ ...matchParams,
+ ...metaParams,
+ }
+
+ const { data } = await clickhouse
+ .query({ query: chartQuery, query_params: queryParams })
+ .then(resultSet =>
+ resultSet.json<{
+ year: number
+ month?: number
+ day?: number
+ hour?: number
+ minute?: number
+ conversions: number
+ uniqueSessions: number
+ }>(),
+ )
+
+ // Initialize arrays with zeros for all time buckets
+ const conversions = Array(xShifted.length).fill(0)
+ const uniqueSessions = Array(xShifted.length).fill(0)
+
+ // Fill in actual data at the correct indices
+ for (const row of data) {
+ const dateString = this.generateDateString(row)
+ const index = xShifted.indexOf(dateString)
+
+ if (index !== -1) {
+ conversions[index] = row.conversions
+ uniqueSessions[index] = row.uniqueSessions
+ }
+ }
+
+ return {
+ chart: {
+ x: xShifted,
+ conversions,
+ uniqueSessions,
+ },
+ }
+ }
+
+ @Get('/:id/sessions')
+ @Auth()
+ async getGoalSessions(
+ @CurrentUserId() userId: string,
+ @Param('id') id: string,
+ @Query('period') period: string,
+ @Query('from') from?: string,
+ @Query('to') to?: string,
+ @Query('take', new ParseIntPipe({ optional: true })) take: number = 30,
+ @Query('skip', new ParseIntPipe({ optional: true })) skip: number = 0,
+ @Query('timezone') timezone?: string,
+ ) {
+ this.logger.log(
+ { userId, id, period, from, to, take, skip },
+ 'GET /goal/:id/sessions',
+ )
+
+ const goal = await this.goalService.findOne(id)
+
+ if (_isEmpty(goal)) {
+ throw new NotFoundException('Goal not found')
+ }
+
+ const project = await this.projectService.getFullProject(goal.projectId)
+ this.projectService.allowedToView(project, userId)
+
+ const safeTimezone = this.analyticsService.getSafeTimezone(timezone)
+ const timeBucket = getLowestPossibleTimeBucket(period, from, to)
+
+ const { groupFromUTC, groupToUTC } = this.analyticsService.getGroupFromTo(
+ from,
+ to,
+ timeBucket,
+ period,
+ safeTimezone,
+ )
+
+ const table = goal.type === GoalType.CUSTOM_EVENT ? 'customEV' : 'analytics'
+
+ const { condition: matchCondition, params: matchParams } =
+ this.buildGoalMatchCondition(goal, table)
+ const { condition: metaCondition, params: metaParams } =
+ this.buildMetadataCondition(goal)
+
+ const sessionsQuery = `
+ SELECT
+ DISTINCT psid,
+ any(cc) as country,
+ any(br) as browser,
+ any(os) as os,
+ any(dv) as device,
+ min(created) as firstConversion,
+ count(*) as conversionCount
+ FROM ${table}
+ WHERE
+ pid = {pid:FixedString(12)}
+ AND ${matchCondition}
+ ${metaCondition}
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ AND psid IS NOT NULL
+ GROUP BY psid
+ ORDER BY firstConversion DESC
+ LIMIT {take:UInt32} OFFSET {skip:UInt32}
+ `
+
+ const countQuery = `
+ SELECT count(DISTINCT psid) as total
+ FROM ${table}
+ WHERE
+ pid = {pid:FixedString(12)}
+ AND ${matchCondition}
+ ${metaCondition}
+ AND created BETWEEN {groupFrom:String} AND {groupTo:String}
+ AND psid IS NOT NULL
+ `
+
+ const queryParams = {
+ pid: goal.projectId,
+ groupFrom: groupFromUTC,
+ groupTo: groupToUTC,
+ take,
+ skip,
+ ...matchParams,
+ ...metaParams,
+ }
+
+ const [sessionsResult, countResult] = await Promise.all([
+ clickhouse
+ .query({ query: sessionsQuery, query_params: queryParams })
+ .then(resultSet => resultSet.json<any>()),
+ clickhouse
+ .query({ query: countQuery, query_params: queryParams })
+ .then(resultSet => resultSet.json<{ total: number }>()),
+ ])
+
+ return {
+ sessions: sessionsResult.data,
+ total: countResult.data[0]?.total || 0,
+ }
+ }
+}
diff --git a/backend/apps/community/src/goal/goal.module.ts b/backend/apps/community/src/goal/goal.module.ts
new file mode 100644
index 000000000..6a0f3492d
--- /dev/null
+++ b/backend/apps/community/src/goal/goal.module.ts
@@ -0,0 +1,15 @@
+import { Module } from '@nestjs/common'
+
+import { ProjectModule } from '../project/project.module'
+import { AppLoggerModule } from '../logger/logger.module'
+import { AnalyticsModule } from '../analytics/analytics.module'
+import { GoalService } from './goal.service'
+import { GoalController } from './goal.controller'
+
+@Module({
+ imports: [ProjectModule, AppLoggerModule, AnalyticsModule],
+ providers: [GoalService],
+ exports: [GoalService],
+ controllers: [GoalController],
+})
+export class GoalModule {}
diff --git a/backend/apps/community/src/goal/goal.service.ts b/backend/apps/community/src/goal/goal.service.ts
new file mode 100644
index 000000000..7c8e42d03
--- /dev/null
+++ b/backend/apps/community/src/goal/goal.service.ts
@@ -0,0 +1,272 @@
+import { Injectable, NotFoundException } from '@nestjs/common'
+import { randomUUID } from 'crypto'
+import dayjs from 'dayjs'
+import utc from 'dayjs/plugin/utc'
+import _isEmpty from 'lodash/isEmpty'
+import _head from 'lodash/head'
+import _map from 'lodash/map'
+import _filter from 'lodash/filter'
+import _keys from 'lodash/keys'
+import _reduce from 'lodash/reduce'
+
+import { clickhouse } from '../common/integrations/clickhouse'
+import {
+ Goal,
+ ClickhouseGoal,
+ GoalMatchType,
+ MetadataFilter,
+} from './entity/goal.entity'
+import { Pagination } from '../common/pagination'
+import { PaginationOptionsInterface } from '../common/pagination/pagination.results.interface'
+
+dayjs.extend(utc)
+
+const ALLOWED_GOAL_KEYS = [
+ 'name',
+ 'type',
+ 'matchType',
+ 'value',
+ 'metadataFilters',
+ 'active',
+]
+
+@Injectable()
+export class GoalService {
+ formatGoalFromClickhouse(goal: ClickhouseGoal): Goal {
+ if (!goal) {
+ return null
+ }
+
+ let metadataFilters: MetadataFilter[] | null = null
+ if (goal.metadataFilters) {
+ try {
+ metadataFilters = JSON.parse(goal.metadataFilters)
+ } catch {
+ metadataFilters = null
+ }
+ }
+
+ return {
+ id: goal.id,
+ name: goal.name,
+ type: goal.type as Goal['type'],
+ matchType: (goal.matchType as GoalMatchType) || GoalMatchType.EXACT,
+ value: goal.value,
+ metadataFilters,
+ active: Boolean(goal.active),
+ projectId: goal.projectId,
+ created: goal.created,
+ }
+ }
+
+ formatGoalToClickhouse(goal: Partial<Goal>): Partial<ClickhouseGoal> {
+ const result: Partial<ClickhouseGoal> = { ...goal } as any
+
+ if (goal.metadataFilters !== undefined) {
+ result.metadataFilters = goal.metadataFilters
+ ? JSON.stringify(goal.metadataFilters)
+ : null
+ }
+
+ if (goal.active !== undefined) {
+ result.active = goal.active ? 1 : 0
+ }
+
+ return result
+ }
+
+ async findOne(id: string): Promise<Goal | null> {
+ const query = `SELECT * FROM goal WHERE id = {id:FixedString(36)}`
+
+ const { data } = await clickhouse
+ .query({
+ query,
+ query_params: { id },
+ })
+ .then(resultSet => resultSet.json<ClickhouseGoal>())
+
+ if (_isEmpty(data)) {
+ return null
+ }
+
+ return this.formatGoalFromClickhouse(_head(data))
+ }
+
+ async findOneWithProject(id: string): Promise<Goal | null> {
+ return this.findOne(id)
+ }
+
+ async findByProject(projectId: string): Promise<Goal[]> {
+ const query = `
+ SELECT * FROM goal
+ WHERE projectId = {projectId:FixedString(12)}
+ AND active = 1
+ ORDER BY name ASC
+ `
+
+ const { data } = await clickhouse
+ .query({
+ query,
+ query_params: { projectId },
+ })
+ .then(resultSet => resultSet.json<ClickhouseGoal>())
+
+ return _map(data, goal => this.formatGoalFromClickhouse(goal))
+ }
+
+ async paginate(
+ options: PaginationOptionsInterface,
+ projectId: string,
+ ): Promise<Pagination<Goal>> {
+ const take = options.take || 100
+ const skip = options.skip || 0
+
+ const countQuery = `
+ SELECT count() as total
+ FROM goal
+ WHERE projectId = {projectId:FixedString(12)}
+ `
+
+ const dataQuery = `
+ SELECT * FROM goal
+ WHERE projectId = {projectId:FixedString(12)}
+ ORDER BY name ASC
+ LIMIT {take:UInt32} OFFSET {skip:UInt32}
+ `
+
+ const [countResult, dataResult] = await Promise.all([
+ clickhouse
+ .query({
+ query: countQuery,
+ query_params: { projectId },
+ })
+ .then(resultSet => resultSet.json<{ total: number }>()),
+ clickhouse
+ .query({
+ query: dataQuery,
+ query_params: { projectId, take, skip },
+ })
+ .then(resultSet => resultSet.json<ClickhouseGoal>()),
+ ])
+
+ const total = countResult.data[0]?.total || 0
+ const results = _map(dataResult.data, goal =>
+ this.formatGoalFromClickhouse(goal),
+ )
+
+ return new Pagination<Goal>({
+ results,
+ total,
+ })
+ }
+
+ async count(projectId: string): Promise<number> {
+ const query = `
+ SELECT count() as total
+ FROM goal
+ WHERE projectId = {projectId:FixedString(12)}
+ `
+
+ const { data } = await clickhouse
+ .query({
+ query,
+ query_params: { projectId },
+ })
+ .then(resultSet => resultSet.json<{ total: number }>())
+
+ return data[0]?.total || 0
+ }
+
+ async create(goalData: Partial<Goal>): Promise<Goal> {
+ const id = randomUUID()
+ const created = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')
+
+ const formattedGoal = this.formatGoalToClickhouse({
+ ...goalData,
+ active: goalData.active ?? true,
+ })
+
+ await clickhouse.insert({
+ table: 'goal',
+ format: 'JSONEachRow',
+ values: [
+ {
+ id,
+ name: formattedGoal.name,
+ type: formattedGoal.type,
+ matchType: formattedGoal.matchType || GoalMatchType.EXACT,
+ value: formattedGoal.value || null,
+ metadataFilters: formattedGoal.metadataFilters || null,
+ active: formattedGoal.active ?? 1,
+ projectId: goalData.projectId,
+ created,
+ },
+ ],
+ })
+
+ return this.findOne(id)
+ }
+
+ async update(id: string, goalData: Partial<Goal>): Promise<Goal> {
+ const filtered = _reduce(
+ _filter(
+ _keys(goalData),
+ key =>
+ ALLOWED_GOAL_KEYS.includes(key) &&
+ goalData[key as keyof Goal] !== undefined,
+ ),
+ (obj, key) => {
+ obj[key] = goalData[key as keyof Goal]
+ return obj
+ },
+ {} as Record<string, any>,
+ )
+
+ const columns = _keys(filtered)
+
+ if (_isEmpty(columns)) {
+ return this.findOne(id)
+ }
+
+ const formattedData = this.formatGoalToClickhouse(filtered as Partial<Goal>)
+
+ const params: Record<string, any> = { id }
+
+ const assignments = _map(columns, col => {
+ const value = formattedData[col as keyof ClickhouseGoal]
+ params[col] = value
+
+ if (col === 'active') {
+ return `${col}={${col}:Int8}`
+ }
+ return `${col}={${col}:String}`
+ }).join(', ')
+
+ const query = `ALTER TABLE goal UPDATE ${assignments} WHERE id={id:FixedString(36)}`
+
+ await clickhouse.command({
+ query,
+ query_params: params,
+ })
+
+ return this.findOne(id)
+ }
+
+ async delete(id: string): Promise<void> {
+ const query = `ALTER TABLE goal DELETE WHERE id = {id:FixedString(36)}`
+
+ await clickhouse.command({
+ query,
+ query_params: { id },
+ })
+ }
+
+ async deleteByProject(projectId: string): Promise<void> {
+ const query = `ALTER TABLE goal DELETE WHERE projectId = {projectId:FixedString(12)}`
+
+ await clickhouse.command({
+ query,
+ query_params: { projectId },
+ })
+ }
+}
diff --git a/backend/migrations/clickhouse/selfhosted_2025_12_07_goals.js b/backend/migrations/clickhouse/selfhosted_2025_12_07_goals.js
new file mode 100644
index 000000000..765960c2e
--- /dev/null
+++ b/backend/migrations/clickhouse/selfhosted_2025_12_07_goals.js
@@ -0,0 +1,21 @@
+// This migration adds the goal table for conversion tracking
+const { queriesRunner, dbName } = require('./setup')
+
+const queries = [
+ `CREATE TABLE IF NOT EXISTS ${dbName}.goal
+ (
+ id FixedString(36),
+ name String,
+ type Enum8('pageview' = 1, 'custom_event' = 2),
+ matchType Enum8('exact' = 1, 'contains' = 2, 'regex' = 3) DEFAULT 'exact',
+ value Nullable(String),
+ metadataFilters Nullable(String),
+ active Int8 DEFAULT 1,
+ projectId FixedString(12),
+ created DateTime
+ )
+ ENGINE = MergeTree()
+ PRIMARY KEY id;`,
+]
+
+queriesRunner(queries)