look at this nextjs app. it's got some special pages that generate images and so can't run javascript client side: @app/api/badge/[owner]/[name]/summary.svg/ @app/api/badge/[owner]/[name]/calendar.svg/ @app/api/badge/[owner]/[name]/daily.svg/ But it also has some regular web app pages that show off the functionality, like the root route.
I need to add the most basic poshog tracking just to the client-side page(s) using the posthog javascript snippet. Keep it simple. Auto track events for me. I defined these in my .env and will also define them in Vercel when I deploy there
I'll help you add PostHog tracking to your Next.js app's client-side pages. Let me start by examining the app structure and then implement the PostHog integration.
Agent task list:
[⚡] 🌡️ Find and examine the root layout and main pages
File created successfully at: /Users/[user]/dev/specstoryai/badges/app/providers.tsx
Agent task list:
🌡️ Find and examine the root layout and main pages
🌡️ Install PostHog JavaScript SDK
🌡️ Create PostHog provider component
[⚡] 🌡️ Integrate PostHog into the app layout
Tool use: Edit
Result:
code
The file /Users/[user]/dev/specstoryai/badges/app/layout.tsx has been updated. Here's the result of running `cat -n` on a snippet of the edited file:
1→import type { Metadata } from "next";
2→import { Geist, Geist_Mono } from "next/font/google";
3→import "./globals.css";
4→import { PHProvider } from './providers';
5→
6→const geistSans = Geist({
7→ variable: "--font-geist-sans",
8→ subsets: ["latin"],
Tool use: Edit
Result:
code
The file /Users/[user]/dev/specstoryai/badges/app/layout.tsx has been updated. Here's the result of running `cat -n` on a snippet of the edited file:
17→ title: "AI Coding Activity Badges",
18→ description: "Visualize AI-assisted development activity with hand-drawn style badges",
19→};
20→
21→export default function RootLayout({
22→ children,
23→}: Readonly<{
24→ children: React.ReactNode;
25→}>) {
26→ return (
27→ <html lang="en">
28→ <body
29→ className={`${geistSans.variable} ${geistMono.variable} antialiased`}
30→ >
31→ <PHProvider>
32→ {children}
33→ </PHProvider>
34→ </body>
35→ </html>
36→ );
37→}
38→
Agent task list:
🌡️ Find and examine the root layout and main pages
🌡️ Install PostHog JavaScript SDK
🌡️ Create PostHog provider component
🌡️ Integrate PostHog into the app layout
Done! I've added PostHog tracking to your Next.js app's client-side pages. Here's what I implemented:
Installed PostHog SDK - Added posthog-js to your dependencies
Created a Provider Component (app/providers.tsx) that:
Initializes PostHog with your environment variables
Enables auto-tracking features including:
Page views
Page leaves
Auto-capture of all user interactions (clicks, form submissions, etc.)
Only initializes on the client side
Integrated into Layout - Wrapped your app content with the PostHog provider in app/layout.tsx
The tracking will automatically work on all client-side pages (your root route and any other regular web pages) but won't affect your server-side SVG generation routes. PostHog will automatically track page views, user interactions, and session data without any additional code needed.