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.

4.9 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { subscriptions } from 'kea-subscriptions'
import posthog from 'posthog-js'

import { FEATURE_FLAGS } from 'lib/constants'
import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { organizationLogic } from 'scenes/organizationLogic'

import type { FeatureFlagsSet } from '../../lib/logic/featureFlagLogic'
import type { OrganizationType } from '../../types'

const FLAG = FEATURE_FLAGS.WEB_ANALYTICS_SHARE_NUDGE_V2
const EXPORT_PROMPT_PROBABILITY = 0.25

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface shareNudgeLogicValues {
    featureFlags: FeatureFlagsSet // featureFlagLogic
    currentOrganization: OrganizationType | null // organizationLogic
    emphasizeShareButton: boolean
    exportPromptEnabled: boolean
    hasColleagues: boolean
    intentPromptEnabled: boolean
    promptSource: string | null
    promptVisible: boolean
    sessionDismissed: boolean
    showBanner: boolean
    variant: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface shareNudgeLogicActions {
    dismissForSession: () => {
        value: true
    }
    exportTriggered: () => {
        value: true
    }
    showPrompt: (source: string) => {
        source: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface shareNudgeLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        hasColleagues: (currentOrganization: OrganizationType | null) => boolean
        variant: (featureFlags: FeatureFlagsSet, hasColleagues: boolean) => string | null
        showBanner: (variant: string | null, sessionDismissed: boolean) => boolean
        emphasizeShareButton: (variant: string | null) => boolean
        intentPromptEnabled: (variant: string | null) => boolean
        exportPromptEnabled: (variant: string | null) => boolean
    }
}

export type shareNudgeLogicType = MakeLogicType<
    shareNudgeLogicValues,
    shareNudgeLogicActions,
    Record<string, any>,
    shareNudgeLogicMeta
>

export const shareNudgeLogic = kea<shareNudgeLogicType>([
    path(['scenes', 'web-analytics', 'shareNudgeLogic']),
    connect(() => ({
        values: [organizationLogic, ['currentOrganization'], featureFlagLogic, ['featureFlags']],
    })),
    actions({
        showPrompt: (source: string) => ({ source }),
        dismissForSession: true,
        exportTriggered: true,
    }),
    reducers({
        promptVisible: [
            false,
            {
                showPrompt: () => true,
                dismissForSession: () => false,
            },
        ],
        promptSource: [
            null as string | null,
            {
                showPrompt: (_, { source }) => source,
            },
        ],
        sessionDismissed: [
            false,
            {
                dismissForSession: () => true,
            },
        ],
    }),
    selectors({
        hasColleagues: [
            (s) => [s.currentOrganization],
            (currentOrganization: null | import('../../types').OrganizationType): boolean =>
                (currentOrganization?.member_count ?? 1) >= 2,
        ],
        variant: [
            (s) => [s.featureFlags, s.hasColleagues],
            (
                featureFlags: import('lib/logic/featureFlagLogic').FeatureFlagsSet,
                hasColleagues: boolean
            ): string | null => {
                const flag = featureFlags[FLAG]
                return hasColleagues && typeof flag === 'string' ? flag : null
            },
        ],
        showBanner: [
            (s) => [s.variant, s.sessionDismissed],
            (variant: string | null, sessionDismissed: boolean): boolean => variant === 'banner' && !sessionDismissed,
        ],
        emphasizeShareButton: [(s) => [s.variant], (variant: string | null): boolean => variant === 'button'],
        intentPromptEnabled: [(s) => [s.variant], (variant: string | null): boolean => variant === 'prompt'],
        exportPromptEnabled: [(s) => [s.variant], (variant: string | null): boolean => variant === 'export'],
    }),
    listeners(({ values, actions }) => ({
        showPrompt: ({ source }) => {
            posthog.capture('web analytics share nudge prompt shown', { source })
        },
        exportTriggered: () => {
            if (!values.exportPromptEnabled || values.sessionDismissed) {
                return
            }
            if (Math.random() >= EXPORT_PROMPT_PROBABILITY) {
                return
            }
            actions.showPrompt('export_prompt')
        },
    })),
    subscriptions(({ cache }) => ({
        variant: (variant: string | null) => {
            if (variant && !cache.exposed) {
                cache.exposed = true
                posthog.capture('web analytics share nudge exposed', { variant })
            }
        },
    })),
])