PostHog Analytics

PostHog is the open-source analytics platform wired in by default. Track every page view, product interaction, checkout step, and purchase. All without sending your data to third parties. posthog-js is already listed in your dependencies.

Last updated: 2026-03-29

Create a Project & Add Your Key

Sign up at posthog.com, create a new project, and copy your Project API key. Add it to your .env file. Flux reads it via import.meta.env so it's never hard-coded.

.env
# .env
PUBLIC_POSTHOG_KEY=phc_your_project_api_key
PUBLIC_POSTHOG_HOST=https://us.i.posthog.com

Wire It Into the Flux Layout

The template uses a BaseHead.astro component loaded on every page. Drop the Analytics component there so PostHog initialises once and auto-captures every route change.

src/components/Analytics.jsx
// src/components/Analytics.jsx
import { useEffect } from 'react';
import posthog from 'posthog-js';

export default function Analytics() {
  useEffect(() => {
    if (typeof window === 'undefined') return;
    posthog.init(import.meta.env.PUBLIC_POSTHOG_KEY, {
      api_host: import.meta.env.PUBLIC_POSTHOG_HOST,
      capture_pageview: true,
      capture_pageleave: true,
      persistence: 'localStorage',
    });
  }, []);
  return null;
}

// src/layout/BaseHead.astro — add inside <head>
---
import Analytics from '../components/Analytics.jsx';
---
<Analytics client:load />

Template-Specific Events to Track

Flux ships with e-commerce, blog, and SaaS pages. Here are the key events to capture across the template so you can build funnels that actually mean something.

Terminal
import posthog from 'posthog-js';

// — Hero / Landing —
posthog.capture('hero_cta_clicked', { variant: 'primary', page: '/' });

// — Gallery / Showcase —
posthog.capture('template_viewed', { slug: 'breville-machines', category: 'e-commerce' });
posthog.capture('demo_link_clicked', { slug: 'breville-machines' });
posthog.capture('buy_button_clicked', { slug: 'breville-machines', price: 120 });

// — Checkout (Polar.sh / Stripe) —
posthog.capture('checkout_started', { plan: 'lifetime', amount: 149 });
posthog.capture('purchase_completed', { plan: 'lifetime', amount: 149, currency: 'usd' });

// — Blog —
posthog.capture('post_read', { slug: 'getting-started', read_time_seconds: 240 });

// — Auth (Clerk) —
posthog.capture('sign_up_completed', { method: 'email' });
posthog.identify(user.id, { email: user.emailAddresses[0].emailAddress, plan: 'lifetime' });
EventWhere to fire itKey properties
hero_cta_clickedsrc/components/hero/Hero.jsxvariant, page
template_viewedsrc/components/pages/Gallery.jsxslug, category, price
buy_button_clickedsrc/components/pages/Gallery.jsxslug, price
checkout_startedPolar.sh redirect handlerplan, amount
purchase_completedStripe / Polar webhookplan, amount, currency
post_readsrc/pages/blog/[slug].astroslug, read_time_seconds
sign_up_completedClerk onSignUp callbackmethod