Skip to content

"Optional Feature: Adding Web Analytics"

Why Analytics Aren’t Included by Default

Section titled “Why Analytics Aren’t Included by Default”

This template intentionally does not include any analytics provider out-of-the-box. This decision is rooted in two core principles:

  1. Privacy-First: We believe in respecting user privacy. Including analytics by default, especially those that use cookies or track personal data, would go against this principle.
  2. User Choice: The world of web analytics is vast. Forcing a specific provider on you would be overly prescriptive. You should have the freedom to choose the tool that best fits your project’s needs and your company’s privacy policy.

This guide provides simple, copy-paste-ready recipes for adding two popular, privacy-respecting analytics providers: Plausible and Fathom.

The process for adding any analytics provider follows three simple steps:

  1. Create an Analytics.astro component to hold the provider’s script.
  2. Add your site-specific keys to your environment variables.
  3. Import and place the component in your BaseLayout.astro file.

Plausible is a lightweight, open-source, and cookie-free analytics tool.

Create a new file at src/components/Analytics.astro and add the following code. This component will only render in a production environment.

src/components/Analytics.astro
---
const { PUBLIC_PLAUSIBLE_DOMAIN } = import.meta.env;
---
{
import.meta.env.PROD && PUBLIC_PLAUSIBLE_DOMAIN && (
<script
is:inline
defer
data-domain={PUBLIC_PLAUSIBLE_DOMAIN}
src="https://plausible.io/js/script.js"
/>
)
}

Open your .env file (or create one by copying .env.example) and add your Plausible domain.

.env
PUBLIC_PLAUSIBLE_DOMAIN="your-domain.com"

Finally, import and add the component to the <head> section of your src/layouts/BaseLayout.astro file.

src/layouts/BaseLayout.astro
---
import Analytics from '@/components/Analytics.astro'; // <-- Import the component
// ... other imports
---
<html lang="en">
<head>
{/* ... other head tags ... */}
<Analytics /> {/* <-- Add it here */}
</head>
<body>
{/* ... body content ... */}
</body>
</html>

Fathom is another popular, simple, and privacy-focused analytics service.

Create a new file at src/components/Analytics.astro and add the following code.

src/components/Analytics.astro
---
const { PUBLIC_FATHOM_SITE_ID } = import.meta.env;
---
{
import.meta.env.PROD && PUBLIC_FATHOM_SITE_ID && (
<script
is:inline
src="https://cdn.usefathom.com/script.js"
data-site={PUBLIC_FATHOM_SITE_ID}
defer
/>
)
}

Open your .env file and add your Fathom Site ID.

.env
PUBLIC_FATHOM_SITE_ID="YOUR_FATHOM_SITE_ID"

Just like with Plausible, import and add the component to the <head> section of your src/layouts/BaseLayout.astro file.

src/layouts/BaseLayout.astro
---
import Analytics from '@/components/Analytics.astro'; // <-- Import the component
// ... other imports
---
<html lang="en">
<head>
{/* ... other head tags ... */}
<Analytics /> {/* <-- Add it here */}
</head>
<body>
{/* ... body content ... */}
</body>
</html>