trackr

Forwarding

Send events through a server on the site's own domain to a central collect endpoint, with the visitor's data and a site secret.

When the browser posts to a route on the site’s own domain (a site worker, an Astro endpoint, a Zaraz component) and that server passes the event on to a central trackr endpoint, the central endpoint only sees the forwarding server: its IP, its User-Agent, no Origin. Forwarding mode carries the visitor’s data along and proves that the forwarder belongs to the site.

On the site: createForwarder

import { createForwarder } from "@casoon/trackr/server";

const forward = createForwarder({
  endpoint: "https://trackr.example.com/collect",
  secret: env.TRACKR_FORWARD_SECRET,
  siteId: "my-site",
});

// POST /api/track on the site's domain
export default { fetch: (request: Request) => forward(request) };

The forwarder refuses requests whose Origin is another site, sets siteId, adds client: { ip, ua, country } from the request (CF-Connecting-IP, User-Agent, CF-IPCountry; clientIp and countryHeader change the lookup) and sends the event with Authorization: Bearer <secret>. It answers with the endpoint’s status.

On the endpoint: forwardSecrets

createHandler treats a request with an Authorization header as forwarded. The site from resolveSite must list the SHA-256 of the secret in forwardSecrets:

import { createHandler, hashSecret } from "@casoon/trackr/server";

const handler = createHandler({
  storage,
  salt,
  resolveSite: async (id) =>
    id === "my-site"
      ? { id, domains: ["example.com"], forwardSecrets: [await hashSecret(SECRET)] }
      : null,
});

With a matching secret, the Origin check is skipped and IP, User-Agent and country come from client. A wrong secret, a malformed header or an unknown site gives 403. Without Authorization, client in the body is ignored, so a browser cannot claim another IP.

The IP from client is used like a direct visitor’s IP: anonymised if anonymizeIp is on, hashed with the daily salt into the session ID, and never stored. A visitor gets the same session ID whether the event arrives directly or forwarded.

Secrets

createSecret() returns a random secret (trs_…), hashSecret(secret) its SHA-256 as hex. Store only the hash on the endpoint; give the secret to the forwarder (for a Worker: wrangler secret put TRACKR_FORWARD_SECRET). forwardSecrets takes several hashes, so a new secret can be added before the old one is removed.

Edit this page on GitHub