# How to add analytics to an Astro game website

> Add the Manalens script tag to an Astro game website. Count one page view per page with View Transitions, and track wishlist clicks and newsletter signups.

By Yos Riady. Updated 2026-11-24. Published 2026-11-24. Canonical URL: https://manalens.dev/integrations/astro

An Astro game website, such as a devlog or a press kit, sends page views and events with the Manalens script tag in the base layout. With View Transitions (the ClientRouter component), turn off automatic page views and call page() on each astro:page-load event. Then every page, also after back and forward, counts once with the right title.

## Key takeaways

- An Astro game website adds the Manalens script tag once, in the head of the base layout, with is:inline.
- With View Transitions, set data-auto-pageviews to false and call page() on every astro:page-load event.
- Put only the Publishable key in a PUBLIC_ variable, because Astro writes PUBLIC_ values into the built pages.

## How do I get the SDK for an Astro site?

The Manalens JavaScript SDK is open source (MIT) on GitHub. It is not on npm yet. For a script tag, clone the repository, run pnpm install and pnpm build, and copy dist/snippet.global.js into the public folder of your Astro site.

1. Build snippet.global.js from the manalens-js repository and copy it into public/.
2. Add the script tag to your base layout, with data-auto-pageviews="false".
3. Call page() on each astro:page-load event, in a head script.
4. Send track() for wishlist clicks and identify() after a newsletter signup.
5. Open the site on your project domain and check the project activity feed.

- [Manalens JavaScript SDK on GitHub](https://github.com/manalens/manalens-js)
- [Example: with-astro](https://github.com/manalens/examples/tree/main/with-astro)

## How do I add the script tag to the base layout?

Add the tag once, in the head of the layout that every page uses. is:inline tells Astro to keep the tag as it is. Read the Publishable key and the endpoint from PUBLIC_ variables. Astro writes PUBLIC_ values into the built pages, so put only the Publishable key there, never a Secret key or an API key.

Add integrity and crossorigin with the SHA-384 hash of the file, so the browser refuses a changed file. Build again after you change a PUBLIC_ value, because Astro reads them at build time.

*src/layouts/Base.astro*

```astro
---
const key = import.meta.env.PUBLIC_MANALENS_PUBLISHABLE_KEY;
const endpoint = import.meta.env.PUBLIC_MANALENS_ENDPOINT;
---
<head>
  {key && (
    <script is:inline src="/snippet.global.js"
      data-publishable-key={key} data-endpoint={endpoint}
      data-auto-pageviews="false" defer></script>
  )}
</head>
```

## How do I count page views with View Transitions?

With the ClientRouter component, a link does not load a new page. Astro swaps the content, changes the URL, and then fires astro:page-load. It also fires on the first load and after back and forward. So turn off automatic page views and call page() on every astro:page-load. Each page then counts once, with the title of the new page.

Put this script in the head, so it runs once. Astro runs body scripts again after each swap, and a body script would add one more listener on each navigation. Call page() without properties: a call without properties sends nothing when the path is the same as the last page view.

*Head of the base layout*

```html
<script is:inline>
  document.addEventListener('astro:page-load', () => {
    window.manalens?.page();
  });
</script>
```

## Which events should a game website send?

Send an event when a visitor does something that matters for the game: a click on the Steam wishlist button, a newsletter signup, a press kit download. Use flat snake_case names. Put the listeners on document, so they work on the new content after a View Transitions swap. Call identify() after a signup succeeds, with your own subscriber ID, never the email address.

- [How to connect Steam for wishlists, sales and UTM data](https://manalens.dev/integrations/steam)

*Site script*

```javascript
document.addEventListener('click', event => {
  if (event.target.closest('[data-wishlist]')) {
    window.manalens?.track('wishlist_clicked', { store: 'steam' });
  }
});
window.manalens?.identify('subscriber_42', { newsletter: true });
```

## How do I check that Astro events arrive?

Open the built site on your project domain, or use a project with the domain localhost for a local test. Click between pages, also with back and forward, and check that each page sends one page view with the right title. Then click the wishlist button and check for wishlist_clicked in the project activity feed. The SDK sends a batch every 5 seconds, when the page hides, and at once after identify().

## Frequently asked questions

### Why turn off automatic page views in Astro?

Because with View Transitions, a link does not load a new page. A page() call on each astro:page-load event sends one page view per page with the title of the new page, also after back and forward. With automatic page views on as well, the same navigation could be counted by two listeners.

### Does it work without View Transitions?

Yes. Without the ClientRouter component, each link loads a full page and astro:page-load never fires. Leave automatic page views on, or send page() on the window load event. The with-astro example does both: the home page loads in full, and the devlog uses View Transitions.

### Is it safe to put the key in a PUBLIC_ variable?

Yes, for the Publishable key. It is public and safe in a page, and Astro writes PUBLIC_ values into the built pages. Never put a Secret key or an API key in a PUBLIC_ variable. Build the site again after you change the value, because Astro reads it at build time.

## Sources

- [Astro: View Transitions](https://docs.astro.build/en/guides/view-transitions/)
- [Astro: Using environment variables](https://docs.astro.build/en/guides/environment-variables/)
- [MDN: Subresource Integrity](https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity)

## Related guides

- [Example project: with-astro](https://github.com/manalens/examples/tree/main/with-astro)
- [How to track an HTML5 game](https://manalens.dev/integrations/html5)
- [How to connect Steam for wishlists, sales and UTM data](https://manalens.dev/integrations/steam)
