Skip to content

feat(analytics): add PostHog analytics package - #16

Merged
vijaysingh2219 merged 1 commit into
mainfrom
feat/analytics-package
Nov 23, 2026
Merged

vijaysingh2219 merged 1 commit into
mainfrom
feat/analytics-package

Conversation

@vijaysingh2219

Copy link
Copy Markdown
Member

Problem

Applications built on Stackbase need product analytics, user identification, and feature flag evaluation across both the Next.js frontend (apps/web) and the backend API (apps/api). Right now developers have to wire posthog-js, posthog-node, environment validation, and local development no-op guards manually inside each app.

Change

Adds @workspace/analytics (templates/base/packages/analytics) as a dual client and server analytics package powered by PostHog:

  • Server Analytics & Feature Flags (src/server.ts, @workspace/analytics/server): getPostHogServer, captureServerEvent, identifyServerUser, isFeatureEnabled, getFeatureFlag (with fail-open defaultValue fallbacks on network errors), and shutdownAnalytics powered by posthog-node.
  • Client Analytics & Feature Flags (src/client.ts, @workspace/analytics/client): initPostHogClient, captureClientEvent, identifyClientUser, resetClientAnalytics, isClientFeatureEnabled, and getClientFeatureFlag powered by posthog-js without pulling server dependencies into browser bundles.
  • Zero-Config Local Dev & Environment Schema (src/keys.ts, .env.example, turbo.json): Validates POSTHOG_API_KEY, POSTHOG_HOST, NEXT_PUBLIC_POSTHOG_KEY, and NEXT_PUBLIC_POSTHOG_HOST via @t3-oss/env-nextjs and Zod, no-ops when keys are unset or still set to template placeholders (your-posthog-*), and registers posthog-js and posthog-node in templates/base/pnpm-workspace.yaml.

Scope

Closes #15

Third module in the stackbase add expansion (@workspace/payments, @workspace/ai, @workspace/analytics).

Verification

Verified environment schema defaults, template placeholder no-op behavior, server event capture and feature flag evaluation, graceful shutdown, and client event capture and flag checks in templates/base/packages/analytics using:

pnpm --dir templates/base --filter @workspace/analytics check-types
pnpm --dir templates/base --filter @workspace/analytics lint
pnpm --dir templates/base --filter @workspace/analytics test
pnpm --dir templates/base --filter @workspace/analytics build

- Add @workspace/analytics package powered by PostHog to base template
- Expose dual ./client (posthog-js) and ./server (posthog-node) entrypoints
- Implement client and server event tracking, user identification, and feature flag helpers
- Gracefully no-op in local development when PostHog keys are unset or use template placeholders
- Validate PostHog environment variables with @t3-oss/env-nextjs and Zod
- Add unit test suite for server and client analytics and feature flag evaluation
@vercel

vercel Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
stackbase-docs Ready Ready Preview Nov 23, 2026 6:08pm UTC

@vijaysingh2219 vijaysingh2219 self-assigned this Nov 23, 2026
@vijaysingh2219 vijaysingh2219 added the area:templates Starter templates (templates/base, templates/web, templates/api) label Nov 23, 2026
@vijaysingh2219
vijaysingh2219 merged commit 5284a25 into main Nov 23, 2026
10 checks passed

This branch was successfully deployed

1 active deployment
Preview — e0a3b065 Deployed Nov 23, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:templates Starter templates (templates/base, templates/web, templates/api)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(analytics): add analytics package

1 participant