Skip to content

Commit

Permalink
Add posthog analytics (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
sepal committed Oct 30, 2023
1 parent e07311c commit 28dd755
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 7 deletions.
79 changes: 79 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
"lexical": "^0.12.2",
"lucide-react": "^0.286.0",
"next": "13.5.4",
"posthog-js": "^1.87.5",
"posthog-node": "^3.1.3",
"react": "^18",
"react-dom": "^18",
"replicate": "^0.20.1",
Expand Down
15 changes: 11 additions & 4 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import type { Metadata } from "next";
import { Inter } from "next/font/google";
import { AnalyticsBrowser } from "@june-so/analytics-next";
import { UserAnalytics } from "@/components/Analytics";
import { Suspense } from "react";
import { PHProvider, PostHogPageview } from "./providers";

const inter = Inter({ subsets: ["latin"] });

Expand All @@ -24,10 +26,15 @@ export default function RootLayout({
return (
<ClerkProvider>
<html lang="en">
<body className={inter.className}>
{children}
<UserAnalytics />
</body>
<Suspense>
<PostHogPageview />
</Suspense>
<PHProvider>
<body className={inter.className}>
{children}
<UserAnalytics />
</body>
</PHProvider>
</html>
</ClerkProvider>
);
Expand Down
10 changes: 10 additions & 0 deletions src/app/posthog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PostHog } from "posthog-node";

export default function PostHogClient() {
const posthogClient = new PostHog(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
flushAt: 1,
flushInterval: 0,
});
return posthogClient;
}
36 changes: 36 additions & 0 deletions src/app/providers.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"use client";
import posthog from "posthog-js";
import { PostHogProvider } from "posthog-js/react";
import { usePathname, useSearchParams } from "next/navigation";
import { useEffect } from "react";

if (typeof window !== "undefined" && process.env.NEXT_PUBLIC_POSTHOG_KEY) {
console.log("Init posthog");
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
capture_pageview: false, // Disable automatic pageview capture, as we capture manually
});
}

export function PostHogPageview(): JSX.Element {
const pathname = usePathname();
const searchParams = useSearchParams();

useEffect(() => {
if (pathname) {
let url = window.origin + pathname;
if (searchParams && searchParams.toString()) {
url = url + `?${searchParams.toString()}`;
}
posthog.capture("$pageview", {
$current_url: url,
});
}
}, [pathname, searchParams]);

return <></>;
}

export function PHProvider({ children }: { children: React.ReactNode }) {
return <PostHogProvider client={posthog}>{children}</PostHogProvider>;
}
12 changes: 12 additions & 0 deletions src/components/Analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
"use client";
import { useJune } from "@/lib/hooks/useJune";
import { useAuth, useUser } from "@clerk/nextjs";
import { usePostHog } from "posthog-js/react";
import { useEffect } from "react";

const UserAnalytics = () => {
const { isLoaded, isSignedIn, user } = useUser();
const analytics = useJune();
const posthog = usePostHog();
useEffect(() => {
if (!isLoaded || !isSignedIn || !user) {
return;
}
posthog.identify(user.id, {
email: user.emailAddresses[0].emailAddress,
});
}, []);

if (!isLoaded || !isSignedIn || !user) {
return null;
Expand All @@ -14,6 +25,7 @@ const UserAnalytics = () => {
email: user.emailAddresses[0].emailAddress,
avatar: user.imageUrl,
});

return <></>;
};

Expand Down
11 changes: 8 additions & 3 deletions src/components/Editor/Editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useRouter } from "next/navigation";
import { useJune } from "@/lib/hooks/useJune";
import { ArrowDownTrayIcon } from "@heroicons/react/24/solid";
import { cn } from "@/lib/utils";
import { usePostHog } from "posthog-js/react";

enum State {
EMPTY_VIDEO,
Expand Down Expand Up @@ -51,6 +52,7 @@ const Editor = ({ defaultCredits, defaultMeme = undefined }: Props) => {
const [credits, setCredits] = useState<number | undefined>(defaultCredits);
const router = useRouter();
const analytics = useJune();
const posthog = usePostHog();

const getCredits = async () => {
const resp = await fetch("/api/user");
Expand Down Expand Up @@ -133,7 +135,8 @@ const Editor = ({ defaultCredits, defaultMeme = undefined }: Props) => {
e.preventDefault();
const text = prompt.trim();
if (text.length <= 3) return;
analytics?.track("Generate Video");
analytics?.track("generate_video");
posthog.capture("generate_video");
generateVideo();
}}
>
Expand Down Expand Up @@ -181,7 +184,8 @@ const Editor = ({ defaultCredits, defaultMeme = undefined }: Props) => {
onClick={(e) => {
e.preventDefault();
generateVideo();
analytics?.track("Retry generating Video");
analytics?.track("retry_generating_video");
posthog.capture("retry_generating_video");
}}
disabled={state == State.SAVING_MEME}
>
Expand All @@ -192,7 +196,8 @@ const Editor = ({ defaultCredits, defaultMeme = undefined }: Props) => {
e.preventDefault();
setState(State.SAVING_MEME);
await generateGif();
await analytics?.track("Save meme");
await analytics?.track("save_meme");
await posthog.capture("save_meme");
router.push(`/meme/${meme?.id}`);
}}
disabled={state == State.SAVING_MEME}
Expand Down

0 comments on commit 28dd755

Please sign in to comment.