Documentation

Add affiliate tracking to your website in minutes.

Emil Klitmose

Written by Emil Klitmose

Updated recently

Add the Tracking Script

Add this script to the <head> of your website. Replace the program ID with your own from the dashboard.

<script
  async
  src="https://www.revshare.so/tracking.js"
  data-program-id="YOUR_PROGRAM_ID"
  data-domain=".yourdomain.com"
  data-cookie-duration="30"
></script>

The data-domain enables tracking across subdomains (e.g., example.com and app.example.com). Replace with your domain, keeping the dot prefix.

Using different domains? If your landing page and app have completely different domains (like mysite.com → myapp.io), pass the ?ref=CODE in your redirect URL.

Access Referral Data

The script automatically captures referral codes from URLs and stores them in cookies. Both ?via= (current) and ?ref= (legacy, still supported) work. Access the data via:

window.revshare_metadata.program_id // your program ID

window.revshare_metadata.ref_code // affiliate's referral code

window.revshare_metadata.click_id // unique click ID for exact attribution

window.revshare_metadata.referrer // where visitor came from

Tip: Open your browser console and type console.log(window.revshare_metadata) to verify tracking is working.

Besides plain ?ref=CODE links, every affiliate can use a redirect link that records the click on our servers before forwarding the visitor:

https://www.revshare.so/r/YOUR_PROGRAM_SLUG/AFFILIATE_CODE
→ redirects to https://yoursite.com/?via=AFFILIATE_CODE&rs_cid=rc_a1b2c3...

Click IDs (rs_cid): Each click gets a unique ID. The tracking script stores it and exposes it as window.revshare_metadata.click_id. When it's included in checkout metadata, the sale is matched to the exact click — attribution works even when cookies are blocked or the purchase happens in a different browser.

Deep links: Append ?to=/pricing to a redirect link to send visitors to a specific page.

Direct ?ref= links don't need any of this — the script generates a click ID automatically on first click.

First-Party Domain (Optional)

Optionally serve affiliate links and the tracking script from your own subdomain so all tracking is first-party. In your program settings, add a domain like go.yourstore.com under Settings → Tracking and create the CNAME record shown there.

<script
  async
  src="https://go.yourstore.com/tracking.js"
  data-program-id="YOUR_PROGRAM_ID"
  data-api-host="https://go.yourstore.com"
></script>

Affiliate links then work as https://go.yourstore.com/AFFILIATE_CODE. This is entirely optional — your existing links and the standard script keep working without it.

Every program has an attribution model: last click (the default) or first click. You choose it under Edit program. The script remembers both the first and the latest affiliate a visitor clicked, and the model decides which of them earns the commission.

How cookies work

First referral click: When a visitor arrives through a link with ?ref=..., the script stores the affiliate code in two cookies, revshare_ref_PROGRAM_ID (first click) and revshare_last_PROGRAM_ID (latest click), and a click ID in revshare_cid_PROGRAM_ID.

A later click on another affiliate's link: Only revshare_last_PROGRAM_ID changes, and its expiration starts again from now. revshare_ref_PROGRAM_ID and the click ID keep the first affiliate until that cookie expires.

No new click: If a visitor returns without a ?ref= parameter, the cookies stay as they are.

Which click earns the commission

Day 1: User clicks ?ref=affiliate1 → first click and latest click are affiliate1

Day 5: User clicks ?ref=affiliate2 → latest click is affiliate2, first click is still affiliate1

Day 10: User purchases → last click program: affiliate2 earns. First click program: affiliate1 earns

Stripe Checkout and Shopify: Both clicks travel with the order, and we apply your program's attribution model.

Referral API: We credit the ref_code you send and do not apply the attribution model. window.revshare_metadata.ref_code is the first click and window.revshare_metadata.last_click_ref is the latest. Send the one that matches your program. If you send the latest click, leave out click_id, because the click ID belongs to the first click.

Browser limits: While you can set data-cookie-duration up to 365 days, most browsers cap cookies to ~180 days due to Intelligent Tracking Prevention (ITP) and similar privacy policies. Safari is particularly strict.

Stripe Checkout

Pass the referral data as metadata when creating a Stripe Checkout session. We automatically track sales via webhook.

// In the browser: send window.revshare_metadata to your server
// with the request that starts the checkout. On your server:
const session = await stripe.checkout.sessions.create({
  line_items: [{ price: 'price_xxx', quantity: 1 }],
  mode: 'payment',
  success_url: 'https://example.com/success',
  cancel_url: 'https://example.com/cancel',
  metadata: revshareMetadata, // window.revshare_metadata from the browser
});

Connect your Stripe account in the dashboard to enable automatic tracking.

Shopify Integration

For Shopify stores, use our dedicated tracking script that automatically handles cart attributes and checkout.

<script
  async
  src="https://www.revshare.so/shopify-tracking.js"
  data-program-id="YOUR_PROGRAM_ID"
  data-cookie-duration="30"
></script>

Add this script to your Shopify theme. Go to Online Store → Themes → Edit code → theme.liquid and paste it before the closing </head> tag.

How Shopify tracking works

1. The script captures ?ref=CODE from the URL and stores it in cookies + localStorage

2. It automatically injects ref_code and program_id into cart attributes

3. When checkout completes, our webhook receives the order with attribution data

4. Commission is calculated and credited to the affiliate

Dual storage: The script uses both cookies and localStorage as backup. This ensures tracking persists even if cookies are blocked or cleared.

Cart token sync

The script syncs the Shopify cart token with our server. This provides a fallback if cart attributes are lost during checkout (some themes clear attributes).

window.revshare_ref // the current ref code

Access the ref code via window.revshare_ref if you need it for custom integrations.

Referral API

For custom payment providers, call our API directly when a sale is made.

Endpoint: POST https://www.revshare.so/api/referral

Header: x-api-key: YOUR_API_KEY

Find your API key in Dashboard → Account & Billing. The same key works for all your programs.

await fetch("https://www.revshare.so/api/referral", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-api-key": process.env.REVSHARE_API_KEY,
  },
  body: JSON.stringify({
    ref_code: refCode,           // from frontend
    click_id: clickId,           // optional: window.revshare_metadata.click_id
    program_id: "YOUR_PROGRAM_ID",
    spent_amount: 5000,          // 50.00 in cents, never "50.00"
    currency: "USD",
    transaction_id: "order_123", // unique per order in your program
    customer_id: "cust_456",     // the buyer, the same on every order
  }),
});

Request Body

ref_code string, required (unless click_id is provided)

click_id string, optional. Links the sale to the exact click, and finds the affiliate when ref_code is missing

program_id string, required

spent_amount integer, required. The amount in the smallest currency unit

transaction_id string, required. Unique per order in your program

currency string, optional. Defaults to USD

customer_id string, strongly recommended. Identifies the buyer

Amounts are in the smallest currency unit. Send cents, not dollars: 50.00 USD is 5000. We do not convert decimals, so 50 or "50.00" is recorded as 50 cents. Currencies without decimals, such as JPY, are sent as they are: 5000 yen is 5000. A value that is not a number, or a negative one, is refused with a 400.

Always send customer_id. It is how we know that two orders come from the same buyer. Without it, every order counts as a new customer's first purchase: a program that pays on the first purchase only pays on every order, and a recurring period never ends.

Sending an order twice is safe. A transaction_id is recorded once per program, and a repeat returns the sale already recorded. If your program is set to manual approval, the sale is recorded as pending and earns nothing until you approve it under Transactions.

Environment Variable

Add your API key to your environment variables:

REVSHARE_API_KEY="your_api_key_here"

Never expose your API key in client-side code. Always call the API from your backend.

Connect to Any Provider

Works with Paddle, Lemon Squeezy, Gumroad, Polar, Dodo, Razorpay, or any payment provider with webhooks.

TL;DR — The Flow

1. Visitor clicks affiliate link → yoursite.com?ref=john123

2. Our script saves the ref code → stored in cookie + window.revshare_metadata

3. Customer buys something → your payment provider sends you a webhook

4. Your webhook handler calls our API → we handle the rest

Step 1: Pass ref_code to your checkout

When opening checkout, pass the referral data in custom_data (Paddle) or custom (Lemon Squeezy):

// Paddle example
Paddle.Checkout.open({
  items: [{ priceId: 'pri_xxx', quantity: 1 }],
  customData: {
    ref_code: window.revshare_metadata?.ref_code,
    program_id: window.revshare_metadata?.program_id,
  },
});

// Lemon Squeezy example
LemonSqueezy.Url.Open('https://yourstore.lemonsqueezy.com/buy/xxx', {
  custom: {
    ref_code: window.revshare_metadata?.ref_code,
  },
});

Step 2: Create a webhook handler

When your payment provider sends a webhook, call our API:

// Example: /api/paddle-webhook (Next.js)
export async function POST(req) {
  const event = await req.json();
  
  // Verify webhook signature here...
  
  if (event.event_type === 'transaction.completed') {
    const { custom_data, details, customer_id, id } = event.data;
    
    // Only call if there's a ref_code (affiliate sale)
    if (custom_data?.ref_code) {
      await fetch('https://www.revshare.so/api/referral', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
          'x-api-key': process.env.REVSHARE_API_KEY,
        },
        body: JSON.stringify({
          ref_code: custom_data.ref_code,
          program_id: custom_data.program_id,
          spent_amount: details.totals.total, // amount in cents
          currency: details.totals.currency_code,
          customer_id: customer_id,  // the buyer, the same on every order
          transaction_id: id,        // unique transaction ID
        }),
      });
    }
  }
  
  return Response.json({ received: true });
}

Tip: The pattern is the same for any provider. Just extract the sale amount, customer ID, and transaction ID from their webhook payload.

Recurring Revenue Tracking

To track recurring payments from the same customer, always pass the same customer_id.

TL;DR — How it works

Same customer_id = we know it's the same customer paying again.

Sale 1: { customer_id: "cust_abc", amount: 4900 } → First purchase ✓

Sale 2: { customer_id: "cust_abc", amount: 4900 } → Recurring ✓

Sale 3: { customer_id: "cust_abc", amount: 4900 } → Recurring ✓

Use whatever customer ID your payment provider gives you:

Paddle: event.data.customer_id

Lemon Squeezy: event.data.attributes.customer_id

Stripe: session.customer

Or use: Customer email as a fallback

Commission rules: In your program settings, you can configure whether affiliates earn commission on first purchase only, for X months, or lifetime.

Integrations

Connect external networks like Impact, PartnerAds, OnlyTraffic, Adtraction, Awin, CJ, Sovrn, Stripe, and AdSense from your affiliate dashboard.

Start with the Integrations docs, or read the Impact setup guide for required API scopes.

Lead Tracking

Track form submissions and pay affiliates for qualified leads. Perfect for lead generation programs.

Endpoint: POST https://www.revshare.so/api/leads/submit

Header: x-api-key: YOUR_API_KEY

Find your API key in Dashboard → Account & Billing.

// 1. In the browser: send the ref code along with your form
const refCode = window.revshare_metadata?.ref_code; // from tracking script
await fetch("/api/contact", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ ...formData, refCode }),
});

// 2. On your server (/api/contact): pass the lead on with your API key
export async function POST(req) {
  const { refCode, ...formData } = await req.json();

  if (refCode) {
    await fetch("https://www.revshare.so/api/leads/submit", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        "x-api-key": process.env.REVSHARE_API_KEY,
      },
      body: JSON.stringify({
        program_id: "YOUR_PROGRAM_ID",
        ref_code: refCode,
        lead_data: {
          name: formData.name,
          email: formData.email,
          phone: formData.phone, // optional
          company: formData.company, // optional
          message: formData.message, // optional
          // Add any other form fields
        },
      }),
    });
  }

  return Response.json({ ok: true });
}

Request Body

program_id string, required

ref_code string, required — affiliate referral code from tracking script

lead_data object, required — must include email at minimum

• lead_data.email is required

• All other fields in lead_data are optional

How it works: The tracking script automatically captures the ref_code from the URL (?ref=CODE) and stores it in window.revshare_metadata.ref_code. That value only exists in the browser, so send it to your server with the form, and let your server call the API with it to attribute the lead to the affiliate.

Important: Always call the API from your backend to keep your API key secure. Never expose it in client-side code.

Lead Approval

In your program settings, you can choose to auto-approve every lead or manually review each submission. Approved leads generate commissions for affiliates.