{
  "name": "Technical SEO Monitor",
  "nodes": [
    {
      "parameters": {
        "rule": {
          "interval": [{"field": "cronExpression", "expression": "0 9 * * 1"}]
        }
      },
      "id": "schedule-trigger",
      "name": "Weekly Schedule",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [250, 300]
    },
    {
      "parameters": {
        "url": "={{$json.siteUrl}}/robots.txt",
        "options": {}
      },
      "id": "fetch-robots",
      "name": "Fetch robots.txt",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [470, 200]
    },
    {
      "parameters": {
        "url": "={{$json.siteUrl}}/sitemap.xml",
        "options": {}
      },
      "id": "fetch-sitemap",
      "name": "Fetch Sitemap",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [470, 400]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://www.googleapis.com/pagespeedonline/v5/runPagespeed",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {"name": "url", "value": "={{$json.siteUrl}}"},
            {"name": "strategy", "value": "mobile"},
            {"name": "category", "value": "performance"},
            {"name": "category", "value": "accessibility"},
            {"name": "category", "value": "best-practices"},
            {"name": "category", "value": "seo"}
          ]
        },
        "options": {}
      },
      "id": "pagespeed-api",
      "name": "PageSpeed Insights",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [470, 600]
    },
    {
      "parameters": {
        "jsCode": "const robots = $input.first().json.body || '';\nconst sitemap = $input.first().json.body || '';\nconst pagespeed = $input.first().json;\n\nconst issues = [];\n\n// Check robots.txt\nif (!robots.includes('User-agent')) {\n  issues.push({severity: 'critical', issue: 'robots.txt is empty or missing'});\n}\nif (!robots.includes('Sitemap')) {\n  issues.push({severity: 'warning', issue: 'No sitemap directive in robots.txt'});\n}\nif (robots.includes('Disallow: /')) {\n  issues.push({severity: 'critical', issue: 'robots.txt blocks entire site'});\n}\n\n// Check sitemap\nif (!sitemap.includes('<?xml') && !sitemap.includes('<urlset')) {\n  issues.push({severity: 'critical', issue: 'sitemap.xml is missing or invalid'});\n}\n\n// Check PageSpeed\nif (pagespeed?.lighthouseResult) {\n  const scores = pagespeed.lighthouseResult.categories;\n  if (scores?.performance?.score < 0.5) {\n    issues.push({severity: 'critical', issue: `Performance score: ${Math.round(scores.performance.score * 100)}`});\n  } else if (scores?.performance?.score < 0.9) {\n    issues.push({severity: 'warning', issue: `Performance score: ${Math.round(scores.performance.score * 100)}`});\n  }\n  if (scores?.seo?.score < 0.9) {\n    issues.push({severity: 'warning', issue: `SEO score: ${Math.round(scores.seo.score * 100)}`});\n  }\n}\n\nreturn [{json: {issues, timestamp: new Date().toISOString()}}];"
      },
      "id": "analyze-results",
      "name": "Analyze Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [690, 400]
    },
    {
      "parameters": {
        "conditions": {
          "options": {"caseSensitive": true, "leftValue": "", "typeValidation": "strict"},
          "conditions": [
            {
              "leftValue": "={{$json.issues.length}}",
              "rightValue": "0",
              "operator": {"type": "number", "operation": "gt"}
            }
          ],
          "combinator": "and"
        }
      },
      "id": "check-issues",
      "name": "Issues Found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [910, 400]
    },
    {
      "parameters": {
        "toEmail": "hi@leebeirne.com",
        "subject": "=Technical SEO Alert: {{$json.issues.length}} issues found",
        "message": "=The following technical SEO issues were found:\n\n{{$json.issues.map(i => `- [${i.severity.toUpperCase()}] ${i.issue}`).join('\\n')}}\n\nTimestamp: {{$json.timestamp}}"
      },
      "id": "send-email",
      "name": "Send Alert Email",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [1130, 300]
    },
    {
      "parameters": {
        "toEmail": "hi@leebeirne.com",
        "subject": "Technical SEO: All Clear",
        "message": "No technical SEO issues found. Timestamp: {{$json.timestamp}}"
      },
      "id": "send-all-clear",
      "name": "Send All Clear",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [1130, 500]
    }
  ],
  "connections": {
    "Weekly Schedule": {
      "main": [
        [
          {"node": "Fetch robots.txt", "type": "main", "index": 0},
          {"node": "Fetch Sitemap", "type": "main", "index": 0},
          {"node": "PageSpeed Insights", "type": "main", "index": 0}
        ]
      ]
    },
    "Fetch robots.txt": {
      "main": [[{"node": "Analyze Results", "type": "main", "index": 0}]]
    },
    "Fetch Sitemap": {
      "main": [[{"node": "Analyze Results", "type": "main", "index": 0}]]
    },
    "PageSpeed Insights": {
      "main": [[{"node": "Analyze Results", "type": "main", "index": 0}]]
    },
    "Analyze Results": {
      "main": [[{"node": "Issues Found?", "type": "main", "index": 0}]]
    },
    "Issues Found?": {
      "main": [
        [{"node": "Send Alert Email", "type": "main", "index": 0}],
        [{"node": "Send All Clear", "type": "main", "index": 0}]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "tags": [{"name": "SEO"}]
}
