fix(export): resolve build worker crash and implement robust Google Ads CSV/HTML exports
This commit is contained in:
@@ -10,6 +10,7 @@ import modelAdapter from "@/lib/services/adapter-instance";
|
||||
import { Megaphone, Copy, Loader2, CheckCircle2, Settings, Plus, X, ChevronDown, ChevronUp, Wand2, Target, TrendingUp, ShieldAlert, BarChart3, Users, Rocket, Download, FileSpreadsheet } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { GoogleAdsResult } from "@/types";
|
||||
import { downloadFile, generateGoogleAdsCSV, generateGoogleAdsHTML } from "@/lib/export-utils";
|
||||
import { translations } from "@/lib/i18n/translations";
|
||||
|
||||
export default function GoogleAdsGenerator() {
|
||||
@@ -306,94 +307,16 @@ export default function GoogleAdsGenerator() {
|
||||
|
||||
const exportCSV = () => {
|
||||
if (!googleAdsResult && !magicWandResult) return;
|
||||
|
||||
let rows: string[][] = [];
|
||||
|
||||
if (googleAdsResult) {
|
||||
// Keywords
|
||||
rows.push(["KEYWORDS RESEARCH"]);
|
||||
rows.push(["Type", "Keyword", "CPC"]);
|
||||
const addKw = (type: string, list?: any[]) => {
|
||||
if (list) list.forEach(k => rows.push([type, k.keyword, k.cpc || 'N/A']));
|
||||
};
|
||||
addKw("Primary", googleAdsResult.keywords?.primary);
|
||||
addKw("Long-tail", googleAdsResult.keywords?.longTail);
|
||||
addKw("Negative", googleAdsResult.keywords?.negative);
|
||||
rows.push([]);
|
||||
|
||||
// Ad Copies
|
||||
rows.push(["AD COPIES"]);
|
||||
rows.push(["Headlines", "Descriptions", "CTA"]);
|
||||
googleAdsResult.adCopies?.forEach(ad => {
|
||||
rows.push([
|
||||
ad.headlines?.join(' | ') || '',
|
||||
ad.descriptions?.join(' | ') || '',
|
||||
ad.callToAction || ''
|
||||
]);
|
||||
});
|
||||
rows.push([]);
|
||||
|
||||
// Campaigns
|
||||
rows.push(["CAMPAIGN STRUCTURE"]);
|
||||
rows.push(["Name", "Type", "Budget", "Locations", "Schedule"]);
|
||||
googleAdsResult.campaigns?.forEach(c => {
|
||||
rows.push([
|
||||
c.name,
|
||||
c.type,
|
||||
`${c.budget?.daily || 0} ${c.budget?.currency}`,
|
||||
c.targeting?.locations?.join('; ') || 'All',
|
||||
c.targeting?.schedule?.join('; ') || 'All'
|
||||
]);
|
||||
});
|
||||
rows.push([]);
|
||||
|
||||
// Implementation
|
||||
rows.push(["IMPLEMENTATION GUIDE"]);
|
||||
const impl = googleAdsResult.implementation;
|
||||
if (impl) {
|
||||
rows.push(["Setup Steps", impl.setupSteps?.join('; ') || '']);
|
||||
rows.push(["Quality Score Tips", impl.qualityScoreTips?.join('; ') || '']);
|
||||
rows.push(["Optimization Tips", impl.optimizationTips?.join('; ') || '']);
|
||||
}
|
||||
rows.push([]);
|
||||
|
||||
// Predictions
|
||||
if (googleAdsResult.predictions) {
|
||||
rows.push(["PERFORMANCE PREDICTIONS"]);
|
||||
const p = googleAdsResult.predictions;
|
||||
rows.push(["Metric", "Estimate"]);
|
||||
rows.push(["Clicks", p.estimatedClicks || "N/A"]);
|
||||
rows.push(["Impressions", p.estimatedImpressions || "N/A"]);
|
||||
rows.push(["CTR", p.estimatedCtr || "N/A"]);
|
||||
rows.push(["Conversions", p.estimatedConversions || "N/A"]);
|
||||
rows.push([]);
|
||||
}
|
||||
}
|
||||
|
||||
if (magicWandResult) {
|
||||
rows.push(["MARKET ANALYSIS"]);
|
||||
const ma = magicWandResult.marketAnalysis;
|
||||
rows.push(["Growth Rate", ma?.growthRate || 'N/A']);
|
||||
rows.push(["Top Competitors", ma?.topCompetitors?.join('; ') || 'N/A']);
|
||||
rows.push(["Market Trends", ma?.marketTrends?.join('; ') || 'N/A']);
|
||||
}
|
||||
|
||||
const csvContent = "data:text/csv;charset=utf-8," +
|
||||
rows.map(row => row.map(cell => `"${(cell || '').replace(/"/g, '""')}"`).join(",")).join("\n");
|
||||
|
||||
const encodedUri = encodeURI(csvContent);
|
||||
const link = document.createElement("a");
|
||||
link.setAttribute("href", encodedUri);
|
||||
link.setAttribute("download", `google-ads-report-${new Date().toISOString().split('T')[0]}.csv`);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
const csvContent = generateGoogleAdsCSV(googleAdsResult || undefined, magicWandResult || undefined);
|
||||
downloadFile(`google-ads-report-${new Date().toISOString().split('T')[0]}.csv`, csvContent, 'text/csv;charset=utf-8;');
|
||||
};
|
||||
|
||||
const exportHTML = () => {
|
||||
if (!googleAdsResult && !magicWandResult) return;
|
||||
|
||||
alert("Please use 'Export CSV' for the full detailed report. HTML export is disabled."); return; /*
|
||||
const htmlContent = generateGoogleAdsHTML(googleAdsResult || undefined, magicWandResult || undefined);
|
||||
downloadFile(`google-ads-report-${new Date().toISOString().split('T')[0]}.html`, htmlContent, 'text/html');
|
||||
return; /*
|
||||
parts.push(`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Google Ads Strategy Report</title>`);
|
||||
parts.push(`<style>
|
||||
:root { --bg: #0f172a; --card: #1e293b; --text: #e2e8f0; --accent: #6366f1; }
|
||||
|
||||
169
lib/export-utils.ts
Normal file
169
lib/export-utils.ts
Normal file
@@ -0,0 +1,169 @@
|
||||
import { GoogleAdsResult, MagicWandResult } from "../types";
|
||||
|
||||
export const downloadFile = (filename: string, content: string, contentType: string) => {
|
||||
if (typeof window === 'undefined') return;
|
||||
const blob = new Blob([content], { type: contentType });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const link = document.createElement("a");
|
||||
link.setAttribute("href", url);
|
||||
link.setAttribute("download", filename);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
URL.revokeObjectURL(url);
|
||||
};
|
||||
|
||||
export const generateGoogleAdsCSV = (googleAds?: any, magic?: any): string => {
|
||||
const rows: string[][] = [];
|
||||
|
||||
if (googleAds) {
|
||||
rows.push(["GOOGLE ADS STRATEGY REPORT"]);
|
||||
rows.push(["Generated at", new Date().toLocaleString()]);
|
||||
rows.push([]);
|
||||
|
||||
const kw = googleAds.keywords;
|
||||
if (kw) {
|
||||
rows.push(["--- KEYWORD RESEARCH ---"]);
|
||||
rows.push(["Type", "Keyword", "CPC"]);
|
||||
if (Array.isArray(kw.primary)) kw.primary.forEach((k: any) => rows.push(["Primary", k.keyword, k.cpc || 'N/A']));
|
||||
if (Array.isArray(kw.longTail)) kw.longTail.forEach((k: any) => rows.push(["Long-tail", k.keyword, k.cpc || 'N/A']));
|
||||
if (Array.isArray(kw.negative)) kw.negative.forEach((k: any) => rows.push(["Negative", k.keyword, k.cpc || 'N/A']));
|
||||
rows.push([]);
|
||||
}
|
||||
|
||||
const ads = googleAds.adCopies;
|
||||
if (Array.isArray(ads)) {
|
||||
rows.push(["--- AD COPY VARIATIONS ---"]);
|
||||
rows.push(["Variation", "Headlines", "Descriptions", "CTA"]);
|
||||
ads.forEach((ad: any, i: number) => {
|
||||
const hl = Array.isArray(ad.headlines) ? ad.headlines.join(' | ') : '';
|
||||
const ds = Array.isArray(ad.descriptions) ? ad.descriptions.join(' | ') : '';
|
||||
rows.push([`Variation ${i + 1}`, hl, ds, ad.callToAction || '']);
|
||||
});
|
||||
rows.push([]);
|
||||
}
|
||||
|
||||
const camps = googleAds.campaigns;
|
||||
if (Array.isArray(camps)) {
|
||||
rows.push(["--- CAMPAIGN STRUCTURE ---"]);
|
||||
rows.push(["Name", "Type", "Daily Budget", "Locations", "Schedule"]);
|
||||
camps.forEach((c: any) => {
|
||||
const targeting = c.targeting;
|
||||
const locs = (targeting && Array.isArray(targeting.locations)) ? targeting.locations.join('; ') : 'All';
|
||||
const sched = (targeting && Array.isArray(targeting.schedule)) ? targeting.schedule.join('; ') : 'All';
|
||||
rows.push([
|
||||
c.name || 'Campaign',
|
||||
c.type || 'Search',
|
||||
`${c.budget?.daily || 0} ${c.budget?.currency || 'USD'}`,
|
||||
locs,
|
||||
sched
|
||||
]);
|
||||
});
|
||||
rows.push([]);
|
||||
}
|
||||
}
|
||||
|
||||
if (magic) {
|
||||
rows.push(["--- MARKET INTELLIGENCE ---"]);
|
||||
const ma = magic.marketAnalysis;
|
||||
if (ma) {
|
||||
rows.push(["Growth Rate", ma.growthRate || 'N/A']);
|
||||
const comps = ma.topCompetitors;
|
||||
rows.push(["Top Competitors", Array.isArray(comps) ? comps.join('; ') : 'N/A']);
|
||||
const trends = ma.marketTrends;
|
||||
rows.push(["Market Trends", Array.isArray(trends) ? trends.join('; ') : 'N/A']);
|
||||
}
|
||||
const rationale = magic.rationale;
|
||||
if (rationale) {
|
||||
rows.push(["Strategy Rationale", rationale]);
|
||||
}
|
||||
}
|
||||
|
||||
return rows.map(row => row.map(cell => `"${String(cell || '').replace(/"/g, '""')}"`).join(",")).join("\n");
|
||||
};
|
||||
|
||||
export const generateGoogleAdsHTML = (googleAds?: any, magic?: any): string => {
|
||||
const parts: string[] = [];
|
||||
parts.push(`<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Ads Report</title>`);
|
||||
parts.push(`<style>
|
||||
:root { --bg: #0f172a; --card: #1e293b; --text: #e2e8f0; --accent: #6366f1; }
|
||||
body { font-family: system-ui, -apple-system, sans-serif; background: var(--bg); color: var(--text); line-height: 1.6; padding: 40px; margin: 0; }
|
||||
.container { max-width: 1000px; margin: 0 auto; }
|
||||
.section { background: var(--card); border-radius: 16px; padding: 32px; margin-bottom: 32px; border: 1px solid rgba(255,255,255,0.1); }
|
||||
h1 { font-size: 2.5rem; background: linear-gradient(to right, #818cf8, #c084fc); -webkit-background-clip: text; -webkit-text-fill-color: transparent; margin: 0 0 1rem 0; }
|
||||
h2 { font-size: 1.25rem; color: #818cf8; margin-top: 0; border-bottom: 1px solid rgba(255,255,255,0.1); padding-bottom: 12px; }
|
||||
.tag { display: inline-block; background: rgba(99,102,241,0.15); border: 1px solid rgba(99,102,241,0.3); color: #c3dafe; padding: 6px 14px; border-radius: 99px; font-size: 0.85rem; margin: 0 8px 8px 0; }
|
||||
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; }
|
||||
.card { background: rgba(0,0,0,0.2); padding: 20px; border-radius: 12px; border: 1px solid rgba(255,255,255,0.05); }
|
||||
.metric { font-size: 1.5rem; font-weight: 700; color: #4ade80; }
|
||||
.label { font-size: 0.75rem; text-transform: uppercase; color: #94a3b8; font-weight: 600; }
|
||||
</style></head><body><div class="container">`);
|
||||
|
||||
parts.push(`<h1>Google Ads Strategy Report</h1><p style="color:#94a3b8; margin-bottom: 40px">Generated on ${new Date().toLocaleDateString()}</p>`);
|
||||
|
||||
if (googleAds) {
|
||||
parts.push(`<div class="section"><h2>🎯 Keyword Intelligence</h2>`);
|
||||
const kw = googleAds.keywords;
|
||||
if (kw) {
|
||||
const primary = kw.primary;
|
||||
if (Array.isArray(primary)) {
|
||||
parts.push(`<div style="margin-bottom:20px"><div class="label">Primary Keywords</div>`);
|
||||
primary.forEach((k: any) => parts.push(`<span class="tag">${k.keyword} <small>(${k.cpc || 'N/A'})</small></span>`));
|
||||
parts.push(`</div>`);
|
||||
}
|
||||
const longTail = kw.longTail;
|
||||
if (Array.isArray(longTail)) {
|
||||
parts.push(`<div style="margin-bottom:20px"><div class="label">Long-tail Opportunities</div>`);
|
||||
longTail.forEach((k: any) => parts.push(`<span class="tag">${k.keyword}</span>`));
|
||||
parts.push(`</div>`);
|
||||
}
|
||||
}
|
||||
parts.push(`</div>`);
|
||||
|
||||
const ads = googleAds.adCopies;
|
||||
if (Array.isArray(ads)) {
|
||||
parts.push(`<div class="section"><h2>✍️ Ad Copy Variations</h2><div class="grid">`);
|
||||
ads.forEach((ad: any, i: number) => {
|
||||
parts.push(`<div class="card"><div class="label">Variation ${i + 1}</div>`);
|
||||
const headlines = ad.headlines;
|
||||
if (Array.isArray(headlines)) headlines.forEach(h => parts.push(`<div style="font-weight:700; color:#f1f5f9; margin-top:4px">${h}</div>`));
|
||||
const descriptions = ad.descriptions;
|
||||
if (Array.isArray(descriptions)) descriptions.forEach(d => parts.push(`<div style="margin-top:12px; font-size:0.9rem; color:#cbd5e1">${d}</div>`));
|
||||
if (ad.callToAction) parts.push(`<div style="margin-top:16px; font-weight:700; color:#818cf8">${ad.callToAction}</div>`);
|
||||
parts.push(`</div>`);
|
||||
});
|
||||
parts.push(`</div></div>`);
|
||||
}
|
||||
|
||||
const camps = googleAds.campaigns;
|
||||
if (Array.isArray(camps)) {
|
||||
parts.push(`<div class="section"><h2>🏗️ Campaign Architecture</h2><div class="grid">`);
|
||||
camps.forEach((c: any) => {
|
||||
parts.push(`<div class="card"><b>${c.name || 'Campaign'}</b><br><small>${c.type || 'Search'}</small>`);
|
||||
parts.push(`<div style="margin-top:10px; color:#4ade80; font-weight:bold">${c.budget?.daily || 0} ${c.budget?.currency || 'USD'}/day</div></div>`);
|
||||
});
|
||||
parts.push(`</div></div>`);
|
||||
}
|
||||
}
|
||||
|
||||
if (magic) {
|
||||
parts.push(`<div class="section"><h2>🧠 Market Intelligence</h2>`);
|
||||
const ma = magic.marketAnalysis;
|
||||
if (ma) {
|
||||
parts.push(`<p><b>Market Growth:</b> ${ma.growthRate || 'Stable'}</p>`);
|
||||
const comps = ma.topCompetitors;
|
||||
if (Array.isArray(comps)) {
|
||||
parts.push(`<div class="label">Key Competitors</div>`);
|
||||
comps.forEach(c => parts.push(`<div style="margin:4px 0">${c}</div>`));
|
||||
}
|
||||
}
|
||||
const rationale = magic.rationale;
|
||||
if (rationale) {
|
||||
parts.push(`<div style="margin-top:20px"><b>Strategy:</b><p>${rationale}</p></div>`);
|
||||
}
|
||||
parts.push(`</div>`);
|
||||
}
|
||||
|
||||
parts.push(`</div></body></html>`);
|
||||
return parts.join('');
|
||||
};
|
||||
Reference in New Issue
Block a user