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
20 changes: 13 additions & 7 deletions backend/apps/cloud/src/analytics/analytics.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1116,11 +1116,10 @@ export class AnalyticsController {
errorDTO.profileId,
)

await this.analyticsService.recordSession(
await this.analyticsService.recordSessionActivity(
psid,
errorDTO.pid,
profileId,
false,
)

const { city, region, regionCode, country } = getGeoDetails(ip, errorDTO.tz)
Expand Down Expand Up @@ -1256,11 +1255,10 @@ export class AnalyticsController {
eventsDTO.profileId,
)

await this.analyticsService.recordSession(
await this.analyticsService.recordSessionActivity(
psid,
eventsDTO.pid,
profileId,
false,
)

const transformed = customEventTransformer(
Expand Down Expand Up @@ -1345,7 +1343,7 @@ export class AnalyticsController {
)

await this.analyticsService.extendSessionTTL(psid)
await this.analyticsService.updateSessionActivity(psid, pid, profileId)
await this.analyticsService.recordSessionActivity(psid, pid, profileId)

return {}
}
Expand Down Expand Up @@ -1380,7 +1378,11 @@ export class AnalyticsController {
logDTO.profileId,
)

await this.analyticsService.recordSession(psid, logDTO.pid, profileId, true)
await this.analyticsService.recordSessionActivity(
psid,
logDTO.pid,
profileId,
)

if (!unique && logDTO.unique) {
throw new ForbiddenException(
Expand Down Expand Up @@ -1521,7 +1523,11 @@ export class AnalyticsController {
ip,
)

await this.analyticsService.recordSession(psid, logDTO.pid, profileId, true)
await this.analyticsService.recordSessionActivity(
psid,
logDTO.pid,
profileId,
)

const { city, region, regionCode, country } = getGeoDetails(ip)

Expand Down
49 changes: 8 additions & 41 deletions backend/apps/cloud/src/analytics/analytics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1218,48 +1218,16 @@ export class AnalyticsService {
return profileId.startsWith(AnalyticsService.PROFILE_PREFIX_USER)
}

async recordSession(
async recordSessionActivity(
psid: string,
pid: string,
profileId: string,
isPageview: boolean = true,
): Promise<void> {
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')

try {
await clickhouse.insert({
table: 'sessions',
format: 'JSONEachRow',
values: [
{
psid,
pid,
profileId,
firstSeen: now,
lastSeen: now,
pageviews: isPageview ? 1 : 0,
events: isPageview ? 0 : 1,
},
],
clickhouse_settings: { async_insert: 1 },
})
} catch (error) {
console.error('Failed to record session:', error)
}
}

async updateSessionActivity(
psid: string,
pid: string,
profileId: string,
): Promise<void> {
const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')

try {
// Read current session data to preserve pageviews/events counts
// TODO: Potentially slow because every write request reads from the database - consider caching or other optimisation
const query = `
SELECT firstSeen, pageviews, events
SELECT firstSeen
FROM sessions FINAL
WHERE psid = {psid:UInt64}
AND pid = {pid:FixedString(12)}
Expand All @@ -1274,8 +1242,6 @@ export class AnalyticsService {
.then(resultSet =>
resultSet.json<{
firstSeen: string
pageviews: number
events: number
}>(),
)

Expand All @@ -1291,14 +1257,12 @@ export class AnalyticsService {
profileId,
firstSeen: existingSession?.firstSeen ?? now,
lastSeen: now,
pageviews: existingSession?.pageviews ?? 0,
events: existingSession?.events ?? 0,
},
],
clickhouse_settings: { async_insert: 1 },
})
} catch (error) {
console.error('Failed to update session activity:', error)
console.error('Failed to record session:', error)
}
}

Expand Down Expand Up @@ -4178,7 +4142,8 @@ export class AnalyticsService {
SELECT
CAST(psid, 'String') AS psidCasted,
pid,
avg(dateDiff('second', firstSeen, lastSeen)) as avg_duration
avg(dateDiff('second', firstSeen, lastSeen)) as avg_duration,
any(profileId) as profileId
FROM sessions FINAL
WHERE pid = {pid:FixedString(12)}
GROUP BY psidCasted, pid
Expand All @@ -4194,7 +4159,9 @@ export class AnalyticsService {
dsf.sessionStart,
dsf.lastActivity,
if(dateDiff('second', dsf.lastActivity, now()) < ${LIVE_SESSION_THRESHOLD_SECONDS}, 1, 0) AS isLive,
sda.avg_duration AS sdur
sda.avg_duration AS sdur,
sda.profileId AS profileId,
if(startsWith(sda.profileId, '${AnalyticsService.PROFILE_PREFIX_USER}'), 1, 0) AS isIdentified
FROM distinct_sessions_filtered dsf
LEFT JOIN pageview_counts pc ON dsf.psidCasted = pc.psidCasted AND dsf.pid = pc.pid
LEFT JOIN event_counts ec ON dsf.psidCasted = ec.psidCasted AND dsf.pid = ec.pid
Expand Down
2 changes: 2 additions & 0 deletions backend/apps/cloud/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { PingModule } from './ping/ping.module'
import { MarketplaceModule } from './marketplace/marketplace.module'
import { AlertModule } from './alert/alert.module'
import { GoalModule } from './goal/goal.module'
import { FeatureFlagModule } from './feature-flag/feature-flag.module'
import { AiModule } from './ai/ai.module'
import { getI18nConfig } from './configs'
import { AuthModule } from './auth/auth.module'
Expand Down Expand Up @@ -80,6 +81,7 @@ const modules = [
MarketplaceModule,
AlertModule,
GoalModule,
FeatureFlagModule,
AiModule,
AuthModule,
CaptchaModule,
Expand Down
Loading