2
0

🖐️ Analytics drop off rates

This commit is contained in:
Baptiste Arnaud
2022-01-03 17:39:59 +01:00
parent 1093453c07
commit 6322402c96
38 changed files with 876 additions and 147 deletions

View File

@ -25,6 +25,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
where: {
typebotId,
typebot: { ownerId: user.id },
answers: { some: {} },
},
orderBy: {
createdAt: 'desc',

View File

@ -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

View File

@ -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)
}

View File

@ -3,7 +3,6 @@ import { Board } from 'components/board/Board'
import { Seo } from 'components/Seo'
import { TypebotHeader } from 'components/shared/TypebotHeader'
import { EditorContext } from 'contexts/EditorContext'
import { GraphProvider } from 'contexts/GraphContext'
import { TypebotContext } from 'contexts/TypebotContext'
import { useRouter } from 'next/router'
import { KBarProvider } from 'kbar'
@ -23,9 +22,7 @@ const TypebotEditPage = () => {
<KBar />
<Flex overflow="hidden" h="100vh" flexDir="column">
<TypebotHeader />
<GraphProvider>
<Board />
</GraphProvider>
<Board />
</Flex>
</KBarProvider>
</EditorContext>