Multi-Channel Affiliate Link Management System

${totalLinks}

Total Clicks

${totalClicks.toLocaleString()}

Conversions

${totalConversions.toLocaleString()}

Total Sales

$${totalSales.toLocaleString(undefined, {minimumFractionDigits:2})}

Conv. Rate

${convRate.toFixed(2)}%

EPC

$${epc.toFixed(2)}

`; // Charts renderDashboardCharts(); // Top links table const topLinks = [...state.links].sort((a,b) => b.sales - a.sales).slice(0, 5); const topLinksBody = document.getElementById('top-links-table-body'); topLinksBody.innerHTML = topLinks.map(l => ` ${l.product} ${l.affiliate} / ${l.channel} $${l.sales.toLocaleString(undefined, {minimumFractionDigits:2})} $${(l.clicks > 0 ? l.sales / l.clicks : 0).toFixed(2)} `).join(''); if (topLinks.length === 0) topLinksBody.innerHTML = `No link data available.`; }; const renderDashboardCharts = () => { const salesByChannel = state.channels.map(channel => ({ name: channel, total: state.links.filter(l => l.channel === channel).reduce((sum, l) => sum + l.sales, 0) })); const salesByAffiliate = state.affiliates.map(aff => ({ name: aff, total: state.links.filter(l => l.affiliate === aff).reduce((sum, l) => sum + l.sales, 0) })); if (salesByChannelChart) salesByChannelChart.destroy(); salesByChannelChart = new Chart(document.getElementById('sales-by-channel-chart').getContext('2d'), { type: 'bar', data: { labels: salesByChannel.map(d => d.name), datasets: [{ data: salesByChannel.map(d => d.total), backgroundColor: '#60a5fa' }] }, options: { plugins: { legend: { display: false } } } }); if (salesByAffiliateChart) salesByAffiliateChart.destroy(); salesByAffiliateChart = new Chart(document.getElementById('sales-by-affiliate-chart').getContext('2d'), { type: 'bar', data: { labels: salesByAffiliate.map(d => d.name), datasets: [{ data: salesByAffiliate.map(d => d.total), backgroundColor: '#a78bfa' }] }, options: { plugins: { legend: { display: false } } } }); }; const renderLinksManagement = () => { const affiliateOptions = state.affiliates.map(a => ``).join(''); const channelOptions = state.channels.map(c => ``).join(''); document.getElementById('select-affiliate').innerHTML = affiliateOptions; document.getElementById('select-channel').innerHTML = channelOptions; const linksTableBody = document.getElementById('links-table-body'); linksTableBody.innerHTML = state.links.map(link => { const url = new URL(link.baseUrl); url.searchParams.set('aff_id', link.affiliate.toLowerCase().replace(/\s/g, '_')); url.searchParams.set('channel', link.channel.toLowerCase().replace(/\s/g, '_')); const generatedUrl = url.href; return `

${link.product}

${link.affiliate} / ${link.channel}

`; }).join(''); if (state.links.length === 0) linksTableBody.innerHTML = `No links generated yet.`; }; const renderConfiguration = () => { const createListItem = (item, type) => `
${item}
`; document.getElementById('affiliates-list').innerHTML = state.affiliates.map(a => createListItem(a, 'affiliate')).join(''); document.getElementById('channels-list').innerHTML = state.channels.map(c => createListItem(c, 'channel')).join(''); }; // --- EVENT HANDLERS --- const handleTabClick = (tabName) => { state.activeTab = tabName; renderAll(); }; const handleNavClick = (direction) => { const currentIndex = TABS.indexOf(state.activeTab); const newIndex = direction === 'next' ? currentIndex + 1 : currentIndex - 1; if (newIndex >= 0 && newIndex < TABS.length) { state.activeTab = TABS[newIndex]; renderAll(); } }; const handleGenerateLink = () => { const product = document.getElementById('product-name').value.trim(); const baseUrl = document.getElementById('base-url').value.trim(); const affiliate = document.getElementById('select-affiliate').value; const channel = document.getElementById('select-channel').value; if (!product || !baseUrl || !affiliate || !channel) { alert("Please fill all fields to generate a link."); return; } const newLink = { id: Date.now(), product, baseUrl, affiliate, channel, clicks: 0, conversions: 0, sales: 0 }; state.links.push(newLink); renderLinksManagement(); document.getElementById('product-name').value = ''; document.getElementById('base-url').value = ''; }; const handleLinkTableInteraction = e => { const target = e.target; // Update data if (target.matches('.table-input')) { const id = parseInt(target.dataset.id); const field = target.dataset.field; const value = parseFloat(target.value) || 0; const link = state.links.find(l => l.id === id); if(link) link[field] = value; // Note: No re-render here to avoid losing focus. Data is updated in state. return; } // Delete button if(target.closest('.delete-link-btn')) { const id = parseInt(target.closest('.delete-link-btn').dataset.id); state.links = state.links.filter(l => l.id !== id); renderLinksManagement(); } // Copy button if(target.closest('.copy-link-btn')) { const linkToCopy = target.closest('.copy-link-btn').dataset.link; navigator.clipboard.writeText(linkToCopy).then(() => { const originalText = target.closest('.copy-link-btn').innerHTML; target.closest('.copy-link-btn').innerHTML = ``; setTimeout(() => { target.closest('.copy-link-btn').innerHTML = originalText; }, 1500); }); } }; const handleAddConfigItem = (type) => { const input = document.getElementById(type === 'affiliate' ? 'new-affiliate-name' : 'new-channel-name'); const name = input.value.trim(); if (name) { const list = type === 'affiliate' ? state.affiliates : state.channels; if (!list.includes(name)) { list.push(name); input.value = ''; renderConfiguration(); } else { alert(`${name} already exists.`); } } }; const handleDeleteConfigItem = (e) => { const btn = e.target.closest('.delete-config-btn'); if (btn) { const {type, name} = btn.dataset; if (type === 'affiliate') { state.affiliates = state.affiliates.filter(a => a !== name); } else { state.channels = state.channels.filter(c => c !== name); } renderConfiguration(); } }; const generatePDF = () => { const { jsPDF } = window.jspdf; const pdfContent = document.getElementById('pdf-content'); const pdfBtnContainer = document.getElementById('pdf-button-container'); if (!pdfContent || !pdfBtnContainer) return; pdfBtnContainer.style.display = 'none'; html2canvas(pdfContent, { scale: 2, useCORS: true }).then(canvas => { const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'l', unit: 'mm', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const imgWidth = pdfWidth - 20; const imgHeight = (canvas.height * imgWidth) / canvas.width; pdf.setFontSize(22); pdf.setFont('helvetica', 'bold'); pdf.text('Affiliate Performance Dashboard', pdfWidth / 2, 15, { align: 'center' }); pdf.addImage(imgData, 'PNG', 10, 25, imgWidth, imgHeight); pdf.save('affiliate-performance-dashboard.pdf'); pdfBtnContainer.style.display = 'block'; }).catch(err => { console.error("Error generating PDF:", err); pdfBtnContainer.style.display = 'block'; }); }; // --- ATTACH EVENT LISTENERS --- Object.keys(tabButtons).forEach(key => tabButtons[key].addEventListener('click', () => handleTabClick(key))); prevBtn.addEventListener('click', () => handleNavClick('prev')); nextBtn.addEventListener('click', () => handleNavClick('next')); document.getElementById('generate-link-btn').addEventListener('click', handleGenerateLink); document.getElementById('links-table-body').addEventListener('click', handleLinkTableInteraction); document.getElementById('links-table-body').addEventListener('change', handleLinkTableInteraction); document.getElementById('add-affiliate-btn').addEventListener('click', () => handleAddConfigItem('affiliate')); document.getElementById('add-channel-btn').addEventListener('click', () => handleAddConfigItem('channel')); document.getElementById('config').addEventListener('click', handleDeleteConfigItem); downloadPdfBtn.addEventListener('click', generatePDF); // --- INITIAL RENDER --- renderAll(); });
Scroll to Top