Skip to main content
skill CX Codex

integration-astro-ssr — Claude Code skill: setup guide

integration-astro-ssr adds PostHog analytics to server-rendered Astro setups with API endpoints. Connects frontend tracking with backend routes.

MIT 109 installs ★ 80 updated 2026-10-24

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add PostHog/skills --skill integration-astro-ssr -a claude-code
Open on GitHub

What integration-astro-ssr does

The skill outlines a four-stage workflow to embed analytics into server-side Astro projects. It instructs the assistant to build a shared src/components/posthog.astro template and inject it through a root layout. Client script tags receive the is:inline attribute to prevent Astro from parsing them and throwing TypeScript build errors.

For server endpoints under src/pages/api/, tracking relies on the posthog-node library. The instructions enforce a singleton setup inside src/lib/posthog-server.ts to stop API routes from spinning up redundant instances.

Cross-boundary tracking passes X-POSTHOG-DISTINCT-ID and X-POSTHOG-SESSION-ID headers from frontend calls into backend handlers. Hardcoding API tokens is strictly forbidden; public keys must adopt the PUBLIC_ prefix in environment files.

What it helps with

  • set up PostHog tracking in SSR Astro applications
  • correlate frontend sessions with backend API requests via custom headers
  • configure a posthog-node singleton for server endpoints
  • embed error tracking across critical user flows and API boundaries

Example prompts for the agent

  • Integrate PostHog into this Astro SSR project following the official pattern
  • Set up posthog-node singleton for our Astro API routes
  • Add user identification across Astro frontend and backend endpoints

How to install integration-astro-ssr

1Skills CLI (Vercel)

npx skills add PostHog/skills --skill integration-astro-ssr -a claude-code

2Manually: put the files in place

~/.claude/skills/integration-astro-ssr/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/integration-astro-ssr/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about integration-astro-ssr

How to install integration-astro-ssr in Claude Code?

Run npx skills add PostHog/skills --skill integration-astro-ssr -a claude-code in your workspace.

What does integration-astro-ssr do?

It guides code assistants through integrating PostHog client scripts and posthog-node server handlers into Astro SSR projects.

Why does the skill require the is:inline directive?

Astro parses standard script tags by default, which triggers TypeScript compilation errors on PostHog snippet code.

Source text

SKILL.md · MIT
---
name: integration-astro-ssr
description: PostHog integration for server-rendered Astro applications with API routes
metadata:
  author: PostHog
  version: 1.9.4
---

# PostHog integration for Astro (SSR)

This skill helps you add PostHog analytics to Astro (SSR) applications.

## Workflow

Follow these steps in order to complete the integration:

1. `basic-integration-1.0-begin.md` - PostHog Setup - Begin ← **Start here**
2. `basic-integration-1.1-edit.md` - PostHog Setup - Edit
3. `basic-integration-1.2-revise.md` - PostHog Setup - Revise
4. `basic-integration-1.3-conclude.md` - PostHog Setup - Conclusion

## Reference files

- `references/EXAMPLE.md` - Astro (SSR) example project code
- `references/astro.md` - Astro - docs
- `references/identify-users.md` - Identify users - docs
- `references/basic-integration-1.0-begin.md` - PostHog setup - begin
- `references/basic-integration-1.1-edit.md` - PostHog setup - edit
- `references/basic-integration-1.2-revise.md` - PostHog setup - revise
- `references/basic-integration-1.3-conclude.md` - PostHog setup - conclusion

The example project shows the target implementation pattern. Consult the documentation for API details.

## Key principles

- **Environment variables**: Always use environment variables for PostHog keys. Never hardcode them.
- **Minimal changes**: Add PostHog code alongside existing integrations. Don't replace or restructure existing code.
- **Match the example**: Your implementation should follow the example project's patterns as closely as possible.

## Framework guidelines

- Always use the is:inline directive on PostHog script tags to prevent Astro from processing them and causing TypeScript errors
- Use PUBLIC_ prefix for client-side environment variables in Astro (e.g., PUBLIC_POSTHOG_PROJECT_TOKEN)
- Create a posthog.astro component in src/components/ for reusable initialization across pages
- Import the PostHog component in a Layout and wrap all pages with that layout
- Use posthog-node in API routes under src/pages/api/ for server-side event tracking
- Store the posthog-node client instance in a singleton pattern (src/lib/posthog-server.ts) to avoid creating multiple clients
- Pass the client session ID to server via X-PostHog-Session-Id header for unified session tracking

## Identifying users

Identify users during login and signup events. Refer to the example code and documentation for the correct identify pattern for this framework. If both frontend and backend code exist, pass the client-side session and distinct ID using `X-POSTHOG-DISTINCT-ID` and `X-POSTHOG-SESSION-ID` headers to maintain correlation.

## Error tracking

Add PostHog error tracking to relevant files, particularly around critical user flows and API boundaries.

© PostHog · MIT · PostHog/skills

Similar skills

All skills for Frontend and UI →

From the same set112

The whole set →
feature-flags-react feature-flags-react adds PostHog feature flag checks to a React app and keeps them clear of the code that already works. CX Codex +1 112installs feature-flags-api The feature-flags-api skill guides agents through adding PostHog feature flag checks to API applications. Use it when you want to introduce flags without restructuring existing code. CX Codex +1 111installs integration-react-native The integration-react-native skill guides an agent through adding PostHog analytics to a React Native app, including user identification and error tracking. CX Codex +1 109installs feature-flags-react-native feature-flags-react-native integrates PostHog feature flags into React Native apps following best practices for hooks, environment variables, and navigation. CX Codex +1 109installs integration-swift integration-swift guides PostHog analytics integration into Swift applications across iOS and macOS targets. CX Codex +1 108installs integration-react-vite integration-react-vite adds PostHog analytics to a React app built with Vite and no router: setup in four steps, user identification, feature flags and error tracking. CX Codex +1 107installs omnibus-instrument-llm-analytics omnibus-instrument-llm-analytics adds PostHog LLM analytics to your code so each generation reports tokens, latency and cost. Installs the SDK too when the project has none. CX Codex +1 106installs integration-nextjs-pages-router The integration-nextjs-pages-router skill guides PostHog setup in Next.js Pages Router apps. It covers analytics events, user identity, feature flags, and error tracking. CX Codex +1 103installs

Project sponsors

Companies that keep this analytics open