🖐️ Analytics drop off rates
This commit is contained in:
@ -25,6 +25,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
where: {
|
||||
typebotId,
|
||||
typebot: { ownerId: user.id },
|
||||
answers: { some: {} },
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'desc',
|
||||
|
@ -0,0 +1,41 @@
|
||||
import { PublicTypebot } from 'bot-engine'
|
||||
import { User } from 'db'
|
||||
import prisma from 'libs/prisma'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import { getSession } from 'next-auth/react'
|
||||
import { methodNotAllowed } from 'utils'
|
||||
|
||||
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const session = await getSession({ req })
|
||||
|
||||
if (!session?.user)
|
||||
return res.status(401).send({ message: 'Not authenticated' })
|
||||
|
||||
const user = session.user as User
|
||||
if (req.method === 'GET') {
|
||||
const typebotId = req.query.typebotId.toString()
|
||||
const typebot = await prisma.typebot.findUnique({
|
||||
where: { id: typebotId },
|
||||
include: { publishedTypebot: true },
|
||||
})
|
||||
if (!typebot) return res.status(404).send({ answersCounts: [] })
|
||||
if (typebot?.ownerId !== user.id)
|
||||
return res.status(403).send({ message: 'Forbidden' })
|
||||
|
||||
const answersCounts: { blockId: string; totalAnswers: number }[] =
|
||||
await Promise.all(
|
||||
(typebot.publishedTypebot as PublicTypebot).blocks.map(
|
||||
async (block) => {
|
||||
const totalAnswers = await prisma.answer.count({
|
||||
where: { blockId: block.id },
|
||||
})
|
||||
return { blockId: block.id, totalAnswers }
|
||||
}
|
||||
)
|
||||
)
|
||||
return res.status(200).send({ answersCounts })
|
||||
}
|
||||
return methodNotAllowed(res)
|
||||
}
|
||||
|
||||
export default handler
|
@ -1,3 +1,4 @@
|
||||
import { Stats } from 'bot-engine'
|
||||
import { User } from 'db'
|
||||
import prisma from 'libs/prisma'
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
@ -13,13 +14,33 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const user = session.user as User
|
||||
if (req.method === 'GET') {
|
||||
const typebotId = req.query.typebotId.toString()
|
||||
const totalResults = await prisma.result.count({
|
||||
|
||||
const totalViews = await prisma.result.count({
|
||||
where: {
|
||||
typebotId,
|
||||
typebot: { ownerId: user.id },
|
||||
},
|
||||
})
|
||||
return res.status(200).send({ totalResults })
|
||||
const totalStarts = await prisma.result.count({
|
||||
where: {
|
||||
typebotId,
|
||||
typebot: { ownerId: user.id },
|
||||
answers: { some: {} },
|
||||
},
|
||||
})
|
||||
const totalCompleted = await prisma.result.count({
|
||||
where: {
|
||||
typebotId,
|
||||
typebot: { ownerId: user.id },
|
||||
isCompleted: true,
|
||||
},
|
||||
})
|
||||
const stats: Stats = {
|
||||
totalViews,
|
||||
totalStarts,
|
||||
completionRate: Math.round((totalCompleted / totalStarts) * 100),
|
||||
}
|
||||
return res.status(200).send({ stats })
|
||||
}
|
||||
return methodNotAllowed(res)
|
||||
}
|
Reference in New Issue
Block a user