: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(cohorts): offer to email a cohort from the toast after creating it (#119893)
Daniel Marchuk8045da0792a7
diff --git a/frontend/src/scenes/cohorts/cohortEditLogic.test.ts b/frontend/src/scenes/cohorts/cohortEditLogic.test.tsindex 8b1325be34ed..da8fc3b05fd6 100644--- a/frontend/src/scenes/cohorts/cohortEditLogic.test.ts+++ b/frontend/src/scenes/cohorts/cohortEditLogic.test.ts@@ -22,6 +22,9 @@ import { ActorsQuery, DataTableNode, NodeKind } from '~/queries/schema/schema-geimport { initKeaTests } from '~/test/init'import { mockCohort } from '~/test/mocks'import {+ AccessControlLevel,+ AccessControlResourceType,+ AppContext,BehavioralEventType,BehavioralLifecycleType,CohortCriteriaGroupFilter,@@ -564,6 +567,10 @@ describe('cohortEditLogic', () => {.toDispatchActions(['setCohort', 'submitCohort', 'submitCohortSuccess', 'saveCohortSuccess']).toNotHaveDispatchedActions(['loadUsedIn'])expect(api.update).toHaveBeenCalledTimes(1)+ expect(lemonToast.success).toHaveBeenCalledWith(+ expect.stringContaining('Cohort saved'),+ expect.objectContaining({ button: undefined })+ )})it('do not save with invalid name', async () => {@@ -1040,6 +1047,13 @@ describe('cohortEditLogic', () => {})it('can create static cohort from criteria without csv', async () => {+ window.POSTHOG_APP_CONTEXT = {+ ...window.POSTHOG_APP_CONTEXT,+ resource_access_control: {+ ...window.POSTHOG_APP_CONTEXT?.resource_access_control,+ [AccessControlResourceType.Workflow]: AccessControlLevel.Editor,+ },+ } as AppContextawait initCohortLogic({ id: 'new' })const createdCohort = {...mockCohort,@@ -1093,12 +1107,65 @@ describe('cohortEditLogic', () => {expect(createPayload.get('is_static')).toEqual('true')expect(createPayload.get('filters')).toContain('"values"')expect(createPayload.get('filters')).not.toContain('"properties":{}')+ expect(lemonToast.success).toHaveBeenCalledWith(+ expect.stringContaining('Cohort saved'),+ expect.objectContaining({ button: expect.objectContaining({ label: 'Email this cohort' }) })+ )// Let the post-submit refreshPersonsData debounce finish before the test endsawait expectLogic(logic).toFinishAllListeners()setTimeoutSpy.mockRestore()})+ it('offers no email button for a new cohort a broadcast cannot send to', async () => {+ window.POSTHOG_APP_CONTEXT = {+ ...window.POSTHOG_APP_CONTEXT,+ resource_access_control: {+ ...window.POSTHOG_APP_CONTEXT?.resource_access_control,+ [AccessControlResourceType.Workflow]: AccessControlLevel.Editor,+ },+ } as AppContext+ await initCohortLogic({ id: 'new' })+ const behavioralFilters = {+ properties: {+ ...mockCohort.filters.properties,+ values: [+ {+ id: '70427',+ type: FilterLogicalOperator.Or,+ values: [+ {+ type: BehavioralFilterKey.Behavioral,+ value: BehavioralEventType.PerformEvent,+ event_type: TaxonomicFilterGroupType.Events,+ time_value: 30,+ time_interval: TimeUnitType.Day,+ key: 'dashboard date range changed',+ },+ ],+ },+ ],+ },+ }+ jest.spyOn(api.cohorts, 'create').mockResolvedValue({+ ...mockCohort,+ id: 3,+ is_static: false,+ filters: behavioralFilters,+ })++ await expectLogic(logic, async () => {+ logic.actions.setCohort({ ...mockCohort, id: 'new', is_static: false, filters: behavioralFilters })+ logic.actions.submitCohort()+ }).toDispatchActions(['setCohort', 'submitCohort', 'submitCohortSuccess'])++ expect(lemonToast.success).toHaveBeenCalledWith(+ expect.stringContaining('Cohort saved'),+ expect.objectContaining({ button: undefined })+ )+ await expectLogic(logic).toFinishAllListeners()+ })+it('do not save static cohort with empty csv', async () => {await initCohortLogic({ id: 'new' })await expectLogic(logic, async () => {diff --git a/frontend/src/scenes/cohorts/cohortEditLogic.ts b/frontend/src/scenes/cohorts/cohortEditLogic.tsindex 72c7df93e428..271eb31032f2 100644--- a/frontend/src/scenes/cohorts/cohortEditLogic.ts+++ b/frontend/src/scenes/cohorts/cohortEditLogic.ts@@ -35,8 +35,10 @@ import { COHORT_USED_BY_WORKFLOWS_ERROR_CODE, loadBlockingWorkflows } from 'scenimport { cohortBlockingWorkflowsModalLogic } from 'scenes/cohorts/cohortBlockingWorkflowsModalLogic'import { NEW_COHORT, NEW_CRITERIA, NEW_CRITERIA_GROUP } from 'scenes/cohorts/CohortFilters/constants'import { BehavioralFilterKey } from 'scenes/cohorts/CohortFilters/types'+import { emailCohortToastButton } from 'scenes/cohorts/cohortSavedToast'import {applyAllCriteriaGroup,+ cohortBroadcastDisabledReason,applyAllNestedCriteria,cleanCriteria,createCohortDataNodeLogicKey,@@ -1021,6 +1023,14 @@ export const cohortEditLogic = kea<cohortEditLogicType>([refreshTreeItem('cohort', cohort.id)lemonToast.success('Cohort saved. Please wait up to a few minutes for it to be calculated', {toastId: `cohort-saved-${key}`,+ // A cohort the broadcast launch would reject, such as one with event conditions, gets no button.+ button:+ existingCohort.id === 'new' && !cohortBroadcastDisabledReason(cohort)+ ? emailCohortToastButton(+ { id: cohort.id as number, name: cohort.name },+ 'cohort_created'+ )+ : undefined,})actions.checkIfFinishedCalculating(cohort)if (existingCohort.id === 'new') {diff --git a/frontend/src/scenes/cohorts/cohortSavedToast.ts b/frontend/src/scenes/cohorts/cohortSavedToast.tsindex 2f66ab6728fd..fbc181f44c06 100644--- a/frontend/src/scenes/cohorts/cohortSavedToast.ts+++ b/frontend/src/scenes/cohorts/cohortSavedToast.ts@@ -1,6 +1,6 @@import { router } from 'kea-router'-import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'+import { ToastButton, lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'import { urls } from 'scenes/urls'import { CohortType } from '~/types'@@ -12,6 +12,31 @@ import {} from 'products/workflows/frontend/MessageAudience/messageAudience'import { openMessageAudience } from 'products/workflows/frontend/MessageAudience/messageAudienceWarnings'+/** Opens a broadcast for the cohort, or nothing when the user cannot use workflows. */+export function emailCohortToastButton(+ cohort: { id: number; name?: string | null },+ source: string+): ToastButton | undefined {+ if (messageAudienceAccessDisabledReason()) {+ return undefined+ }+ return {+ label: 'Email this cohort',+ dataAttr: `message-audience-${source}-broadcast`,+ action: () => {+ captureMessageAudienceClicked(source, 'broadcast')+ void openMessageAudience(+ {+ properties: cohortAudienceProperties(cohort),+ source,+ draftSource: { source: 'cohort', source_id: String(cohort.id) },+ },+ 'broadcast'+ )+ },+ }+}+/** Confirms a static cohort saved from a list of people, with links to the cohort and to a broadcast for it. */export function cohortSavedToast(cohort: Pick<CohortType, 'id' | 'name'>, source: string, toastId?: string): void {const savedCohort = { id: cohort.id as number, name: cohort.name }@@ -21,22 +46,6 @@ export function cohortSavedToast(cohort: Pick<CohortType, 'id' | 'name'>, sourcelabel: 'View cohort',action: () => router.actions.push(urls.cohort(savedCohort.id)),},- secondaryButton: messageAudienceAccessDisabledReason()- ? undefined- : {- label: 'Email this cohort',- dataAttr: `message-audience-${source}-broadcast`,- action: () => {- captureMessageAudienceClicked(source, 'broadcast')- void openMessageAudience(- {- properties: cohortAudienceProperties(savedCohort),- source,- draftSource: { source: 'cohort', source_id: String(savedCohort.id) },- },- 'broadcast'- )- },- },+ secondaryButton: emailCohortToastButton(savedCohort, source),})}