From 7e1cced604e5db09d18ac186592f3c37edfcb86c Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 17 Nov 2026 12:18:33 +0000 Subject: [PATCH] refactor(firecms_cloud): extract the login screen's abandonment reporting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `view_abandoned` shipped in d139c76 inline in the login view, where it could only be tested by standing up @firecms/ui and @firecms/core. It carries the funnel's largest number — 31.0% of login-screen visitors never clicked any sign-in method in May 2026, 42.4% in October, 41.4% in November — so it should not be the one piece of that flow with no test behind it. `useAbandonmentReporter` takes only React, so it can be covered directly. The view keeps what is genuinely its own, whether the terms box was ticked and how many times the gate stopped the visitor, and calls `markResolved` when a sign-in method is attempted. Behaviour is unchanged: the same two exits (`pagehide`, which fires on mobile where `beforeunload` does not, and unmount for in-app navigation), the same guards, the same params. The hook keeps reading the callback through a ref and subscribing on `[]`: hosts pass a fresh closure every render, and subscribing on its identity would run the cleanup — and so report an abandonment — on each one. Tests live in firecmsco/saas, whose suite already aliases @firecms/* to these sources and tests this package's contract for the same reason (see src/remote/lazy_export_contract.test.ts). Adding a runner here would mean new devDependencies and a pnpm-lock.yaml regeneration, which is a separate change. Verified: the saas app suite, which compiles this source, stays at 409 passing across 37 files. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_011VZh1qoNGKbXQrrrFiDoeQ --- .../components/auth/FireCMSCloudLoginView.tsx | 50 +++++----------- packages/firecms_cloud/src/hooks/index.ts | 1 + .../src/hooks/useAbandonmentReporter.ts | 58 +++++++++++++++++++ 3 files changed, 73 insertions(+), 36 deletions(-) create mode 100644 packages/firecms_cloud/src/hooks/useAbandonmentReporter.ts diff --git a/packages/firecms_cloud/src/components/auth/FireCMSCloudLoginView.tsx b/packages/firecms_cloud/src/components/auth/FireCMSCloudLoginView.tsx index a524b672a..7c9918b35 100644 --- a/packages/firecms_cloud/src/components/auth/FireCMSCloudLoginView.tsx +++ b/packages/firecms_cloud/src/components/auth/FireCMSCloudLoginView.tsx @@ -19,6 +19,7 @@ import { FireCMSBackend } from "../../types/index.js"; import { LoginButton } from "@firecms/firebase"; import { CloudUserPasswordForm } from "./CloudUserPasswordForm.js"; import { describeGraduatedPrice, formatAmount, preferredCurrency } from "../../utils/pro_pricing.js"; +import { useAbandonmentReporter } from "../../hooks/useAbandonmentReporter.js"; export interface FireCMSCloudLoginViewProps { fireCMSBackend: FireCMSBackend; @@ -76,22 +77,23 @@ export function FireCMSCloudLoginView({ const [passwordLoginSelected, setPasswordLoginSelected] = useState(false); // Why people leave this screen is the funnel's largest unknown: between a - // third and a half of everyone who reaches it never clicks any method, and - // the only events were "screen shown" and "method clicked", with nothing in - // between to tell those cases apart. These refs let the view say what the - // visitor did before they went. - const attemptedRef = React.useRef(false); - const abandonReportedRef = React.useRef(false); - const shownAtRef = React.useRef(Date.now()); + // third and a half of everyone who reaches it never clicks any method. These + // two refs are what the view has to say about such a visitor; the reporting + // itself is useAbandonmentReporter's. const timesBlockedRef = React.useRef(0); const termsAcceptedRef = React.useRef(false); - // Host apps pass a fresh closure on every render, so the abandonment effect - // reads the callback through a ref and never re-subscribes on it. - const onAnalyticsEventRef = React.useRef(onAnalyticsEvent); - onAnalyticsEventRef.current = onAnalyticsEvent; + + const { markResolved } = useAbandonmentReporter((reason, secondsOnScreen) => { + onAnalyticsEvent?.("view_abandoned", { + reason, + seconds_on_screen: secondsOnScreen, + terms_accepted: termsAcceptedRef.current, + times_blocked: timesBlockedRef.current + }); + }); const attemptMethod = (event: string) => { - attemptedRef.current = true; + markResolved(); onAnalyticsEvent?.(event); }; @@ -129,30 +131,6 @@ export function FireCMSCloudLoginView({ return () => clearTimeout(timer); }, [onAnalyticsEvent, variant]); - // Leaving without trying to sign in. `pagehide` is the signal that fires on - // mobile, where `beforeunload` does not; the cleanup covers navigating away - // inside the app. Both are guarded so this is sent at most once, and never - // once a method has been attempted — a successful sign-in unmounts this - // view and must not look like an abandonment. - useEffect(() => { - const report = (reason: string) => { - if (attemptedRef.current || abandonReportedRef.current) return; - abandonReportedRef.current = true; - onAnalyticsEventRef.current?.("view_abandoned", { - reason, - seconds_on_screen: Math.round((Date.now() - shownAtRef.current) / 1000), - terms_accepted: termsAcceptedRef.current, - times_blocked: timesBlockedRef.current - }); - }; - const onPageHide = () => report("page_hidden"); - window.addEventListener("pagehide", onPageHide); - return () => { - window.removeEventListener("pagehide", onPageHide); - report("unmounted"); - }; - }, []); - function buildErrorView() { let errorView: any; const ignoredCodes = ["auth/popup-closed-by-user", "auth/cancelled-popup-request"]; diff --git a/packages/firecms_cloud/src/hooks/index.ts b/packages/firecms_cloud/src/hooks/index.ts index 76742cbcf..bf85f4306 100644 --- a/packages/firecms_cloud/src/hooks/index.ts +++ b/packages/firecms_cloud/src/hooks/index.ts @@ -9,3 +9,4 @@ export * from "./useBuildCloudUserManagement.js"; export * from "./useDataTalkMode.js"; export * from "./useFirestoreExplorerMode.js"; export * from "./useLicensesForUserController.js"; +export * from "./useAbandonmentReporter.js"; diff --git a/packages/firecms_cloud/src/hooks/useAbandonmentReporter.ts b/packages/firecms_cloud/src/hooks/useAbandonmentReporter.ts new file mode 100644 index 000000000..92c466e87 --- /dev/null +++ b/packages/firecms_cloud/src/hooks/useAbandonmentReporter.ts @@ -0,0 +1,58 @@ +import { useCallback, useEffect, useRef } from "react"; + +/** + * Reports a visitor leaving a screen without doing the thing the screen exists + * to get them to do. + * + * Screens normally emit "shown" and "did the thing", and nothing at all for the + * people who do neither — the largest group on the Cloud login screen, where + * between a third and a half of everyone who arrives never clicks any sign-in + * method. Those exits leave no event, so there is nothing to tell apart the + * visitor with no Google session from the one who read the permissions wording + * and backed out. + * + * `report` is called at most once per mount, and never once `markResolved` has + * been called, so a screen that unmounts *because* the visitor succeeded does + * not also read as an abandonment. + * + * It fires on `pagehide` as well as on unmount: closing the tab or switching + * apps on a phone unmounts nothing, and `beforeunload` does not fire on mobile + * Safari or Chrome for Android. + * + * @param report called with why the screen was left and how long the visitor + * was on it. Read anything else you want to send through a ref: this is + * deliberately not re-subscribed when the callback identity changes, because + * hosts pass a fresh closure on every render and resubscribing would run the + * cleanup — and so report an abandonment — on each one. + */ +export function useAbandonmentReporter( + report: (reason: "page_hidden" | "unmounted", secondsOnScreen: number) => void +): { markResolved: () => void } { + + const reportRef = useRef(report); + reportRef.current = report; + + const resolvedRef = useRef(false); + const reportedRef = useRef(false); + const shownAtRef = useRef(Date.now()); + + const markResolved = useCallback(() => { + resolvedRef.current = true; + }, []); + + useEffect(() => { + const fire = (reason: "page_hidden" | "unmounted") => { + if (resolvedRef.current || reportedRef.current) return; + reportedRef.current = true; + reportRef.current(reason, Math.round((Date.now() - shownAtRef.current) / 1000)); + }; + const onPageHide = () => fire("page_hidden"); + window.addEventListener("pagehide", onPageHide); + return () => { + window.removeEventListener("pagehide", onPageHide); + fire("unmounted"); + }; + }, []); + + return { markResolved }; +}