: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
fix(data-management): clamp coverage trends window to event lifetime (#66772)
Christiaan Hendriksencc1d3bd13c39
diff --git a/frontend/src/scenes/data-management/events/EventDefinitionSchema.tsx b/frontend/src/scenes/data-management/events/EventDefinitionSchema.tsxindex 105bdaa5426d..a3a4c0c99870 100644--- a/frontend/src/scenes/data-management/events/EventDefinitionSchema.tsx+++ b/frontend/src/scenes/data-management/events/EventDefinitionSchema.tsx@@ -42,17 +42,19 @@ function PropertyRow({ property }: { property: SchemaPropertyGroupProperty }): Jfunction PropertyGroupCard({schema,eventName,+ eventFirstSeen,onEdit,onRemove,}: {schema: EventSchemaeventName: string+ eventFirstSeen?: string | nullonEdit: () => voidonRemove: () => void}): JSX.Element {const queryResult = useMemo(- () => buildPropertyGroupTrendsQuery(eventName, schema.property_group.properties),- [eventName, schema.property_group.properties]+ () => buildPropertyGroupTrendsQuery(eventName, schema.property_group.properties, eventFirstSeen),+ [eventName, eventFirstSeen, schema.property_group.properties])const linkQuery = useMemo(@@ -108,7 +110,7 @@ function PropertyGroupCard({<div className="p-4 bg-bg-light"><h4 className="font-semibold mb-2 text-sm flex items-center gap-1"><Link to={insightUrl} className="text-default hover:text-link">- Property Coverage Trends (90 days)+ Property Coverage Trends ({queryResult.dateRangeLabel})</Link><Tooltip title="% of events containing this property"><IconInfo className="text-xl text-secondary shrink-0" />@@ -211,6 +213,7 @@ export function EventDefinitionSchema({ definition }: { definition: EventDefinitkey={schema.id}schema={schema}eventName={definition.name}+ eventFirstSeen={definition.created_at}onEdit={() => {setEditingPropertyGroup(schema.property_group)setPropertyGroupModalOpen(true)diff --git a/frontend/src/scenes/data-management/events/propertyGroupTrendsQuery.test.ts b/frontend/src/scenes/data-management/events/propertyGroupTrendsQuery.test.tsnew file mode 100644index 000000000000..63e3117d0076--- /dev/null+++ b/frontend/src/scenes/data-management/events/propertyGroupTrendsQuery.test.ts@@ -0,0 +1,79 @@+import { dayjs } from 'lib/dayjs'++import { TrendsQuery } from '~/queries/schema/schema-general'+import { PropertyType } from '~/types'++import { SchemaPropertyGroupProperty } from '../schema/schemaManagementLogic'+import { buildPropertyGroupTrendsQuery } from './propertyGroupTrendsQuery'++describe('buildPropertyGroupTrendsQuery', () => {+ const properties: SchemaPropertyGroupProperty[] = [+ {+ id: '1',+ name: 'plan',+ property_type: PropertyType.String,+ is_required: true,+ is_optional_in_types: false,+ description: '',+ },+ {+ id: '2',+ name: 'price',+ property_type: PropertyType.Numeric,+ is_required: false,+ is_optional_in_types: false,+ description: '',+ },+ ]++ const sourceOf = (...args: Parameters<typeof buildPropertyGroupTrendsQuery>): TrendsQuery =>+ buildPropertyGroupTrendsQuery(...args).query.source as TrendsQuery++ it('builds one coverage formula (propertyCount / eventCount) per property', () => {+ const source = sourceOf('purchase', properties)++ // Base count series A, then one is_set series per property (B, C, …).+ expect(source.series).toHaveLength(3)+ expect(source.series[0]).toMatchObject({ event: 'purchase', math: 'total' })+ expect(source.series[1]).toMatchObject({+ event: 'purchase',+ properties: [{ key: 'plan', operator: 'is_set' }],+ })+ expect(source.trendsFilter?.formulaNodes).toEqual([+ { formula: 'B/A * 100', custom_name: 'plan' },+ { formula: 'C/A * 100', custom_name: 'price' },+ ])+ expect(source.trendsFilter?.aggregationAxisFormat).toBe('percentage')+ })++ it('truncates to 25 properties so series labels stay within B–Z', () => {+ const many = Array.from({ length: 30 }, (_, i) => ({ ...properties[0], id: String(i), name: `p${i}` }))+ const result = buildPropertyGroupTrendsQuery('e', many)++ expect(result.isTruncated).toBe(true)+ expect(result.totalProperties).toBe(30)+ expect(result.displayedProperties).toBe(25)+ expect((result.query.source as TrendsQuery).series).toHaveLength(26) // base + 25+ })++ // The bug this guards: weeks before the event existed evaluate coverage as 0/0 → 0%, painting a+ // flat 0% line across the whole window. Clamping the start to the event's first-seen removes them.+ it('clamps the window to first-seen for an event younger than 90 days', () => {+ const firstSeen = dayjs().subtract(10, 'day').toISOString()+ const result = buildPropertyGroupTrendsQuery('e', properties, firstSeen)++ expect((result.query.source as TrendsQuery).dateRange?.date_from).toBe(dayjs(firstSeen).format('YYYY-MM-DD'))+ expect(result.dateRangeLabel).toBe(`since ${dayjs(firstSeen).format('MMM D, YYYY')}`)+ })++ it.each([+ ['first-seen is older than the window', dayjs().subtract(200, 'day').toISOString()],+ ['first-seen is unknown', undefined],+ ['first-seen is unparseable', 'not-a-date'],+ ])('keeps the default 90-day window when %s', (_label, firstSeen) => {+ const result = buildPropertyGroupTrendsQuery('e', properties, firstSeen)++ expect((result.query.source as TrendsQuery).dateRange?.date_from).toBe('-90d')+ expect(result.dateRangeLabel).toBe('last 90 days')+ })+})diff --git a/frontend/src/scenes/data-management/events/propertyGroupTrendsQuery.ts b/frontend/src/scenes/data-management/events/propertyGroupTrendsQuery.tsindex 0ae748ee108d..bceb1e59f195 100644--- a/frontend/src/scenes/data-management/events/propertyGroupTrendsQuery.ts+++ b/frontend/src/scenes/data-management/events/propertyGroupTrendsQuery.ts@@ -1,20 +1,26 @@+import { dayjs } from 'lib/dayjs'+import { EventsNode, InsightVizNode, NodeKind, TrendsQuery } from '~/queries/schema/schema-general'import { BaseMathType, PropertyFilterType, PropertyOperator } from '~/types'import { SchemaPropertyGroupProperty } from '../schema/schemaManagementLogic'const MAX_PROPERTIES = 25+const LOOKBACK_DAYS = 90export interface PropertyGroupTrendsQueryResult {query: InsightVizNodeisTruncated: booleantotalProperties: numberdisplayedProperties: number+ /** Human-readable description of the charted window, e.g. "last 90 days" or "since Jun 15, 2026". */+ dateRangeLabel: string}export function buildPropertyGroupTrendsQuery(eventName: string,- properties: SchemaPropertyGroupProperty[]+ properties: SchemaPropertyGroupProperty[],+ eventFirstSeen?: string | null): PropertyGroupTrendsQueryResult {// Limit to 25 properties since we use letters B-Z for series labels (A is reserved for the base series)const isTruncated = properties.length > MAX_PROPERTIES@@ -56,6 +62,16 @@ export function buildPropertyGroupTrendsQuery(}})+ // Coverage is `propertyCount / eventCount`, so weeks where the event never fired evaluate to 0/0,+ // which the formula engine renders as 0% rather than a gap. For an event younger than the lookback+ // window that paints a misleading flat 0% line across every week before the event existed, hiding+ // the real coverage in the final buckets. Never start the window before the event first appeared.+ const defaultFrom = dayjs().subtract(LOOKBACK_DAYS, 'day')+ const firstSeen = eventFirstSeen ? dayjs(eventFirstSeen) : null+ const clampedFrom = firstSeen && firstSeen.isValid() && firstSeen.isAfter(defaultFrom) ? firstSeen : null+ const dateFrom = clampedFrom ? clampedFrom.format('YYYY-MM-DD') : `-${LOOKBACK_DAYS}d`+ const dateRangeLabel = clampedFrom ? `since ${clampedFrom.format('MMM D, YYYY')}` : `last ${LOOKBACK_DAYS} days`+const trendsQuery: TrendsQuery = {kind: NodeKind.TrendsQuery,series,@@ -63,7 +79,7 @@ export function buildPropertyGroupTrendsQuery(interval: 'week',dateRange: {date_to: null,- date_from: '-90d',+ date_from: dateFrom,explicitDate: false,},trendsFilter: {@@ -85,5 +101,6 @@ export function buildPropertyGroupTrendsQuery(isTruncated,totalProperties: properties.length,displayedProperties: limitedProperties.length,+ dateRangeLabel,}}