@addilytics/sveltekit
v0.0.1
Published
SvelteKit server analytics and bundled navigation tracking.
Readme
@addilytics/sveltekit
// src/hooks.server.ts
import { addilytics } from '@addilytics/sveltekit';
import { env } from '$env/dynamic/private';
export const handle = addilytics({
endpoint: 'https://addilytics.example',
siteKey: env.ADDILYTICS_KEY
});Compose with sequence() if you already have a handle. Put authentication or session handles first,
then Addilytics, then application handles that route requests or change responses.
import { sequence } from '@sveltejs/kit/hooks';
const analyticsHandle = addilytics({
...options,
user: (event) => event.locals.userId
});
export const handle = sequence(sessionHandle, analyticsHandle, applicationHandle);This order matters in hybrid and client modes. The Addilytics handle answers relay requests itself,
so handles after it do not run for POST /__addilytics. Resolve identity from event.locals only
when an earlier handle populates it. A resolver that reads the raw request can stay inside the
Addilytics options.
Set trustProxy: true only when your hosting platform overwrites forwarded IP and country headers.
SvelteKit's getClientAddress() remains the preferred IP source.
The handle records every eligible HTML document request that reaches SvelteKit, not only the first request in a session. Reloads and full-page navigations count on the server. Prerendered pages, CDN cache hits, client-side route changes that don't request HTML, and browser back-forward cache restores don't run it.
Client-side navigation
Use hybrid mode to keep the initial document on the server and record later client-side navigation:
// src/hooks.server.ts
import { addilytics } from '@addilytics/sveltekit';
import { env } from '$env/dynamic/private';
export const handle = addilytics({
endpoint: 'https://addilytics.example',
mode: 'hybrid',
siteKey: env.ADDILYTICS_KEY
});The handle intercepts POST /__addilytics before calling resolve. Register the browser helper
once in the root layout. It uses SvelteKit's afterNavigate hook, which runs after navigation has
committed.
<!-- src/routes/+layout.svelte -->
<script lang="ts">
import { trackAddilyticsNavigations } from '@addilytics/sveltekit/browser';
trackAddilyticsNavigations({ mode: 'hybrid' });
</script>
<slot />Use mode: 'client' in both files when the browser should record the initial view too. Client mode
turns off automatic server pageviews while keeping custom server events available. The server and
browser modes must match. If the server uses relayPath, pass the same path as endpoint to the
browser helper.
Your app bundles the browser helper; Addilytics does not load a hosted or CDN script. The browser
sends only a generated event ID, timestamp, path, campaign query, and referrer to the same-origin
relay. The site key remains in hooks.server.ts. The relay rejects cross-origin requests,
unexpected methods and content types, malformed fields, oversized bodies, and stale timestamps.
Back-forward cache restores are recorded. Hash-only changes are ignored unless you set
trackHashChanges: true.
