Skip to content

Conversation

@AbdallahAHO
Copy link
Contributor

@AbdallahAHO AbdallahAHO commented Jun 15, 2025

Deployment.mp4

Summary

This PR adds complete Astro framework support to the PostHog wizard, enabling developers to seamlessly integrate PostHog analytics into their Astro projects with AI-powered setup. The implementation follows PostHog's established patterns while optimizing specifically for Astro's architecture with an inline script approach.

What's Added

Core Features

  • Framework Detection: Automatic detection of Astro projects via astro package in package.json
  • AI-Powered Setup: Intelligent code generation using LLM to modify existing Astro projects
  • Optimized Integration: Inline PostHog script approach eliminating package dependencies
  • Complete Wizard Workflow: Full integration including component creation, layout setup, and configuration

New Files

  • src/astro/astro-wizard.ts - Main Astro wizard implementation
  • src/astro/docs.ts - Astro-specific documentation with dynamic variable injection
  • src/utils/rules/astro-rules.md - AI coding rules for Astro projects
  • e2e-tests/test-applications/astro-test-app/ - Complete Astro test application
  • e2e-tests/tests/astro.test.ts - E2E test suite for Astro integration

Modified Files

  • src/lib/constants.ts - Added Astro to Integration enum and descriptions
  • src/lib/config.ts - Added Astro detection logic and optimized success messages
  • src/run.ts - Added Astro wizard import and routing
  • bin.ts - Added 'astro' to CLI integration choices
  • README.md - Updated to include Astro integration documentation

Implementation Approach

Astro-Optimized Architecture (following PostHog Astro docs)

1. Inline Script Integration

Unlike other frameworks that rely on npm packages, Astro uses PostHog's inline script approach:

<!-- src/components/posthog.astro -->
<script is:inline type="text/javascript" id="posthog-js">
  !(function(t, e) {
    // Full PostHog loader script embedded directly
    // ... PostHog initialization code ...
  })(document, window.posthog || []);
  posthog.init('actual_api_key_here', { 
    api_host: 'actual_api_host_here' 
  });
</script>

Benefits:

  • No package dependencies
  • No environment variables needed
  • Zero configuration after setup
  • Smaller bundle size
  • Immediate functionality

2. Component-Based Architecture

Creates reusable Astro components following framework best practices:

PostHog Component (src/components/posthog.astro):

  • Self-contained PostHog initialization
  • Uses is:inline directive to prevent Astro processing
  • Direct API key injection

Layout System (src/layouts/PostHogLayout.astro):

  • Consistent analytics across all pages
  • Automatic PostHog component inclusion
  • Follows Astro layout patterns

Architecture Consistency

Follows PostHog wizard patterns:

  1. Detection Logic: Uses hasPackageInstalled('astro', packageJson) to auto-pick framework
  2. Wizard Structure: Implements standard step-by-step workflow
  3. AI Integration: Uses LLM prompts for intelligent code modification
  4. Step System: Integrates with existing steps (rules, MCP setup) - Didn't test them enough though but should work out of the box

Usage

Users can now run:

# Automatic detection
npx @posthog/wizard

# Explicit Astro selection
npx @posthog/wizard --integration=astro

The wizard will:

  1. ✅ Create PostHog component with embedded script and actual API keys
  2. ✅ Set up layout system for consistent tracking
  3. ✅ Add AI coding rules for future development
  4. ✅ Set up MCP server integration

What's NOT needed:

  • ❌ Package installation (posthog-js)
  • ❌ Environment variable configuration
  • ❌ Build configuration changes

Testing

E2E Testing

Created comprehensive test suite following existing patterns:

  • checkPackageJson() - Verifies no unnecessary package installations
  • checkIfRunsOnDevMode() - Tests development server
  • checkIfBuilds() - Verifies build process
  • checkIfRunsOnProdMode() - Tests preview mode

Test Application

  • Uses official create-astro minimal template
  • Includes TypeScript support
  • Follows standard Astro project structure
  • Builds and runs successfully without configuration

Manual Testing ✅

  • Framework detection works correctly
  • CLI integration choices include Astro
  • Wizard launches and completes successfully
  • Generated components work immediately
  • No environment variable setup required
  • PostHog tracking functional out of the box

User Experience

  • Zero Configuration: Works immediately after wizard completion
  • Faster Setup: No package installation or environment variable management
  • Fewer Dependencies: No additional npm packages required
  • Deployment Ready: No hosting provider environment variable setup needed

Breaking Changes

None. This is a pure addition that doesn't affect existing functionality.

Code Quality

  • All linting rules pass (pnpm lint)
  • TypeScript compilation successful (pnpm build)
  • All existing tests continue to pass (pnpm test)
  • Follows existing code patterns and conventions
  • Comprehensive E2E test coverage

Checklist

  • Added new Astro framework support following PostHog Wizard patterns
  • Implemented optimized inline script approach
  • Created comprehensive documentation with dynamic variable injection
  • Built complete E2E test suite with test application
  • Updated CLI and configuration files
  • Verified all existing functionality remains intact
  • Tested framework detection and wizard execution
  • Optimized for Astro-specific architecture and best practices as defined in PostHog docs
  • Updated README with Astro integration information

@AbdallahAHO AbdallahAHO marked this pull request as ready for review June 15, 2025 21:00
@codecov-commenter
Copy link

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@daniloc
Copy link
Collaborator

daniloc commented Jun 24, 2025

This is great, thank you so much for taking the time to do this.

just a couple of tweaks if you could:

  • in the initializer, set the defaults argument as in defaults: '2025-05-24'. It's helpful when the wizard implementations closely match the up-to-date docs so people can check the robot's work. We started using that argument recently.
  • instead of committing an already-integrated astro app in e2e-tests/test-applications, could you instead include a bare one? Makes it easier to check later that everything works as it should

Implements a basic structure for an Astro test application.

Includes essential files like configuration, package.json, and a README for guidance. Also introduces a PostHog component for analytics tracking and basic layout integration.

Updates the .gitignore to exclude unnecessary files and folders specific to build processes and local configurations.

Prepares for project expansion by setting up TypeScript support.

This foundational setup ensures a clean start for future development and testing.
Implements an interactive wizard for integrating PostHog with Astro applications.

The wizard guides users through installation steps, including setting up environment variables, confirming use of TypeScript, and handling package installations. It ensures users have the necessary packages and provides documentation for further configuration.

This addition enhances user experience by automating setup tasks and providing clear installation instructions. It further sets up analytics to track Astro version and SDK installation status.

No related issues or tickets are referenced.
Implements support for the Astro integration, enhancing the application’s analytics setup capabilities.

- Introduces the Astro configuration including detection and file filtering patterns.
- Adds Astro to the integration order and allows for running the associated setup wizard.
- Updates integration descriptions for better clarity in user prompts.

This addition streamlines the setup process for Astro projects and improves overall analytics functionality.
Implements a suite of end-to-end tests for the Astro application, ensuring its functionality in various environments.

Tests include verification of `package.json` updates, successful runs in development and production modes, and build correctness.

This facilitates automated testing and quality assurance in the deployment process.
Adds Astro to the list of supported frameworks for the wizard.

Updates the CLI integration options to reflect this new addition.

Encourages users to suggest further integrations via GitHub issues.
Refactors the Astro wizard to streamline PostHog integration steps.

Removes unnecessary TypeScript checks and redundant package installations, simplifying the user flow.

Introduces a new documentation structure and code snippets for easier implementation guidance.

Aims to improve user experience and integration efficiency for analytics in Astro projects.
Eliminates the steps for adding or updating environment variables in the Astro wizard process.

Updates the default changes in the integration configuration to reflect these removals while maintaining the functionality of the PostHog component.

This streamlining aims to simplify the wizard experience and reduce complexity in managing environment variables.
Cleans up the documentation by removing irrelevant Next Steps
related to PostHog instrumentation. This improves clarity
for users by eliminating confusion over previously suggested
steps no longer applicable.

Updates content to streamline the user experience in the
Astro and Next.js documentation sources.
Incorporates a default value for the date in the posthog initialization.

This change enhances the analytics integration by setting a specific defaults parameter, allowing for more consistent data handling.
Upgrades the Astro framework version to 5.10.1 and removes the PostHog integration components to have bare minimum for more accurate E2E tests
@AbdallahAHO
Copy link
Contributor Author

AbdallahAHO commented Jun 24, 2025

Hey @daniloc,

Thanks for the review. Addressed both points with 2eb5868 and aa909b4 and rebased to latest main

Let me know if there's any other feedback.

@daniloc daniloc self-requested a review June 25, 2025 15:39
Copy link
Collaborator

@daniloc daniloc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@daniloc daniloc merged commit 7d28b6a into PostHog:main Jun 25, 2025
6 checks passed
@daniloc
Copy link
Collaborator

daniloc commented Jun 25, 2025

Thanks again for the help here!

daniloc pushed a commit that referenced this pull request Oct 27, 2025
* Adds initial Astro test application setup

Implements a basic structure for an Astro test application.

Includes essential files like configuration, package.json, and a README for guidance. Also introduces a PostHog component for analytics tracking and basic layout integration.

Updates the .gitignore to exclude unnecessary files and folders specific to build processes and local configurations.

Prepares for project expansion by setting up TypeScript support.

This foundational setup ensures a clean start for future development and testing.

* add PostHog integration AI guidelines for Astro project

* Adds PostHog Astro wizard for seamless integration

Implements an interactive wizard for integrating PostHog with Astro applications.

The wizard guides users through installation steps, including setting up environment variables, confirming use of TypeScript, and handling package installations. It ensures users have the necessary packages and provides documentation for further configuration.

This addition enhances user experience by automating setup tasks and providing clear installation instructions. It further sets up analytics to track Astro version and SDK installation status.

No related issues or tickets are referenced.

* Adds Astro integration support to analytics setup

Implements support for the Astro integration, enhancing the application’s analytics setup capabilities.

- Introduces the Astro configuration including detection and file filtering patterns.
- Adds Astro to the integration order and allows for running the associated setup wizard.
- Updates integration descriptions for better clarity in user prompts.

This addition streamlines the setup process for Astro projects and improves overall analytics functionality.

* Adds end-to-end tests for Astro application

Implements a suite of end-to-end tests for the Astro application, ensuring its functionality in various environments.

Tests include verification of `package.json` updates, successful runs in development and production modes, and build correctness.

This facilitates automated testing and quality assurance in the deployment process.

* Updates README to include Astro integration

Adds Astro to the list of supported frameworks for the wizard.

Updates the CLI integration options to reflect this new addition.

Encourages users to suggest further integrations via GitHub issues.

* Enhances Astro Wizard for improved PostHog integration

Refactors the Astro wizard to streamline PostHog integration steps.

Removes unnecessary TypeScript checks and redundant package installations, simplifying the user flow.

Introduces a new documentation structure and code snippets for easier implementation guidance.

Aims to improve user experience and integration efficiency for analytics in Astro projects.

* Removes environment variable steps from wizard

Eliminates the steps for adding or updating environment variables in the Astro wizard process.

Updates the default changes in the integration configuration to reflect these removals while maintaining the functionality of the PostHog component.

This streamlining aims to simplify the wizard experience and reduce complexity in managing environment variables.

* Removes outdated PostHog documentation sections

Cleans up the documentation by removing irrelevant Next Steps
related to PostHog instrumentation. This improves clarity
for users by eliminating confusion over previously suggested
steps no longer applicable.

Updates content to streamline the user experience in the
Astro and Next.js documentation sources.

* Updates posthog initialization with default date

Incorporates a default value for the date in the posthog initialization.

This change enhances the analytics integration by setting a specific defaults parameter, allowing for more consistent data handling.

* Removes posthog components from Astro test app for more integral test

Upgrades the Astro framework version to 5.10.1 and removes the PostHog integration components to have bare minimum for more accurate E2E tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants