: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 { AnnotationScope, EventDefinition, PropertyDefinition, PropertyType, RawAnnotationType } from '~/types'
const friday = '2024-06-14T16:00:00.000Z'
const setupWeek = '2024-06-03T10:00:00.000Z'
const days = ['2024-06-10', '2024-06-11', '2024-06-12', '2024-06-13', '2024-06-14']
const labels = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri']
export const eventDefinitions: EventDefinition[] = [
{
id: 'evt-001',
name: '$pageview',
description: 'Scoreboard website viewed',
tags: ['web'],
last_seen_at: friday,
created_at: setupWeek,
},
{
id: 'evt-002',
name: 'Napped',
description: 'A hedgehog took a nap',
tags: ['nap'],
last_seen_at: friday,
created_at: setupWeek,
},
{
id: 'evt-003',
name: 'ZeroCounts',
description: 'Event with one empty and one active series',
tags: [],
last_seen_at: friday,
created_at: setupWeek,
},
{
id: 'evt-004',
name: 'Minimal',
description: 'Event with no action metadata',
tags: [],
last_seen_at: friday,
created_at: setupWeek,
},
{
id: 'evt-005',
name: 'NoActivity',
description: 'Event whose series is all zeros (drives empty-state branch)',
tags: [],
last_seen_at: friday,
created_at: setupWeek,
},
]
export const propertyDefinitions: PropertyDefinition[] = [
{
id: 'prop-101',
name: 'hedgehog',
description: 'Name of the hedgehog',
tags: [],
is_numerical: false,
property_type: PropertyType.String,
},
]
export const propertyValues: Record<string, string[]> = {
hedgehog: ['Spike', 'Bramble', 'Thistle', 'Conker', 'Prickles'],
}
export const personProperties = [
{ id: 1, name: 'email', count: 30 },
{ id: 2, name: 'name', count: 30 },
]
export const sessionPropertyDefinitions: PropertyDefinition[] = [
{
id: 'session-001',
name: '$session_duration',
description: 'Duration of the session in seconds',
is_numerical: true,
property_type: PropertyType.Numeric,
},
]
export const actionDefinitions: object[] = []
export interface SeriesData {
label: string
data: number[]
labels?: string[]
days?: string[]
breakdown_value?: string | number
compare?: boolean
compare_label?: string
/** Index of the query series this row belongs to — stamped by the mock resolver. */
seriesIndex?: number
}
type CannedSeries = SeriesData & { labels: string[]; days: string[] }
export const trendsSeries = {
pageviews: {
label: '$pageview',
data: [45, 82, 134, 210, 95],
days,
labels,
} satisfies CannedSeries,
napped: {
label: 'Napped',
data: [1, 3, 5, 8, 2],
days,
labels,
} satisfies CannedSeries,
napsByHedgehog: [
{ label: 'Spike', data: [1, 2, 3, 4, 1], days, labels, breakdown_value: 'Spike' },
{ label: 'Bramble', data: [0, 0, 1, 1, 0], days, labels, breakdown_value: 'Bramble' },
{ label: 'Thistle', data: [0, 1, 0, 2, 1], days, labels, breakdown_value: 'Thistle' },
{ label: 'Conker', data: [0, 0, 0, 0, 0], days, labels, breakdown_value: 'Conker' },
{ label: 'Prickles', data: [0, 0, 1, 1, 0], days, labels, breakdown_value: 'Prickles' },
] satisfies CannedSeries[],
// A part that sums negative, as a trends formula like `A - B` can.
napsByHedgehogWithNegativePart: [
{ label: 'Spike', data: [10, 0, 0, 0, 0], days, labels, breakdown_value: 'Spike' },
{ label: 'Bramble', data: [-4, 0, 0, 0, 0], days, labels, breakdown_value: 'Bramble' },
] satisfies CannedSeries[],
napsByManyHedgehogs: Array.from({ length: 21 }, (_, i) => ({
label: `Hedgehog ${i + 1}`,
data: [1, 0, 0, 0, 0],
days,
labels,
breakdown_value: `Hedgehog ${i + 1}`,
})) satisfies CannedSeries[],
pageviewsByHedgehog: [
{ label: 'Spike', data: [30, 50, 90, 140, 60], days, labels, breakdown_value: 'Spike' },
{ label: 'Bramble', data: [15, 32, 44, 70, 35], days, labels, breakdown_value: 'Bramble' },
] satisfies CannedSeries[],
withZeroCounts: [
{ label: 'EmptySeries', data: [0, 0, 0, 0, 0], days, labels },
{ label: 'ActiveSeries', data: [1, 2, 3, 2, 2], days, labels },
] satisfies CannedSeries[],
minimal: {
label: 'Minimal',
data: [1, 1, 1, 1, 1],
days,
labels,
} satisfies CannedSeries,
// Week display labels omit the year, so a multi-year weekly range returns the same label
// string for buckets in different years — the shape that folds a chart keyed by labels.
weeklyAcrossYears: {
label: 'WeeklyAcrossYears',
data: [10, 20, 30, 40, 50, 60],
days: ['2020-06-01', '2020-06-08', '2020-06-15', '2026-06-01', '2026-06-08', '2026-06-15'],
labels: ['1–7 Jun', '8–14 Jun', '15–21 Jun', '1–7 Jun', '8–14 Jun', '15–21 Jun'],
} satisfies CannedSeries,
noActivity: {
label: 'NoActivity',
data: [0, 0, 0, 0, 0],
days,
labels,
} satisfies CannedSeries,
pageviewsCompare: [
{ label: '$pageview', data: [45, 82, 134, 210, 95], days, labels, compare: true, compare_label: 'current' },
{
label: '$pageview',
data: [30, 60, 100, 180, 70],
days: ['2024-06-03', '2024-06-04', '2024-06-05', '2024-06-06', '2024-06-07'],
labels,
compare: true,
compare_label: 'previous',
},
] satisfies CannedSeries[],
}
// Maps (event name, optional breakdown) → canned series data.
// The mock query handler calls this to resolve a query into response data.
interface EventSeriesConfig {
default: SeriesData
breakdowns?: Record<string, SeriesData[]>
}
const seriesByEvent: Record<string, EventSeriesConfig> = {
$pageview: {
default: trendsSeries.pageviews,
breakdowns: {
hedgehog: trendsSeries.pageviewsByHedgehog,
},
},
Napped: {
default: trendsSeries.napped,
breakdowns: {
hedgehog: trendsSeries.napsByHedgehog,
},
},
NappedWithNegativePart: {
default: trendsSeries.napped,
breakdowns: {
hedgehog: trendsSeries.napsByHedgehogWithNegativePart,
},
},
NappedByManyHedgehogs: {
default: trendsSeries.napped,
breakdowns: {
hedgehog: trendsSeries.napsByManyHedgehogs,
},
},
EmptySeries: { default: trendsSeries.withZeroCounts[0] },
ActiveSeries: { default: trendsSeries.withZeroCounts[1] },
Minimal: { default: trendsSeries.minimal },
NoActivity: { default: trendsSeries.noActivity },
WeeklyAcrossYears: { default: trendsSeries.weeklyAcrossYears },
}
/** Resolver for compare queries — returns current + previous period series. */
export function lookupCompareSeries(eventName: string): SeriesData[] | null {
if (eventName === '$pageview') {
return trendsSeries.pageviewsCompare
}
return null
}
const fallbackSeries: SeriesData = {
label: 'Unknown',
data: [1, 1, 1, 1, 1],
days,
labels,
}
export function lookupSeries(eventName: string, breakdownProperty?: string): SeriesData[] {
const config = seriesByEvent[eventName]
if (!config) {
return [{ ...fallbackSeries, label: eventName }]
}
if (breakdownProperty && config.breakdowns?.[breakdownProperty]) {
return config.breakdowns[breakdownProperty]
}
return [config.default]
}
// Canned actor lookups keyed by (event, breakdown_value, day). Lets the
// default ActorsQuery mock return different persons for different click
// contexts so tests can assert on displayed names instead of poking at the
// raw query body — if the wrong persons show up, we know the query was wrong.
export interface ActorFixture {
email: string
}
/** Nested map: event → breakdown_value ('__none__' when no breakdown) → day → actors. */
const actorsByEventBreakdownDay: Record<string, Record<string, Record<string, ActorFixture[]>>> = {
$pageview: {
__none__: {
'2024-06-10': [{ email: '[email protected]' }],
'2024-06-11': [{ email: '[email protected]' }],
'2024-06-12': [{ email: '[email protected]' }, { email: '[email protected]' }],
'2024-06-13': [{ email: '[email protected]' }],
'2024-06-14': [{ email: '[email protected]' }],
},
},
Napped: {
Spike: {
'2024-06-12': [{ email: '[email protected]' }],
},
Bramble: {
'2024-06-12': [{ email: '[email protected]' }],
},
Thistle: {
'2024-06-12': [{ email: '[email protected]' }],
},
},
}
export interface ActorsLookupQuery {
event?: string
breakdown?: string | number | null
day?: string | number | null
}
/** Build a RawAnnotationType for use in setupInsightMocks({ annotations }).
* Generates a random id so identifiers don't carry deterministic state across tests. */
export function buildAnnotation(overrides: Partial<RawAnnotationType> = {}): RawAnnotationType {
return {
id: Math.floor(Math.random() * 1_000_000),
scope: AnnotationScope.Project,
content: 'Hedgehog spotted',
date_marker: '2024-06-12T12:00:00Z',
created_at: '2024-06-10T00:00:00Z',
updated_at: '2024-06-10T00:00:00Z',
...overrides,
}
}
export function lookupActors({ event, breakdown, day }: ActorsLookupQuery): ActorFixture[] {
if (!event || !day) {
return []
}
const breakdownKey = breakdown == null ? '__none__' : String(breakdown)
return actorsByEventBreakdownDay[event]?.[breakdownKey]?.[String(day)] ?? []
}
// Funnel trends-viz response shape: `data` holds the conversion percentage over time.
export interface FunnelStepData {
count: number
data: number[]
days: string[]
labels: string[]
name?: string
breakdown_value?: string | number
// The funnels runner tags compare rows with `compare_label` (it doesn't set `compare`).
compare_label?: 'current' | 'previous'
// Per-period conversion counts, index-aligned with `data`. Absent where a fixture stands in for a cached result.
reached_from_step_count?: number[]
reached_to_step_count?: number[]
}
export const funnelTrendsSteps = {
default: {
count: 50,
data: [10, 25, 40, 60, 35],
days,
labels,
name: '$pageview → Napped',
reached_from_step_count: [200, 200, 200, 200, 200],
reached_to_step_count: [20, 50, 80, 120, 70],
} satisfies FunnelStepData,
byBreakdown: {
hedgehog: [
{
count: 30,
data: [20, 35, 50, 70, 45],
days,
labels,
name: '$pageview → Napped',
breakdown_value: 'Spike',
},
{
count: 20,
data: [5, 15, 30, 50, 25],
days,
labels,
name: '$pageview → Napped',
breakdown_value: 'Bramble',
},
] satisfies FunnelStepData[],
// Query order (Safari, Chrome, Firefox), alphabetical order, and value-at-index-2 order
// (Firefox 60, Safari 40, Chrome 20) all differ, so a tooltip sorted by descending value
// can't accidentally match the input order.
browser: [
{
count: 40,
data: [30, 35, 40, 45, 38],
days,
labels,
name: '$pageview → Napped',
breakdown_value: 'Safari',
},
{
count: 20,
data: [10, 15, 20, 25, 18],
days,
labels,
name: '$pageview → Napped',
breakdown_value: 'Chrome',
},
{
count: 60,
data: [50, 55, 60, 65, 58],
days,
labels,
name: '$pageview → Napped',
breakdown_value: 'Firefox',
},
] satisfies FunnelStepData[],
} as Record<string, FunnelStepData[]>,
// Compare-to-previous + breakdown: current and previous rows per breakdown value, each
// tagged with `compare_label` so the tooltip can split them into their own rows.
compareByBreakdown: {
hedgehog: [
{
count: 30,
data: [20, 35, 50, 70, 45],
days,
labels,
name: '$pageview → Napped',
breakdown_value: 'Spike',
compare_label: 'current',
},
{
count: 28,
data: [18, 30, 45, 65, 40],
days,
labels,
name: '$pageview → Napped',
breakdown_value: 'Spike',
compare_label: 'previous',
},
{
count: 20,
data: [5, 15, 30, 50, 25],
days,
labels,
name: '$pageview → Napped',
breakdown_value: 'Bramble',
compare_label: 'current',
},
{
count: 18,
data: [3, 10, 25, 45, 20],
days,
labels,
name: '$pageview → Napped',
breakdown_value: 'Bramble',
compare_label: 'previous',
},
] satisfies FunnelStepData[],
} as Record<string, FunnelStepData[]>,
}
// Keyed by breakdown value (or '__none__'), then by calendar date.
const funnelActorsByBreakdownDay: Record<string, Record<string, ActorFixture[]>> = {
__none__: {
'2024-06-12': [{ email: '[email protected]' }, { email: '[email protected]' }],
'2024-06-13': [{ email: '[email protected]' }],
},
Spike: {
'2024-06-12': [{ email: '[email protected]' }],
},
Bramble: {
'2024-06-12': [{ email: '[email protected]' }],
},
}
export interface FunnelActorsLookupQuery {
day?: string | null
breakdown?: string | number | null
}
export function lookupFunnelActors({ day, breakdown }: FunnelActorsLookupQuery): ActorFixture[] {
if (!day) {
return []
}
const breakdownKey = breakdown == null ? '__none__' : String(breakdown)
return funnelActorsByBreakdownDay[breakdownKey]?.[day] ?? []
}