Coupon Code Tracking

Track affiliate referrals using promo codes

Table of Contents

Coupon Code Tracking

Coupon codes provide an alternative attribution method when link tracking isn’t practical—perfect for podcasts, videos, print materials, or word-of-mouth promotion.

How It Works

  1. Each affiliate receives a unique promo code
  2. Customers enter the code at checkout
  3. Code is passed to Stripe in metadata
  4. AffiliateBase matches code to affiliate
  5. Commission attributed to affiliate

Affiliate Promo Codes

Viewing Promo Codes

Each affiliate automatically gets a unique promo code:

  1. Go to Affiliates in your dashboard
  2. Click on an affiliate
  3. Find their Promo Code in their profile

Sharing Promo Codes

Affiliates can share their code for:

  • Podcast mentions (“Use code JOHN20 for 20% off”)
  • YouTube video descriptions
  • Social media posts
  • Print materials
  • Word-of-mouth referrals

Implementation

With Stripe Checkout

Pass the promo code in session metadata:

const session = await stripe.checkout.sessions.create({
  line_items: [{ price: 'price_xxxxx', quantity: 1 }],
  mode: 'payment',
  success_url: 'https://yoursite.com/success',
  cancel_url: 'https://yoursite.com/cancel',
  // Pass promo code for attribution
  metadata: {
    promo_code: customerPromoCode,  // e.g., "JOHN20"
  },
});

With Subscriptions

Same approach for subscriptions:

const subscription = await stripe.subscriptions.create({
  customer: customerId,
  items: [{ price: 'price_xxxxx' }],
  metadata: {
    promo_code: customerPromoCode,
  },
});

Full Example

app.post('/checkout', async (req, res) => {
  const { priceId, promoCode, referralId } = req.body;

  const session = await stripe.checkout.sessions.create({
    line_items: [{ price: priceId, quantity: 1 }],
    mode: 'subscription',
    success_url: `${process.env.DOMAIN}/success`,
    cancel_url: `${process.env.DOMAIN}/pricing`,
    // Link tracking (primary)
    client_reference_id: referralId || undefined,
    // Coupon tracking (backup)
    metadata: {
      promo_code: promoCode || undefined,
    },
  });

  res.json({ url: session.url });
});

Attribution Priority

When both a referral link and promo code exist, AffiliateBase uses this priority:

  1. Promo code (highest priority)
  2. Link/cookie referral (if no promo code)

This ensures affiliates get credit when customers use their code, even if they clicked a different affiliate’s link.

Coupon Code UI

Add a Promo Code Field

<form id="checkout-form">
  <input
    type="text"
    name="promoCode"
    placeholder="Promo code (optional)"
  >
  <button type="submit">Checkout</button>
</form>

<script>
document.getElementById('checkout-form').addEventListener('submit', async (e) => {
  e.preventDefault();

  const promoCode = e.target.promoCode.value.trim();
  const referralId = window.affiliatebase_referral;

  const response = await fetch('/checkout', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
      promoCode,
      referralId,
    }),
  });

  const { url } = await response.json();
  window.location.href = url;
});
</script>

No Website Script Required

Coupon tracking works independently of the tracking script. If your checkout is entirely through Stripe:

  1. Collect promo code from customer
  2. Pass in Stripe metadata
  3. No website script needed

This makes coupon tracking ideal for:

  • API-only integrations
  • Mobile apps
  • Third-party checkout pages

Combining with Discounts

You can use affiliate promo codes alongside actual Stripe discounts:

const session = await stripe.checkout.sessions.create({
  line_items: [{ price: 'price_xxxxx', quantity: 1 }],
  mode: 'payment',
  // Apply actual discount
  discounts: [{ coupon: 'DISCOUNT20' }],
  // Track affiliate (separate from discount)
  metadata: {
    promo_code: affiliatePromoCode,
  },
});

The affiliate code in metadata is for attribution tracking, while discounts applies the actual discount.

Best Practices

Consistent Code Format

Keep codes simple and memorable:

  • JOHN20 (name + discount)
  • SARAH (just name)
  • PODCAST50 (channel + discount)

Document Code Rules

Tell affiliates:

  • How to find their code
  • Where customers enter it
  • Whether it provides a discount
  • How attribution works

Validate Codes

Optionally validate codes server-side:

// Check if code exists in your affiliate system
const isValidCode = await validateAffiliateCode(promoCode);

metadata: {
  promo_code: isValidCode ? promoCode : undefined,
}

Testing

  1. Find an affiliate’s promo code in your dashboard
  2. Start checkout and enter the code
  3. Complete purchase with test card
  4. Verify conversion attributed to correct affiliate

Troubleshooting

Code Not Attributed

  • Verify exact code match (case-sensitive)
  • Check code exists for an active affiliate
  • Ensure metadata field is promo_code

Wrong Affiliate Credited

  • Check if customer used wrong code
  • Verify no link referral took priority (shouldn’t happen)
  • Review attribution logs in dashboard

Next Steps