文档
浏览文档

Add Privatus Analytics to SvelteKit

Add cookieless analytics to a SvelteKit app in src/app.html, track client-side navigation automatically and send client and server-side events from actions.

查看 Markdown

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://privatusanalytics.com/js/pa.js" data-site="pa_YOURSITEID"></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>

Server-side events#

In +page.server.js actions or +server.js endpoints, use the server-side ingest API with your secret ingest key. Forward the visitor's User-Agent and IP (event.getClientAddress()).