RefRef LogoRefRef

Attribution Script

Track referral codes automatically with the RefRef attribution script for accurate referral attribution

The RefRef attribution script is a lightweight JavaScript library that automatically captures referral codes from URL parameters and stores them in browser cookies for referral tracking.

How It Works

  1. User clicks referral link: yourproduct.refref.ai/abc123 (RefRef shortlink)
  2. RefRef redirects to your configured landing URL: yoursite.com?refcode=abc123
  3. Script detects refcode: Reads the refcode URL parameter
  4. Cookie storage: Stores refcode in refref-refcode cookie for 90 days
  5. Access the refcode: Read the refref-refcode cookie in your backend. Optionally, enable automatic form field population for convenience

Optional: The refcode persists in the browser for 90 days. If your conversion flow takes multiple sessions, or you want server-side backup tracking, you should capture the refcode in your backend or CRM when users first provide information (e.g., newsletter signup, demo request). This ensures you have the refcode even if browser cookies are cleared.

Installation

Step 1: Add the Script Tag

Add the attribution script to your website's <head> section or before the closing </body> tag:

<script src="https://assets.refref.ai/attribution.js"></script>

The script automatically initializes when the DOM is ready. No additional configuration is required for basic usage.

Step 2: Using the Refcode (Optional)

The refcode is stored in the refref-refcode cookie. You can read this cookie in your backend, or use automatic form integration.

Form integration is optional. If you're reading the cookie in your backend, you can skip this step entirely.

To enable automatic form field population:

Add the data-refref attribute to your forms:

<form data-refref action="/api/signup" method="POST">
  <input type="email" name="email" required />
  <input type="password" name="password" required />
  <!-- Hidden refcode field will be auto-created and populated -->
  <button type="submit">Sign Up</button>
</form>

The script automatically detects all forms with data-refref attribute and adds a hidden refcode field. This works for:

  • Static HTML forms
  • Server-rendered pages
  • Single-page applications (SPAs)
  • Forms added after page load (modals, dynamic content, etc.)

No manual attachment needed. The script automatically detects forms whenever they're added to the page.

Next Steps: Once you've captured the refcode (from cookie or form submission), you'll need to send events to RefRef to attribute conversions. If auto attribution is not enabled for your product, you must manually fire events via the Events API.

Configuration

The script works with sensible defaults - no configuration needed for most use cases.

Advanced Usage

Troubleshooting

  • Widget - Add a referral sharing widget to your application
  • Programs - Configure referral programs and reward rules

On this page