Background
⌘K

React Integration

Learn how to integrate Affonso affiliate tracking into your React application with React Router.

Silvestro
Written by Silvestro
Updated more than a month ago

React provides flexible ways to add tracking scripts, either through the useEffect hook for dynamic loading or directly in your HTML template for immediate loading.

Add tracking script to your React app

The recommended approach is to add the script dynamically in your root component using React's useEffect hook for proper cleanup and lifecycle management.

  1. Open your root component file, typically src/App.jsx
  2. Import useEffect from React
  3. Add the script loading logic inside useEffect:
import { useEffect } from 'react';

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

    return () => {
      // Cleanup on unmount
      if (document.head.contains(script)) {
        document.head.removeChild(script);
      }
    };
  }, []);

  return <div className='App'>{/* Your components */}</div>;
}

export default App;

Alternative: Add the script directly to your public/index.html file in the <head> section for immediate loading.

Find your Program ID →

Testing

  1. Start your development server: npm start
  2. Visit localhost:3000?atp=test
  3. Check browser DevTools → Console → type window.affonso_referral
  4. Deploy and verify tracking in your Affonso dashboard

Next Steps

Once installed, you can access affiliate referral data and pass it to your payment provider. Connect your payment provider →

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.