Skip to content

feat: add PostHog analytics integration - #16

Merged
harshjdhv merged 1 commit into
mainfrom
posthog/instrumentation-0c58a0
Aug 1, 2026
Merged

feat: add PostHog analytics integration#16
harshjdhv merged 1 commit into
mainfrom
posthog/instrumentation-0c58a0

Conversation

@posthog

@posthog posthog Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR adds PostHog analytics to the apps/web Next.js app using the posthog-js SDK.

  • apps/web/instrumentation-client.ts - new PostHog client initialization (reads the project token/host from env vars, enables exception capture)
  • apps/web/app/global-error.tsx - new Next.js global error boundary that reports uncaught client exceptions to PostHog
  • apps/web/components/copy-button.tsx, apps/web/components/docs-code-panel.tsx, apps/web/components/package-manager-command.tsx - capture a content_copied / component_install_command_copied event when a user copies code or an install command
  • apps/web/components/landing/hero-buttons.tsx - capture components_browse_started on the hero "Browse components" click
  • apps/web/components/landing/custom-work-cta.tsx - capture custom_work_contact_started on the custom-work email CTA
  • apps/web/app/sponsors/page.tsx - capture sponsor_checkout_started (with sponsorship_tier) on sponsor checkout CTAs
  • apps/web/package.json / pnpm-lock.yaml - adds posthog-js@^1.407.2
  • apps/web/.env.example - documents the two required env vars
  • turbo.json - declares NODE_ENV in globalEnv so the new instrumentation file passes this repo's lint rule (turbo/no-undeclared-env-vars)

All captured events are anonymous, visitor-level interactions — no PII is collected, and no new dashboards/insights were added beyond what the wizard created (listed below).

Insights and dashboards created

Type Name Link
Dashboard Your starter dashboard https://us.posthog.com/project/537835/dashboard/1938816
Dashboard Analytics basics (wizard) https://us.posthog.com/project/537835/dashboard/1938824
Insight Pageviews (last 7 days) https://us.posthog.com/project/537835/insights/M6g2fXZk
Insight Sessions (last 7 days) https://us.posthog.com/project/537835/insights/FwxnTM3l
Insight Daily active users (DAUs) https://us.posthog.com/project/537835/insights/6j3rYrRE
Insight Weekly active users (WAUs) https://us.posthog.com/project/537835/insights/zWTCCn1D
Insight Active users (last 30 days) https://us.posthog.com/project/537835/insights/knfiJ2fg
Insight Retention https://us.posthog.com/project/537835/insights/Rsse4WCs
Insight Top referrers https://us.posthog.com/project/537835/insights/4lUCOn92
Insight Visit to interaction funnel https://us.posthog.com/project/537835/insights/y3KdhUE7
Insight Component browsing starts (wizard) https://us.posthog.com/project/537835/insights/PcLgwZNy
Insight Install command copies over time (wizard) https://us.posthog.com/project/537835/insights/aRQgxWF3
Insight Content copies over time (wizard) https://us.posthog.com/project/537835/insights/BOwFsAzM
Insight Custom work contacts (wizard) https://us.posthog.com/project/537835/insights/MuCvnZGb
Insight Sponsorship checkout starts (wizard) https://us.posthog.com/project/537835/insights/5QKJmi7a

How to verify

  1. Set the environment variables from the section below, then run the app locally (pnpm dev in apps/web) or deploy this branch.
  2. Click around the site: browse components from the hero, copy an install/code snippet, visit /sponsors and click a sponsorship CTA, or use the custom-work contact CTA.
  3. In PostHog, open Activity — you should see $pageview and the custom events above (e.g. components_browse_started, content_copied) arriving within a minute.

Environment variables (action needed before PostHog works)

This app deploys to Vercel (per the README's Vercel OSS badge), which keeps production environment variables in the Vercel dashboard rather than in a file committed to this repository — .env* is git-ignored and no .vercel project link exists in this checkout, so I could not configure these automatically.

Environment variables are named settings your hosting provider passes to the app when it runs — they let the app know things (like the PostHog project token below) without hardcoding them in the code. PostHog will not receive any data until these two are set:

Name Value
NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN phc_tNUeWQywgt8aB4VGcxz98wtrpysvWBvfGWRrFV5pEAvx
NEXT_PUBLIC_POSTHOG_HOST https://us.i.posthog.com

To set them via the dashboard:

  1. Open https://vercel.com/dashboard and click on this project.
  2. Go to Settings, then Environment Variables in the left sidebar.
  3. For each row in the table above: paste the Name and Value, leave all environments checked, and click Save.
  4. Redeploy the app (Deployments tab, "..." menu on the latest deployment, Redeploy) — the new settings only take effect on the next deployment.

Setting environment variables via a local CLI

If you have the Vercel CLI installed and linked to this project, you can set them from your terminal instead:

echo "phc_tNUeWQywgt8aB4VGcxz98wtrpysvWBvfGWRrFV5pEAvx" | npx vercel env add NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN production
echo "https://us.i.posthog.com" | npx vercel env add NEXT_PUBLIC_POSTHOG_HOST production

Notes

  • apps/web/components/floating-docs-sidebar-panel.tsx has one pre-existing eslint react-hooks/exhaustive-deps warning unrelated to this change (present on main before this PR); left untouched per scope.

Created with PostHog Code

Adds posthog-js and instruments key visitor interactions (component browsing, install command copies, content copies, sponsor checkout, custom work contact) plus client-side exception capture. Declares NODE_ENV in turbo.json's globalEnv so the new instrumentation client passes lint.

Generated-By: PostHog Code
Task-Id: f45ab670-9c2c-4f9f-bd3e-76ac29457c69
@vercel

vercel Bot commented Aug 1, 2026

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
componentry Ready Ready Preview Aug 1, 2026 3:10pm

Request Review

@harshjdhv
harshjdhv marked this pull request as ready for review August 1, 2026 15:24
Copilot AI review requested due to automatic review settings August 1, 2026 15:24
@harshjdhv
harshjdhv merged commit 34cecb2 into main Aug 1, 2026
4 checks passed

Copilot AI 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.

Pull request overview

Adds PostHog analytics to the apps/web Next.js app, wiring up SDK initialization, global error reporting, and several UI interaction events (copy actions, hero CTA, custom-work email CTA, and sponsor CTAs).

Changes:

  • Add posthog-js dependency and initialize PostHog on the client (env-driven, debug in development, exception capture enabled).
  • Track key UX interactions via posthog.capture(...) across landing, docs/code copy, and sponsors flows.
  • Document required env vars and adjust Turbo config for env-var linting.

Reviewed changes

Copilot reviewed 11 out of 12 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
turbo.json Declares global env vars for Turbo/ESLint env-var tracking.
pnpm-lock.yaml Locks posthog-js and transitive dependencies.
apps/web/package.json Adds posthog-js dependency.
apps/web/instrumentation-client.ts Initializes PostHog based on NEXT_PUBLIC_* env vars.
apps/web/app/global-error.tsx Reports uncaught client errors to PostHog.
apps/web/components/copy-button.tsx Captures copy events when users copy content.
apps/web/components/docs-code-panel.tsx Threads optional copy-event naming to CopyButton.
apps/web/components/package-manager-command.tsx Captures install-command copy event.
apps/web/components/landing/hero-buttons.tsx Captures “browse components” start and install copy.
apps/web/components/landing/custom-work-cta.tsx Captures “custom work contact” start on mailto click.
apps/web/app/sponsors/page.tsx Captures sponsor checkout started events (incl tier on pricing cards).
apps/web/.env.example Documents PostHog env vars.
Files not reviewed (1)
  • pnpm-lock.yaml: Generated file

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread turbo.json
{
"$schema": "https://turbo.build/schema.json",
"ui": "tui",
"globalEnv": ["NODE_ENV"],
Comment on lines +17 to +19
throw new Error(
`${missingVariable} variable required by PostHog is missing or un-configured, this causes events to be silently missed. This error stops appearing once ${missingVariable} is configured`,
);
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