Add Hardal to Astro With Partytown

Load the Hardal tracker through Astro's official Partytown integration and verify that the worker, tracker, and event request all run.

Berkay Demirbas
2 min read
Guides

Astro's official Partytown integration can load the Hardal tracker in a web worker. Install @astrojs/partytown, mark the tracker script as type="text/partytown", and test the full event request. A loaded script is only the first check. Worker compatibility, consent behavior, and page navigation must also pass.

Install the integration

Use Astro's integration command:

yarn astro add partytown

Or configure it manually in astro.config.mjs:

import { defineConfig } from "astro/config";
import partytown from "@astrojs/partytown";

export default defineConfig({
  integrations: [partytown()],
});

Astro maintains the current installation and configuration details in the @astrojs/partytown guide.

Add the tracker to a shared layout

Copy the website ID and endpoint from your Hardal signal setup. Put the script in a layout used by every page that should be measured:

---
const websiteId = import.meta.env.PUBLIC_HARDAL_WEBSITE_ID;
const endpoint = import.meta.env.PUBLIC_HARDAL_ENDPOINT;
---

<script
  type="text/partytown"
  src={`${endpoint}/hardal`}
  data-website-id={websiteId}
  data-host-url={endpoint}
  data-auto-track="true"
></script>

Set PUBLIC_HARDAL_WEBSITE_ID and PUBLIC_HARDAL_ENDPOINT in the deployment environment. Values with Astro's PUBLIC_ prefix are exposed to the browser, so do not place secrets in them.

Verify more than the script file

  1. Open DevTools and check that the Partytown worker initializes.
  2. Confirm the Hardal script is requested through the worker.
  3. Confirm an event reaches the configured endpoint.
  4. Check the URL, referrer, timestamp, and signal ID in Hardal.
  5. Navigate between Astro pages and repeat the event check.
  6. Test the denied and granted states if the tracker is consent-gated.

If the tracker or another third-party script depends on a browser API Partytown cannot proxy, load that script normally and measure the performance difference. Do not keep a worker setup that silently drops events.

For the wider migration sequence, use the server-side tracking checklist.

Never miss a resource.

Subscribe to our newsletter.

Ready to switch first-party and server-side measurement?

Join hundreds of companies using Hardal for better and faster data quality.