diff --git a/components/GoogleAdsGenerator.tsx b/components/GoogleAdsGenerator.tsx index 6e12261..c3735c4 100644 --- a/components/GoogleAdsGenerator.tsx +++ b/components/GoogleAdsGenerator.tsx @@ -147,20 +147,54 @@ export default function GoogleAdsGenerator() { if (result.success && result.data) { try { - const parsedData = typeof result.data === 'string' ? JSON.parse(result.data) : result.data; + // Optimized parsing helper + const extractJson = (text: string) => { + try { + // 1. Direct parse + return JSON.parse(text); + } catch (e) { + // 2. Extract from markdown code blocks + const jsonMatch = text.match(/```json\s*([\s\S]*?)\s*```/i) || + text.match(/```\s*([\s\S]*?)\s*```/i); + + if (jsonMatch && jsonMatch[1]) { + try { + return JSON.parse(jsonMatch[1].trim()); + } catch (e2) { + console.error("Failed to parse extracted JSON block", e2); + } + } + + // 3. Last resort: extract anything between the first { and last } + const braceMatch = text.match(/(\{[\s\S]*\})/); + if (braceMatch) { + try { + return JSON.parse(braceMatch[0].trim()); + } catch (e3) { + console.error("Failed to parse content between braces", e3); + } + } + + throw new Error("Invalid format: AI response did not contain valid JSON"); + } + }; + + const rawData = typeof result.data === 'string' ? result.data : JSON.stringify(result.data); + const parsedData = extractJson(rawData); + const adsResult: GoogleAdsResult = { ...parsedData, id: Math.random().toString(36).substr(2, 9), websiteUrl, productsServices: filteredProducts, generatedAt: new Date(), - rawContent: typeof result.data === 'string' ? result.data : JSON.stringify(result.data, null, 2) + rawContent: rawData }; setGoogleAdsResult(adsResult); setActiveTab("keywords"); } catch (e) { console.error("Failed to parse ads data:", e); - setError("Failed to parse the generated ads content. Please try again."); + setError("Failed to parse the generated ads content. Please try again or switch AI providers."); } } else { setError(result.error || "Failed to generate Google Ads campaign"); @@ -216,16 +250,16 @@ export default function GoogleAdsGenerator() { }; return ( -
-
+
+
-

- - Google Ads Gen - PRO +

+ + Google Ads Strategist + Premium AI

-

- Generate high-converting keywords, ad copy, and campaign structures with AI. +

+ Convert concepts into high-ROI Google Ads campaigns with precision-engineered keywords and copy.

@@ -246,15 +280,15 @@ export default function GoogleAdsGenerator() {
{/* Input Panel */}
- -
- - - - Campaign Parameters + +
+ + + + Campaign Inputs - - Define your goals and target audience. + + Configure your primary triggers and constraints. @@ -264,10 +298,10 @@ export default function GoogleAdsGenerator() { Website URL setWebsiteUrl(e.target.value)} - className="bg-muted/30 focus-visible:ring-blue-500" + className="bg-white/50 border-blue-100 focus:border-blue-500 focus-visible:ring-blue-500 h-11 text-base shadow-sm" />
@@ -476,20 +510,27 @@ export default function GoogleAdsGenerator() { )} {isProcessing && ( -
-
-
+
+ {/* Decorative background blur */} +
+ +
+
- +
+ +
-

Analyzing Domain Content

-
-
-
-

- Scanning {websiteUrl}, fetching search volumes, and drafting high-converting copy... + +

Strategizing Your Campaign...

+

+ Scouring {websiteUrl} for conversion triggers and competitive edges.

+ +
+
+
{[ { label: "Keywords", delay: "0s" },