Skip to content

Wire PostHog analytics into AI Notebook template#55

Merged
CarmenDou merged 2 commits into
mainfrom
feat/posthog-analytics
Jun 10, 2026
Merged

Wire PostHog analytics into AI Notebook template#55
CarmenDou merged 2 commits into
mainfrom
feat/posthog-analytics

Conversation

@CarmenDou

@CarmenDou CarmenDou commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds PostHog analytics across the AI Notebook template, instrumented via the official PostHog Next.js wizard. Tracks 11 custom events spanning auth, chat, document upload, workspace creation, and AI-feature usage, plus identifies signed-in users for cohort analysis. Already deployed to https://aipdfchat.insforge.site and emitting events to PostHog.

What lands in this PR

New files:

  • instrumentation-client.ts — client-side PostHog init via Next 13.4+ instrumentation hook, with /ingest reverse-proxy hosts
  • lib/posthog-server.ts — server-side singleton for API-route event captures

Config:

  • next.config.js/ingest/* rewrites so client requests avoid ad blockers
  • package.json — adds posthog-js and posthog-node
  • .env.example — documents the two new optional env vars (NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN, NEXT_PUBLIC_POSTHOG_HOST); template still works with them blank

Instrumented events (11):

Event File Notable properties
user_signed_up components/sign-up-form.tsx also calls posthog.identify
user_signed_in components/sign-in-form.tsx also calls posthog.identify
document_uploaded app/api/documents/upload/route.ts file_size_bytes, chunk_count, ingest_status
chat_message_sent app/api/chat/route.ts is_new_chat, workspace_id
workspace_created app/api/workspaces/route.ts workspace_id, workspace_name
flashcards_generated components/flashcards-modal.tsx card_count
flashcard_graded app/api/flashcards/[id]/grade/route.ts grade, next_interval_days
audio_overview_generated app/api/workspaces/[id]/audio/route.ts document_count, script_turn_count
mindmap_generated app/api/workspaces/[id]/mindmap/route.ts document_count
chat_share_link_created components/share-chat-button.tsx chat_id
chat_share_link_revoked components/share-chat-button.tsx chat_id

Opt-in design

The template stays functional with no PostHog token configured. With the env vars blank, all posthog.* calls become inert no-ops at runtime. Fork-and-go users do not need a PostHog account to ship.

Test plan

  • npm run build passes
  • With NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN set, sign-up triggers user_signed_up in PostHog Live Events
  • Uploading a PDF triggers document_uploaded with non-zero chunk_count
  • Generating a mindmap triggers mindmap_generated
  • Generating audio triggers audio_overview_generated
  • Without the env var, the app still runs and no requests hit /ingest/*

🤖 Generated with Claude Code


Summary by cubic

Adds PostHog analytics to the AI Notebook template with client/server setup, reverse‑proxied /ingest endpoints that honor NEXT_PUBLIC_POSTHOG_HOST, and 11 events across auth, chat, uploads, workspaces, and learning tools. Identifies signed‑in users and gracefully swallows analytics failures; fully optional when env vars are unset.

  • New Features

    • Client init via Next.js instrumentation and a server capture helper; /ingest/* is reverse‑proxied and the assets host is derived from NEXT_PUBLIC_POSTHOG_HOST (US/EU/self‑host) to avoid ad blockers.
    • Captures 11 events with useful properties (auth sign‑in/up with identify and captureException, chat messages, document uploads, workspace creation, flashcards generated/graded, audio overview, mindmap, share link create/revoke); API routes call posthog.shutdown() and swallow failures so requests don't error.
    • Added dependencies: posthog-js, posthog-node.
  • Migration

    • To enable, set NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN and optionally NEXT_PUBLIC_POSTHOG_HOST (example: https://us.i.posthog.com).

Written for commit 6b91b19. Summary will update on new commits.

Review in cubic

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 issues found across 16 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread ai-pdf-chatbot/app/api/chat/route.ts Outdated
Comment thread ai-pdf-chatbot/app/api/documents/upload/route.ts Outdated
Comment thread ai-pdf-chatbot/app/api/flashcards/[id]/grade/route.ts Outdated
Comment thread ai-pdf-chatbot/next.config.js Outdated

@Fermionic-Lyu Fermionic-Lyu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Approved.

@CarmenDou
CarmenDou merged commit f671cc9 into main Jun 10, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants