Wire PostHog analytics into AI Notebook template#55
Merged
Conversation
There was a problem hiding this comment.
4 issues found across 16 files
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
…OG_HOST in rewrites
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/ingestreverse-proxy hostslib/posthog-server.ts— server-side singleton for API-route event capturesConfig:
next.config.js—/ingest/*rewrites so client requests avoid ad blockerspackage.json— addsposthog-jsandposthog-node.env.example— documents the two new optional env vars (NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN,NEXT_PUBLIC_POSTHOG_HOST); template still works with them blankInstrumented events (11):
user_signed_upcomponents/sign-up-form.tsxposthog.identifyuser_signed_incomponents/sign-in-form.tsxposthog.identifydocument_uploadedapp/api/documents/upload/route.tsfile_size_bytes,chunk_count,ingest_statuschat_message_sentapp/api/chat/route.tsis_new_chat,workspace_idworkspace_createdapp/api/workspaces/route.tsworkspace_id,workspace_nameflashcards_generatedcomponents/flashcards-modal.tsxcard_countflashcard_gradedapp/api/flashcards/[id]/grade/route.tsgrade,next_interval_daysaudio_overview_generatedapp/api/workspaces/[id]/audio/route.tsdocument_count,script_turn_countmindmap_generatedapp/api/workspaces/[id]/mindmap/route.tsdocument_countchat_share_link_createdcomponents/share-chat-button.tsxchat_idchat_share_link_revokedcomponents/share-chat-button.tsxchat_idOpt-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 buildpassesNEXT_PUBLIC_POSTHOG_PROJECT_TOKENset, sign-up triggersuser_signed_upin PostHog Live Eventsdocument_uploadedwith non-zerochunk_countmindmap_generatedaudio_overview_generated/ingest/*🤖 Generated with Claude Code
Summary by cubic
Adds PostHog analytics to the AI Notebook template with client/server setup, reverse‑proxied
/ingestendpoints that honorNEXT_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
/ingest/*is reverse‑proxied and the assets host is derived fromNEXT_PUBLIC_POSTHOG_HOST(US/EU/self‑host) to avoid ad blockers.identifyandcaptureException, chat messages, document uploads, workspace creation, flashcards generated/graded, audio overview, mindmap, share link create/revoke); API routes callposthog.shutdown()and swallow failures so requests don't error.posthog-js,posthog-node.Migration
NEXT_PUBLIC_POSTHOG_PROJECT_TOKENand optionallyNEXT_PUBLIC_POSTHOG_HOST(example:https://us.i.posthog.com).Written for commit 6b91b19. Summary will update on new commits.