Skip to content

: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.

feat(marketing-analytics): let conversion goals be flagged as revenue or customer (#83287)

Javier Bahamondesdd571e4f3e5e
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 136a3fd95d60..5f7982802ea0 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -7613,9 +7613,9 @@ snapshots:
scenes-app-settings-environment--settings-environment-integrations--light:
hash: v1.k794b7964.f249ac0b4d1984c3d0259394fef46e89186d039a4089c7d2acb19922e1d939d5.T0FKR0TfOB2Flt5kqA4HboFgQlUHjdI92AdocREcaoQ
scenes-app-settings-environment--settings-environment-marketing-analytics--dark:
- hash: v1.k794b7964.72d483d5eeffbd82927b8b748abb1f6a624ed157f86db39b7c231b26e1c0a574.DdQZfV_I433UB3bh4bmxnivledUaDRg0EVOIWqWfTOQ
+ hash: v1.k794b7964.0c2038ca1ba04301309d89f6abed6353c1cd1cc948342a960ecfcce3a9619a48.K04BuaEcGkJJ30IFKapU7MOOl-6jjagnB0ZddGTrkaU
scenes-app-settings-environment--settings-environment-marketing-analytics--light:
- hash: v1.k794b7964.e6cb6106f70e446ecd3094a56965f08b14c51f8e28960cc51c219ce97c8d9398.wFhZy2LcIz1kyik9no1KGftANILnyQ-k7g8612peWGA
+ hash: v1.k794b7964.dfd28876bac00e6bb2aa3ab49bcd2c9e70278cda4faa5eb3670e3b381ed2302a.1AcmLKo0J4iBvrfykKx0r-bmrYe1VsQ8w2tPLai2D8Y
scenes-app-settings-environment--settings-environment-max--dark:
hash: v1.k794b7964.5fba71e345f66c96bd52f80c2d78ee18a79f717cfc32188057c677cb0837b79b.BK_CfpOtoMIJqBf8dKx9h8mKmik2W-B9npTPyW3-nSM
scenes-app-settings-environment--settings-environment-max--light:
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/ConversionGoalsConfiguration.tsx b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/ConversionGoalsConfiguration.tsx
index 7fe57b065234..10187340b742 100644
--- a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/ConversionGoalsConfiguration.tsx
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/ConversionGoalsConfiguration.tsx
@@ -6,6 +6,7 @@ import { LemonButton, LemonInput } from '@posthog/lemon-ui'
import { RestrictionScope, useRestrictedArea } from 'lib/components/RestrictedArea'
import { TeamMembershipLevel } from 'lib/constants'
+import { LemonCheckbox } from 'lib/lemon-ui/LemonCheckbox'
import { LemonTable } from 'lib/lemon-ui/LemonTable'
import { uuid } from 'lib/utils/dom'
import { QUERY_TYPES_METADATA } from 'scenes/saved-insights/SavedInsights'
@@ -25,6 +26,7 @@ import {
defaultConversionGoalFilter,
getConfiguredConversionGoalsLabel,
} from './constants'
+import { revenueDisabledReason, withValidFlags } from './conversionGoalFlags'
interface ConversionGoalFormState {
filter: ConversionGoalFilter
@@ -36,6 +38,35 @@ const createEmptyFormState = (): ConversionGoalFormState => ({
name: '',
})
+function CountsAsToggles({
+ goal,
+ onChange,
+ disabledReason,
+}: {
+ goal: ConversionGoalFilter
+ onChange: (goal: ConversionGoalFilter) => void
+ disabledReason?: string | null
+}): JSX.Element {
+ return (
+ <div className="flex flex-col gap-1">
+ <LemonCheckbox
+ size="small"
+ label="Revenue"
+ checked={!!goal.counts_as_revenue}
+ onChange={(counts_as_revenue) => onChange({ ...goal, counts_as_revenue })}
+ disabledReason={disabledReason || revenueDisabledReason(goal)}
+ />
+ <LemonCheckbox
+ size="small"
+ label="Customer"
+ checked={!!goal.counts_as_customer}
+ onChange={(counts_as_customer) => onChange({ ...goal, counts_as_customer })}
+ disabledReason={disabledReason}
+ />
+ </div>
+ )
+}
+
export function ConversionGoalsConfiguration({
hideTitle = false,
hideDescription = false,
@@ -60,11 +91,11 @@ export function ConversionGoalsConfiguration({
if (conversionGoalName === '') {
conversionGoalName = formState.filter.custom_name || formState.filter.name || 'No name'
}
- const newGoal: ConversionGoalFilter = {
+ const newGoal: ConversionGoalFilter = withValidFlags({
...formState.filter,
conversion_goal_id: formState.filter.conversion_goal_id || uuid(),
conversion_goal_name: conversionGoalName,
- }
+ })
addOrUpdateConversionGoal(newGoal)
setFormState(createEmptyFormState())
@@ -77,7 +108,7 @@ export function ConversionGoalsConfiguration({
const handleSaveEdit = (): void => {
if (editingGoal) {
- addOrUpdateConversionGoal(editingGoal)
+ addOrUpdateConversionGoal(withValidFlags(editingGoal))
}
setEditingGoalId(null)
setEditingGoal(null)
@@ -134,6 +165,12 @@ export function ConversionGoalsConfiguration({
/>
</div>
+ <CountsAsToggles
+ goal={formState.filter}
+ onChange={(filter) => setFormState((prev) => ({ ...prev, filter }))}
+ disabledReason={restrictedReason}
+ />
+
<div className="flex gap-2">
<LemonButton
type="primary"
@@ -220,6 +257,30 @@ export function ConversionGoalsConfiguration({
return goal.custom_name || goal.name || 'No name'
},
},
+ {
+ key: 'counts_as',
+ title: 'Counts as',
+ render: (_, goal: ConversionGoalFilter) => {
+ if (editingGoalId === goal.conversion_goal_id && editingGoal) {
+ return (
+ <CountsAsToggles
+ goal={editingGoal}
+ onChange={setEditingGoal}
+ disabledReason={restrictedReason}
+ />
+ )
+ }
+ const flags = [
+ goal.counts_as_revenue && 'Revenue',
+ goal.counts_as_customer && 'Customer',
+ ].filter(Boolean)
+ return flags.length ? (
+ <span className="text-xs">{flags.join(', ')}</span>
+ ) : (
+ <span className="text-xs text-muted">Conversions only</span>
+ )
+ },
+ },
{
key: 'schema',
title: 'Schema mapping',
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/conversionGoalFlags.test.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/conversionGoalFlags.test.ts
new file mode 100644
index 000000000000..5a220a0e8923
--- /dev/null
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/conversionGoalFlags.test.ts
@@ -0,0 +1,52 @@
+import { ConversionGoalFilter, NodeKind } from '~/queries/schema/schema-general'
+import { BaseMathType, PropertyMathType } from '~/types'
+
+import { revenueDisabledReason, withValidFlags } from './conversionGoalFlags'
+
+const goal = (overrides: Partial<ConversionGoalFilter> = {}): ConversionGoalFilter =>
+ ({
+ kind: NodeKind.EventsNode,
+ event: 'purchase',
+ conversion_goal_id: 'goal-1',
+ conversion_goal_name: 'Purchase',
+ schema_map: {},
+ ...overrides,
+ }) as ConversionGoalFilter
+
+describe('revenueDisabledReason', () => {
+ it('allows the flag once the goal sums a property', () => {
+ expect(revenueDisabledReason(goal({ math: PropertyMathType.Sum, math_property: 'revenue' }))).toBeNull()
+ })
+
+ it('blocks counting math, which would divide spend by a conversion count', () => {
+ expect(revenueDisabledReason(goal({ math: BaseMathType.TotalCount }))).toContain('Sum')
+ })
+
+ it('blocks sum math with no property, which sums nothing', () => {
+ expect(revenueDisabledReason(goal({ math: PropertyMathType.Sum }))).toContain('property')
+ })
+})
+
+describe('withValidFlags', () => {
+ it('clears counts_as_revenue when math no longer qualifies', () => {
+ const cleared = withValidFlags(goal({ math: BaseMathType.TotalCount, counts_as_revenue: true }))
+ expect(cleared.counts_as_revenue).toBe(false)
+ })
+
+ it('leaves counts_as_customer alone, which has no math requirement', () => {
+ const kept = withValidFlags(goal({ math: BaseMathType.TotalCount, counts_as_customer: true }))
+ expect(kept.counts_as_customer).toBe(true)
+ })
+
+ it('keeps a valid revenue goal untouched', () => {
+ const valid = goal({ math: PropertyMathType.Sum, math_property: 'revenue', counts_as_revenue: true })
+ expect(withValidFlags(valid)).toBe(valid)
+ })
+
+ it('returns the same reference when the flag is already off', () => {
+ // An unflagged goal with counting math has nothing to clear, so rebuilding it
+ // would break identity for every caller comparing by reference.
+ const unflagged = goal({ math: BaseMathType.TotalCount })
+ expect(withValidFlags(unflagged)).toBe(unflagged)
+ })
+})
diff --git a/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/conversionGoalFlags.ts b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/conversionGoalFlags.ts
new file mode 100644
index 000000000000..718379343750
--- /dev/null
+++ b/frontend/src/scenes/web-analytics/tabs/marketing-analytics/frontend/components/settings/conversionGoalFlags.ts
@@ -0,0 +1,20 @@
+import { ConversionGoalFilter } from '~/queries/schema/schema-general'
+import { PropertyMathType } from '~/types'
+
+// Mirrors `_revenue_goal_error` on the write path. The flag on its own changes nothing: the query
+// only sums when math is a sum type, and sums nothing without a property, so a goal flagged here
+// would feed ROAS a conversion count or a row of zeros.
+export const revenueDisabledReason = (goal: ConversionGoalFilter): string | null => {
+ if (goal.math !== PropertyMathType.Sum) {
+ return 'Set this goal\'s math to "Sum" first, so the amount is totalled rather than counted.'
+ }
+ if (!goal.math_property) {
+ return 'Choose the property holding the amount first.'
+ }
+ return null
+}
+
+// Changing math can invalidate a flag that was legal when it was set. Returns the same
+// reference when there's nothing to clear, so callers can compare by identity.
+export const withValidFlags = (goal: ConversionGoalFilter): ConversionGoalFilter =>
+ goal.counts_as_revenue && revenueDisabledReason(goal) ? { ...goal, counts_as_revenue: false } : goal