: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
feat(workflows): suggest broadcast audiences from the project's events (#119315)
Daniel Marchuk572279bdfa43
diff --git a/frontend/src/lib/constants.tsx b/frontend/src/lib/constants.tsxindex 202f5bc17fc1..7373857c3bf8 100644--- a/frontend/src/lib/constants.tsx+++ b/frontend/src/lib/constants.tsx@@ -615,6 +615,7 @@ export const FEATURE_FLAGS = {WORKFLOW_CLASSIFY_ACTION: 'workflow-classify-action', // owner: #team-workflowsWORKFLOW_OPT_OUT_OF_MESSAGES_ACTION: 'workflow-opt-out-of-messages-action', // owner: #team-workflowsWORKFLOWS_AI_FIRST_NEW: 'workflows-ai-first-new', // owner: @mayteio #team-workflows+ WORKFLOWS_BROADCAST_AUDIENCE_SUGGESTIONS: 'workflows-broadcast-audience-suggestions', // owner: @dmarchuk #team-workflows, AI audience ideas on the Broadcasts tabWORKFLOWS_COHORT_CONDITIONS: 'workflows-cohort-conditions', // owner: @meikelmosby #team-workflowsWORKFLOWS_EMAIL_DOMAIN_AGENT_SETUP: 'workflows-email-domain-agent-setup', // owner: @silthus #team-workflowsWORKFLOWS_EMAIL_DOMAIN_WIZARD: 'workflows-email-domain-wizard', // owner: @silthus #team-workflowsdiff --git a/products/workflows/frontend/Broadcasts/BroadcastsScene.tsx b/products/workflows/frontend/Broadcasts/BroadcastsScene.tsxindex 3bc4c3b29c95..b25c45aa9b4d 100644--- a/products/workflows/frontend/Broadcasts/BroadcastsScene.tsx+++ b/products/workflows/frontend/Broadcasts/BroadcastsScene.tsx@@ -27,6 +27,7 @@ import {import { SendCalendar } from '../SendCalendar/SendCalendar'import { SendCalendarTabLabel } from '../SendCalendar/SendCalendarTabLabel'import { NewWorkflowModal } from '../Workflows/NewWorkflowModal'+import { BroadcastAudienceSuggestions } from './audienceSuggestions/BroadcastAudienceSuggestions'import { BroadcastsFeaturePreview } from './BroadcastsFeaturePreview'import { BroadcastsTable } from './BroadcastsTable'import { newBroadcastAgentLogic } from './newBroadcastAgentLogic'@@ -40,6 +41,7 @@ export function BroadcastsScene(): JSX.Element {const { location } = useValues(router)const { startNewBroadcast } = useActions(newBroadcastAgentLogic)// The tab routes are literal paths, so the tab is the last path segment rather than a route param.+ const showAudienceSuggestions = useFeatureFlag('WORKFLOWS_BROADCAST_AUDIENCE_SUGGESTIONS')const todayRailNav = useFeatureFlag('TODAY_RAIL_NAV')const lastSegment = location.pathname.split('/').pop() as MessagingNavTabKey | 'calendar'const currentTab: MessagingNavTabKey | 'broadcasts' | 'calendar' =@@ -56,6 +58,7 @@ export function BroadcastsScene(): JSX.Element {content: (<><BroadcastsFeaturePreview />+ {showAudienceSuggestions && <BroadcastAudienceSuggestions />}<BroadcastsTable /></>),diff --git a/products/workflows/frontend/Broadcasts/audienceSuggestions/BroadcastAudienceSuggestions.tsx b/products/workflows/frontend/Broadcasts/audienceSuggestions/BroadcastAudienceSuggestions.tsxnew file mode 100644index 000000000000..615c4cfa6b46--- /dev/null+++ b/products/workflows/frontend/Broadcasts/audienceSuggestions/BroadcastAudienceSuggestions.tsx@@ -0,0 +1,108 @@+import { useActions, useValues } from 'kea'++import { IconSparkles } from '@posthog/icons'+import { LemonButton, LemonCard, LemonSkeleton, LemonTag, Spinner, Tooltip } from '@posthog/lemon-ui'++import { TaxonomicFilterGroupType } from 'lib/components/TaxonomicFilter/types'+import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'+import { humanFriendlyNumber } from 'lib/utils/numbers'++import { getFilterLabel } from '~/taxonomy/helpers'+import { AccessControlLevel, AccessControlResourceType } from '~/types'++import type { AudienceSuggestionApi } from '../../generated/api.schemas'+import { broadcastAudienceSuggestionsLogic } from './broadcastAudienceSuggestionsLogic'++const LOADING_CARD_COUNT = 3++function audienceRule(suggestion: AudienceSuggestionApi): JSX.Element {+ // PostHog's own events show their label, like "Pageview", rather than "$pageview".+ const label = (name: string): string => getFilterLabel(name, TaxonomicFilterGroupType.Events)+ const event = <code className="text-primary">{label(suggestion.event)}</code>+ const days = `${suggestion.window_days} days`+ switch (suggestion.kind) {+ case 'not_yet':+ return (+ <>+ Did {event} in the last {days}, but not{' '}+ <code className="text-primary">{label(suggestion.missing_event ?? '')}</code>+ </>+ )+ case 'regulars':+ return (+ <>+ Did {event} {suggestion.min_count}+ times in the last {days}+ </>+ )+ case 'lapsed':+ return (+ <>+ Did {event} in the last 90 days, but not in the last {days}+ </>+ )+ }+}++export function BroadcastAudienceSuggestions(): JSX.Element | null {+ const { suggestions, generating, creatingId } = useValues(broadcastAudienceSuggestionsLogic)+ const { pickSuggestion } = useActions(broadcastAudienceSuggestionsLogic)+ const editorDisabledReason = getAccessControlDisabledReason(+ AccessControlResourceType.Workflow,+ AccessControlLevel.Editor+ )++ // Suggestions are optional: a failed or empty run shows nothing.+ if (!generating && suggestions.length === 0) {+ return null+ }++ return (+ <div className="flex flex-col gap-2 mb-4" data-attr="broadcast-audience-suggestions">+ <div className="flex items-center gap-2">+ {generating ? <Spinner className="size-4" /> : <IconSparkles className="size-4" />}+ <h3 className="mb-0 text-sm font-semibold">Audiences you could send to</h3>+ <Tooltip title="AI suggests these from your project's events. Check the audience and the email before you send.">+ <LemonTag type="completion" size="small">+ Beta+ </LemonTag>+ </Tooltip>+ </div>+ <div className="grid grid-cols-[repeat(auto-fill,minmax(16rem,1fr))] gap-2">+ {generating+ ? Array.from({ length: LOADING_CARD_COUNT }, (_, index) => (+ <LemonSkeleton key={index} className="h-44 rounded" />+ ))+ : suggestions.map((suggestion) => (+ <LemonCard key={suggestion.id} hoverEffect={false} className="flex flex-col gap-2 p-4">+ <div className="flex flex-col gap-1">+ <h4 className="mb-0 text-base font-semibold">{suggestion.name}</h4>+ <span className="text-xs text-secondary">+ <span translate="no">{humanFriendlyNumber(suggestion.reachable_count)}</span>{' '}+ {suggestion.reachable_count === 1 ? 'person' : 'people'} with an email+ </span>+ </div>+ <p className="mb-0 text-sm text-secondary">{suggestion.reason}</p>+ <p className="mb-0 text-xs text-tertiary">{audienceRule(suggestion)}</p>+ <div className="mt-auto flex pt-1">+ <LemonButton+ type="secondary"+ size="small"+ loading={creatingId === suggestion.id}+ disabledReason={+ editorDisabledReason ??+ (creatingId && creatingId !== suggestion.id+ ? 'Another audience is being created'+ : undefined)+ }+ onClick={() => pickSuggestion(suggestion)}+ data-attr="broadcast-audience-suggestion-use"+ >+ Write a broadcast+ </LemonButton>+ </div>+ </LemonCard>+ ))}+ </div>+ </div>+ )+}diff --git a/products/workflows/frontend/Broadcasts/audienceSuggestions/broadcastAudienceSuggestionsLogic.test.ts b/products/workflows/frontend/Broadcasts/audienceSuggestions/broadcastAudienceSuggestionsLogic.test.tsnew file mode 100644index 000000000000..3d9f1a6eb5ad--- /dev/null+++ b/products/workflows/frontend/Broadcasts/audienceSuggestions/broadcastAudienceSuggestionsLogic.test.ts@@ -0,0 +1,108 @@+import { MOCK_DEFAULT_USER } from 'lib/api.mock'++import { router } from 'kea-router'+import { expectLogic } from 'kea-test-utils'++import { initKeaTests } from '~/test/init'++import { cohortsCreate, cohortsList } from 'products/cohorts/frontend/generated/api'++import { broadcastAudienceSuggestionsGenerate } from '../../generated/api'+import type { AudienceSuggestionApi } from '../../generated/api.schemas'+import { AUDIENCE_PREFILL_PARAM } from '../broadcastAudiencePrefill'+import { broadcastAudienceSuggestionsLogic } from './broadcastAudienceSuggestionsLogic'++jest.mock('../../generated/api', () => ({+ broadcastAudienceSuggestionsCurrent: jest.fn(),+ broadcastAudienceSuggestionsGenerate: jest.fn(),+}))+jest.mock('products/cohorts/frontend/generated/api', () => ({+ cohortsCreate: jest.fn(),+ cohortsList: jest.fn(),+}))++const conditions = [+ {+ key: 'signed_up',+ type: 'behavioral',+ value: 'performed_event_multiple',+ event_type: 'events',+ operator: 'gte',+ operator_value: 1,+ time_value: 14,+ time_interval: 'day',+ },+ { key: 'email', type: 'person', operator: 'icontains', value: '@' },+]+const filtersWith = (values: Record<string, any>[]): Record<string, any> => ({+ properties: { type: 'AND', values: [{ type: 'AND', values }] },+})++const suggestion: AudienceSuggestionApi = {+ id: 'suggestion-1',+ name: 'Signed up, no project yet',+ description: 'People who signed up but never created a project.',+ reason: 'A nudge helps them start.',+ kind: 'not_yet' as AudienceSuggestionApi['kind'],+ event: 'signed_up',+ missing_event: 'project_created',+ window_days: 14,+ min_count: null,+ reachable_count: 34,+ cohort_filters: filtersWith(conditions),+}+// What the cohort API stores for the same conditions: defaults filled in and bytecode compiled.+const savedFilters = filtersWith(+ conditions.map((condition) => ({ ...condition, negation: false, bytecode: ['_H', 1], conditionHash: 'abc123' }))+)++describe('broadcastAudienceSuggestionsLogic', () => {+ beforeEach(() => {+ jest.mocked(broadcastAudienceSuggestionsGenerate).mockResolvedValue({+ status: 'ready',+ suggestions: [suggestion],+ })+ jest.mocked(cohortsCreate).mockResolvedValue({ id: 41, name: suggestion.name } as any)+ initKeaTests()+ })++ const minutesAgo = (minutes: number): string => new Date(Date.now() - minutes * 60_000).toISOString()+ const snapshot = (overrides: Record<string, any> = {}): Record<string, any> => ({+ id: 7,+ name: suggestion.name,+ description: suggestion.description,+ deleted: false,+ is_static: true,+ errors_calculating: 0,+ created_by: { uuid: MOCK_DEFAULT_USER.uuid },+ created_at: minutesAgo(10),+ filters: suggestion.cohort_filters,+ ...overrides,+ })++ it.each([+ ['creates a cohort for a new idea', [], 41, 1],+ ["reuses the user's own fresh snapshot of the idea", [snapshot()], 7, 0],+ ['reuses a snapshot whose filters the server filled in', [snapshot({ filters: savedFilters })], 7, 0],+ ['ignores a deleted cohort with the same name', [snapshot({ deleted: true })], 41, 1],+ ['ignores a snapshot whose filters were edited', [snapshot({ filters: { properties: {} } })], 41, 1],+ ['ignores a snapshot over an hour old', [snapshot({ created_at: minutesAgo(61) })], 41, 1],+ ["ignores another user's snapshot", [snapshot({ created_by: { uuid: 'someone-else' } })], 41, 1],+ ['ignores a dynamic cohort with the same name', [snapshot({ is_static: false })], 41, 1],+ ])('%s', async (_name, existing, cohortId, creates) => {+ jest.mocked(cohortsList).mockResolvedValue({ results: existing } as any)+ const logic = broadcastAudienceSuggestionsLogic()+ logic.mount()++ await expectLogic(logic, () => logic.actions.pickSuggestion(suggestion)).toFinishAllListeners()++ expect(cohortsCreate).toHaveBeenCalledTimes(creates)+ if (creates) {+ // A dynamic cohort with event conditions can't be a broadcast audience, so the idea is a static snapshot.+ expect(jest.mocked(cohortsCreate).mock.calls[0][1]).toMatchObject({ is_static: true })+ }+ const audience = JSON.parse(router.values.searchParams[AUDIENCE_PREFILL_PARAM])+ expect(audience).toEqual([expect.objectContaining({ type: 'cohort', value: cohortId })])+ expect(logic.values.creatingId).toBeNull()+ })+})diff --git a/products/workflows/frontend/Broadcasts/audienceSuggestions/broadcastAudienceSuggestionsLogic.ts b/products/workflows/frontend/Broadcasts/audienceSuggestions/broadcastAudienceSuggestionsLogic.tsnew file mode 100644index 000000000000..fe3932b40535--- /dev/null+++ b/products/workflows/frontend/Broadcasts/audienceSuggestions/broadcastAudienceSuggestionsLogic.ts@@ -0,0 +1,322 @@+import {+ MakeLogicType,+ actions,+ afterMount,+ connect,+ isBreakpoint,+ kea,+ listeners,+ path,+ reducers,+ selectors,+} from 'kea'+import { loaders } from 'kea-loaders'+import { router } from 'kea-router'+import posthog from 'posthog-js'++import { lemonToast } from '@posthog/lemon-ui'++import { objectsEqual } from 'lib/utils/objects'+import { projectLogic } from 'scenes/projectLogic'+import { userLogic } from 'scenes/userLogic'++import { PropertyFilterType, PropertyOperator } from '~/types'++import { cohortsCreate, cohortsList } from 'products/cohorts/frontend/generated/api'++import type { UserType } from '../../../../../frontend/src/types'+import { broadcastAudienceSuggestionsCurrent, broadcastAudienceSuggestionsGenerate } from '../../generated/api'+import type { AudienceSuggestionApi, AudienceSuggestionsResponseApi } from '../../generated/api.schemas'+import { urlForNewBroadcastWithAudience } from '../broadcastAudiencePrefill'++const POLL_INTERVAL_MS = 2000+const POLL_DEADLINE_MS = 5 * 60 * 1000+const REQUEST_TIMEOUT_MS = 10_000+// A snapshot older than this may hold people who no longer match the idea.+const REUSABLE_COHORT_MAX_AGE_MS = 60 * 60 * 1000+const CREATE_FAILED_MESSAGE = "We couldn't create this audience. Try again."+// pinned: source value - the broadcast launch event reports it+export const AUDIENCE_SUGGESTION_SOURCE = 'audience_suggestion'+const COMPUTED_FILTER_KEYS = new Set(['bytecode', 'bytecode_error', 'conditionHash'])+const NEGATABLE_FILTER_TYPES = new Set(['behavioral', 'cohort', 'person', 'person_metadata'])++// Saving drops nulls, fills `negation` and compiles bytecode, so an unchanged snapshot only matches in this form.+function canonicalCohortCriteria(node: unknown): unknown {+ if (Array.isArray(node)) {+ return node.map(canonicalCohortCriteria)+ }+ if (node === null || typeof node !== 'object') {+ return node+ }+ const canonical: Record<string, unknown> = {}+ for (const [key, value] of Object.entries(node)) {+ if (value !== null && value !== undefined && !COMPUTED_FILTER_KEYS.has(key)) {+ canonical[key] = canonicalCohortCriteria(value)+ }+ }+ if (+ typeof canonical.type === 'string' &&+ NEGATABLE_FILTER_TYPES.has(canonical.type) &&+ !('negation' in canonical)+ ) {+ canonical.negation = false+ }+ return canonical+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface broadcastAudienceSuggestionsLogicValues {+ currentProjectId: number | null // projectLogic+ user: UserType | null // userLogic+ creatingId: string | null+ generating: boolean+ pollingTimedOut: boolean+ result: AudienceSuggestionsResponseApi | null+ resultLoading: boolean+ suggestions: AudienceSuggestionApi[]+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface broadcastAudienceSuggestionsLogicActions {+ giveUpPolling: () => {+ value: true+ }+ loadResult: () => any+ loadResultFailure: (+ error: string,+ errorObject?: any+ ) => {+ error: string+ errorObject?: any+ }+ loadResultSuccess: (+ result: AudienceSuggestionsResponseApi | null,+ payload?: any+ ) => {+ result: AudienceSuggestionsResponseApi | null+ payload?: any+ }+ pickSuggestion: (suggestion: AudienceSuggestionApi) => {+ suggestion: AudienceSuggestionApi+ }+ pollResult: () => any+ pollResultFailure: (+ error: string,+ errorObject?: any+ ) => {+ error: string+ errorObject?: any+ }+ pollResultSuccess: (+ result: AudienceSuggestionsResponseApi | null,+ payload?: any+ ) => {+ result: AudienceSuggestionsResponseApi | null+ payload?: any+ }+ setCreatingId: (id: string | null) => {+ id: string | null+ }+}++// Generated by kea-typegen. Update if you're an agent, ignore if you're human.+export interface broadcastAudienceSuggestionsLogicMeta {+ __keaTypeGenInternalSelectorTypes: {+ suggestions: (result: AudienceSuggestionsResponseApi | null) => AudienceSuggestionApi[]+ generating: (+ result: AudienceSuggestionsResponseApi | null,+ resultLoading: boolean,+ pollingTimedOut: boolean+ ) => boolean+ }+}++export type broadcastAudienceSuggestionsLogicType = MakeLogicType<+ broadcastAudienceSuggestionsLogicValues,+ broadcastAudienceSuggestionsLogicActions,+ Record<string, any>,+ broadcastAudienceSuggestionsLogicMeta+>++export const broadcastAudienceSuggestionsLogic = kea<broadcastAudienceSuggestionsLogicType>([+ path([+ 'products',+ 'workflows',+ 'frontend',+ 'Broadcasts',+ 'audienceSuggestions',+ 'broadcastAudienceSuggestionsLogic',+ ]),+ connect(() => ({+ values: [projectLogic, ['currentProjectId'], userLogic, ['user']],+ })),+ actions({+ giveUpPolling: true,+ pickSuggestion: (suggestion: AudienceSuggestionApi) => ({ suggestion }),+ setCreatingId: (id: string | null) => ({ id }),+ }),+ reducers({+ creatingId: [+ null as string | null,+ {+ setCreatingId: (_, { id }) => id,+ },+ ],+ pollingTimedOut: [+ false,+ {+ giveUpPolling: () => true,+ },+ ],+ }),+ loaders(({ values }) => ({+ result: [+ null as AudienceSuggestionsResponseApi | null,+ {+ // Returns the cached cards, or starts a background run and answers with status generating.+ loadResult: async (): Promise<AudienceSuggestionsResponseApi | null> => {+ try {+ return await broadcastAudienceSuggestionsGenerate(+ String(values.currentProjectId),+ {},+ { signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) }+ )+ } catch {+ // Suggestions are optional, so a failed call shows nothing rather than an error.+ return null+ }+ },+ pollResult: async (): Promise<AudienceSuggestionsResponseApi | null> => {+ try {+ return await broadcastAudienceSuggestionsCurrent(String(values.currentProjectId), {+ signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),+ })+ } catch {+ return values.result+ }+ },+ },+ ],+ })),+ selectors({+ suggestions: [+ (s) => [s.result],+ (result: AudienceSuggestionsResponseApi | null): AudienceSuggestionApi[] =>+ result?.status === 'ready' ? result.suggestions : [],+ ],+ generating: [+ (s) => [s.result, s.resultLoading, s.pollingTimedOut],+ (result: AudienceSuggestionsResponseApi | null, loading: boolean, timedOut: boolean): boolean =>+ !timedOut && (loading || result?.status === 'generating'),+ ],+ }),+ listeners(({ actions, values, cache }) => {+ const handleResult = (result: AudienceSuggestionsResponseApi | null): void => {+ cache.disposables.dispose('poll')+ if (result?.status === 'generating') {+ if (Date.now() > cache.pollDeadline) {+ actions.giveUpPolling()+ return+ }+ cache.disposables.add(() => {+ const timer = window.setTimeout(() => actions.pollResult(), POLL_INTERVAL_MS)+ return () => window.clearTimeout(timer)+ }, 'poll')+ return+ }+ if (result?.status === 'ready' && result.suggestions.length > 0) {+ // pinned: analytics event name - renaming breaks dashboards+ posthog.capture('broadcast audience suggestions shown', {+ kinds: result.suggestions.map((suggestion) => suggestion.kind),+ reachable_counts: result.suggestions.map((suggestion) => suggestion.reachable_count),+ })+ }+ }+ return {+ loadResultSuccess: ({ result }) => {+ cache.pollDeadline = Date.now() + POLL_DEADLINE_MS+ handleResult(result)+ },+ pollResultSuccess: ({ result }) => handleResult(result),+ pickSuggestion: async ({ suggestion }, breakpoint) => {+ if (values.creatingId) {+ return+ }+ actions.setCreatingId(suggestion.id)+ // pinned: analytics event name - renaming breaks dashboards+ posthog.capture('broadcast audience suggestion clicked', {+ kind: suggestion.kind,+ reachable_count: suggestion.reachable_count,+ })+ try {+ const projectId = String(values.currentProjectId)+ // A second click on the same idea reuses its fresh snapshot rather than adding a copy to the cohorts list.+ const existing = (+ await cohortsList(+ projectId,+ { search: suggestion.name, limit: 20 },+ { signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) }+ )+ ).results.find(+ (item) =>+ !item.deleted &&+ item.is_static === true &&+ item.errors_calculating === 0 &&+ item.created_by?.uuid === values.user?.uuid &&+ item.name === suggestion.name &&+ !!item.created_at &&+ Date.now() - new Date(item.created_at).getTime() < REUSABLE_COHORT_MAX_AGE_MS &&+ objectsEqual(+ canonicalCohortCriteria(item.filters),+ canonicalCohortCriteria(suggestion.cohort_filters)+ )+ )+ breakpoint()+ // Broadcasts can't send to a dynamic cohort with event conditions, so the idea is saved as a+ // static snapshot that the server fills once from the same filters.+ const cohort =+ existing ??+ (await cohortsCreate(+ projectId,+ {+ name: suggestion.name,+ description: suggestion.description,+ filters: suggestion.cohort_filters,+ is_static: true,+ } as Parameters<typeof cohortsCreate>[1],+ { signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) }+ ))+ breakpoint()+ router.actions.push(+ urlForNewBroadcastWithAudience({+ properties: [+ {+ key: 'id',+ type: PropertyFilterType.Cohort,+ operator: PropertyOperator.In,+ value: cohort.id,+ cohort_name: cohort.name ?? suggestion.name,+ },+ ],+ name: suggestion.name,+ source: AUDIENCE_SUGGESTION_SOURCE,+ draftSource: { source: 'cohort', source_id: String(cohort.id) },+ })+ )+ } catch (error: any) {+ if (isBreakpoint(error)) {+ throw error+ }+ lemonToast.error(error?.detail || CREATE_FAILED_MESSAGE)+ } finally {+ if (!cache.disposables.isDisposed) {+ actions.setCreatingId(null)+ }+ }+ },+ }+ }),+ afterMount(({ actions }) => {+ actions.loadResult()+ }),+])