Background
⌘K

Svelte/SvelteKit Integration

Learn how to integrate Affonso affiliate tracking into your Svelte or SvelteKit application.

Silvestro
Written by Silvestro
Updated more than a month ago

Svelte and SvelteKit offer flexible ways to add tracking scripts, with SvelteKit providing an HTML template approach and regular Svelte using component lifecycle hooks.

Add tracking script to your Svelte app

For SvelteKit, the recommended approach is to modify your app.html template to include the tracking script globally across all routes.

  1. Open src/app.html in your project
  2. Add the tracking script in the <head> section
  3. Make sure it's before the %sveltekit.head% placeholder:
<!DOCTYPE html>
<html lang="en" %sveltekit.theme%>
  <head>
    <meta charset="utf-8" />
    <link rel="icon" href="%sveltekit.assets%/favicon.png" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />

    <script
      async
      defer
      src="https://affonso.io/js/pixel.min.js"
      data-affonso="YOUR_PUBLIC_PROGRAM_ID"
      data-cookie_duration="YOUR_COOKIE_DURATION"
    ></script>

    %sveltekit.head%
  </head>
  <body data-sveltekit-preload-data="hover">
    <div style="display: contents">%sveltekit.body%</div>
  </body>
</html>

Find your Program ID →

For regular Svelte: Use the onMount lifecycle function in your main App.svelte component to add the script dynamically.

Testing

  1. Start your development server: npm run dev
  2. Visit localhost:5173?atp=test (or your dev server URL)
  3. Check browser DevTools → Console → type window.affonso_referral
  4. Deploy and verify tracking in your Affonso dashboard

Next Steps

Once installed, you can access affiliate referral data and pass it to your payment provider. Connect your payment provider →

Was this article helpful?

If you still need help, our support team is here for you.

Contact Support
bg

Ready to Scale Your SaaS?

Affonso is the easiest way to launch your own affiliate program. We take care of the technical stuff, so you can focus on growing your business.