: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 {
MakeLogicType,
actions,
afterMount,
beforeUnmount,
connect,
kea,
listeners,
path,
reducers,
selectors,
sharedListeners,
} from 'kea'
import type { BreakPointFunction } from 'kea'
import posthog from 'posthog-js'
import { FEATURE_FLAGS } from 'lib/constants'
import { FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { uuid } from 'lib/utils/dom'
import { teamLogic } from 'scenes/teamLogic'
import { dataNodeCollectionLogic } from '~/queries/nodes/DataNode/dataNodeCollectionLogic'
import { webAnalyticsAchievementsRecordVisit } from 'products/web_analytics/frontend/generated/api'
import { isWebAnalyticsAchievementsEnabled } from './achievements/gating'
import { webAnalyticsAchievementsLogic } from './achievements/webAnalyticsAchievementsLogic'
import { webAnalyticsAchievementsPreferencesLogic } from './achievements/webAnalyticsAchievementsPreferencesLogic'
import { WEB_ANALYTICS_DATA_COLLECTION_NODE_ID } from './common'
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface webAnalyticsLoadTimeLogicValues {
areAnyLoading: boolean // dataNodeCollectionLogic
featureFlags: FeatureFlagsSet // featureFlagLogic
currentProjectId: number | string // teamLogic
achievementsOptOut: boolean // webAnalyticsAchievementsPreferencesLogic
hasObservedLoading: boolean
tileSkeletonsEnabled: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface webAnalyticsLoadTimeLogicActions {
collectionNodeLoadData: (
id: string,
kind?: string | undefined
) => {
id: string
kind: string | undefined
} // dataNodeCollectionLogic
collectionNodeLoadDataFailure: (id: string) => {
id: string
} // dataNodeCollectionLogic
collectionNodeLoadDataSuccess: (
id: string,
meta?: import('~/queries/nodes/DataNode/dataNodeCollectionLogic').CollectionNodeLoadMeta | undefined
) => {
id: string
meta: import('~/queries/nodes/DataNode/dataNodeCollectionLogic').CollectionNodeLoadMeta | undefined
} // dataNodeCollectionLogic
endPageVisit: () => {
value: true
} // dataNodeCollectionLogic
startPageVisit: (visitId: string) => {
visitId: string
} // dataNodeCollectionLogic
recordVisit: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface webAnalyticsLoadTimeLogicMeta {
sharedListeners: {
maybeCaptureLoaded: (
payload: any,
breakpoint: BreakPointFunction,
action: {
type: string
payload: any
},
previousState: any
) => void | Promise<void>
}
__keaTypeGenInternalSelectorTypes: {
tileSkeletonsEnabled: (featureFlags: FeatureFlagsSet) => boolean
}
}
export type webAnalyticsLoadTimeLogicType = MakeLogicType<
webAnalyticsLoadTimeLogicValues,
webAnalyticsLoadTimeLogicActions,
Record<string, any>,
webAnalyticsLoadTimeLogicMeta
>
export const webAnalyticsLoadTimeLogic = kea<webAnalyticsLoadTimeLogicType>([
path(['scenes', 'webAnalytics', 'webAnalyticsLoadTimeLogic']),
connect(() => ({
actions: [
dataNodeCollectionLogic({ key: WEB_ANALYTICS_DATA_COLLECTION_NODE_ID }),
[
'collectionNodeLoadData',
'collectionNodeLoadDataSuccess',
'collectionNodeLoadDataFailure',
'startPageVisit',
'endPageVisit',
],
],
values: [
dataNodeCollectionLogic({ key: WEB_ANALYTICS_DATA_COLLECTION_NODE_ID }),
['areAnyLoading'],
featureFlagLogic,
['featureFlags'],
teamLogic,
['currentProjectId'],
webAnalyticsAchievementsPreferencesLogic,
['achievementsOptOut'],
],
})),
actions({
recordVisit: true,
}),
reducers({
hasObservedLoading: [
false,
{
collectionNodeLoadData: () => true,
},
],
}),
selectors({
tileSkeletonsEnabled: [
(s) => [s.featureFlags],
(flags: FeatureFlagsSet): boolean => !!flags[FEATURE_FLAGS.WEB_ANALYTICS_TILE_SKELETONS],
],
}),
sharedListeners(({ cache, values }) => ({
maybeCaptureLoaded: () => {
if (values.areAnyLoading || !values.hasObservedLoading || cache.hasCapturedLoaded) {
return
}
cache.hasCapturedLoaded = true
posthog.capture('web_analytics_dashboard_loaded', {
duration_ms: Math.round(performance.now() - cache.mountStart),
tile_skeletons_enabled: values.tileSkeletonsEnabled,
visit_id: cache.visitId,
})
},
})),
listeners(({ sharedListeners, cache, values }) => ({
collectionNodeLoadDataSuccess: sharedListeners.maybeCaptureLoaded,
collectionNodeLoadDataFailure: sharedListeners.maybeCaptureLoaded,
recordVisit: async () => {
if (
cache.recordedVisitThisSession ||
!isWebAnalyticsAchievementsEnabled(values.featureFlags, values.achievementsOptOut)
) {
return
}
const projectId = values.currentProjectId
if (projectId === undefined || projectId === null) {
return
}
cache.recordedVisitThisSession = true
try {
await webAnalyticsAchievementsRecordVisit(String(projectId))
webAnalyticsAchievementsLogic.findMounted()?.actions.loadAchievements()
} catch {
cache.recordedVisitThisSession = false
}
},
})),
afterMount(({ cache, values, actions }) => {
cache.mountStart = performance.now()
cache.hasCapturedLoaded = false
cache.visitId = uuid()
actions.startPageVisit(cache.visitId)
posthog.capture('web_analytics_dashboard_mounted', {
tile_skeletons_enabled: values.tileSkeletonsEnabled,
visit_id: cache.visitId,
})
actions.recordVisit()
}),
beforeUnmount(({ actions }) => {
actions.endPageVisit()
}),
])