-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ (setVariable) Add Transcription system var (#1507)
Closes #1484
- Loading branch information
1 parent
ec7ff8d
commit 40f2120
Showing
102 changed files
with
2,911 additions
and
986 deletions.
There are no files selected for viewing
137 changes: 137 additions & 0 deletions
137
apps/builder/src/features/analytics/api/getInDepthAnalyticsData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
import prisma from '@typebot.io/lib/prisma' | ||
import { authenticatedProcedure } from '@/helpers/server/trpc' | ||
import { TRPCError } from '@trpc/server' | ||
import { z } from 'zod' | ||
import { canReadTypebots } from '@/helpers/databaseRules' | ||
import { totalAnswersSchema } from '@typebot.io/schemas/features/analytics' | ||
import { parseGroups } from '@typebot.io/schemas' | ||
import { isInputBlock } from '@typebot.io/schemas/helpers' | ||
import { defaultTimeFilter, timeFilterValues } from '../constants' | ||
import { | ||
parseFromDateFromTimeFilter, | ||
parseToDateFromTimeFilter, | ||
} from '../helpers/parseDateFromTimeFilter' | ||
|
||
export const getInDepthAnalyticsData = authenticatedProcedure | ||
.meta({ | ||
openapi: { | ||
method: 'GET', | ||
path: '/v1/typebots/{typebotId}/analytics/inDepthData', | ||
protect: true, | ||
summary: | ||
'List total answers in blocks and off-default paths visited edges', | ||
tags: ['Analytics'], | ||
}, | ||
}) | ||
.input( | ||
z.object({ | ||
typebotId: z.string(), | ||
timeFilter: z.enum(timeFilterValues).default(defaultTimeFilter), | ||
timeZone: z.string().optional(), | ||
}) | ||
) | ||
.output( | ||
z.object({ | ||
totalAnswers: z.array(totalAnswersSchema), | ||
offDefaultPathVisitedEdges: z.array( | ||
z.object({ edgeId: z.string(), total: z.number() }) | ||
), | ||
}) | ||
) | ||
.query( | ||
async ({ input: { typebotId, timeFilter, timeZone }, ctx: { user } }) => { | ||
const typebot = await prisma.typebot.findFirst({ | ||
where: canReadTypebots(typebotId, user), | ||
select: { publishedTypebot: true }, | ||
}) | ||
if (!typebot?.publishedTypebot) | ||
throw new TRPCError({ | ||
code: 'NOT_FOUND', | ||
message: 'Published typebot not found', | ||
}) | ||
|
||
const fromDate = parseFromDateFromTimeFilter(timeFilter, timeZone) | ||
const toDate = parseToDateFromTimeFilter(timeFilter, timeZone) | ||
|
||
const totalAnswersPerBlock = await prisma.answer.groupBy({ | ||
by: ['blockId', 'resultId'], | ||
where: { | ||
result: { | ||
typebotId: typebot.publishedTypebot.typebotId, | ||
createdAt: fromDate | ||
? { | ||
gte: fromDate, | ||
lte: toDate ?? undefined, | ||
} | ||
: undefined, | ||
}, | ||
blockId: { | ||
in: parseGroups(typebot.publishedTypebot.groups, { | ||
typebotVersion: typebot.publishedTypebot.version, | ||
}).flatMap((group) => | ||
group.blocks.filter(isInputBlock).map((block) => block.id) | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
const totalAnswersV2PerBlock = await prisma.answerV2.groupBy({ | ||
by: ['blockId', 'resultId'], | ||
where: { | ||
result: { | ||
typebotId: typebot.publishedTypebot.typebotId, | ||
createdAt: fromDate | ||
? { | ||
gte: fromDate, | ||
lte: toDate ?? undefined, | ||
} | ||
: undefined, | ||
}, | ||
blockId: { | ||
in: parseGroups(typebot.publishedTypebot.groups, { | ||
typebotVersion: typebot.publishedTypebot.version, | ||
}).flatMap((group) => | ||
group.blocks.filter(isInputBlock).map((block) => block.id) | ||
), | ||
}, | ||
}, | ||
}) | ||
|
||
const uniqueCounts = totalAnswersPerBlock | ||
.concat(totalAnswersV2PerBlock) | ||
.reduce<{ | ||
[key: string]: Set<string> | ||
}>((acc, { blockId, resultId }) => { | ||
acc[blockId] = acc[blockId] || new Set() | ||
acc[blockId].add(resultId) | ||
return acc | ||
}, {}) | ||
|
||
const offDefaultPathVisitedEdges = await prisma.visitedEdge.groupBy({ | ||
by: ['edgeId'], | ||
where: { | ||
result: { | ||
typebotId: typebot.publishedTypebot.typebotId, | ||
createdAt: fromDate | ||
? { | ||
gte: fromDate, | ||
lte: toDate ?? undefined, | ||
} | ||
: undefined, | ||
}, | ||
}, | ||
_count: { resultId: true }, | ||
}) | ||
|
||
return { | ||
totalAnswers: Object.keys(uniqueCounts).map((blockId) => ({ | ||
blockId, | ||
total: uniqueCounts[blockId].size, | ||
})), | ||
offDefaultPathVisitedEdges: offDefaultPathVisitedEdges.map((e) => ({ | ||
edgeId: e.edgeId, | ||
total: e._count.resultId, | ||
})), | ||
} | ||
} | ||
) |
78 changes: 0 additions & 78 deletions
78
apps/builder/src/features/analytics/api/getTotalAnswers.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
import { router } from '@/helpers/server/trpc' | ||
import { getTotalAnswers } from './getTotalAnswers' | ||
import { getTotalVisitedEdges } from './getTotalVisitedEdges' | ||
import { getStats } from './getStats' | ||
import { getInDepthAnalyticsData } from './getInDepthAnalyticsData' | ||
|
||
export const analyticsRouter = router({ | ||
getTotalAnswers, | ||
getTotalVisitedEdges, | ||
getInDepthAnalyticsData, | ||
getStats, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.