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
- Each affiliate receives a unique promo code
- Customers enter the code at checkout
- Code is passed to Stripe in metadata
- AffiliateBase matches code to affiliate
- Commission attributed to affiliate
Affiliate Promo Codes
Viewing Promo Codes
Each affiliate automatically gets a unique promo code:
- Go to Affiliates in your dashboard
- Click on an affiliate
- 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:
- Promo code (highest priority)
- 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:
- Collect promo code from customer
- Pass in Stripe metadata
- 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
- Find an affiliate’s promo code in your dashboard
- Start checkout and enter the code
- Complete purchase with test card
- 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
- Set up link tracking for web traffic
- Configure Stripe Checkout with both methods
- Learn about attribution priorities