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.

3.1 KB
Raw Blame
import type { ReachChannel } from './wizard/onboardingWizardSteps'

// pinned: analytics property values for the setup guide events - renaming breaks dashboards
export const SETUP_GUIDE_STEP_KEYS = ['channel', 'domain', 'opt-outs', 'email-template', 'first-journey'] as const
export type SetupGuideStepKey = (typeof SETUP_GUIDE_STEP_KEYS)[number]

export interface SetupGuideStep {
    key: SetupGuideStepKey
    done: boolean
}

/** What the guide reads to decide which steps are done. `null` means the value has not loaded yet. */
export interface SetupGuideInputs {
    emailChannels: { verified: boolean }[] | null
    pushChannelCount: number | null
    reachChannels: readonly ReachChannel[] | null
    optOutCategoryCount: number | null
    emailTemplateCount: number | null
    hasMessagingWorkflow: boolean | null
}

function emailInScope(
    emailChannelCount: number,
    pushChannelCount: number,
    reachChannels: readonly ReachChannel[] | null
): boolean {
    if (emailChannelCount > 0) {
        return true
    }
    if (reachChannels) {
        return reachChannels.includes('email')
    }
    return pushChannelCount === 0
}

/**
 * The messaging setup steps, in order. Returns `null` until every input has loaded, so the guide never
 * shows a step as open only because its data is still on the way.
 */
export function deriveSetupGuideSteps(inputs: SetupGuideInputs): SetupGuideStep[] | null {
    const {
        emailChannels,
        pushChannelCount,
        reachChannels,
        optOutCategoryCount,
        emailTemplateCount,
        hasMessagingWorkflow,
    } = inputs
    if (
        emailChannels === null ||
        pushChannelCount === null ||
        optOutCategoryCount === null ||
        emailTemplateCount === null ||
        hasMessagingWorkflow === null
    ) {
        return null
    }
    const done: Record<SetupGuideStepKey, boolean> = {
        channel: emailChannels.length > 0 || pushChannelCount > 0,
        domain: emailChannels.some((channel) => channel.verified),
        'opt-outs': optOutCategoryCount > 0,
        'email-template': emailTemplateCount > 0,
        'first-journey': hasMessagingWorkflow,
    }
    const withEmail = emailInScope(emailChannels.length, pushChannelCount, reachChannels)
    return SETUP_GUIDE_STEP_KEYS.filter((key) => withEmail || !EMAIL_ONLY_STEPS.includes(key)).map((key) => ({
        key,
        done: done[key],
    }))
}

const EMAIL_ONLY_STEPS: SetupGuideStepKey[] = ['domain', 'email-template']

export const SETUP_GUIDE_STEP_LABELS: Record<SetupGuideStepKey, string> = {
    channel: 'Connect a channel',
    domain: 'Verify your sending domain',
    'opt-outs': 'Add opt-out categories',
    'email-template': 'Design your first email',
    'first-journey': 'Start your first journey',
}

/** The setup tab a step opens. The first journey opens the workflow templates instead, so it has no tab. */
export const SETUP_GUIDE_STEP_TABS: Record<SetupGuideStepKey, 'channels' | 'opt-outs' | 'library' | null> = {
    channel: 'channels',
    domain: 'channels',
    'opt-outs': 'opt-outs',
    'email-template': 'library',
    'first-journey': null,
}