integration-astro-static

Integrate PostHog analytics into static Astro sites with an inline snippet and reusable layout.

58|5|Updated Jan 19, 2026
One-click install
npx skills add https://github.com/PostHog/skills --skill integration-astro-static
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: integration-astro-static
Source: https://github.com/PostHog/skills/tree/main/skills/posthog/integration/skills/astro-static
Command: npx skills add https://github.com/PostHog/skills --skill integration-astro-static

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve?

Enable analytics and error tracking for Astro static sites by integrating PostHog's JavaScript Web SDK with a reusable, is:inline-based snippet and layout pattern.

Core Features & Use Cases

  • Inline PostHog snippet to prevent Astro from processing client-side code
  • Reusable PostHogLayout and PostHog component to wrap pages and initialize analytics consistently
  • Guidance and examples for identifying users, tracking events, and handling errors in Astro static projects

Quick Start

Copy the integration directory into your Astro project, set your PostHog tokens in environment variables, and wrap your pages with the PostHogLayout to enable analytics and error tracking.

Frequently Asked Questions about integration-astro-static

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I add PostHog analytics to an Astro static site?

Add PostHog analytics to an Astro static site by embedding an inline PostHog snippet and wrapping pages with a reusable layout. This approach initializes product analytics without altering existing build workflows.

Why does my PostHog script break when integrated into Astro?

PostHog scripts break when Astro processes client-side code instead of treating it as static. Use an is:inline snippet within a dedicated layout to prevent Astro from transforming the PostHog JavaScript SDK during the build.

Do I need environment variables to track events in Astro with PostHog?

Yes, environment variables are required to store PostHog tokens securely. The integration enforces environment-variable usage for keys to initialize event tracking and user identification safely within your Astro project.

Can I identify users and track errors in a static Astro project?

You can identify users, track events, and handle errors in a static Astro project using the PostHog JavaScript Web SDK. The integration provides reusable components and guidance to implement these analytics features consistently.

What is the best way to initialize PostHog across multiple Astro pages?

The best way to initialize PostHog across multiple Astro pages is using a reusable PostHogLayout. Wrapping pages with this layout ensures the inline PostHog snippet initializes analytics consistently throughout the static site.