{
  "name": "07 - Real-Time Competitor Intelligence Monitor",
  "nodes": [
    {
      "id": "node-1",
      "name": "Schedule - Daily 7AM Weekdays",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.1,
      "position": [240, 300],
      "parameters": { "rule": { "interval": [{ "field": "cronExpression", "expression": "0 7 * * 1-5" }] } }
    },
    {
      "id": "node-2",
      "name": "Code - Competitor URL List",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [480, 300],
      "parameters": {
        "jsCode": "return [\n  { json: { name: 'Competitor A', url: 'https://competitor-a.com/pricing', page_type: 'pricing' } },\n  { json: { name: 'Competitor B', url: 'https://competitor-b.com/pricing', page_type: 'pricing' } },\n  { json: { name: 'Competitor C', url: 'https://competitor-c.com/features', page_type: 'features' } }\n];"
      }
    },
    {
      "id": "node-3",
      "name": "HTTP Request - Fetch Page",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [720, 300],
      "parameters": {
        "method": "GET",
        "url": "={{ $json.url }}",
        "options": { "response": { "response": { "responseFormat": "text" } } }
      }
    },
    {
      "id": "node-4",
      "name": "Code - Hash & Extract Text",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [960, 300],
      "parameters": {
        "jsCode": "const html = $input.first().json.data || '';\nconst text = html.replace(/<script[\\s\\S]*?<\\/script>/gi,'').replace(/<style[\\s\\S]*?<\\/style>/gi,'').replace(/<[^>]+>/g,' ').replace(/\\s+/g,' ').trim().substring(0,5000);\nconst crypto = require('crypto');\nconst hash = crypto.createHash('md5').update(text).digest('hex');\nconst comp = $('Code - Competitor URL List').first().json;\nreturn [{ json: { name: comp.name, url: comp.url, page_type: comp.page_type, current_hash: hash, content_text: text, checked_at: new Date().toISOString() } }];"
      }
    },
    {
      "id": "node-5",
      "name": "Google Sheets - Get Previous Hash",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [1200, 300],
      "credentials": { "googleSheetsOAuth2Api": { "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID", "name": "Google Sheets account" } },
      "parameters": {
        "resource": "sheet",
        "operation": "read",
        "documentId": { "__rl": true, "value": "YOUR_SPREADSHEET_ID", "mode": "id" },
        "sheetName": { "__rl": true, "value": "Competitor_Tracking", "mode": "name" },
        "options": {}
      }
    },
    {
      "id": "node-6",
      "name": "Code - Detect Changes",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [1440, 300],
      "parameters": {
        "jsCode": "const current = $('Code - Hash & Extract Text').first().json;\nconst rows = $input.first().json || [];\nconst match = Array.isArray(rows) ? rows.find(r => r.Name === current.name) : null;\nconst prevHash = match ? match.Hash : null;\nreturn [{ json: { ...current, previous_hash: prevHash, has_changed: prevHash !== null && prevHash !== current.current_hash, is_new: prevHash === null } }];"
      }
    },
    {
      "id": "node-7",
      "name": "IF - Page Changed",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [1680, 300],
      "parameters": {
        "conditions": {
          "conditions": [{ "id": "c1", "leftValue": "={{ $json.has_changed }}", "rightValue": true, "operator": { "type": "boolean", "operation": "true" } }],
          "combinator": "and"
        }
      }
    },
    {
      "id": "node-8",
      "name": "OpenAI - Analyze Changes",
      "type": "n8n-nodes-base.openAi",
      "typeVersion": 1.8,
      "position": [1920, 160],
      "credentials": { "openAiApi": { "id": "YOUR_OPENAI_CREDENTIAL_ID", "name": "OpenAI account" } },
      "parameters": {
        "resource": "chat",
        "operation": "complete",
        "modelId": { "__rl": true, "value": "gpt-4o", "mode": "list", "cachedResultName": "gpt-4o" },
        "messages": {
          "values": [
            { "role": "system", "content": "You are a competitive intelligence analyst. Identify pricing changes, new features, and messaging shifts. Be concise and business-focused." },
            { "role": "user", "content": "=Analyze this {{ $json.page_type }} page from {{ $json.name }}: {{ $json.content_text.substring(0,3000) }}" }
          ]
        },
        "options": { "temperature": 0.3 }
      }
    },
    {
      "id": "node-9",
      "name": "Slack - Send Intel Brief",
      "type": "n8n-nodes-base.slack",
      "typeVersion": 2.3,
      "position": [2160, 160],
      "credentials": { "slackApi": { "id": "YOUR_SLACK_CREDENTIAL_ID", "name": "Slack account" } },
      "parameters": {
        "resource": "message",
        "operation": "send",
        "channel": { "__rl": true, "value": "#competitive-intel", "mode": "name" },
        "text": "=👀 *Competitor Change Detected!*\n\n*Competitor:* {{ $('Code - Detect Changes').item.json.name }}\n*Page Type:* {{ $('Code - Detect Changes').item.json.page_type }}\n*URL:* {{ $('Code - Detect Changes').item.json.url }}\n\n*AI Analysis:*\n{{ $input.first().json.message.content }}",
        "otherOptions": {}
      }
    },
    {
      "id": "node-10",
      "name": "Google Sheets - Update Hash",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [1920, 440],
      "credentials": { "googleSheetsOAuth2Api": { "id": "YOUR_GOOGLE_SHEETS_CREDENTIAL_ID", "name": "Google Sheets account" } },
      "parameters": {
        "resource": "sheet",
        "operation": "appendOrUpdate",
        "documentId": { "__rl": true, "value": "YOUR_SPREADSHEET_ID", "mode": "id" },
        "sheetName": { "__rl": true, "value": "Competitor_Tracking", "mode": "name" },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Name": "={{ $json.name }}",
            "URL": "={{ $json.url }}",
            "Hash": "={{ $json.current_hash }}",
            "Last Checked": "={{ $json.checked_at }}",
            "Changed": "={{ $json.has_changed }}"
          }
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "Schedule - Daily 7AM Weekdays": { "main": [[{ "node": "Code - Competitor URL List", "type": "main", "index": 0 }]] },
    "Code - Competitor URL List": { "main": [[{ "node": "HTTP Request - Fetch Page", "type": "main", "index": 0 }]] },
    "HTTP Request - Fetch Page": { "main": [[{ "node": "Code - Hash & Extract Text", "type": "main", "index": 0 }]] },
    "Code - Hash & Extract Text": { "main": [[{ "node": "Google Sheets - Get Previous Hash", "type": "main", "index": 0 }]] },
    "Google Sheets - Get Previous Hash": { "main": [[{ "node": "Code - Detect Changes", "type": "main", "index": 0 }]] },
    "Code - Detect Changes": { "main": [[{ "node": "IF - Page Changed", "type": "main", "index": 0 }]] },
    "IF - Page Changed": {
      "main": [
        [{ "node": "OpenAI - Analyze Changes", "type": "main", "index": 0 }],
        [{ "node": "Google Sheets - Update Hash", "type": "main", "index": 0 }]
      ]
    },
    "OpenAI - Analyze Changes": { "main": [[{ "node": "Slack - Send Intel Brief", "type": "main", "index": 0 }]] }
  },
  "settings": { "executionOrder": "v1" },
  "active": false,
  "tags": [{ "name": "Portfolio" }, { "name": "Business Intelligence" }, { "name": "AI" }]
}
