Add Privatus Analytics to SvelteKit
Add cookieless analytics to a SvelteKit app in src/app.html, track client-side navigation automatically and send custom events.
Add the snippet to src/app.html, inside <head> before %sveltekit.head%:
html
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<script defer src="https://privatusstats.com/js/pa.js" data-site="pa_YOURSITEID" data-modules="engage,auto,vitals,clicks,ask"></script>
%sveltekit.head%
</head>
<body data-sveltekit-preload-data="hover">
<div style="display: contents">%sveltekit.body%</div>
</body>
</html>
SvelteKit's client-side navigation uses history.pushState, which the
tracker follows automatically. Preloading data on hover doesn't send
pageviews.
Events#
svelte
<script>
function subscribe() {
window.privatus?.track('Subscribe', { list: 'weekly' })
}
</script>
<button on:click={subscribe}>Subscribe</button>