- Published on
Installing PostHog in my SaaS
- Authors
- Name
- Karlo Karlović
Motivation
Adding PostHog Analytics to my SaaS, Karstack: A (literal) 5-Minute Integration
I just added PostHog analytics to Karstack.com, and I'm genuinely impressed by how seamless the integration process was. As someone who values efficient developer workflows, the experience was refreshingly straightforward.
First, let's talk about why I chose PostHog. I needed a comprehensive analytics solution that could handle event tracking, session replay, and feature flags without overwhelming complexity. PostHog ticked all these boxes while allowing me to keep data within my control.
The Technical Implementation
The integration took literally minutes. Here's how I did it:
npm install --save posthog-js
Next, I added my PostHog API key and host to my environment variables:
// .env.local
NEXT_PUBLIC_POSTHOG_KEY=phc_myKeyHere
NEXT_PUBLIC_POSTHOG_HOST=https://us.i.posthog.com
Since I'm using Next.js 15.3+, I took advantage of the new instrumentation-client.ts file for initialization:
// instrumentation-client.ts
import posthog from 'posthog-js'
posthog.init(process.env.NEXT_PUBLIC_POSTHOG_KEY, {
api_host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
defaults: '2025-05-24',
})
That's it! No need for complicated provider wrapping or convoluted setup. PostHog automatically started capturing pageviews and user interactions across Karstack.
PostHog also offers a survey feature that allows you to collect user feedback directly within your application. It honestly took me longer to think about the survey questions than it took to implement PostHog.
Why This Matters
The simplicity of this integration allowed me to focus on what matters - building Karstack's core features. PostHog's approach respects modern development patterns, particularly Next.js's hybrid rendering model.
I'm especially impressed with how PostHog handled feature flags. I can now safely roll out experimental features to specific user segments without pushing potentially breaking changes to everyone at once.
// Example of a feature flag check in a component
'use client'
import posthog from 'posthog-js'
export default function NewFeature() {
const enabled = posthog.isFeatureEnabled('new-dashboard')
if (!enabled) return null
return <div>New dashboard experience!</div>
}
PostHog's documentation is clear and includes specific Next.js integration examples, which saved me from spending hours figuring out best practices.
Final Thoughts
If you're building a SaaS product on Next.js, I highly recommend giving PostHog a try. The developer experience is exceptional, and the insights you'll gain about your users are invaluable. The fact that it took me longer to write this blog post than to implement PostHog speaks volumes.
Next up, I'll be exploring their server-side analytics to track important backend events without relying on client-side code. Stay tuned!