Skip to content

Commit

Permalink
feat(results): 🛂 Limit analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Feb 13, 2022
1 parent ec470b5 commit f46ba38
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
50 changes: 35 additions & 15 deletions apps/builder/components/shared/Graph/Edges/DropOffEdge.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import { VStack, Tag, Text } from '@chakra-ui/react'
import { VStack, Tag, Text, Tooltip } from '@chakra-ui/react'
import { useGraph } from 'contexts/GraphContext'
import { useTypebot } from 'contexts/TypebotContext'
import { useUser } from 'contexts/UserContext'
import React, { useMemo } from 'react'
import { AnswersCount } from 'services/analytics'
import {
getEndpointTopOffset,
computeSourceCoordinates,
computeDropOffPath,
} from 'services/graph'
import { isFreePlan } from 'services/user'
import { byId, isDefined } from 'utils'

type Props = {
blockId: string
answersCounts: AnswersCount[]
onUnlockProPlanClick?: () => void
}

export const DropOffEdge = ({ answersCounts, blockId }: Props) => {
export const DropOffEdge = ({
answersCounts,
blockId,
onUnlockProPlanClick,
}: Props) => {
const { user } = useUser()
const { sourceEndpoints, graphPosition, blocksCoordinates } = useGraph()
const { publishedTypebot } = useTypebot()

const isUserOnFreePlan = isFreePlan(user)

const totalAnswers = useMemo(
() => answersCounts.find((a) => a.blockId === blockId)?.totalAnswers,
[answersCounts, blockId]
Expand Down Expand Up @@ -77,23 +87,33 @@ export const DropOffEdge = ({ answersCounts, blockId }: Props) => {
fill="none"
/>
<foreignObject
width="80"
width="100"
height="80"
x={labelCoordinates.x - 20}
x={labelCoordinates.x - 30}
y={labelCoordinates.y + 80}
>
<VStack
bgColor={'red.500'}
color="white"
rounded="md"
p="2"
justifyContent="center"
w="full"
h="full"
<Tooltip
label="Unlock Drop-off rate by upgrading to Pro plan"
isDisabled={!isUserOnFreePlan}
>
<Text>{dropOffRate}%</Text>
<Tag colorScheme="red">{totalDroppedUser} users</Tag>
</VStack>
<VStack
bgColor={'red.500'}
color="white"
rounded="md"
p="2"
justifyContent="center"
w="full"
h="full"
filter={isUserOnFreePlan ? 'blur(4px)' : ''}
onClick={isUserOnFreePlan ? onUnlockProPlanClick : undefined}
cursor={isUserOnFreePlan ? 'pointer' : 'auto'}
>
<Text>{isUserOnFreePlan ? 'X' : dropOffRate}%</Text>
<Tag colorScheme="red">
{isUserOnFreePlan ? 'n' : totalDroppedUser} users
</Tag>
</VStack>
</Tooltip>
</foreignObject>
</>
)
Expand Down
8 changes: 7 additions & 1 deletion apps/builder/components/shared/Graph/Edges/Edges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import { Edge } from './Edge'
type Props = {
edges: EdgeProps[]
answersCounts?: AnswersCount[]
onUnlockProPlanClick?: () => void
}
export const Edges = ({ edges, answersCounts }: Props) => {
export const Edges = ({
edges,
answersCounts,
onUnlockProPlanClick,
}: Props) => {
return (
<chakra.svg
width="full"
Expand All @@ -29,6 +34,7 @@ export const Edges = ({ edges, answersCounts }: Props) => {
key={answerCount.blockId}
answersCounts={answersCounts}
blockId={answerCount.blockId}
onUnlockProPlanClick={onUnlockProPlanClick}
/>
))}
<marker
Expand Down
8 changes: 7 additions & 1 deletion apps/builder/components/shared/Graph/Graph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ import { AnswersCount } from 'services/analytics'
export const Graph = ({
typebot,
answersCounts,
onUnlockProPlanClick,
...props
}: {
typebot?: Typebot | PublicTypebot
answersCounts?: AnswersCount[]
onUnlockProPlanClick?: () => void
} & FlexProps) => {
const { draggedStepType, setDraggedStepType, draggedStep, setDraggedStep } =
useStepDnd()
Expand Down Expand Up @@ -99,7 +101,11 @@ export const Graph = ({
transform,
}}
>
<Edges edges={typebot?.edges ?? []} answersCounts={answersCounts} />
<Edges
edges={typebot?.edges ?? []}
answersCounts={answersCounts}
onUnlockProPlanClick={onUnlockProPlanClick}
/>
{typebot?.blocks.map((block, idx) => (
<BlockNode block={block as Block} blockIndex={idx} key={block.id} />
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ export enum LimitReached {
BRAND = 'Remove branding',
CUSTOM_DOMAIN = 'Custom domain',
FOLDER = 'Create folders',
ANALYTICS = 'Unlock analytics',
}

type UpgradeModalProps = {
Expand Down
6 changes: 5 additions & 1 deletion apps/builder/layouts/results/AnalyticsContent.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Flex, useToast } from '@chakra-ui/react'
import { Flex, useDisclosure, useToast } from '@chakra-ui/react'
import { StatsCards } from 'components/analytics/StatsCards'
import { Graph } from 'components/shared/Graph'
import { UpgradeModal } from 'components/shared/modals/UpgradeModal.'
import { GraphProvider } from 'contexts/GraphContext'
import { useTypebot } from 'contexts/TypebotContext/TypebotContext'
import { Stats } from 'models'
import React from 'react'
import { useAnswersCount } from 'services/analytics'

export const AnalyticsContent = ({ stats }: { stats?: Stats }) => {
const { isOpen, onOpen, onClose } = useDisclosure()
const { typebot, publishedTypebot } = useTypebot()

const toast = useToast({
Expand All @@ -31,13 +33,15 @@ export const AnalyticsContent = ({ stats }: { stats?: Stats }) => {
<Graph
flex="1"
typebot={publishedTypebot}
onUnlockProPlanClick={onOpen}
answersCounts={[
{ ...answersCounts[0], totalAnswers: stats?.totalStarts },
...answersCounts?.slice(1),
]}
/>
</GraphProvider>
)}
<UpgradeModal onClose={onClose} isOpen={isOpen} />
<StatsCards stats={stats} pos="absolute" top={10} />
</Flex>
)
Expand Down

3 comments on commit f46ba38

@vercel
Copy link

@vercel vercel bot commented on f46ba38 Feb 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

viewer-v2 – ./apps/viewer

typebot-viewer.vercel.app
viewer-v2-git-main-typebot-io.vercel.app
viewer-v2-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on f46ba38 Feb 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

landing-page-v2 – ./apps/landing-page

landing-page-v2-git-main-typebot-io.vercel.app
landing-page-v2-jade.vercel.app
landing-page-v2-typebot-io.vercel.app

@vercel
Copy link

@vercel vercel bot commented on f46ba38 Feb 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-git-main-typebot-io.vercel.app
builder-v2-typebot-io.vercel.app
next.typebot.io

Please sign in to comment.