{
  "name": "Analyze Facebook Ads & send insights to Google Sheets with Gemini AI",
  "nodes": [
    {
      "id": "ce48c496-4d57-41c3-992e-046c1da7d6f6",
      "name": "When clicking ‘Test workflow’",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -3184,
        768
      ]
    },
    {
      "id": "89a2936e-7719-4cd8-8638-7db0d13e1a3f",
      "name": "Stringify Everything",
      "type": "n8n-nodes-base.code",
      "position": [
        -96,
        448
      ]
    },
    {
      "id": "48d96bc5-b537-4fb0-9f09-e8c75f57a1c2",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        560,
        720
      ]
    },
    {
      "id": "e5ce198d-0740-456f-9160-6f7b253cdbe0",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        752,
        720
      ]
    },
    {
      "id": "733a2556-a9d9-4892-9acb-6fcdb32a326e",
      "name": "Split Out",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        944,
        528
      ]
    },
    {
      "id": "aa9a05cc-532f-4f6f-844b-5511178b1722",
      "name": "Sending Raw Data To A Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        272,
        -16
      ]
    },
    {
      "id": "0cbd26d6-c758-47f8-bca1-190d4af58626",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2992,
        448
      ],
      "parameters": {
        "width": 1740,
        "height": 756,
        "content": "### Step 1: Securely Manage Your Facebook API Token\n\nThis section retrieves your long-term access token and automatically refreshes it if it's about to expire, ensuring the workflow always has valid c"
      }
    },
    {
      "id": "7b2463ab-0eac-4bd5-8185-744ea231ef1d",
      "name": "Getting Long-Term Token",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        -2928,
        768
      ]
    },
    {
      "id": "e0d95e0a-4cc2-42a7-9f6d-337bb1dc68b9",
      "name": "Does it Need A Token Refresh?",
      "type": "n8n-nodes-base.code",
      "position": [
        -2704,
        768
      ]
    },
    {
      "id": "5f27af56-537f-42f3-9c11-52189918c631",
      "name": "Getting Long-Lived Access Token1",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -2240,
        704
      ]
    },
    {
      "id": "72f99e3f-1c6c-4186-b0d8-1c10d5c32fa7",
      "name": "Calculating End Date of Token1",
      "type": "n8n-nodes-base.code",
      "position": [
        -2016,
        704
      ]
    },
    {
      "id": "c53eca75-6433-46fb-95ac-3b85a1b6420a",
      "name": "Updating Token",
      "type": "n8n-nodes-base.nocoDb",
      "position": [
        -1824,
        704
      ]
    },
    {
      "id": "d82dc7c6-9dc2-427d-9164-0073ee5b611d",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        48,
        -352
      ],
      "parameters": {
        "width": 752,
        "height": 540,
        "content": "### Step 3a: Log Processed Data to Google Sheets\n\nThis node takes the cleaned and calculated performance data for each ad creative and sends it to your Google Sheet.\n\n**Why this step is important:**\nI"
      }
    },
    {
      "id": "fe5643c3-3012-4573-84b7-d11c93261bee",
      "name": "Getting Long-Term Token1",
      "type": "n8n-nodes-base.set",
      "position": [
        -1872,
        976
      ]
    },
    {
      "id": "ce1cff8f-e076-4ec5-9a86-58db01605894",
      "name": "Splitting Out In Table Format",
      "type": "n8n-nodes-base.code",
      "position": [
        -896,
        528
      ]
    },
    {
      "id": "83a6b0a8-8422-4705-9576-deca22f7f092",
      "name": "Sorting Based on Spends",
      "type": "n8n-nodes-base.sort",
      "position": [
        -304,
        448
      ]
    },
    {
      "id": "c8bda278-bb77-423b-b2bc-49f23d6d3eb5",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1072,
        272
      ],
      "parameters": {
        "width": 512,
        "height": 620,
        "content": "### Step 6: Update Google Sheets with AI Insights\n\nThis final step takes the AI's analysis for each ad and updates your Google Sheet, matching the ad by its unique `ad_id`.\n\n**➡️ Action Required:**\n- "
      }
    },
    {
      "id": "fdb93589-5381-4dfc-a0ec-7dbd01b2bc64",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1232,
        272
      ],
      "parameters": {
        "width": 320,
        "height": 524,
        "content": "### Step 2: Fetch Facebook Ad Data\n\nThis node calls the Facebook Graph API to get performance data for all your ads from the last 28 days.\n\n**➡️ Action Required:**\n- In the URL parameter, replace `act"
      }
    },
    {
      "id": "5b88a75e-945c-46b2-92d0-a8ddd399433a",
      "name": "Sticky Note6",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -592,
        208
      ],
      "parameters": {
        "width": 680,
        "height": 744,
        "content": "### Step 3: Calculate Ad & Benchmark KPIs\n\nThe workflow splits here to perform two crucial calculations in parallel:\n- **Top Path**: Calculates performance metrics for each *individual ad creative*.\n-"
      }
    },
    {
      "id": "2fdd3e2f-92fa-4034-841a-500dad9f91bc",
      "name": "Filtering Only For Sales Campaigns",
      "type": "n8n-nodes-base.filter",
      "position": [
        -704,
        528
      ]
    },
    {
      "id": "b2f42335-8b1e-4d00-a9a8-f7f631f1927d",
      "name": "Getting Data For the Past 28 Days Segemented Per Campaign Adset and Ad",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -1120,
        528
      ]
    },
    {
      "id": "2dfd3d0c-0988-42df-8dfc-c545188d4ea2",
      "name": "Senior Facebook Ads Media Buyer",
      "type": "@n8n/n8n-nodes-langchain.chainLlm",
      "position": [
        608,
        528
      ]
    },
    {
      "id": "6256c78d-67fb-4bc1-afb9-07b6191e254d",
      "name": "Combining Ad Data and Benchmarking Data",
      "type": "n8n-nodes-base.merge",
      "position": [
        224,
        512
      ]
    },
    {
      "id": "88276317-cd5c-4529-8329-51f1fc9bcfb0",
      "name": "Sticky Note7",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        160,
        272
      ],
      "parameters": {
        "width": 356,
        "height": 544,
        "content": "### Step 4: Prepare Data for AI Analysis\n\nThese nodes take the individual ad data and the overall benchmark data, convert them into clean JSON strings, and merge them together. This prepares a complet"
      }
    },
    {
      "id": "f44bb155-47a1-422d-b857-6007bbcb9844",
      "name": "Updating Ad Insights Into Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1184,
        528
      ]
    },
    {
      "id": "21dcc80e-ac2f-40ce-9890-6c8ba2ee463c",
      "name": "Does Token Needs Refreshing?",
      "type": "n8n-nodes-base.if",
      "position": [
        -2464,
        768
      ]
    },
    {
      "id": "96584b1b-2f7f-46d1-96c7-268c0ea9603b",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3856,
        176
      ],
      "parameters": {
        "width": 608,
        "height": 1024,
        "content": "## AI-Powered Facebook Ad Analysis for E-commerce\n\nThis workflow automates the analysis of your Facebook ad performance, acting as an AI-powered media buyer to give you actionable insights. It fetches"
      }
    },
    {
      "id": "5ce47343-8189-4d1d-9038-f0e0bcaf9802",
      "name": "Extracting Long Term Token",
      "type": "n8n-nodes-base.set",
      "position": [
        -1600,
        704
      ]
    },
    {
      "id": "5e3dc574-4049-4bee-ae81-8a899b553615",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        528,
        272
      ],
      "parameters": {
        "width": 528,
        "height": 624,
        "content": "### Step 5: AI-Powered Ad Creative Analysis\n\nA powerful LLM (Google Gemini) acts as a Senior Media Buyer. It compares each ad's performance against the account benchmarks and categorizes it with a jus"
      }
    },
    {
      "id": "2eaabe3c-fec3-4804-8f2b-db39a00cb0c4",
      "name": "Calculate Account Benchmarks",
      "type": "n8n-nodes-base.code",
      "position": [
        -464,
        784
      ]
    },
    {
      "id": "23411043-d16f-40f4-8fcc-3c6653f3595c",
      "name": "Stringify Benchmark Data",
      "type": "n8n-nodes-base.code",
      "position": [
        -48,
        784
      ]
    },
    {
      "id": "cf388ebd-dff0-4658-b13a-59619c8cf701",
      "name": "Aggregate Metrics by Ad Creative",
      "type": "n8n-nodes-base.code",
      "position": [
        -496,
        448
      ]
    },
    {
      "id": "3b80f5a8-4aa8-4a65-882b-ebc391da066c",
      "name": "Combine Ad & Benchmark Data for LLM",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        400,
        512
      ]
    },
    {
      "id": "dbc2be05-df91-463b-bd18-d935809393a1",
      "name": "Extracting Access Token",
      "type": "n8n-nodes-base.set",
      "position": [
        -1360,
        800
      ]
    }
  ],
  "connections": {
    "Split Out": {
      "main": [
        [
          {
            "node": "Updating Ad Insights Into Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Updating Token": {
      "main": [
        [
          {
            "node": "Extracting Long Term Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Stringify Everything": {
      "main": [
        [
          {
            "node": "Combining Ad Data and Benchmarking Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extracting Access Token": {
      "main": [
        [
          {
            "node": "Getting Data For the Past 28 Days Segemented Per Campaign Adset and Ad",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Getting Long-Term Token": {
      "main": [
        [
          {
            "node": "Does it Need A Token Refresh?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Sorting Based on Spends": {
      "main": [
        [
          {
            "node": "Sending Raw Data To A Google Sheet",
            "type": "main",
            "index": 0
          },
          {
            "node": "Stringify Everything",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Getting Long-Term Token1": {
      "main": [
        [
          {
            "node": "Extracting Access Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "Senior Facebook Ads Media Buyer",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Stringify Benchmark Data": {
      "main": [
        [
          {
            "node": "Combining Ad Data and Benchmarking Data",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "Senior Facebook Ads Media Buyer",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Extracting Long Term Token": {
      "main": [
        [
          {
            "node": "Extracting Access Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculate Account Benchmarks": {
      "main": [
        [
          {
            "node": "Stringify Benchmark Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Does Token Needs Refreshing?": {
      "main": [
        [
          {
            "node": "Getting Long-Lived Access Token1",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Getting Long-Term Token1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Does it Need A Token Refresh?": {
      "main": [
        [
          {
            "node": "Does Token Needs Refreshing?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Splitting Out In Table Format": {
      "main": [
        [
          {
            "node": "Filtering Only For Sales Campaigns",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Calculating End Date of Token1": {
      "main": [
        [
          {
            "node": "Updating Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Senior Facebook Ads Media Buyer": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate Metrics by Ad Creative": {
      "main": [
        [
          {
            "node": "Sorting Based on Spends",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Getting Long-Lived Access Token1": {
      "main": [
        [
          {
            "node": "Calculating End Date of Token1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Test workflow’": {
      "main": [
        [
          {
            "node": "Getting Long-Term Token",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filtering Only For Sales Campaigns": {
      "main": [
        [
          {
            "node": "Aggregate Metrics by Ad Creative",
            "type": "main",
            "index": 0
          },
          {
            "node": "Calculate Account Benchmarks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combine Ad & Benchmark Data for LLM": {
      "main": [
        [
          {
            "node": "Senior Facebook Ads Media Buyer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Combining Ad Data and Benchmarking Data": {
      "main": [
        [
          {
            "node": "Combine Ad & Benchmark Data for LLM",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Getting Data For the Past 28 Days Segemented Per Campaign Adset and Ad": {
      "main": [
        [
          {
            "node": "Splitting Out In Table Format",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}