How to Create an Affiliate Program with Vue | AffiliateBase

Vue.js is a progressive JavaScript framework for building user interfaces. If you're looking to monetize your Vue 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 Vue, from initial setup to tracking conversions using Vue 3 composables and plugins.

Whether you're building a single-page application, progressive web app, or using Vue with Nuxt, 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 Vue:

1

Add the Tracking Script

Add the AffiliateBase tracking script to your Vue app. Add this to your index.html file. Using jsDelivr CDN ensures fast global delivery and automatic updates. No hosting required.
html
<script>(function(w,r){w._abq=w._abq||[];w[r]=w[r]||function(){(w[r].q=w[r].q||[]).push(arguments)}})(window,'affiliatebase');</script>
<script async src="https://cdn.jsdelivr.net/npm/affiliatebase-tracking@1/src/index.js" data-org-id='YOUR_ORG_ID'></script>
2

Or Install via npm

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

// In your main.ts or main.js
import 'affiliatebase-tracking';
3

Create a Composable

Create a Vue 3 composable to access affiliate tracking throughout your app.
typescript
// composables/useAffiliateTracking.ts
import { ref, onMounted } from 'vue';

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

Pass Referral ID to Stripe

Pass the referral ID as client_reference_id to Stripe for accurate conversion tracking.
javascript
// Get referral ID from tracking script
const referralId = window.affiliatebase_referral || '';

const session = await stripe.checkout.sessions.create({
  success_url: 'https://example.com/success',
  cancel_url: 'https://example.com/cancel',
  customer_creation: 'always',
  ...(referralId && { client_reference_id: referralId }),
  line_items: [
    { price: 'price_xxx', quantity: 1 },
  ],
  mode: 'subscription',
});
5

Use in Components

Use the composable in your Vue components to access tracking.
vue
<!-- components/CheckoutButton.vue -->
<script setup lang="ts">
import { useAffiliateTracking } from '@/composables/useAffiliateTracking';

const { referralId, trackConversion } = useAffiliateTracking();

const handleCheckout = async () => {
  // Pass referralId to your checkout API
  const response = await fetch('/api/checkout', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      referralId: referralId.value,
      priceId: 'price_xxx',
    }),
  });
  const { url } = await response.json();
  window.location.href = url;
};
</script>

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

Why Use AffiliateBase with Vue?

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

Vue 3 Ready

Works perfectly with Vue 3 Composition API and composables.

TypeScript Support

Full TypeScript support for type-safe affiliate tracking in your Vue app.

Automatic Stripe Integration

Works seamlessly with Stripe for tracking subscription payments.

Reactive Integration

Integrates naturally with Vue's reactivity system.

Frequently Asked Questions

Does AffiliateBase work with Vue 2?
Yes! The tracking script works with any Vue version. The composable example is for Vue 3, but you can create a similar mixin for Vue 2.
Can I use AffiliateBase with Pinia?
Yes, you can store the referral ID in a Pinia store and access it throughout your app.
How do I track conversions with Vue Router?
Use navigation guards or watch the route changes to track page views. The referral is automatically tracked on page load.
What tracking parameters does AffiliateBase support?
AffiliateBase automatically detects ?via=, ?ref=, ?affiliate=, and ?a= URL parameters.

Ready to Launch Your Vue Affiliate Program?

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