Affiliate Marketing Report Generator

Affiliate Marketing Report Generator

Analyze affiliate performance and generate comprehensive sales reports.

${topPerformer}

Performance by Affiliate

${tableRows}
AffiliateRateConversionsSales ValueCommissions
`; renderSalesChart(affiliateData); document.getElementById('download-pdf-btn').addEventListener('click', generatePdf); }; const renderSalesChart = (affiliateData) => { const ctx = document.getElementById('sales-chart').getContext('2d'); if (charts.sales) charts.sales.destroy(); charts.sales = new Chart(ctx, { type: 'bar', data: { labels: affiliateData.map(a => a.name), datasets: [ { label: 'Commissions', data: affiliateData.map(a => a.commissionEarned), backgroundColor: '#8b5cf6', yAxisID: 'y1' }, { label: 'Sales Value', data: affiliateData.map(a => a.salesValue), backgroundColor: '#a78bfa', yAxisID: 'y' } ] }, options: { responsive: true, maintainAspectRatio: false, plugins: { legend: { display: true } }, scales: { y: { beginAtZero: true, position: 'left', ticks: { callback: (v) => formatCurrency(v) }, title: { display: true, text: 'Sales Value' } }, y1: { beginAtZero: true, position: 'right', grid: { drawOnChartArea: false }, ticks: { callback: (v) => formatCurrency(v) }, title: { display: true, text: 'Commissions' } } } } }); }; const renderDataConfig = () => { const configContent = document.getElementById('content-data-configuration'); if (!configContent) return; const affiliatesHtml = appData.affiliates.map(a => ``).join(''); const salesHtml = appData.sales.map(s => ``).join(''); configContent.innerHTML = `

Manage Affiliates

${affiliatesHtml}
NameCommission Rate (%)ID

Manage Sales Records

${salesHtml}
Customer IDAmount ($)Affiliate
`; lucide.createIcons(); attachConfigListeners(); }; // --- EVENT HANDLERS & LOGIC --- const attachConfigListeners = () => { const handler = () => { document.querySelectorAll('.config-input-affiliate, .config-input-sale').forEach(input => { const id = input.dataset.id; const field = input.dataset.field; let value = input.value; let dataArray = input.classList.contains('config-input-affiliate') ? appData.affiliates : appData.sales; let item = dataArray.find(x => x.id.toString() === id); if (item) { if (input.type === 'number') value = parseFloat(value) || 0; item[field] = value; } }); appData.analysisResults = null; renderDashboard(); }; document.querySelectorAll('.config-input-affiliate, .config-input-sale').forEach(input => input.addEventListener('change', handler)); document.querySelectorAll('.remove-affiliate-btn, .remove-sale-btn').forEach(btn => btn.addEventListener('click', e => { const id = e.currentTarget.dataset.id; if (e.currentTarget.classList.contains('remove-affiliate-btn')) { appData.affiliates = appData.affiliates.filter(a => a.id !== id); appData.sales = appData.sales.filter(s => s.affiliateId !== id); } else { appData.sales = appData.sales.filter(s => s.id !== parseInt(id)); } appData.analysisResults = null; renderDataConfig(); renderDashboard(); })); document.getElementById('add-affiliate-btn').addEventListener('click', () => { const newId = `AFF${Math.floor(Math.random() * 900) + 100}`; appData.affiliates.push({ id: newId, name: 'New Affiliate', commissionRate: 10 }); renderDataConfig(); }); document.getElementById('add-sale-btn').addEventListener('click', () => { const newId = appData.sales.length > 0 ? Math.max(...appData.sales.map(s => s.id)) + 1 : 1; appData.sales.push({ id: newId, customerId: `CUST-${100+newId}`, amount: 0, affiliateId: appData.affiliates[0]?.id || '' }); renderDataConfig(); }); }; const generatePdf = () => { loadingOverlay.style.display = 'flex'; const { jsPDF } = window.jspdf; document.getElementById('pdf-generated-date').textContent = new Date().toLocaleString(); const pdfHeader = document.getElementById('pdf-header'); pdfHeader.classList.remove('hidden'); const fullContent = document.getElementById('pdf-content-area'); html2canvas(fullContent, { scale: 2, useCORS: true, logging: false, windowWidth: 1200 }) .then(canvas => { pdfHeader.classList.add('hidden'); const imgData = canvas.toDataURL('image/jpeg', 0.9); const pdf = new jsPDF({ orientation: 'landscape', unit: 'px', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const imgProps = pdf.getImageProperties(imgData); const imgHeight = (imgProps.height * pdfWidth) / imgProps.width; pdf.addImage(imgData, 'JPEG', 0, 0, pdfWidth, imgHeight); pdf.save('Affiliate-Marketing-Report.pdf'); loadingOverlay.style.display = 'none'; }).catch(err => { console.error("PDF generation failed:", err); pdfHeader.classList.add('hidden'); loadingOverlay.style.display = 'none'; alert('An error occurred generating the PDF.'); }); }; // --- TAB NAVIGATION & INITIALIZATION --- const switchTab = (tabIndex) => { activeTabIndex = tabIndex; document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex)); document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex)); updateNavButtons(); }; const updateNavButtons = () => { prevTabBtn.disabled = activeTabIndex === 0; nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1; }; const initializeUI = () => { const tabs = [ { name: 'Report Dashboard', id: 'report-dashboard' }, { name: 'Data Configuration', id: 'data-configuration' } ]; tabIdentifiers = tabs.map(t => t.id); tabsContainer.innerHTML = tabs.map(tab => ``).join(''); mainContent.innerHTML = tabs.map(tab => `
`).join(''); tabs.forEach((tab, index) => { document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index)); }); renderDashboard(); renderDataConfig(); switchTab(0); }; initializeUI(); prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); }); nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); }); });
Scroll to Top