Install DataPocket on Astro

Updated

Static or server-rendered. Navigations with <ClientRouter /> (view transitions) are tracked automatically. Sign in and these snippets fill in your site ID and domain. Until then, swap YOUR_SITE_ID and YOUR_DOMAIN with the values from your Install page.

  1. 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. 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. 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. 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. 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. 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. 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>"}'

Related guides

Install DataPocket on Astro · DataPocket