{
  "name": "YA-RE-22: Weekly Performance Narrative Agent",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtHour": 8,
              "triggerAtMinute": 0,
              "triggerAtDay": 1
            }
          ]
        }
      },
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [220, 300],
      "id": "node-schedule-weekly",
      "name": "Schedule: Monday 8:00 AM"
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://YOUR_ANALYTICS_SERVICE_URL/api/v1/reports/meta-ads-weekly",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_REPORTING_API_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 200],
      "id": "node-http-meta-metrics",
      "name": "HTTP: Fetch Meta Weekly Metrics"
    },
    {
      "parameters": {
        "method": "GET",
        "url": "=https://YOUR_ANALYTICS_SERVICE_URL/api/v1/reports/google-ads-weekly",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_REPORTING_API_TOKEN"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [440, 400],
      "id": "node-http-google-metrics",
      "name": "HTTP: Fetch Google Weekly Metrics"
    },
    {
      "parameters": {
        "jsCode": "// Aggregate Cross-Channel Paid Media & Pipeline Revenue for Executive Narrative\nconst metaData = $('HTTP: Fetch Meta Weekly Metrics').first().json || { spend: 15000, leads: 120, sqls: 30 };\nconst googleData = $('HTTP: Fetch Google Weekly Metrics').first().json || { spend: 18000, leads: 90, sqls: 35 };\n\nconst totalSpend = Number(metaData.spend || 0) + Number(googleData.spend || 0);\nconst totalLeads = Number(metaData.leads || 0) + Number(googleData.leads || 0);\nconst totalSqls = Number(metaData.sqls || 0) + Number(googleData.sqls || 0);\n\nconst blendedCpl = totalLeads > 0 ? (totalSpend / totalLeads).toFixed(2) : 0;\nconst blendedCostPerSql = totalSqls > 0 ? (totalSpend / totalSqls).toFixed(2) : 0;\nconst qualificationRate = totalLeads > 0 ? ((totalSqls / totalLeads) * 100).toFixed(1) : 0;\n\nreturn [{\n  json: {\n    report_period: \"Previous 7 Days\",\n    currency: \"USD\",\n    total_spend: totalSpend,\n    total_leads: totalLeads,\n    total_sqls: totalSqls,\n    blended_cpl: blendedCpl,\n    blended_cost_per_sql: blendedCostPerSql,\n    qualification_rate_percent: qualificationRate,\n    channel_breakdown: {\n      meta: metaData,\n      google: googleData\n    }\n  }\n}];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [680, 300],
      "id": "node-code-aggregate",
      "name": "Code: Aggregate Metrics"
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "=You are a Senior Fractional CMO and Real Estate Performance Marketing Director drafting an executive weekly memo for developer board leadership.\n\nReview this aggregated weekly performance payload:\n{{ JSON.stringify($json) }}\n\nWrite an objective, practitioner-level executive narrative containing:\n1. Executive Summary (2-3 sentences covering spend pacing, lead volume, and qualification velocity).\n2. Channel Performance Highlights (Meta vs Google Search efficiency).\n3. Risk & Anomaly Watch (Identify any CAC or qualification rate drag).\n4. Recommended Budget Actions for the upcoming week (Specific shifts or campaign adjustments).",
        "hasOutputParser": true
      },
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.7,
      "position": [900, 300],
      "id": "node-ai-narrative-agent",
      "name": "AI Agent: Executive Narrative Drafter"
    },
    {
      "parameters": {
        "model": "gpt-4o-mini",
        "options": {
          "temperature": 0.2
        }
      },
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1,
      "position": [840, 520],
      "id": "node-openai-model",
      "name": "OpenAI Chat Model",
      "credentials": {
        "openAiApi": {
          "id": "YOUR_OPENAI_CREDENTIAL_ID",
          "name": "OpenAI account"
        }
      }
    },
    {
      "parameters": {
        "jsonSchema": "{\n  \"type\": \"object\",\n  \"properties\": {\n    \"executive_summary\": {\n      \"type\": \"string\"\n    },\n    \"channel_analysis\": {\n      \"type\": \"string\"\n    },\n    \"identified_risks\": {\n      \"type\": \"array\",\n      \"items\": { \"type\": \"string\" }\n    },\n    \"recommended_actions\": {\n      \"type\": \"array\",\n      \"items\": { \"type\": \"string\" }\n    },\n    \"overall_health_score\": {\n      \"type\": \"string\",\n      \"enum\": [\"HEALTHY\", \"MONITOR\", \"CRITICAL_ACTION_REQUIRED\"]\n    }\n  },\n  \"required\": [\"executive_summary\", \"channel_analysis\", \"identified_risks\", \"recommended_actions\", \"overall_health_score\"]\n}"
      },
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "typeVersion": 1.2,
      "position": [1020, 520],
      "id": "node-structured-parser",
      "name": "Structured Output Parser"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://YOUR_INTERNAL_SLACK_WEBHOOK_URL",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"text\": \"*Weekly Real Estate Performance Narrative (AI Generated)*\\n\\n*Status:* {{ $json.output.overall_health_score }}\\n\\n*Summary:*\\n{{ $json.output.executive_summary }}\\n\\n*Recommended Actions:*\\n- {{ $json.output.recommended_actions.join('\\n- ') }}\"\n}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1160, 300],
      "id": "node-http-slack-alert",
      "name": "HTTP: Post Memo to Leadership Channel"
    }
  ],
  "connections": {
    "Schedule: Monday 8:00 AM": {
      "main": [
        [
          {
            "node": "HTTP: Fetch Meta Weekly Metrics",
            "type": "main",
            "index": 0
          },
          {
            "node": "HTTP: Fetch Google Weekly Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP: Fetch Meta Weekly Metrics": {
      "main": [
        [
          {
            "node": "Code: Aggregate Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP: Fetch Google Weekly Metrics": {
      "main": [
        [
          {
            "node": "Code: Aggregate Metrics",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code: Aggregate Metrics": {
      "main": [
        [
          {
            "node": "AI Agent: Executive Narrative Drafter",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent: Executive Narrative Drafter",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent: Executive Narrative Drafter",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent: Executive Narrative Drafter": {
      "main": [
        [
          {
            "node": "HTTP: Post Memo to Leadership Channel",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "1.0.0",
  "meta": {
    "templateCredsSetupCompleted": false,
    "instanceId": "yehia-ahmed-re-playbook"
  },
  "tags": [
    {
      "name": "AI Agent"
    },
    {
      "name": "Executive Reporting"
    },
    {
      "name": "Performance Marketing"
    }
  ]
}
