// Growth: store analytics snapshots against the listing changes from git (`analytics compare --json`, offline; a new // snapshot and the App Store report request are confirm-gated writes), App Store Product Page Optimization tests // (status read; create and upload treatments: dry run, dialog, real run; start / stop are owner actions with their // own dialog, never MCP tools), and Google Play store listing experiments (no API: Play Console). import { useMemo, useState } from 'react'; import { type AnalyticsCompare, api, type PpoStatus, type PpoTest } from '../../api'; import { t } from '../../i18n'; import type { UnifiedApp } from '../../lib/identity'; import { analyticsCsv, exportName } from '../../lib/exports'; import { ago, date, num, pct } from '../../lib/format'; import { chartSeries, effectDelta } from '../../lib/views'; import { useNav } from '../../nav'; import { useData, useRead, useWrite } from '../../store'; import { useExports } from '../../ui/Exports'; import { Icon, StoreGlyph } from '../../ui/icons'; import { Card, Chip, Empty, ErrorNotice, Kpi, Notice, Seg, Skeleton } from '../../ui/kit'; import { LineChart } from '../../ui/LineChart'; import { WriteButton } from '../../ui/WriteButton'; import { ascUrl, PLAY_CONSOLE } from '../AppView'; const DONE = ['COMPLETED', 'STOPPED']; const started = (x: PpoTest) => !!x.startDate && !x.endDate && !DONE.includes(x.state); const stateTone = (s: string): 'ok' | 'warn' | 'bad' | 'info' | 'neutral' | 'accent' => s === 'REJECTED' ? 'bad' : DONE.includes(s) ? 'neutral' : ['WAITING_FOR_REVIEW', 'IN_REVIEW'].includes(s) ? 'warn' : ['ACCEPTED', 'APPROVED'].includes(s) ? 'ok' : 'info'; function Analytics({ projectId, hasApple, hasPlay, name, playReports }: { projectId: string; hasApple: boolean; hasPlay: boolean; name: string; playReports: boolean }) { const r = useRead(projectId, 'analytics.compare', { network: false }); const w = useWrite(projectId); const a = r.data; const stores = ([hasApple && 'apple', hasPlay && 'play'].filter(Boolean) as ('apple' | 'play')[]).filter(s => !a || (s === 'apple' ? a.series.apple.length : a.series.play.length)); const [store, setStore] = useState<'apple' | 'play'>(hasApple ? 'apple' : 'play'); const st = stores.includes(store) ? store : stores[0] ?? store; const s = useMemo(() => (a ? chartSeries(a, st) : null), [a, st]); useExports('growth', [{ id: 'analytics-csv', label: t('export.analytics'), icon: 'chart', kind: 'csv', file: exportName(name, 'analytics'), disabled: a ? false : t('export.readFirst'), build: () => analyticsCsv(a!) }]); const none = !!r.error && /no snapshots/i.test(r.error); const actions = ( <> {hasApple && {t('gr.setup')}} {t('gr.snapshot')} ); const sum = (xs: number[]) => xs.reduce((n, v) => n + v, 0); const reach = s ? sum(s.reach) : 0, inst = s ? sum(s.installs) : 0; const effects = (a?.effects ?? []).filter(e => e.store === st).slice(-8).reverse(); return ( {stores.length > 1 && }{actions}}> {none ? ( {t('gr.noneText')}{hasPlay && !playReports ? ` ${t('gr.noBucket')}` : ''} ) : r.error ?
: !a || !s ?
: !s.dates.length ? ( {a.errors?.[st] ?? t('gr.noDataText')}{st === 'play' && !playReports ? ` ${t('gr.noBucket')}` : ''} ) : (
{s.dates.length > 1 ? ( ) : {t('gr.oneDay')}} {effects.length > 0 && ( {effects.map((e, i) => { const dlt = effectDelta(e.before.conversion, e.after.conversion); return ( ); })}
{t('gr.change')}{t('col.date')}{t('gr.before')}{t('gr.after')}{t('col.diff')}
{e.event.subject}
{e.event.commit} · {e.event.files.slice(0, 3).join(', ')}
{date(e.event.date)} {pct(e.before.conversion ?? null, 1)}
{t('gr.daysN', { count: e.daysBefore })}
{pct(e.after.conversion ?? null, 1)}
{t('gr.daysN', { count: e.daysAfter })}
{dlt === null ? '–' : = 0 ? 'ok' : 'warn'} dot={false}>{`${dlt >= 0 ? '+' : ''}${num(dlt, 1)} %`}}
)}

{t('gr.reading')}

)}
); } function Experiments({ app }: { app: UnifiedApp }) { const d = useData(); const target = app.project?.id ?? app.target; const r = useRead(target, 'apple.ppo_status', { network: true, title: t('gr.ppo') }); const w = useWrite(app.project?.id ?? null); const variants = d.statusFor(app)?.summary?.variants ?? []; const [pickV, setPickV] = useState([]); const [traffic, setTraffic] = useState(30); const [up, setUp] = useState(''); const chosen = pickV.length ? pickV : variants.slice(0, 1); const upV = up || variants[0] || ''; const tests = r.data?.tests ?? []; const editable = tests.find(x => !started(x) && !DONE.includes(x.state)); const asc = app.ios ? ascUrl(app.ios.app.id) : ascUrl(d.statusFor(app)?.app?.platforms.ios?.ascAppId); return ( {r.data ? (r.fromCache ? t('common.cachedFrom', { when: ago(r.at) }) : t('common.readWhen', { when: ago(r.at) })) : t('gr.ppoSub')}} pad={false} actions={<> }> {r.error &&
} {!r.data ? (r.loading && !r.error ?
: {t('gr.ppoNotReadText')}) : !tests.length ? {variants.length ? t('gr.ppoNoneText') : t('gr.ppoNoVariants')} : ( {tests.map(x => ( ))}
{t('gr.test')}{t('gr.state')}{t('gr.traffic')}{t('gr.dates')}{t('gr.treatments')}{t('gr.owner')}
{x.name} {x.state.replace(/_/g, ' ').toLowerCase()} {x.trafficProportion !== undefined ? `${x.trafficProportion} %` : '–'} {x.startDate ? `${date(x.startDate)} → ${x.endDate ? date(x.endDate) : t('gr.running')}` : t('gr.notStarted')} {x.treatments.length ? x.treatments.map(tr => t('gr.treatment', { name: tr.name, count: tr.locales })).join(' · ') : '–'} {app.project && !DONE.includes(x.state) && (started(x) ? {t('gr.stopBtn')} : {t('gr.startBtn')})}
)} {app.project ? (
{!variants.length ? {t('gr.ppoNoVariants')} : (
{t('gr.create')}
{variants.map(v => ( ))}
{t('gr.createBtn')}
{t('gr.upload')}
{t('gr.uploadBtn')} {editable ? t('gr.uploadInto', { name: editable.name }) : t('gr.uploadNoTest')}
)}

{t('gr.ppoNote')}

) :
{t('gr.ppoStoreOnly')}
}
); } function PlayExperiments({ app }: { app: UnifiedApp }) { const nav = useNav(); return ( {t('gr.playExpSub')}} actions={}>

{t('gr.playExpText')}

{app.project &&
}
); } export function GrowthTab({ app }: { app: UnifiedApp }) { const d = useData(); const s = d.statusFor(app)?.summary; const hasApple = !!(s?.apple ?? app.ios), hasPlay = !!(s?.play ?? app.android); if (app.project && !s) return ; return ( <> {app.project && }
{hasApple && } {hasPlay && }
); }