:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import './LiveUserCount.scss'
import { useActions, useValues } from 'kea'
import { useEffect } from 'react'
import { IconPerson } from '@posthog/icons'
import { Tooltip } from '@posthog/lemon-ui'
import { usePageVisibility } from 'lib/hooks/usePageVisibility'
import { cn } from 'lib/utils/css-classes'
import { humanFriendlyLargeNumber, humanFriendlyNumber } from 'lib/utils/numbers'
import { teamLogic } from 'scenes/teamLogic'
import { LiveUserCountLogicProps, liveUserCountLogic } from './liveUserCountLogic'
interface LiveUserCountTooltipContentProps {
pollIntervalMs?: number
showUpdatedTimeInTooltip?: boolean
}
function LiveUserCountTooltipContent({
pollIntervalMs,
showUpdatedTimeInTooltip,
}: LiveUserCountTooltipContentProps): JSX.Element | null {
const { currentTeam } = useValues(teamLogic)
const logicProps: LiveUserCountLogicProps = { pollIntervalMs }
const { statsUpdatedSecondsAgo, liveUserCount } = useValues(liveUserCountLogic(logicProps))
const { setIsHovering } = useActions(liveUserCountLogic(logicProps))
useEffect(() => {
setIsHovering(true)
return () => setIsHovering(false)
}, [setIsHovering])
if (liveUserCount == null) {
return null
}
const usersOnlineString = `${humanFriendlyNumber(liveUserCount)} ${
liveUserCount === 1 ? 'user has' : 'users have'
} been seen recently`
const inTeamString = currentTeam ? ` in ${currentTeam.name}` : ''
const updatedString = !showUpdatedTimeInTooltip
? null
: statsUpdatedSecondsAgo === 0
? 'updated just now'
: statsUpdatedSecondsAgo == null
? ''
: `updated ${statsUpdatedSecondsAgo} seconds ago`
return (
<div>
<div>
{usersOnlineString}
{inTeamString}
</div>
{updatedString && <div>({updatedString})</div>}
</div>
)
}
export type LiveUserCountProps = {
pollIntervalMs?: number
docLink?: string
showUpdatedTimeInTooltip?: boolean
dataAttr?: string
}
export function LiveUserCount({
pollIntervalMs = 30000,
docLink,
showUpdatedTimeInTooltip = true,
}: LiveUserCountProps): JSX.Element | null {
const { liveUserCount } = useValues(liveUserCountLogic({ pollIntervalMs }))
const { pauseStream, resumeStream } = useActions(liveUserCountLogic({ pollIntervalMs }))
const { isVisible } = usePageVisibility()
useEffect(() => {
if (isVisible) {
resumeStream()
} else {
pauseStream()
}
}, [isVisible, resumeStream, pauseStream])
const isOnline = (liveUserCount ?? 0) > 0
return liveUserCount === null ? null : (
<Tooltip
title={
<LiveUserCountTooltipContent
pollIntervalMs={pollIntervalMs}
showUpdatedTimeInTooltip={showUpdatedTimeInTooltip}
/>
}
interactive={!!docLink}
delayMs={0}
docLink={docLink}
>
<div
className={cn(
'flex items-center gap-1.5 px-2 py-1 rounded-md transition-colors',
isOnline ? 'bg-success-highlight' : 'bg-border-light'
)}
>
<div className={cn('live-user-indicator', isOnline ? 'online' : 'offline')} />
<IconPerson className="size-4 shrink-0 min-[660px]:hidden" />
<span className="text-xs font-medium whitespace-nowrap" data-attr="web-analytics-live-user-count">
<strong>{humanFriendlyLargeNumber(liveUserCount)}</strong>
</span>
<span className="hidden min-[660px]:inline">recently online</span>
</div>
</Tooltip>
)
}