Server-Side Revenue Tracking (Recommended)
✅ Recommended: Server-side tracking provides the most accurate revenue attribution by linking payments directly to user sessions from your backend, bypassing ad blockers and client-side issues.
How It Works
When a payment is completed on your server, send the user's session information along with the payment details to VersaTailor. This correctly attributes the payment to its source and shows you a clear picture of your user's journey.
Basic Implementation
Make a POST request to the VersaTailor API when a payment is successfully processed:
// Server-side revenue attribution
const response = await fetch('https://versatailor.com/api/v1/attribute-payment', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer YOUR_VERSATAILOR_API_KEY' // Replace with your API key
},
body: JSON.stringify({
session_id: userSessionId, // From VersaTailor cookies
visitor_id: userVisitorId, // From VersaTailor cookies
website_id: 'your_website_id', // Your VersaTailor website ID
email: 'customer@example.com', // Customer's email
revenue: 2999, // Amount in cents ($29.99)
}),
});
if (response.ok) {
console.log('Revenue attributed successfully');
} else {
console.error('Failed to attribute revenue');
}
Required Parameters
VersaTailor API Key
Get it from here
session_id & visitor_id
Retrieved from VersaTailor cookies on the user's browser. See extraction methods below.
website_id
Your unique website identifier from the VersaTailor dashboard URL.
Customer's email address. Use "dummy@example.com"
if privacy is a concern.
revenue
Payment amount in cents. For example: $29.99 = 2999, $100.00 = 10000
Step 1: Get Your Website ID
- Log in to your VersaTailor dashboard
- Navigate to your website
- Copy the website ID from the URL:
versatailor.com/dashboard/[website_id]
Step 2: Extract Session & Visitor IDs
The VersaTailor tracking script stores session and visitor IDs in cookies. Extract these on your server or send them from the frontend:
// app/api/create-checkout/route.ts
import { cookies } from 'next/headers';
// Helper function to track revenue
async function trackRevenue(sessionId: string | undefined, visitorId: string | undefined, amount: number, email: string) {
// Convert dollars to cents if needed (e.g., $5.00 becomes 500)
const amountInCents = Math.round(amount * 100);
// Make the API call to attribute revenue
const res = await fetch("https://versatailor.com/api/attribute-revenue", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
session_id: sessionId,
visitor_id: visitorId,
website_id: "your_website_id", // Replace with your actual website ID
email: email, // Customer's email address
revenue: amountInCents // Amount in cents
})
});
return res.json();
}
export async function POST(request: Request) {
// Get the request body which contains the order amount and customer email
const { orderAmount, customerEmail } = await request.json();
// Get cookies from the request
const userCookies = await cookies();
// Extract VersaTailor session and visitor IDs
const sessionId = userCookies.get('versatailor_session_id')?.value;
const visitorId = userCookies.get('versatailor_visitor_id')?.value;
try {
// Use the helper function to track revenue
const data = await trackRevenue(sessionId, visitorId, orderAmount, customerEmail);
// Return success response
return Response.json({ success: true, data });
} catch (error) {
console.error('Failed to attribute revenue:', error);
// Return error response
return Response.json({ success: false, error: 'Failed to attribute revenue' }, { status: 500 });
}
};
Implementation Examples
// Payment webhook handler
app.post('/webhook/payment-success', async (req, res) => {
const { orderId, customerId, amount } = req.body;
try {
// Get customer and session info from your database
const order = await getOrder(orderId);
const customer = await getCustomer(customerId);
// Attribute revenue to VersaTailor
const response = await fetch('https://versatailor.com/api/attribute-revenue', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
session_id: order.sessionId,
visitor_id: order.visitorId,
website_id: process.env.VERSATAILOR_WEBSITE_ID,
email: customer.email,
revenue: Math.round(amount * 100), // Convert to cents
}),
});
if (response.ok) {
console.log('Revenue attributed successfully');
}
res.status(200).send('OK');
} catch (error) {
console.error('Attribution failed:', error);
res.status(500).send('Error');
}
});
⚠️ Important Notes
- Only call the API after payment is successfully processed
- Handle API failures gracefully - revenue attribution shouldn't break your payment flow
Troubleshooting
Revenue not appearing?
- Please contact us at support@versatailor.com
Need help? Contact us for assistance.
Suggest features? We'd love your feedback