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:
Install the Tracking Script
<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>
Or Install via npm
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
Create a Custom Hook for Tracking
// 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 };
};
Pass Referral ID to Stripe
// 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',
});
Track Conversions (Optional)
// 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?
Can I use AffiliateBase with server-side rendering?
How do I access the referral ID in my React components?
What tracking parameters does AffiliateBase support?
Related Platform Guides
Create an Affiliate Program with Next.js
Complete guide to setting up an affiliate program in Next.js. Step-by-step integration with code examples and best practices for Next.js developers.
Create an Affiliate Program with Vue
Complete guide to setting up an affiliate program in Vue. Step-by-step integration with Vue 3, composables, and plugins for Vue developers.
Create an Affiliate Program with Svelte
Complete guide to setting up an affiliate program in Svelte. Step-by-step integration with SvelteKit, stores, and components for Svelte developers.
Create an Affiliate Program with Remix
Complete guide to setting up an affiliate program in Remix. Step-by-step integration with Remix loaders, actions, and forms for full-stack React developers.
Create an Affiliate Program with Astro
Complete guide to setting up an affiliate program in Astro. Step-by-step integration with Astro API routes, islands, and components for content-focused sites.
Ready to Launch Your React Affiliate Program?
Get started with AffiliateBase today and start growing your business with affiliate marketing.