- Add PublicCampaignsController for public campaign analytics
- Add public routes for /public/analytics/{slug} and /api/campaigns/{slug}
- Add public campaign analytics view with charts and KPIs
- Support for campaign sharing via custom slugs
- Real-time analytics with ApexCharts integration
1 line
3.7 KiB
JavaScript
1 line
3.7 KiB
JavaScript
export default()=>({charts:{},chartData:{},init(){this.initializePieCharts()},async initializePieCharts(){const t=document.querySelectorAll('[data-chart="pie"]');for(const a of t)await this.createPieChart(a)},async createPieChart(t){const a=t.id;if(!a)return;const e=await this.loadChartData(t),s=this.getPieChartOptions(t,e);this.charts[a]=new ApexCharts(t,s),this.charts[a].render(),this.chartData[a]=e},async loadChartData(t){const a=t.getAttribute("data-api-url"),e=this.getFallbackData(t);if(a)try{const t=await fetch(a);if(t.ok){const s=await t.json();return console.log(`📊 Loaded pie chart data from API: ${a}`,s),this.mergeWithFallback(s,e)}}catch(t){console.warn(`⚠️ API failed for ${a}, using fallback data:`,t)}return console.log(`📊 Using fallback data for pie chart: ${t.id}`),e},mergeWithFallback:(t,a)=>({series:t.series||a.series,labels:t.labels||a.labels,colors:t.colors||a.colors,config:{...a.config,...t.config}}),getFallbackData(t){const a={sales:{series:[44,55,13,43,22],labels:["Vendas","Marketing","Desenvolvimento","Suporte","Outros"],colors:["#3577f1","#51bb25","#f1b44c","#f06548","#5ac7fa"]},traffic:{series:[35,41,62,42,13,18],labels:["Direto","Orgânico","Referral","Social","Email","Outros"],colors:["#3577f1","#51bb25","#f1b44c","#f06548","#5ac7fa","#6c757d"]},revenue:{series:[25,35,45,55,65],labels:["Produto A","Produto B","Produto C","Produto D","Produto E"],colors:["#3577f1","#51bb25","#f1b44c","#f06548","#5ac7fa"]}};return a[t.getAttribute("data-chart-type")||"sales"]||a.sales},getPieChartOptions(t,a){const e=this.getColors(t,a.colors);return{series:a.series,chart:{type:"pie",height:parseInt(t.getAttribute("data-height"))||350,toolbar:{show:"false"!==t.getAttribute("data-toolbar")}},colors:e,labels:a.labels,dataLabels:{enabled:"false"!==t.getAttribute("data-labels"),formatter:function(t,a){return a.w.globals.seriesTotals[a.seriesIndex]+"%"}},plotOptions:{pie:{donut:{size:t.getAttribute("data-donut-size")||"0%",labels:{show:"false"!==t.getAttribute("data-donut-labels"),name:{show:!0,fontSize:"22px",fontFamily:"Helvetica, Arial, sans-serif",fontWeight:600,color:"#6c757d"},value:{show:!0,fontSize:"16px",fontFamily:"Helvetica, Arial, sans-serif",fontWeight:400,color:"#6c757d",formatter:function(t){return t}},total:{show:"false"!==t.getAttribute("data-show-total"),label:t.getAttribute("data-total-label")||"Total",fontSize:"16px",fontWeight:600,color:"#6c757d",formatter:function(t){return t.globals.seriesTotals.reduce((t,a)=>t+a,0)}}}}}},legend:{position:t.getAttribute("data-legend-position")||"bottom",horizontalAlign:t.getAttribute("data-legend-align")||"center"},tooltip:{theme:"dark",y:{formatter:function(t){return t+"%"}}},responsive:[{breakpoint:480,options:{chart:{width:200},legend:{position:"bottom"}}}]}},getColors(t,a=null){const e=t.getAttribute("data-colors");return e?e.split(",").map(t=>t.trim()):a||["#3577f1","#51bb25","#f1b44c","#f06548","#5ac7fa"]},async refreshChartData(t){const a=document.getElementById(t);if(!a)return;const e=await this.loadChartData(a);this.updateChartData(t,e),this.chartData[t]=e},updateChartData(t,a){this.charts[t]&&(this.charts[t].updateSeries(a.series),a.labels&&this.charts[t].updateOptions({labels:a.labels}))},addDataPoint(t,a,e){if(this.charts[t]){const s=this.charts[t],r=s.w.config.series,i=s.w.config.labels;r.push(a),i.push(e),s.updateSeries(r),s.updateOptions({labels:i})}},removeDataPoint(t,a){if(this.charts[t]){const e=this.charts[t],s=e.w.config.series,r=e.w.config.labels;s[a]&&r[a]&&(s.splice(a,1),r.splice(a,1),e.updateSeries(s),e.updateOptions({labels:r}))}},animateChart(t){this.charts[t]&&this.charts[t].updateSeries([],!0,!0)},getChart(t){return this.charts[t]},getChartData(t){return this.chartData[t]}}); |