OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
funnels: drop windowFunnel's strict_increase mode
Carl-Gerhard Lindesvärd5e92441fb833
diff --git a/apps/api/src/config/env.ts b/apps/api/src/config/env.tsindex 8cb847bdc..a4cd9280c 100644--- a/apps/api/src/config/env.ts+++ b/apps/api/src/config/env.ts@@ -592,7 +592,6 @@ const rawSchema = z.object({IMPORT_BATCH_SIZE: optionalPositiveInt,INSIGHTS_RETENTION_DAYS: optionalPositiveInt,WIND_DOWN_MAX_PER_RUN: optionalPositiveInt,- FUNNEL_NON_STRICT_ORDERING: trueOrOneSchema,// --- object-store export ---EXPORT_LAG_SECONDS: optionalPositiveInt,@@ -933,7 +932,6 @@ function deriveCoreConfig(raw: RawEnv): CoreConfig {importBatchSize: raw.IMPORT_BATCH_SIZE,insightsRetentionDays: raw.INSIGHTS_RETENTION_DAYS,windDownMaxPerRun: raw.WIND_DOWN_MAX_PER_RUN,- funnelNonStrictOrdering: raw.FUNNEL_NON_STRICT_ORDERING,},objectStoreExport: {lagSeconds: raw.EXPORT_LAG_SECONDS,diff --git a/apps/public/content/docs/self-hosting/environment-variables.mdx b/apps/public/content/docs/self-hosting/environment-variables.mdxindex c0da6a438..860422214 100644--- a/apps/public/content/docs/self-hosting/environment-variables.mdx+++ b/apps/public/content/docs/self-hosting/environment-variables.mdx@@ -1223,19 +1223,6 @@ BOT_BUFFER_BATCH_SIZE=1000## Analytics Behavior-### FUNNEL_NON_STRICT_ORDERING--**Type**: `boolean` (`1` or `true`)-**Required**: No-**Default**: unset (strict ordering)--Funnels use ClickHouse's `windowFunnel` in `strict_increase` mode by default: every step's timestamp must be strictly greater than the previous step's. Setting this variable switches funnels to non-strict ordering (`>=`), where steps sharing the same timestamp still count as an ordered sequence — matching how Mixpanel and Amplitude evaluate funnels. Useful for server-side senders, batched SDKs, or imported data with coarse timestamps.--**Example**:-```bash-FUNNEL_NON_STRICT_ORDERING=1-```-## Performance & Tuning### EVENT_LIST_MAX_LOOKBACK_DAYSdiff --git a/packages/core/src/config.ts b/packages/core/src/config.tsindex 6fb6f0a97..b104d0178 100644--- a/packages/core/src/config.ts+++ b/packages/core/src/config.ts@@ -196,8 +196,6 @@ export interface QueryConfig {importBatchSize: number | undefined;insightsRetentionDays: number | undefined;windDownMaxPerRun: number | undefined;- /** FUNNEL_NON_STRICT_ORDERING: drops `strict_increase` from windowFunnel. */- funnelNonStrictOrdering: boolean;}export interface ObjectStoreExportConfig {diff --git a/packages/core/src/modules/chart/funnel.service.ts b/packages/core/src/modules/chart/funnel.service.tsindex 0b533d1d3..465d526eb 100644--- a/packages/core/src/modules/chart/funnel.service.ts+++ b/packages/core/src/modules/chart/funnel.service.ts@@ -134,7 +134,6 @@ export async function buildFunnelBase(funnelGroup,cohortMetadata,timezone,- config: deps.config,});}diff --git a/packages/core/src/modules/chart/src/funnel.sql.test.ts b/packages/core/src/modules/chart/src/funnel.sql.test.tsindex 16085f1bf..bea3f7832 100644--- a/packages/core/src/modules/chart/src/funnel.sql.test.ts+++ b/packages/core/src/modules/chart/src/funnel.sql.test.ts@@ -22,7 +22,6 @@import { afterAll, beforeAll, describe, expect, it, mock } from 'bun:test';import type { SqlFragment } from '@openpanel/db/src/clickhouse/sql';-import { testCoreConfig } from '../../../../test/config-fixture';import type {IChartBreakdown,IReportInput,@@ -163,19 +162,6 @@ const chartSql = async (breakdowns: IChartBreakdown[]) =>const chartText = async (breakdowns: IChartBreakdown[]) =>render(await chartStatement({ breakdowns })).text;-/** `deps` with FUNNEL_NON_STRICT_ORDERING flipped, as the loader resolves it. */-function withNonStrictOrdering(- nonStrict: boolean-): import('../../../services').ServiceDeps {- // Object.create, not a spread: `testServiceDeps` exposes `clients`/`buffers`- // as throwing getters and a spread would read them.- return Object.assign(Object.create(deps), {- config: testCoreConfig({- query: { ...deps.config.query, funnelNonStrictOrdering: nonStrict },- }),- });-}-beforeAll(async () => {({ ch } = await import('@openpanel/db/src/clickhouse/client'));({ buildFunnelBase } = await import('../funnel.service'));@@ -388,29 +374,11 @@ describe('funnel.sql / buildFunnelBase — breakdown attribution', () => {});describe('funnel.sql / funnel CTE — windowFunnel ordering', () => {- it('defaults to strict_increase (unchanged behavior)', async () => {- expect(await chartSql([])).toContain("'strict_increase'");- });-- it('drops strict_increase when FUNNEL_NON_STRICT_ORDERING is set', async () => {- const base = await buildFunnelBase(- withNonStrictOrdering(true),- baseInput()- );- const sql = render(funnelChartQuery(base)).sql;- expect(sql).not.toContain('strict_increase');+ it('uses the default (>=) ordering: same-millisecond steps still connect', async () => {+ const sql = await chartSql([]);+ expect(sql).not.toContain('strict_');expect(sql).toMatch(/windowFunnel\(\{p:UInt64\}\)\(/);});-- it('non-strict funnel SQL parses and resolves', async () => {- const base = await buildFunnelBase(- withNonStrictOrdering(true),- baseInput()- );- const statement = funnelChartQuery(base);- expect(render(statement).sql).not.toContain('strict_increase');- await explain(statement);- });});describe('funnel.sql / buildFunnelBase — group breakdowns', () => {diff --git a/packages/core/src/modules/chart/src/funnel.sql.ts b/packages/core/src/modules/chart/src/funnel.sql.tsindex 695a91808..398f373f9 100644--- a/packages/core/src/modules/chart/src/funnel.sql.ts+++ b/packages/core/src/modules/chart/src/funnel.sql.ts@@ -23,7 +23,6 @@// no `IN (subquery)` is introduced or removed.import { type SqlFragment, sql } from '@openpanel/db/src/clickhouse/sql';-import type { CoreConfig } from '../../../config';import type {IChartBreakdown,IChartEvent,@@ -55,21 +54,11 @@ export const EMPTY_BREAKDOWN_LABEL = 'Not set';/** The funnel CTE's events alias — `getSelectPropertyKey` needs the same one. */const EVENTS_ALIAS = 'events';-/**- * windowFunnel's 'strict_increase' mode requires every step's timestamp to be- * strictly greater than the previous step's, so same-timestamp sequences- * (server-side senders, batched SDKs, imported data with coarse timestamps)- * never connect. Mixpanel/Amplitude count those as ordered, so deployments- * migrating from them can opt into the default (>=) mode; strict stays the- * default here.- */-const STRICT_INCREASE_MODE = ", 'strict_increase'";--function windowFunnelMode(config: CoreConfig): SqlFragment {- return config.query.funnelNonStrictOrdering- ? sql.empty- : compiledText(STRICT_INCREASE_MODE);-}+// windowFunnel runs in its default (>=) ordering on purpose. 'strict_increase'+// resets step 1 on every matching row and then rejects a later step at the+// same millisecond, so a track() followed by a screen_view in the same ms+// zeroed every step after the first — and server-stamped back-to-back requests+// do collide at ms precision. Same mode as the conversion query.export interface FunnelBaseInput {projectId: string;@@ -86,8 +75,6 @@ export interface FunnelBaseInput {cohortMetadata: Map<string, CohortMetadata>;/** Travels to `session_timezone`; `toDateTime('…')` parses in it. */timezone: string;- /** FUNNEL_NON_STRICT_ORDERING decides `windowFunnel`'s ordering mode. */- config: CoreConfig;}/** Everything the funnel chart and the funnel profile list share. */@@ -288,7 +275,7 @@ export function funnelBase(input: FunnelBaseInput): FunnelBase {const primaryKey = sql.id(group, FUNNEL_GROUPS);const cteSelects: SqlFragment[] = [primaryKey,- sql`windowFunnel(${sql.uint64(input.funnelWindowMilliseconds)}${windowFunnelMode(input.config)})(toUInt64(toUnixTimestamp64Milli(created_at)), ${sql.join(conditions)}) AS level`,+ sql`windowFunnel(${sql.uint64(input.funnelWindowMilliseconds)})(toUInt64(toUnixTimestamp64Milli(created_at)), ${sql.join(conditions)}) AS level`,];if (group === 'session_id') {// Resolves identity changes mid-session.diff --git a/packages/core/test/config-fixture.ts b/packages/core/test/config-fixture.tsindex 8f30614a6..f33917dc6 100644--- a/packages/core/test/config-fixture.ts+++ b/packages/core/test/config-fixture.ts@@ -140,7 +140,6 @@ export function testCoreConfig(importBatchSize: undefined,insightsRetentionDays: undefined,windDownMaxPerRun: undefined,- funnelNonStrictOrdering: false,},objectStoreExport: {lagSeconds: undefined,