Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions backend/apps/cloud/src/analytics/analytics.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ import { GetKeywordsDto } from './dto/get-keywords.dto'
import { GetBotStatsDto } from './dto/get-bot-stats.dto'
import { GSCService } from '../project/gsc.service'
import { GetProfileIdDto, GetSessionIdDto } from './dto/get-id.dto'
import { ExperimentService } from '../experiment/experiment.service'
import {
ExperimentStatus,
ExposureTrigger,
} from '../experiment/entity/experiment.entity'
import { getExperimentVariant } from '../feature-flag/evaluation'

dayjs.extend(utc)
dayjs.extend(dayjsTimezone)
Expand Down Expand Up @@ -205,6 +211,7 @@ export class AnalyticsController {
private readonly analyticsService: AnalyticsService,
private readonly logger: AppLoggerService,
private readonly gscService: GSCService,
private readonly experimentService: ExperimentService,
) {}

@ApiBearerAuth()
Expand Down Expand Up @@ -1663,6 +1670,13 @@ export class AnalyticsController {
values: [transformed],
clickhouse_settings: { async_insert: 1 },
})

void this.trackCustomEventExperimentExposures(
eventsDTO.pid,
eventsDTO.ev,
profileId,
transformed.created,
)
} catch (reason) {
this.logger.error(reason)
throw new InternalServerErrorException(
Expand All @@ -1673,6 +1687,83 @@ export class AnalyticsController {
return {}
}

private async trackCustomEventExperimentExposures(
pid: string,
eventName: string,
profileId: string,
created: string,
) {
try {
const experiments = await this.experimentService.find({
where: {
project: { id: pid },
status: ExperimentStatus.RUNNING,
exposureTrigger: ExposureTrigger.CUSTOM_EVENT,
customEventName: eventName,
},
relations: ['variants'],
})

if (_isEmpty(experiments)) {
return
}

const exposures = []
for (const experiment of experiments) {
if (!experiment.variants || experiment.variants.length === 0) {
continue
}

const sortedVariants = [...experiment.variants].sort((a, b) =>
a.key.localeCompare(b.key),
)
const variantKey = getExperimentVariant(
experiment.id,
sortedVariants.map((variant) => ({
key: variant.key,
rolloutPercentage: variant.rolloutPercentage,
})),
profileId,
)

if (!variantKey) {
continue
}

exposures.push({
pid,
experimentId: experiment.id,
variantKey,
profileId,
created,
})
}

if (_isEmpty(exposures)) {
return
}

clickhouse
.insert({
table: 'experiment_exposures',
values: exposures,
format: 'JSONEachRow',
clickhouse_settings: { async_insert: 1 },
})
.catch((reason) => {
this.logger.warn(
{ reason },
'Failed to async insert custom event experiment exposures',
)
})
} catch (reason) {
this.logger.warn(
{ reason },
'Failed to track custom event experiment exposures',
)
}
}

@Post('hb')
@Auth(true, true)
async heartbeat(
Expand Down
2 changes: 2 additions & 0 deletions backend/apps/cloud/src/analytics/analytics.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { UserModule } from '../user/user.module'
import { AppLoggerModule } from '../logger/logger.module'
import { ProjectModule } from '../project/project.module'
import { RevenueModule } from '../revenue/revenue.module'
import { ExperimentModule } from '../experiment/experiment.module'

@Module({
imports: [
Expand All @@ -19,6 +20,7 @@ import { RevenueModule } from '../revenue/revenue.module'
AppLoggerModule,
ProjectModule,
forwardRef(() => RevenueModule),
forwardRef(() => ExperimentModule),
],
providers: [
AnalyticsService,
Expand Down
1 change: 1 addition & 0 deletions backend/apps/cloud/src/analytics/bot-detection.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export type BotEndpoint =
| 'pageview'
| 'custom'
| 'error'
| 'feature_flag'
| 'heartbeat'
| 'noscript'

Expand Down
Loading
Loading