Skip to content

Open source CapCut alternative.

TypeScript

feat(site): add typed analytics events for landing, Cut home, and billing

David Le56454a69e636
diff --git a/site/src/app/_components/landing/FinalCTA.tsx b/site/src/app/_components/landing/FinalCTA.tsx
index 1ea3d96b..1df7a002 100644
--- a/site/src/app/_components/landing/FinalCTA.tsx
+++ b/site/src/app/_components/landing/FinalCTA.tsx
@@ -4,6 +4,7 @@ import { ArrowRight } from "lucide-react";
import { PillButton } from "@/app/_components/landing/LandingPrimitives";
import { DONKEY_INSTALL_URL } from "@/app/_components/landing/data";
+import { track } from "@/lib/analytics";
export function FinalCTA() {
return (
@@ -24,7 +25,12 @@ export function FinalCTA() {
Installs in 90 seconds.
</p>
<div className="flex flex-wrap justify-center gap-3">
- <PillButton href={DONKEY_INSTALL_URL} variant="primary" size="lg">
+ <PillButton
+ href={DONKEY_INSTALL_URL}
+ onClick={() => track("home_download_clicked", { location: "final_cta" })}
+ variant="primary"
+ size="lg"
+ >
Download for Mac <ArrowRight size={18} />
</PillButton>
</div>
diff --git a/site/src/app/_components/landing/Hero.tsx b/site/src/app/_components/landing/Hero.tsx
index 287e7cea..4c87e9c4 100644
--- a/site/src/app/_components/landing/Hero.tsx
+++ b/site/src/app/_components/landing/Hero.tsx
@@ -4,6 +4,7 @@ import { ArrowRight } from "lucide-react";
import { PillButton } from "@/app/_components/landing/LandingPrimitives";
import { DONKEY_INSTALL_URL } from "@/app/_components/landing/data";
+import { track } from "@/lib/analytics";
export function Hero() {
return (
@@ -21,7 +22,12 @@ export function Hero() {
your apps to finish it.
</p>
<div className="mt-9 flex flex-wrap gap-3">
- <PillButton href={DONKEY_INSTALL_URL} variant="primary" size="lg">
+ <PillButton
+ href={DONKEY_INSTALL_URL}
+ onClick={() => track("home_download_clicked", { location: "hero" })}
+ variant="primary"
+ size="lg"
+ >
Download for Mac <ArrowRight size={18} />
</PillButton>
</div>
diff --git a/site/src/app/_components/landing/LandingPrimitives.tsx b/site/src/app/_components/landing/LandingPrimitives.tsx
index dbc6d177..7eb0f173 100644
--- a/site/src/app/_components/landing/LandingPrimitives.tsx
+++ b/site/src/app/_components/landing/LandingPrimitives.tsx
@@ -155,17 +155,17 @@ export function PillButton({
...variants[variant],
};
- if (href && !disabled && !onClick) {
+ if (href && !disabled) {
if (isInternalHref(href) && !isAuthPassthroughHref(href)) {
return (
- <Link aria-label={ariaLabel} href={href} style={sharedStyle}>
+ <Link aria-label={ariaLabel} href={href} onClick={onClick} style={sharedStyle}>
{children}
</Link>
);
}
return (
- <a aria-label={ariaLabel} href={href} style={sharedStyle}>
+ <a aria-label={ariaLabel} href={href} onClick={onClick} style={sharedStyle}>
{children}
</a>
);
diff --git a/site/src/app/app/settings/SettingsShell.tsx b/site/src/app/app/settings/SettingsShell.tsx
index 7713a967..333dc592 100644
--- a/site/src/app/app/settings/SettingsShell.tsx
+++ b/site/src/app/app/settings/SettingsShell.tsx
@@ -4,6 +4,7 @@ import Link from "next/link";
import { usePathname, useRouter } from "next/navigation";
import { useEffect, type ReactNode } from "react";
+import { useAppLoaded } from "@/lib/analytics";
import { authClient } from "@/lib/auth-client";
import { cn } from "@/lib/utils";
import { Skeleton } from "@/components/ui/skeleton";
@@ -23,6 +24,8 @@ export function SettingsShell({ children }: { children: ReactNode }) {
const pathname = usePathname();
const { data: session, isPending } = authClient.useSession();
+ useAppLoaded("settings", session?.user);
+
useEffect(() => {
if (!isPending && !session) {
void authClient.signIn.social({
diff --git a/site/src/app/app/settings/_components/CreditsCard.tsx b/site/src/app/app/settings/_components/CreditsCard.tsx
index 587e4dc6..32ca1f46 100644
--- a/site/src/app/app/settings/_components/CreditsCard.tsx
+++ b/site/src/app/app/settings/_components/CreditsCard.tsx
@@ -13,6 +13,7 @@ import {
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { Skeleton } from "@/components/ui/skeleton";
+import { track } from "@/lib/analytics";
import { formatUsd } from "@/lib/credits/format-usd";
import {
creditTopUpDefaultDollars,
@@ -34,6 +35,7 @@ export function CreditsCard() {
const [customAmount, setCustomAmount] = useState("");
const startCheckout = (amountDollars: number) => {
+ track("credits_checkout_started", { amountDollars });
checkout.mutate(amountDollars, {
onSuccess: (result) => window.location.assign(result.url),
});
@@ -136,6 +138,7 @@ function AutoReloadSection({ onNeedsCard }: { onNeedsCard: () => void }) {
amountDollars: number;
}) => {
setNeedsCard(false);
+ track("credit_auto_reload_saved", next);
update.mutate(next, {
onError: (error) => {
if (error instanceof ApiError && error.code === "no_payment_method") {
diff --git a/site/src/app/app/settings/_components/ProCard.tsx b/site/src/app/app/settings/_components/ProCard.tsx
index 6bf8a65a..e38a5fd2 100644
--- a/site/src/app/app/settings/_components/ProCard.tsx
+++ b/site/src/app/app/settings/_components/ProCard.tsx
@@ -16,6 +16,7 @@ import {
CardTitle,
} from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
+import { track } from "@/lib/analytics";
import { formatUsd } from "@/lib/credits/format-usd";
export function ProCard() {
@@ -78,11 +79,12 @@ export function ProCard() {
{isActive ? (
<Button
disabled={portal.isPending}
- onClick={() =>
+ onClick={() => {
+ track("billing_portal_opened");
portal.mutate(undefined, {
onSuccess: (result) => window.location.assign(result.url),
- })
- }
+ });
+ }}
variant="secondary"
>
{portal.isPending ? "Opening…" : "Manage billing"}
@@ -90,11 +92,12 @@ export function ProCard() {
) : (
<Button
disabled={checkout.isPending}
- onClick={() =>
+ onClick={() => {
+ track("pro_checkout_started");
checkout.mutate("pro", {
onSuccess: (result) => window.location.assign(result.url),
- })
- }
+ });
+ }}
>
{checkout.isPending ? "Starting…" : "Subscribe to Pro"}
</Button>
diff --git a/site/src/cut/components/AppSidebar.tsx b/site/src/cut/components/AppSidebar.tsx
index 3de8968a..63293067 100644
--- a/site/src/cut/components/AppSidebar.tsx
+++ b/site/src/cut/components/AppSidebar.tsx
@@ -14,6 +14,7 @@ import {
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { apiFetch } from "@/cut/lib/api";
+import { track } from "@/lib/analytics";
import { NavUser } from "@/cut/components/NavUser";
import { homeHref, projectHref, tabForPath, useCutBase, type CutTab } from "@/cut/lib/nav";
import type { ProjectSummary } from "@/cut/lib/types";
@@ -41,6 +42,7 @@ export function AppSidebar() {
body: JSON.stringify({ name: name.trim() || "Untitled" }),
});
const project = (await res.json()) as ProjectSummary;
+ track("project_created", { source: "sidebar" });
router.push(projectHref(base, project.id, tabForPath(pathname)));
} finally {
setBusy(false);
diff --git a/site/src/cut/components/ConnectGate.tsx b/site/src/cut/components/ConnectGate.tsx
index 31245bf3..18c2053d 100644
--- a/site/src/cut/components/ConnectGate.tsx
+++ b/site/src/cut/components/ConnectGate.tsx
@@ -5,6 +5,7 @@ import { useCallback, useEffect, useState, type ReactNode } from "react";
import { Loader2, Plug, ShieldX } from "lucide-react";
import { DONKEY_DOWNLOAD_URL } from "@/app/_components/landing/data";
+import { track } from "@/lib/analytics";
import { Button } from "@/components/ui/button";
import {
ENGINE_LOST_EVENT,
@@ -196,6 +197,7 @@ export function ConnectGate({ children }: { children: ReactNode }) {
<div className="flex items-center gap-2">
<Button
onClick={() => {
+ track("app_install_clicked", { source: "connect_gate_button" });
window.location.href = DONKEY_DOWNLOAD_URL;
}}
>
@@ -215,6 +217,7 @@ export function ConnectGate({ children }: { children: ReactNode }) {
<a
className="font-medium text-foreground underline underline-offset-2"
href={DONKEY_DOWNLOAD_URL}
+ onClick={() => track("app_install_clicked", { source: "connect_gate_link" })}
>
Install it for Mac
</a>
diff --git a/site/src/cut/components/ProjectsHome.tsx b/site/src/cut/components/ProjectsHome.tsx
index 803d6bc7..f064ec84 100644
--- a/site/src/cut/components/ProjectsHome.tsx
+++ b/site/src/cut/components/ProjectsHome.tsx
@@ -44,6 +44,7 @@ import {
} from "@/components/ui/dropdown-menu";
import { Input } from "@/components/ui/input";
import { apiFetch, apiUrl, engineLost } from "@/cut/lib/api";
+import { track } from "@/lib/analytics";
import { clearProjectThreads } from "@/cut/lib/chatThreads";
import { useGenerate } from "@/cut/lib/generate";
import { useGenScene } from "@/cut/lib/genScene";
@@ -132,6 +133,7 @@ export function ProjectsHome() {
if (res.ok) {
const f = (await res.json()) as ProjectFolder;
setFolders((prev) => [...prev, f]);
+ track("folder_created");
}
};
@@ -192,6 +194,7 @@ export function ProjectsHome() {
body: JSON.stringify({ name: name.trim() || "Untitled", folderId: openFolder }),
});
const project = (await res.json()) as ProjectSummary;
+ track("project_created", { source: "projects_home" });
router.push(projectHref(base, project.id, "projects", openFolder));
} finally {
setBusy(false);
@@ -207,6 +210,7 @@ export function ProjectsHome() {
body: JSON.stringify({ name: "Untitled", folderId }),
});
const project = (await res.json()) as ProjectSummary;
+ track("project_created", { source: "projects_home" });
router.push(projectHref(base, project.id, "projects", folderId));
};
@@ -220,6 +224,7 @@ export function ProjectsHome() {
for (const file of media) {
try {
await createProjectFromFile(file, folderId);
+ track("project_created", { source: "file_import" });
} catch {
// A file the engine can't ingest is skipped; the rest still land.
} finally {
diff --git a/site/src/cut/components/RequireSession.tsx b/site/src/cut/components/RequireSession.tsx
index 371cf383..472626a8 100644
--- a/site/src/cut/components/RequireSession.tsx
+++ b/site/src/cut/components/RequireSession.tsx
@@ -5,6 +5,7 @@ import { useEffect, type ReactNode } from "react";
import { signInHrefFor } from "@/app/_components/landing/useAppEntryHref";
import { setEngineUser } from "@/cut/lib/api";
import { DONKEYCUT_CANONICAL } from "@/cut/lib/hosts";
+import { useAppLoaded } from "@/lib/analytics";
import { authClient } from "@/lib/auth-client";
import { useCutBase } from "@/cut/lib/nav";
@@ -30,6 +31,8 @@ export function RequireSession({ children }: { children: ReactNode }) {
const signedOut = !isPending && !session;
+ useAppLoaded("cut", session?.user);
+
useEffect(() => {
if (!signedOut) return;
const here = window.location.pathname + window.location.search;
diff --git a/site/src/lib/analytics.ts b/site/src/lib/analytics.ts
new file mode 100644
index 00000000..a4d4600b
--- /dev/null
+++ b/site/src/lib/analytics.ts
@@ -0,0 +1,50 @@
+import posthog from "posthog-js";
+import { useEffect } from "react";
+
+// Every product analytics event, in one place. `track` checks the name and
+// its properties against this map at compile time, so call sites can't drift
+// from the list. Events with `void` properties take no second argument.
+export type AnalyticsEvents = {
+ // A signed-in app surface finished loading (fires once per page load).
+ app_loaded: { app: "cut" | "settings" };
+ // Landing page download CTAs.
+ home_download_clicked: { location: "hero" | "final_cta" };
+ // Install prompts on the Cut app home's connect gate.
+ app_install_clicked: { source: "connect_gate_button" | "connect_gate_link" };
+ // Cut projects home.
+ project_created: { source: "projects_home" | "sidebar" | "file_import" };
+ folder_created: void;
+ // Billing (settings).
+ pro_checkout_started: void;
+ billing_portal_opened: void;
+ credits_checkout_started: { amountDollars: number };
+ credit_auto_reload_saved: {
+ enabled: boolean;
+ thresholdDollars: number;
+ amountDollars: number;
+ };
+};
+
+// Binds this browser's events to the signed-in account and records the app
+// surface loading. Call from an app shell once the session is known; the
+// user id becomes the PostHog distinct id, so every later event carries it.
+export function useAppLoaded(
+ app: AnalyticsEvents["app_loaded"]["app"],
+ user: { id: string; email?: string } | undefined,
+): void {
+ const { id, email } = user ?? {};
+ useEffect(() => {
+ if (!id) return;
+ posthog.identify(id, email ? { email } : undefined);
+ track("app_loaded", { app });
+ }, [app, id, email]);
+}
+
+export function track<Name extends keyof AnalyticsEvents>(
+ name: Name,
+ ...props: AnalyticsEvents[Name] extends void
+ ? []
+ : [properties: AnalyticsEvents[Name]]
+): void {
+ posthog.capture(name, (props as [Record<string, unknown>?])[0]);
+}