---
title: "Install DataPocket on Astro"
description: "Add DataPocket web analytics with revenue attribution to Astro. Static or server-rendered. Navigations with <ClientRouter /> (view transitions) are tracked automatically."
url: "https://datapocket.vo1d.studio/docs/astro"
lang: "en"
updated: "2026-11-13"
---

# Install DataPocket on Astro

Static or server-rendered. Navigations with <ClientRouter /> (view transitions) are tracked automatically. Replace `YOUR_SITE_ID` and `YOUR_DOMAIN` with the values from your site's Install page. This guide serves the script from your own domain, so ad blockers can't tell it apart from your own code.

## Steps

1. Add the script to the <head> of your base layout (src/layouts/Layout.astro). is:inline tells Astro to leave the tag exactly as written instead of bundling it:

```html
---
// src/layouts/Layout.astro
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <script
      is:inline
      defer
      data-site-id="YOUR_SITE_ID"
      data-domain="YOUR_DOMAIN"
      src="https://datapocket.vo1d.studio/js/script.js"
    ></script>
  </head>
  <body>
    <slot />
  </body>
</html>
```

2. Using <ClientRouter /> (view transitions)? Nothing else to do: keep the tag in the shared layout so it's identical on every page, and each navigation is recorded as a pageview. Pages that don't use this layout need the same tag.

3. Optional, to get past ad blockers: proxy through your own domain. This needs on-demand rendering (an adapter such as @astrojs/node, Vercel, Netlify or Cloudflare). Create src/pages/dp/js/[file].ts:

```ts
import type { APIRoute } from "astro";

export const prerender = false;

export const GET: APIRoute = async ({ params }) => {
  if (!/^script(\.[a-z]+)?(@\d+\.\d+\.\d+)?\.js$/.test(params.file ?? "")) return new Response(null, { status: 404 });
  const r = await fetch(`https://datapocket.vo1d.studio/js/${params.file}`);
  return new Response(await r.text(), {
    headers: { "Content-Type": "application/javascript", "Cache-Control": "public, max-age=3600" },
  });
};
```

4. And src/pages/dp/api/event.ts, which forwards each event with the visitor's IP so their location stays correct:

```ts
import type { APIRoute } from "astro";

export const prerender = false;

export const POST: APIRoute = async ({ request, clientAddress }) => {
  const r = await fetch("https://datapocket.vo1d.studio/api/event", {
    method: "POST",
    body: await request.text(),
    headers: {
      "Content-Type": "text/plain",
      "User-Agent": request.headers.get("user-agent") ?? "",
      "X-Forwarded-For": clientAddress,
    },
  });
  return new Response(await r.text(), { status: r.status, headers: { "Content-Type": "application/json" } });
};
```

5. Then load the script from your own domain in the layout instead (same attributes, new src):

```html
<script
  is:inline
  defer
  data-site-id="YOUR_SITE_ID"
  data-domain="YOUR_DOMAIN"
  src="/dp/js/script.js"
></script>
```

## Revenue attribution

1. Pass the visitor cookie to Stripe Checkout so each sale is attributed to the channel that brought the buyer:

```js
const session = await stripe.checkout.sessions.create({
  // …your line_items, mode, success_url…
  metadata: {
    datapocket_visitor_id: req.cookies.dp_vid,
    datapocket_session_id: req.cookies.dp_sid,
  },
});
```

2. Using another payment provider? Send payments from your backend (create an API key in Settings):

```bash
curl -X POST https://datapocket.vo1d.studio/api/v1/payments \
  -H "Authorization: Bearer dpk_…" \
  -H "Content-Type: application/json" \
  -d '{"amount": 29.99, "currency": "USD", "transaction_id": "order_123", "visitor_id": "<dp_vid cookie>"}'
```

## Verify

1. Deploy or publish, then open your site in a normal browser window (ad blockers off).
2. In the browser's Network tab, look for a POST to `/api/event` (or `/dp/api/event` when proxied) that returns `{"ok":true}`.
3. The DataPocket Install page for this site switches to "Receiving data" within a few seconds.

## Related guides

- [Next.js](https://datapocket.vo1d.studio/docs/nextjs): App Router or Pages Router. Client-side navigations are tracked automatically.
- [React Router](https://datapocket.vo1d.studio/docs/react-router): Framework mode (Remix) and library mode. pushState navigations are tracked automatically.
- [Vue.js](https://datapocket.vo1d.studio/docs/vue): Vue 3 + Vue Router, Nuxt, Vite. History-mode routing is tracked automatically.
- [JavaScript apps](https://datapocket.vo1d.studio/docs/npm): React, Vue, Svelte or Angular: the script tag with typed calls, plus server-side payments and goals over HTTP.
- [AI crawler tracking](https://datapocket.vo1d.studio/docs/ai-bots)
