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/SvelteKit Integration

Set up Affonso tracking in your Svelte or SvelteKit application with different approaches for regular Svelte apps and full-stack SvelteKit projects.

SvelteKit Integration

Update your src/app.html file:

<!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="30"
    ></script>

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

Method 2: Layout Component

Add to your src/routes/+layout.svelte:

<script>
  import { onMount } from 'svelte';

  onMount(() => {
    const script = document.createElement('script');
    script.async = true;
    script.defer = true;
    script.setAttribute('data-affonso', 'YOUR_PUBLIC_PROGRAM_ID');
    script.setAttribute('data-cookie_duration', '30');
    script.src = 'https://affonso.io/js/pixel.min.js';
    document.head.appendChild(script);
  });
</script>

<main>
  <slot />
</main>

Regular Svelte Integration

Method 1: Main Component

Add to your main App.svelte:

<script>
  import { onMount } from 'svelte';

  onMount(() => {
    const script = document.createElement('script');
    script.async = true;
    script.defer = true;
    script.setAttribute('data-affonso', 'YOUR_PUBLIC_PROGRAM_ID');
    script.setAttribute('data-cookie_duration', '30');
    script.src = 'https://affonso.io/js/pixel.min.js';
    document.head.appendChild(script);
  });
</script>

<main>
  <!-- Your app content -->
</main>

Method 2: Index HTML

Add directly to your public/index.html:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <title>Your Svelte App</title>

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

Find your Program ID →

Next Steps

Once installed, you can access affiliate referral data and pass it to your payment provider. Learn how to integrate with your payment provider →

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

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.