Astro integration
Add trackr to an Astro site with one line; script and endpoint run on the site's own domain and forward to a trackr portal.
@casoon/trackr/astro puts the client script and a tracking route into an Astro site. The
route forwards every event to the collect endpoint of a trackr portal
(forwarding mode), so the browser only talks to the site’s own domain:
no CDN, no request to another domain.
Setup
// astro.config.mjs
import cloudflare from "@astrojs/cloudflare";
import trackr from "@casoon/trackr/astro";
import { defineConfig } from "astro/config";
export default defineConfig({
adapter: cloudflare(),
integrations: [
trackr({
siteId: "your-site-id",
endpoint: "https://trackr.example.com/api/collect",
}),
],
});
Create a forwarding secret for the site in the portal’s admin area and give it to the site, never to the browser:
npx wrangler secret put TRACKR_FORWARD_SECRET
For astro dev and wrangler dev, put it into .dev.vars (and keep that file out of git).
What the integration does
- Bundles the client into every page (
/_astro/…) and callsinit()with the route. - Adds the route
/api/m(prerender = false). It reads the secret withgetSecret()fromastro:env/serverat request time and forwards withcreateForwarder(): the visitor’s IP, User-Agent and country go along, thesiteIdis set on the site, foreign origins get403. - Follows
trailingSlash: with"always", the client posts to/api/m/, because Astro redirects a POST to the path without slash.
The static pages stay static; only the route wakes the worker.
Options
| Option | Default | |
|---|---|---|
siteId |
– | Site id in the portal. |
endpoint |
– | Collect endpoint of the portal. |
route |
/api/m |
Path of the tracking route. Segments starting with _ are refused, Astro ignores them. |
secretEnv |
TRACKR_FORWARD_SECRET |
Environment variable with the forwarding secret. |
debug |
false |
Log every event in the browser console. |
Custom events use the client as usual:
import { track } from "@casoon/trackr/client";
track("signup_click", { plan: "pro" });
Requirements
- Astro 7 with an adapter.
@astrojs/cloudflareis the tested case; any adapter that supportsgetSecret()works. Without an adapter the build stops with an error. - Without a portal, use the library directly: an endpoint with
createHandler()and your own storage (Quickstart). - A static site without an adapter can run the same
createForwarder()in a Pages Function or a Worker route on its zone (Forwarding).
Optional interaction tracking
Enable only the observers the site uses:
trackr({
siteId: "your-site-id",
endpoint: "https://analytics.example.com/api/collect",
autoTrack: { mailto: true, tel: true, outbound: true, download: true, form: true },
})
The events are mailto_click, tel_click, outbound_click, file_download
and form_submit. Set ownHosts in autoTrack to exclude other site domains
from outbound tracking. Outbound links record the target hostname, downloads
record the file extension when recognized, and forms can include a static
data-trackr-form label. Email addresses, phone numbers and form values are
outside these event properties. For other bundlers, use
observeInteractions(track, options) from @casoon/trackr/client/interactions.