How to Create an Affiliate Program with Nuxt | AffiliateBase

Nuxt is a powerful Vue.js framework that makes it easy to build server-rendered and static web applications. If you're looking to monetize your Nuxt app through an affiliate program, you've come to the right place.

This comprehensive guide will walk you through everything you need to know about setting up an affiliate program with Nuxt, from initial setup to tracking conversions using Nuxt's server routes and composables.

Whether you're building a SaaS product, membership platform, or subscription service with Stripe, an affiliate program can help you grow your user base and increase revenue.

Follow these steps to get your affiliate program up and running in Nuxt:

1

Add the Tracking Script

Add the AffiliateBase tracking script to your Nuxt app. Add this to your nuxt.config.ts or app.vue. Using jsDelivr CDN ensures fast global delivery and automatic updates. No hosting required.
typescript
// nuxt.config.ts
export default defineNuxtConfig({
  app: {
    head: {
      script: [
        {
          innerHTML: '(function(w,r){w._abq=w._abq||[];w[r]=w[r]||function(){(w[r].q=w[r].q||[]).push(arguments)}})(window,"affiliatebase");',
        },
        {
          src: 'https://cdn.jsdelivr.net/npm/affiliatebase-tracking@1/src/index.js',
          async: true,
          'data-org-id': 'YOUR_ORG_ID',
        },
      ],
    },
  },
})
2

Or Install via npm

Install via npm for TypeScript support and bundler integration.
bash
npm install affiliatebase-tracking

// In a Nuxt plugin (plugins/affiliatebase.client.ts)
import 'affiliatebase-tracking';

export default defineNuxtPlugin(() => {
  // Plugin runs client-side only
});
3

Create a Composable

Create a Nuxt composable to access affiliate tracking throughout your app.
typescript
// composables/useAffiliateTracking.ts
export const useAffiliateTracking = () => {
  const referralId = ref<string>('');
  const isReady = ref(false);

  onMounted(() => {
    if (typeof window !== 'undefined' && window.affiliatebase) {
      window.affiliatebase('ready', () => {
        referralId.value = window.affiliatebase_referral || '';
        isReady.value = true;
      });
    }
  });

  const trackConversion = (email: string) => {
    window.affiliatebase?.('convert', { email });
  };

  return { referralId, isReady, trackConversion };
};
4

Create Server API Route

Pass the referral ID as client_reference_id to Stripe for accurate conversion tracking. Create a server route for Stripe checkout.
typescript
// server/api/checkout.post.ts
import Stripe from 'stripe';

const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);

export default defineEventHandler(async (event) => {
  const body = await readBody(event);
  const { referralId, priceId } = body;

  const session = await stripe.checkout.sessions.create({
    success_url: `${process.env.NUXT_PUBLIC_URL}/success`,
    cancel_url: `${process.env.NUXT_PUBLIC_URL}/cancel`,
    customer_creation: 'always',
    ...(referralId && { client_reference_id: referralId }),
    line_items: [{ price: priceId, quantity: 1 }],
    mode: 'subscription',
  });

  return { url: session.url };
});
5

Use in Components

Use the composable in your Nuxt components.
vue
<!-- pages/pricing.vue -->
<script setup>
const { referralId } = useAffiliateTracking();

const handleCheckout = async () => {
  const { url } = await $fetch('/api/checkout', {
    method: 'POST',
    body: {
      referralId: referralId.value,
      priceId: 'price_xxx',
    },
  });
  navigateTo(url, { external: true });
};
</script>

<template>
  <button @click="handleCheckout">Subscribe</button>
</template>

Why Use AffiliateBase with Nuxt?

AffiliateBase is the perfect solution for Nuxt developers who want to launch an affiliate program quickly and efficiently.

Nuxt 3 Ready

Works seamlessly with Nuxt 3 server routes, composables, and plugins.

Vue Composables

Use Nuxt composables for clean, reusable affiliate tracking logic.

SSR Compatible

Fully compatible with Nuxt's server-side rendering and static site generation.

TypeScript Support

Full TypeScript support for type-safe affiliate tracking.

Frequently Asked Questions

Does AffiliateBase work with Nuxt 3?
Yes! AffiliateBase fully supports Nuxt 3. Add the script via nuxt.config.ts or use a client-side plugin.
Can I use AffiliateBase with Nuxt static site generation?
Yes, the tracking script runs client-side and works with static generation.
How do I access the referral ID in Nuxt?
Use the useAffiliateTracking composable to access window.affiliatebase_referral reactively.
What tracking parameters does AffiliateBase support?
AffiliateBase automatically detects ?via=, ?ref=, ?affiliate=, and ?a= URL parameters.

Ready to Launch Your Nuxt Affiliate Program?

Get started with AffiliateBase today and start growing your business with affiliate marketing.