Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat: Add PostHog A/B testing to homepage

Richie McIlroy7ec324e53650
diff --git a/apps/web/app/layout.tsx b/apps/web/app/layout.tsx
index 4b0d46f91..1f0a1e921 100644
--- a/apps/web/app/layout.tsx
+++ b/apps/web/app/layout.tsx
@@ -13,6 +13,7 @@ import { PublicEnvContext } from "@/utils/public-env";
import { S3_BUCKET_URL } from "@cap/utils";
import { PropsWithChildren } from "react";
import crypto from "crypto";
+import { getBootstrapData } from "@/utils/getBootstrapData";
//@ts-expect-error
import { script } from "./themeScript";
@@ -32,6 +33,7 @@ export const metadata: Metadata = {
export default async function RootLayout({ children }: PropsWithChildren) {
const user = await getCurrentUser();
+ const bootstrapData = await getBootstrapData();
const intercomSecret = serverEnv().INTERCOM_SECRET;
let intercomHash = "";
if (intercomSecret) {
@@ -72,7 +74,7 @@ export default async function RootLayout({ children }: PropsWithChildren) {
dangerouslySetInnerHTML={{ __html: `(${script.toString()})()` }}
/>
<TooltipPrimitive.Provider>
- <PostHogProvider>
+ <PostHogProvider bootstrapData={bootstrapData}>
<AuthProvider>
<PublicEnvContext
value={{
diff --git a/apps/web/app/page.tsx b/apps/web/app/page.tsx
index c217366b2..c85701589 100644
--- a/apps/web/app/page.tsx
+++ b/apps/web/app/page.tsx
@@ -1,14 +1,19 @@
import { HomePage } from "@/components/pages/HomePage";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";
+import { getBootstrapData } from "@/utils/getBootstrapData";
export default async function Home() {
const cookieStore = cookies();
const sessionCookie = cookieStore.get("next-auth.session-token");
+ const bootstrapData = await getBootstrapData();
+
+ const homepageCopyVariant =
+ (bootstrapData.featureFlags["homepage-copy"] as string) || "";
if (sessionCookie) {
redirect("/dashboard/caps");
}
- return <HomePage />;
+ return <HomePage serverHomepageCopyVariant={homepageCopyVariant} />;
}
diff --git a/apps/web/app/providers.tsx b/apps/web/app/providers.tsx
index 0aee21035..1a258aad8 100644
--- a/apps/web/app/providers.tsx
+++ b/apps/web/app/providers.tsx
@@ -9,7 +9,15 @@ import PostHogPageView from "./PosthogPageView";
import Intercom from "@intercom/messenger-js-sdk";
import { usePathname } from "next/navigation";
-export function PostHogProvider({ children }: PropsWithChildren) {
+export function PostHogProvider({
+ children,
+ bootstrapData,
+}: PropsWithChildren<{
+ bootstrapData?: {
+ distinctID: string;
+ featureFlags: Record<string, string | boolean>;
+ };
+}>) {
useEffect(() => {
const key = buildEnv.NEXT_PUBLIC_POSTHOG_KEY;
const host = buildEnv.NEXT_PUBLIC_POSTHOG_HOST;
@@ -19,6 +27,7 @@ export function PostHogProvider({ children }: PropsWithChildren) {
posthog.init(key, {
api_host: host,
capture_pageview: false,
+ bootstrap: bootstrapData,
loaded: (posthogInstance) => {
console.log("PostHog loaded and ready to capture events");
},
@@ -31,7 +40,7 @@ export function PostHogProvider({ children }: PropsWithChildren) {
"Missing PostHog environment variables. Events will not be tracked."
);
}
- }, []);
+ }, [bootstrapData]);
return (
<PHProvider client={posthog}>
diff --git a/apps/web/components/pages/HomePage/index.tsx b/apps/web/components/pages/HomePage/index.tsx
index 96a9b88fa..6ec6663d5 100644
--- a/apps/web/components/pages/HomePage/index.tsx
+++ b/apps/web/components/pages/HomePage/index.tsx
@@ -24,7 +24,14 @@ import { LogoSection } from "../_components/LogoSection";
import { FeatureCard } from "../SelfHostingPage";
import LeftBlueHue from "./LeftBlueHue";
import PowerfulFeaturesSVG from "./PowerfulFeaturesSVG";
-export const HomePage = () => {
+
+interface HomePageProps {
+ serverHomepageCopyVariant?: string;
+}
+
+export const HomePage: React.FC<HomePageProps> = ({
+ serverHomepageCopyVariant = "",
+}) => {
const [videoToggled, setVideoToggled] = useState(false);
const { platform, isIntel } = useDetectPlatform();
const loading = platform === null;
@@ -57,14 +64,50 @@ export const HomePage = () => {
Record. Edit. Share.
</h3>
<h1 className="fade-in-down text-[2rem] font-bold leading-[2.5rem] md:text-[3.75rem] md:leading-[4rem] relative z-10 text-black mb-4">
- Beautiful screen recordings,
- <br />
- owned by you.
+ {serverHomepageCopyVariant === "1" ? (
+ <>
+ Beautiful screen recordings,
+ <br />
+ owned by you.
+ </>
+ ) : serverHomepageCopyVariant === "2" ? (
+ <>The open source Loom alternative.</>
+ ) : serverHomepageCopyVariant === "3" ? (
+ <>The open source screen recording suite.</>
+ ) : (
+ <>
+ Beautiful screen recordings,
+ <br />
+ owned by you.
+ </>
+ )}
</h1>
<p className="mx-auto mb-8 max-w-3xl text-md sm:text-xl text-zinc-500 fade-in-down animate-delay-1">
- Cap is the open source alternative to Loom. Lightweight, powerful,
- and cross-platform. Record and share securely in seconds with
- custom S3 bucket support.
+ {serverHomepageCopyVariant === "1" ? (
+ <>
+ Cap is the open source alternative to Loom. Lightweight,
+ powerful, and cross-platform. Record and share securely in
+ seconds with custom S3 bucket support.
+ </>
+ ) : serverHomepageCopyVariant === "2" ? (
+ <>
+ Cap is the open source alternative to Loom. Lightweight,
+ powerful, and cross-platform. Record and share securely in
+ seconds. Connect your own storage, domain & more.
+ </>
+ ) : serverHomepageCopyVariant === "3" ? (
+ <>
+ Cap is open source, lightweight, powerful & cross-platform.
+ With Instant Mode for shareable links and Studio Mode for
+ high-quality recordings with local editing.
+ </>
+ ) : (
+ <>
+ Cap is the open source alternative to Loom. Lightweight,
+ powerful, and cross-platform. Record and share securely in
+ seconds with custom S3 bucket support.
+ </>
+ )}
</p>
</div>
<div className="flex flex-col justify-center items-center mb-5 space-y-2 fade-in-up animate-delay-2 sm:flex-row sm:space-y-0 sm:space-x-2">
diff --git a/apps/web/utils/getBootstrapData.ts b/apps/web/utils/getBootstrapData.ts
new file mode 100644
index 000000000..71d895150
--- /dev/null
+++ b/apps/web/utils/getBootstrapData.ts
@@ -0,0 +1,50 @@
+import { PostHog } from 'posthog-node';
+import { cookies } from 'next/headers';
+import { buildEnv } from '@cap/env';
+import { cache } from 'react';
+import { v4 as uuidv4 } from 'uuid';
+
+export const generateId = cache(() => {
+ const id = uuidv4();
+ return id;
+});
+
+export interface BootstrapData {
+ distinctID: string;
+ featureFlags: Record<string, string | boolean>;
+}
+
+export async function getBootstrapData(): Promise<BootstrapData> {
+ let distinct_id = '';
+ const phProjectAPIKey = buildEnv.NEXT_PUBLIC_POSTHOG_KEY || '';
+ const phCookieName = `ph_${phProjectAPIKey}_posthog`;
+ const cookieStore = cookies();
+ const phCookie = cookieStore.get(phCookieName);
+
+ if (phCookie) {
+ try {
+ const phCookieParsed = JSON.parse(phCookie.value);
+ distinct_id = phCookieParsed.distinct_id;
+ } catch (e) {
+ console.error('Error parsing PostHog cookie:', e);
+ }
+ }
+
+ if (!distinct_id) {
+ distinct_id = generateId();
+ }
+
+ const client = new PostHog(
+ phProjectAPIKey,
+ { host: buildEnv.NEXT_PUBLIC_POSTHOG_HOST || "https://app.posthog.com" }
+ );
+
+ const flags = await client.getAllFlags(distinct_id);
+
+ const bootstrap: BootstrapData = {
+ distinctID: distinct_id,
+ featureFlags: flags
+ };
+
+ return bootstrap;
+}
\ No newline at end of file