How to Create an Affiliate Program with React | AffiliateBase

React is the most popular JavaScript library for building user interfaces. If you're looking to monetize your React 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 React, from initial setup to tracking conversions using React hooks and context.

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 React:

1

Install the Tracking Script

Add the AffiliateBase tracking script to your React app. You can use either the CDN (recommended) or npm package. **Option 1: CDN (Recommended)** Add to your index.html or _document.tsx: 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. Install the package and import it in your app entry point:
bash
npm install affiliatebase-tracking

// In your app entry (e.g., main.tsx or App.tsx)
import 'affiliatebase-tracking';

// The script auto-initializes when data-org-id is set
// Or configure programmatically in your HTML
3

Create a Custom Hook for Tracking

Create a React hook to access affiliate tracking state throughout your app.
typescript
// hooks/useAffiliateTracking.ts
import { useState, useEffect } from 'react';

export const useAffiliateTracking = () => {
  const [referralId, setReferralId] = useState<string>('');
  const [isReady, setIsReady] = useState(false);

  useEffect(() => {
    // Wait for tracking script to initialize
    if (typeof window !== 'undefined') {
      window.affiliatebase?.('ready', () => {
        setReferralId(window.affiliatebase_referral || '');
        setIsReady(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. Pass the referral ID as client_reference_id when creating checkout sessions.
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

Track Conversions (Optional)

For non-Stripe conversions, manually track when a customer signs up or purchases.
typescript
// In your signup/checkout component
const { trackConversion } = useAffiliateTracking();

const handleSignup = async (email: string) => {
  // Your signup logic
  await createUser(email);

  // Track the conversion
  trackConversion(email);
};

Why Use AffiliateBase with React?

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

Zero Configuration

Drop in the script tag and start tracking. No complex setup or API keys required.

TypeScript Support

Full TypeScript definitions included for type-safe affiliate tracking in your React app.

Automatic Stripe Integration

Works seamlessly with Stripe Checkout, Payment Links, and Pricing Tables out of the box.

Framework Agnostic

Works with Create React App, Vite, Next.js, Remix, and any React setup.

Frequently Asked Questions

Does AffiliateBase work with React Native?
The tracking script is designed for web browsers. For React Native, you can make direct API calls to track conversions server-side.
Can I use AffiliateBase with server-side rendering?
Yes! The CDN script works with SSR frameworks like Next.js. Just add the script to your document head. The tracking happens client-side after hydration.
How do I access the referral ID in my React components?
Use window.affiliatebase_referral or window.AffiliateBase.referral after the script loads. We recommend using a custom hook with the ready callback.
What tracking parameters does AffiliateBase support?
AffiliateBase automatically detects ?via=, ?ref=, ?affiliate=, and ?a= URL parameters. You can also configure custom parameters.

Ready to Launch Your React Affiliate Program?

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