2
0

(analytics) Keep track of already explored blocks for drop off rate

This commit is contained in:
Baptiste Arnaud
2023-09-11 16:10:59 +02:00
parent eea5d82f40
commit 87fac3e9ff

View File

@@ -5,7 +5,8 @@ import { TotalAnswersInBlock } from '@typebot.io/schemas/features/analytics'
export const computePreviousTotalAnswers = ( export const computePreviousTotalAnswers = (
publishedTypebot: PublicTypebot, publishedTypebot: PublicTypebot,
currentBlockId: string, currentBlockId: string,
totalAnswersInBlocks: TotalAnswersInBlock[] totalAnswersInBlocks: TotalAnswersInBlock[],
visitedBlocks: string[] = []
): number => { ): number => {
let totalAnswers = 0 let totalAnswers = 0
const allBlocks = publishedTypebot.groups.flatMap((group) => group.blocks) const allBlocks = publishedTypebot.groups.flatMap((group) => group.blocks)
@@ -18,10 +19,12 @@ export const computePreviousTotalAnswers = (
) )
const previousBlocks = currentGroup.blocks.slice(0, currentBlockIndex + 1) const previousBlocks = currentGroup.blocks.slice(0, currentBlockIndex + 1)
for (const block of previousBlocks.reverse()) { for (const block of previousBlocks.reverse()) {
if (visitedBlocks.includes(block.id)) continue
if ( if (
currentBlockId !== block.id && currentBlockId !== block.id &&
(isInputBlock(block) || block.type === 'start') (isInputBlock(block) || block.type === 'start')
) ) {
visitedBlocks.push(block.id)
return ( return (
totalAnswersInBlocks.find( totalAnswersInBlocks.find(
(totalAnswersInBlock) => (totalAnswersInBlock) =>
@@ -29,6 +32,7 @@ export const computePreviousTotalAnswers = (
totalAnswersInBlock.itemId === undefined totalAnswersInBlock.itemId === undefined
)?.total ?? 0 )?.total ?? 0
) )
}
const connectedEdges = publishedTypebot.edges.filter( const connectedEdges = publishedTypebot.edges.filter(
(edge) => edge.to.blockId === block.id (edge) => edge.to.blockId === block.id
) )
@@ -37,8 +41,9 @@ export const computePreviousTotalAnswers = (
const connectedBlock = allBlocks.find( const connectedBlock = allBlocks.find(
(block) => block.id === connectedEdge.from.blockId (block) => block.id === connectedEdge.from.blockId
) )
if (connectedBlock) { if (connectedBlock && !visitedBlocks.includes(connectedBlock.id)) {
if (isInputBlock(connectedBlock) || connectedBlock.type === 'start') { if (isInputBlock(connectedBlock) || connectedBlock.type === 'start') {
visitedBlocks.push(connectedBlock.id)
totalAnswers += totalAnswers +=
totalAnswersInBlocks.find( totalAnswersInBlocks.find(
(totalAnswersInBlock) => (totalAnswersInBlock) =>
@@ -49,7 +54,8 @@ export const computePreviousTotalAnswers = (
totalAnswers += computePreviousTotalAnswers( totalAnswers += computePreviousTotalAnswers(
publishedTypebot, publishedTypebot,
connectedBlock.id, connectedBlock.id,
totalAnswersInBlocks totalAnswersInBlocks,
visitedBlocks
) )
} }
} }
@@ -65,8 +71,9 @@ export const computePreviousTotalAnswers = (
const connectedBlock = allBlocks.find( const connectedBlock = allBlocks.find(
(block) => block.id === connectedEdge.from.blockId (block) => block.id === connectedEdge.from.blockId
) )
if (connectedBlock) { if (connectedBlock && !visitedBlocks.includes(connectedBlock.id)) {
if (isInputBlock(connectedBlock) || connectedBlock.type === 'start') { if (isInputBlock(connectedBlock) || connectedBlock.type === 'start') {
visitedBlocks.push(connectedBlock.id)
totalAnswers += totalAnswers +=
totalAnswersInBlocks.find( totalAnswersInBlocks.find(
(totalAnswersInBlock) => (totalAnswersInBlock) =>
@@ -77,7 +84,8 @@ export const computePreviousTotalAnswers = (
totalAnswers += computePreviousTotalAnswers( totalAnswers += computePreviousTotalAnswers(
publishedTypebot, publishedTypebot,
connectedBlock.id, connectedBlock.id,
totalAnswersInBlocks totalAnswersInBlocks,
visitedBlocks
) )
} }
} }