Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
};

Expand Down Expand Up @@ -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"];
Expand Down
1 change: 1 addition & 0 deletions packages/firecms_cloud/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from "./useBuildCloudUserManagement.js";
export * from "./useDataTalkMode.js";
export * from "./useFirestoreExplorerMode.js";
export * from "./useLicensesForUserController.js";
export * from "./useAbandonmentReporter.js";
58 changes: 58 additions & 0 deletions packages/firecms_cloud/src/hooks/useAbandonmentReporter.ts
Original file line number Diff line number Diff line change
@@ -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 };
}
Loading