JavaScript Integration
Add refVenue tracking to your website with our JavaScript snippet
JavaScript Integration
The easiest way to add affiliate tracking to your website is with our JavaScript snippet.
Installation
Step 1: Get Your Keys
- Log in to refVenue
- Go to Settings → API Keys/Secrets
- Copy your Publishable Key
- Note your Program ID from the Programs page
Step 2: Add the Script
Add this to your website's <head> section:
<script async src="https://yourdomain.com/refvenue.js"
data-refvenue="YOUR_PUBLISHABLE_KEY"
data-program-id="YOUR_PROGRAM_ID"></script>Replace:
yourdomain.comwith your actual domainYOUR_PUBLISHABLE_KEYwith your publishable keyYOUR_PROGRAM_IDwith your program ID
Step 3: Track Conversions
Add tracking calls after successful conversions:
<!-- After user signs up -->
<script>
trackRegistration('user@example.com');
</script>
<!-- After user makes a purchase -->
<script>
trackPurchase('user@example.com', 99.99);
</script>Tracking Functions
trackRegistration(email)
Tracks user registrations/signups.
Parameters:
email(string, required): Customer's email address
Example:
trackRegistration('customer@example.com');When to use:
- After successful user registration
- After email verification
- After account creation
trackPurchase(emailOrCoupon, amount)
Tracks purchases. Automatically detects email vs coupon code.
Parameters:
emailOrCoupon(string, required): Customer email OR coupon codeamount(number, required): Purchase amount (after discount)
Examples:
With Email (Referral Tracking):
trackPurchase('customer@example.com', 99.99);With Coupon Code:
trackPurchase('SAVE20', 80.00); // Pass post-discount amountWhen to use:
- After successful payment
- After order confirmation
- After checkout completion
Important: Always pass the net amount (after discounts), not the original price.
How It Works
Automatic Click Tracking
The script automatically:
- Detects
?ref=CODEin the URL - Stores the affiliate code in a cookie
- Tracks the page visit
- Associates future conversions with that affiliate
Cookie Management
Cookie Name: affiliate_ref
Duration: Configurable per program (default: 30 days)
Scope: Domain-wide (works across subdomains)
Email vs Coupon Detection
When you call trackPurchase(), the script:
- Checks if first parameter is an email (regex validation)
- If email: Uses
ref_codefrom cookie, requires prior click - If coupon: Uses
coupon_code, no click needed
// This is detected as EMAIL
trackPurchase('user@example.com', 99.99);
// → Sends: { ref_code: 'JOHN123', customer_email: 'user@example.com' }
// This is detected as COUPON CODE
trackPurchase('SAVE20', 80.00);
// → Sends: { coupon_code: 'SAVE20', revenue_amount: 80.00 }Implementation Examples
React/Next.js
// Add to _app.js or layout
useEffect(() => {
// Script is already loaded via <script> tag in HTML
}, []);
// After signup
const handleSignup = async (email) => {
await createUserAccount(email);
// Track the registration
if (window.trackRegistration) {
window.trackRegistration(email);
}
};
// After purchase
const handlePurchase = async (email, amount) => {
await processPayment(email, amount);
// Track the purchase
if (window.trackPurchase) {
window.trackPurchase(email, amount);
}
};Vue.js
// After signup
methods: {
async completeSignup() {
await this.createAccount();
if (window.trackRegistration) {
window.trackRegistration(this.user.email);
}
}
}
// After purchase
methods: {
async completePurchase() {
const total = this.cart.total;
await this.processPayment();
if (window.trackPurchase) {
window.trackPurchase(this.user.email, total);
}
}
}WordPress
Add to your theme's functions.php:
// Add tracking script to header
function add_refvenue_script() {
?>
<script async src="https://yourdomain.com/refvenue.js"
data-refvenue="YOUR_PUBLISHABLE_KEY"
data-program-id="YOUR_PROGRAM_ID"></script>
<?php
}
add_action('wp_head', 'add_refvenue_script');
// Track after WooCommerce purchase
function track_woocommerce_purchase($order_id) {
$order = wc_get_order($order_id);
$email = $order->get_billing_email();
$total = $order->get_total();
?>
<script>
if (window.trackPurchase) {
trackPurchase('<?php echo esc_js($email); ?>', <?php echo $total; ?>);
}
</script>
<?php
}
add_action('woocommerce_thankyou', 'track_woocommerce_purchase');Shopify
<!-- Add to Settings → Checkout → Order Status Page -->
<script async src="https://yourdomain.com/refvenue.js"
data-refvenue="YOUR_PUBLISHABLE_KEY"
data-program-id="YOUR_PROGRAM_ID"></script>
<script>
// Track purchase
if (window.trackPurchase) {
trackPurchase('{{ order.email }}', {{ total_price | money_without_currency }});
}
</script>Coupon Tracking
Scenario: Customer Uses Coupon at Checkout
// In your checkout flow
const appliedCoupon = 'SAVE20'; // From checkout form
const discountedTotal = 80.00; // After applying coupon
// On purchase success
trackPurchase(appliedCoupon, discountedTotal);What happens:
- Script detects 'SAVE20' is not an email
- Sends
coupon_code: 'SAVE20'to API - refVenue looks up the coupon's owner
- Credits that affiliate with the conversion
- No prior referral click required!
Advanced Configuration
Custom Tracking URL
If your tracking server is on a different port/domain:
<script async src="https://yourdomain.com/refvenue.js"
data-refvenue="YOUR_KEY"
data-program-id="YOUR_PROGRAM_ID"
data-tracking-url="http://localhost:3000"></script>Debug Mode
Check the browser console for tracking activity:
// The script logs:
"refVenue: Affiliate tracking enabled: JOHN123"
"refVenue: Visit tracked successfully"
"refVenue: Registration tracked successfully!"
"refVenue: Purchase tracked successfully!"Error Handling
// Wrap in try-catch for safety
try {
if (window.trackPurchase) {
trackPurchase(email, amount);
}
} catch (error) {
console.error('Tracking error:', error);
// Don't block user flow on tracking errors
}Troubleshooting
Tracking Not Working
Check Console:
- Open browser DevTools → Console
- Look for refVenue messages
- Check for errors
Common Issues:
- Script not loaded: Check
<script>tag is in<head> - Wrong keys: Verify publishable key and program ID
- Ad blockers: Some block tracking scripts
- CORS errors: Check
data-tracking-urlif using custom domain
Conversions Not Appearing
Verify:
- Function exists:
console.log(window.trackPurchase) - Called after success: Place call after payment confirms
- Correct parameters: Email format or valid coupon code
- Network request sent: Check Network tab in DevTools
Rate Limiting
If you see "Rate limit exceeded" in console:
- Clicks: Max 5/hour, 20/day per visitor
- Conversions: Max 2/hour, 10/day per affiliate
- This prevents fraud, not normal usage
Best Practices
Timing
Call tracking after success confirmation:
// ✅ Good
await processPayment();
trackPurchase(email, amount);
// ❌ Bad
trackPurchase(email, amount);
await processPayment(); // What if this fails?Amount Precision
Always pass the post-discount amount:
// ✅ Good
const total = 100;
const discount = 20;
const finalAmount = total - discount; // 80
trackPurchase(email, finalAmount);
// ❌ Bad
trackPurchase(email, 100); // Wrong! Overpays commissionEmail Privacy
Never track without user consent:
- Follow GDPR/privacy laws
- Get user consent for tracking
- Include in privacy policy
- Respect Do Not Track settings (optional)
Next Steps
- Server-Side Tracking - More control
- Stripe Connect - Automatic tracking
- Webhook API - Direct API calls
- API Reference - Full API docs