Docs/INSTALLATION GUIDES/Astro

Astro Installation

Installing TrackFox on your Astro website is simple. You can use our CLI for automatic installation or manually add the tracking script to your layouts.

Quick Install with CLI

The fastest way to get started is using the TrackFox CLI:

npx trackfox add

The CLI will automatically detect your Astro setup and install the tracking script in the right place. Learn more →

Manual Installation

Step 1: Get Your Tracking Script

  1. Log in to your TrackFox dashboard
  2. Click on the website dropdown in the top navigation
  3. Select "Site Settings" from the dropdown menu
  4. In the General tab, copy your unique tracking script

Your tracking script will look like this:

<script
  defer
  src="https://trackfox.app/script.js"
  data-website-id="your-website-id-here"
  data-domain="yourdomain.com"
></script>

Step 2: Add Script to Base Layout

Navigate to your base layout file (typically src/layouts/Layout.astro) and add the tracking script inside the <head> tag:

---
// src/layouts/Layout.astro
interface Props {
  title: string
  description?: string
}

const { title, description } = Astro.props
---

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="description" content={description} />
    <meta name="viewport" content="width=device-width" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="generator" content={Astro.generator} />
    <title>{title}</title>

    <!-- TrackFox Analytics -->
    <script
      defer
      src="https://trackfox.app/script.js"
      data-website-id="your-website-id-here"
      data-domain="yourdomain.com"
    ></script>
  </head>
  <body>
    <slot />
  </body>
</html>

Step 3: Verify Installation

  1. Save your changes and restart your Astro development server
  2. Visit your website in a browser
  3. Return to your TrackFox dashboard
  4. Use the "Verify Installation" button in Site Settings
  5. Confirm that page views are being tracked in your dashboard

Custom Event Tracking

Once the base script is installed, you can track custom events:

In Astro Components

---
// Component script
---

<button id="signup-btn">Sign Up</button>

<script>
  // Track a custom event
  document.getElementById('signup-btn')?.addEventListener('click', () => {
    window?.trackfox('signup', {
      email: 'user@example.com'
    })
  })

  // Track a payment event
  function trackPurchase(amount: number, email: string) {
    window?.trackfox('payment', {
      email: email,
      amnt: amount // Amount in cents
    })
  }
</script>

In Framework Components (React, Vue, Svelte)

If you're using framework components in Astro:

// React component
export default function SignupButton() {
  const handleSignup = () => {
    window?.trackfox("signup", {
      email: "user@example.com",
    });
  };

  return <button onClick={handleSignup}>Sign Up</button>;
}

Environment Variables

Create a .env file with your TrackFox configuration:

PUBLIC_TRACKFOX_WEBSITE_ID=your_website_id
PUBLIC_TRACKFOX_DOMAIN=your_domain.com

Then use them in your layout:

---
const websiteId = import.meta.env.PUBLIC_TRACKFOX_WEBSITE_ID
const domain = import.meta.env.PUBLIC_TRACKFOX_DOMAIN
---

<script
  defer
  src="https://trackfox.app/script.js"
  data-website-id={websiteId}
  data-domain={domain}
></script>

TypeScript Support

Add type definitions for better TypeScript support:

// src/env.d.ts
/// <reference types="astro/client" />

declare global {
  interface Window {
    trackfox: (action: string, ...args: any[]) => void;
  }
}

export {};

Testing

Test your installation by:

  1. Opening your website in a browser
  2. Checking the Network tab for requests to trackfox.app
  3. Verifying data appears in your TrackFox dashboard

Troubleshooting

Common issues and solutions:

  • Script not loading: Check your website ID and domain configuration
  • No data appearing: Ensure the script is in all layouts used by your pages
  • TypeScript errors: Add the type definitions above

Important Notes

Important: Make sure to replace your-website-id-here and yourdomain.com with your actual website ID and domain from the TrackFox dashboard.

Next Steps

Now that you have TrackFox installed on your Astro website:

Need help? Contact us for assistance.

Suggest features? We'd love your feedback